Skip to content

Commit

Permalink
Fix #394
Browse files Browse the repository at this point in the history
Tweaks to rraumberger's contribution to let daemon.js launch tail windows in this way, and to cascade windows.
  • Loading branch information
alainbryden committed Oct 27, 2024
1 parent 654fc9d commit 26f6649
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 19 deletions.
2 changes: 1 addition & 1 deletion autopilot.js
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ async function checkOnRunningScripts(ns, player) {
daemonStartTime = Date.now();
// Open the tail window if it's the start of a new BN. Especially useful to new players.
if (getTimeInBitnode() < 1000 * 60 * 5 || homeRam == 8) // First 5 minutes, or BN1.1 where we have 8GB ram
ns.tail(daemonPid);
tail(ns, daemonPid);
}

// Default work for faction args we think are ideal for speed-running BNs
Expand Down
24 changes: 13 additions & 11 deletions daemon.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ async function startup(ns) {

// PERIODIC SCRIPTS
// These scripts are spawned periodically (at some interval) to do their checks, with an optional condition that limits when they should be spawned
let shouldUpgradeHacknet = async () => ((await whichServerIsRunning(ns, "hacknet-upgrade-manager.js", false)) === null) && reservedMoney(ns) < getPlayerMoney(ns);
let shouldUpgradeHacknet = () => (whichServerIsRunning(ns, "hacknet-upgrade-manager.js", false)[0] === null) && reservedMoney(ns) < getPlayerMoney(ns);
// In BN8 (stocks-only bn) and others with hack income disabled, don't waste money on improving hacking infrastructure unless we have plenty of money to spare
let shouldImproveHacking = () => bitNodeMults.ScriptHackMoneyGain != 0 && !isInBn8 || getPlayerMoney(ns) > 1e12;
// Note: Periodic script are generally run every 30 seconds, but intervals are spaced out to ensure they aren't all bursting into temporary RAM at the same time.
Expand Down Expand Up @@ -508,12 +508,14 @@ async function kickstartHackXp(ns) {

/** Check running status of scripts on servers
* @param {NS} ns
* @returns {Promise<string>} */
async function whichServerIsRunning(ns, scriptName, canUseCache = true) {
for (const server of getAllServers())
if (processList(ns, server.name, canUseCache).some(process => process.filename === scriptName))
return server.name;
return null;
* @returns {[string, pid]} */
function whichServerIsRunning(ns, scriptName, canUseCache = true) {
for (const server of getAllServers()) {
const matches = processList(ns, server.name, canUseCache).filter(process => process.filename === scriptName);
if (matches.length > 1)
return [server.name, matches[0].pid];
}
return [null, null];
}

/** Helper to kick off external scripts
Expand Down Expand Up @@ -569,9 +571,9 @@ async function tryRunTool(ns, tool) {
log(ns, `ERROR: Tool ${tool.name} was not found on ${daemonHost}`, true, 'error');
return false;
}
let runningOnServer = await whichServerIsRunning(ns, tool.name);
let [runningOnServer, runningPid] = whichServerIsRunning(ns, tool.name);
if (runningOnServer != null) {
if (verbose) log(ns, `INFO: Tool ${tool.name} is already running on server ${runningOnServer}.`);
if (verbose) log(ns, `INFO: Tool ${tool.name} is already running on server ${runningOnServer} as pid ${runningPid}.`);
return true;
}
const args = funcResultOrValue(tool.args) || []; // Support either a static args array, or a function returning the args.
Expand All @@ -580,13 +582,13 @@ async function tryRunTool(ns, tool) {
await arbitraryExecution(ns, tool, 1, args, getServerByName(backupServerName).hasRoot() ? backupServerName : daemonHost) :
await exec(ns, tool.name, daemonHost, tool.runOptions, ...args);
if (runResult) {
runningOnServer = await whichServerIsRunning(ns, tool.name, false);
[runningOnServer, runningPid] = whichServerIsRunning(ns, tool.name, false);
if (verbose)
log(ns, `Ran tool: ${tool.name} ` + (args.length > 0 ? `with args ${JSON.stringify(args)} ` : '') +
(runningOnServer ? `on server ${runningOnServer}.` : 'but it shut down right away.'));
if (tool.tail === true && runningOnServer) {
log(ns, `Tailing Tool: ${tool.name} on server ${runningOnServer}` + (args.length > 0 ? ` with args ${JSON.stringify(args)}` : ''));
ns.tail(tool.name, runningOnServer, ...args);
tail(ns, runningPid);
//tool.tail = false; // Avoid popping open additional tail windows in the future
}
return true;
Expand Down
5 changes: 5 additions & 0 deletions dev-console.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/** @param {NS} ns */
export async function main(ns) {
globalThis.webpack_require ?? webpackChunkbitburner.push([[-1], {}, w => globalThis.webpack_require = w]);
Object.keys(webpack_require.m).forEach(k => Object.values(webpack_require(k)).forEach(p => p?.toPage?.('Dev')));
}
19 changes: 13 additions & 6 deletions helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ export async function instanceCount(ns, onHost = "home", warn = true, tailOtherI
log(ns, `WARNING: You cannot start multiple versions of this script (${scriptName}). Please shut down the other instance first.` +
(tailOtherInstances ? ' (To help with this, a tail window for the other instance will be opened)' : ''), true, 'warning');
if (tailOtherInstances) // Tail all but the last pid, since it will belong to the current instance (which will be shut down)
otherInstances.slice(0, otherInstances.length - 1).forEach(pid => ns.tail(pid));
otherInstances.slice(0, otherInstances.length - 1).forEach(pid => tail(ns, pid));
}
return otherInstances.length;
}
Expand Down Expand Up @@ -807,9 +807,16 @@ export function unEscapeArrayArgs(args) {
* Custom tail function which also applies default resizes and tail window placement.
* This algorithm is not perfect but for the most part should not generate overlaps of the window's title bar.
* @param {NS} ns The nestcript instance passed to your script's main entry point
* @param {number|undefined} processId The id of the process to tail, or null to use the current process id
*/
export function tail(ns) {
ns.tail();
ns.resizeTail(ns.ui.windowSize()[0] * 0.75, ns.ui.windowSize()[1] * 0.25, ns.pid);
ns.moveTail(250, (ns.pid % 13) * 35, ns.pid);
}
export function tail(ns, processId = undefined) {
processId ??= ns.pid
ns.tail(processId);
// By default, make all tail windows take up 75% of the width, 25% of the height available
const [width, height] = ns.ui.windowSize();
ns.resizeTail(width * 0.75, height * 0.25, processId);
// Cascade windows: After each tail, shift the window slightly down and over so that they don't overlap
let offsetPct = ((((tailCounter++ % 30.0) / 30.0) + tailCounter) % 6.0) / 6.0;
ns.moveTail(offsetPct * (width * 0.25 - 300) + 250, offsetPct * (height * 0.75 - 100) + 50, processId);
}
let tailCounter = -1;
2 changes: 1 addition & 1 deletion stockmaster.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ let launchSummaryTail = async ns => {
if (await getNsDataThroughFile(ns, `ns.scriptRunning('${summaryTailScript}', ns.getHostname())`, '/Temp/stockmarket-summary-is-running.txt'))
return;
//await getNsDataThroughFile(ns, `ns.scriptKill('${summaryTailScript}', ns.getHostname())`, summaryTailScript.replace('.js', '-kill.js')); // Only needed if we're changing the script below
await runCommand(ns, `ns.disableLog('sleep'); ns.tail(); let lastRead = '';
await runCommand(ns, `ns.disableLog('sleep'); tail(ns); let lastRead = '';
while (true) {
let read = ns.read('${summaryFile}');
if (lastRead != read) ns.print(lastRead = read);
Expand Down

0 comments on commit 26f6649

Please sign in to comment.