forked from alainbryden/bitburner-scripts
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhelpers.js
More file actions
552 lines (515 loc) · 35.6 KB
/
helpers.js
File metadata and controls
552 lines (515 loc) · 35.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
/**
* Return a formatted representation of the monetary amount using scale symbols (e.g. $6.50M)
* @param {number} num - The number to format
* @param {number=} maxSignificantFigures - (default: 6) The maximum significant figures you wish to see (e.g. 123, 12.3 and 1.23 all have 3 significant figures)
* @param {number=} maxDecimalPlaces - (default: 3) The maximum decimal places you wish to see, regardless of significant figures. (e.g. 12.3, 1.2, 0.1 all have 1 decimal)
**/
export function formatMoney(num, maxSignificantFigures = 6, maxDecimalPlaces = 3) {
let numberShort = formatNumberShort(num, maxSignificantFigures, maxDecimalPlaces);
return num >= 0 ? "$" + numberShort : numberShort.replace("-", "-$");
}
const symbols = ["", "k", "m", "b", "t", "q", "Q", "s", "S", "o", "n", "e33", "e36", "e39"];
/**
* Return a formatted representation of the monetary amount using scale sympols (e.g. 6.50M)
* @param {number} num - The number to format
* @param {number=} maxSignificantFigures - (default: 6) The maximum significant figures you wish to see (e.g. 123, 12.3 and 1.23 all have 3 significant figures)
* @param {number=} maxDecimalPlaces - (default: 3) The maximum decimal places you wish to see, regardless of significant figures. (e.g. 12.3, 1.2, 0.1 all have 1 decimal)
**/
export function formatNumberShort(num, maxSignificantFigures = 6, maxDecimalPlaces = 3) {
if (Math.abs(num) > 10 ** (3 * symbols.length)) // If we've exceeded our max symbol, switch to exponential notation
return num.toExponential(Math.min(maxDecimalPlaces, maxSignificantFigures - 1));
for (var i = 0, sign = Math.sign(num), num = Math.abs(num); num >= 1000 && i < symbols.length; i++) num /= 1000;
// TODO: A number like 9.999 once rounded to show 3 sig figs, will become 10.00, which is now 4 sig figs.
return ((sign < 0) ? "-" : "") + num.toFixed(Math.max(0, Math.min(maxDecimalPlaces, maxSignificantFigures - Math.floor(1 + Math.log10(num))))) + symbols[i];
}
/** Convert a shortened number back into a value */
export function parseShortNumber(text = "0") {
let parsed = Number(text);
if (!isNaN(parsed)) return parsed;
for (const sym of symbols.slice(1))
if (text.toLowerCase().endsWith(sym))
return Number.parseFloat(text.slice(0, text.length - sym.length)) * Math.pow(10, 3 * symbols.indexOf(sym));
return Number.NaN;
}
/**
* Return a number formatted with the specified number of significatnt figures or decimal places, whichever is more limiting.
* @param {number} num - The number to format
* @param {number=} minSignificantFigures - (default: 6) The minimum significant figures you wish to see (e.g. 123, 12.3 and 1.23 all have 3 significant figures)
* @param {number=} minDecimalPlaces - (default: 3) The minimum decimal places you wish to see, regardless of significant figures. (e.g. 12.3, 1.2, 0.1 all have 1 decimal)
**/
export function formatNumber(num, minSignificantFigures = 3, minDecimalPlaces = 1) {
return num == 0.0 ? num : num.toFixed(Math.max(minDecimalPlaces, Math.max(0, minSignificantFigures - Math.ceil(Math.log10(num)))));
}
/** Formats some RAM amount as a round number of GB with thousands separators e.g. `1,028 GB` */
export function formatRam(num) { return `${Math.round(num).toLocaleString('en')} GB`; }
/** Return a datatime in ISO format */
export function formatDateTime(datetime) { return datetime.toISOString(); }
/** Format a duration (in milliseconds) as e.g. '1h 21m 6s' for big durations or e.g '12.5s' / '23ms' for small durations */
export function formatDuration(duration) {
if (duration < 1000) return `${duration.toFixed(0)}ms`
if (!isFinite(duration)) return 'forever (Infinity)'
const portions = [];
const msInHour = 1000 * 60 * 60;
const hours = Math.trunc(duration / msInHour);
if (hours > 0) {
portions.push(hours + 'h');
duration -= (hours * msInHour);
}
const msInMinute = 1000 * 60;
const minutes = Math.trunc(duration / msInMinute);
if (minutes > 0) {
portions.push(minutes + 'm');
duration -= (minutes * msInMinute);
}
let seconds = (duration / 1000.0)
// Include millisecond precision if we're on the order of seconds
seconds = (hours == 0 && minutes == 0) ? seconds.toPrecision(3) : seconds.toFixed(0);
if (seconds > 0) {
portions.push(seconds + 's');
duration -= (minutes * 1000);
}
return portions.join(' ');
}
/** Generate a hashCode for a string that is pretty unique most of the time */
export function hashCode(s) { return s.split("").reduce(function (a, b) { a = ((a << 5) - a) + b.charCodeAt(0); return a & a }, 0); }
/** @param {NS} ns **/
export function disableLogs(ns, listOfLogs) { ['disableLog'].concat(...listOfLogs).forEach(log => checkNsInstance(ns, '"disableLogs"').disableLog(log)); }
/** Joins all arguments as components in a path, e.g. pathJoin("foo", "bar", "/baz") = "foo/bar/baz" **/
export function pathJoin(...args) {
return args.filter(s => !!s).join('/').replace(/\/\/+/g, '/');
}
/** Gets the path for the given local file, taking into account optional subfolder relocation via git-pull.js **/
export function getFilePath(file) {
const subfolder = ''; // git-pull.js optionally modifies this when downloading
return pathJoin(subfolder, file);
}
// FUNCTIONS THAT PROVIDE ALTERNATIVE IMPLEMENTATIONS TO EXPENSIVE NS FUNCTIONS
// VARIATIONS ON NS.RUN
/** @param {NS} ns
* Use where a function is required to run a script and you have already referenced ns.run in your script **/
export function getFnRunViaNsRun(ns) { return checkNsInstance(ns, '"getFnRunViaNsRun"').run; }
/** @param {NS} ns
* Use where a function is required to run a script and you have already referenced ns.exec in your script **/
export function getFnRunViaNsExec(ns, host = "home") {
checkNsInstance(ns, '"getFnRunViaNsExec"');
return function (scriptPath, ...args) { return ns.exec(scriptPath, host, ...args); }
}
// VARIATIONS ON NS.ISRUNNING
/** @param {NS} ns
* Use where a function is required to run a script and you have already referenced ns.run in your script */
export function getFnIsAliveViaNsIsRunning(ns) { return checkNsInstance(ns, '"getFnIsAliveViaNsIsRunning"').isRunning; }
/** @param {NS} ns
* Use where a function is required to run a script and you have already referenced ns.ps in your script */
export function getFnIsAliveViaNsPs(ns) {
checkNsInstance(ns, '"getFnIsAliveViaNsPs"');
return function (pid, host) { return ns.ps(host).some(process => process.pid === pid); }
}
/**
* Retrieve the result of an ns command by executing it in a temporary .js script, writing the result to a file, then shuting it down
* Importing incurs a maximum of 1.1 GB RAM (0 GB for ns.read, 1 GB for ns.run, 0.1 GB for ns.isRunning).
* Has the capacity to retry if there is a failure (e.g. due to lack of RAM available). Not recommended for performance-critical code.
* @param {NS} ns - The nestcript instance passed to your script's main entry point
* @param {string} command - The ns command that should be invoked to get the desired data (e.g. "ns.getServer('home')" )
* @param {string=} fName - (default "/Temp/{commandhash}-data.txt") The name of the file to which data will be written to disk by a temporary process
* @param {args=} args - args to be passed in as arguments to command being run as a new script.
* @param {bool=} verbose - (default false) If set to true, pid and result of command are logged.
**/
export async function getNsDataThroughFile(ns, command, fName, args = [], verbose = false, maxRetries = 5, retryDelayMs = 50) {
checkNsInstance(ns, '"getNsDataThroughFile"');
if (!verbose) disableLogs(ns, ['run', 'isRunning']);
return await getNsDataThroughFile_Custom(ns, ns.run, command, fName, args, verbose, maxRetries, retryDelayMs);
}
/**
* An advanced version of getNsDataThroughFile that lets you pass your own "fnRun" implementation to reduce RAM requirements
* Importing incurs no RAM (now that ns.read is free) plus whatever fnRun you provide it
* Has the capacity to retry if there is a failure (e.g. due to lack of RAM available). Not recommended for performance-critical code.
* @param {NS} ns - The nestcript instance passed to your script's main entry point
* @param {function} fnRun - A single-argument function used to start the new sript, e.g. `ns.run` or `(f,...args) => ns.exec(f, "home", ...args)`
* @param {args=} args - args to be passed in as arguments to command being run as a new script.
**/
export async function getNsDataThroughFile_Custom(ns, fnRun, command, fName, args = [], verbose = false, maxRetries = 5, retryDelayMs = 50) {
checkNsInstance(ns, '"getNsDataThroughFile_Custom"');
if (!verbose) disableLogs(ns, ['read']);
const commandHash = hashCode(command);
fName = fName || `/Temp/${commandHash}-data.txt`;
const fNameCommand = (fName || `/Temp/${commandHash}-command`) + '.js'
// Pre-write contents to the file that will allow us to detect if our temp script never got run
const initialContents = "<Insufficient RAM>";
ns.write(fName, initialContents, 'w');
// Prepare a command that will write out a new file containing the results of the command
// unless it already exists with the same contents (saves time/ram to check first)
// If an error occurs, it will write an empty file to avoid old results being misread.
const commandToFile = `let r;try{r=JSON.stringify(\n` +
` ${command}\n` +
`);}catch(e){r="ERROR: "+(typeof e=='string'?e:e.message||JSON.stringify(e));}\n` +
`const f="${fName}"; if(ns.read(f)!==r) ns.write(f,r,'w')`;
// Run the command with auto-retries if it fails
const pid = await runCommand_Custom(ns, fnRun, commandToFile, fNameCommand, args, verbose, maxRetries, retryDelayMs);
// Wait for the process to complete. Note, as long as the above returned a pid, we don't actually have to check it, just the file contents
const fnIsAlive = (ignored_pid) => ns.read(fName) === initialContents;
await waitForProcessToComplete_Custom(ns, fnIsAlive, pid, verbose);
if (verbose) log(ns, `Process ${pid} is done. Reading the contents of ${fName}...`);
// Read the file, with auto-retries if it fails // TODO: Unsure reading a file can fail or needs retrying.
let lastRead;
const fileData = await autoRetry(ns, () => ns.read(fName),
f => (lastRead = f) !== undefined && f !== "" && f !== initialContents && !(typeof f == "string" && f.startsWith("ERROR: ")),
() => `\nns.read('${fName}') returned a bad result: "${lastRead}".` +
`\n Script: ${fNameCommand}\n Args: ${JSON.stringify(args)}\n Command: ${command}` +
(lastRead == undefined ? '\nThe developer has no idea how this could have happened. Please post a screenshot of this error on discord.' :
lastRead == initialContents ? `\nThe script that ran this will likely recover and try again later once you have more free ram.` :
lastRead == "" ? `\nThe file appears to have been deleted before a result could be retrieved. Perhaps there is a conflicting script.` :
`\nThe script was likely passed invalid arguments. Please post a screenshot of this error on discord.`),
maxRetries, retryDelayMs, undefined, verbose, verbose);
if (verbose) log(ns, `Read the following data for command ${command}:\n${fileData}`);
return JSON.parse(fileData); // Deserialize it back into an object/array and return
}
/** Evaluate an arbitrary ns command by writing it to a new script and then running or executing it.
* @param {NS} ns - The nestcript instance passed to your script's main entry point
* @param {string} command - The ns command that should be invoked to get the desired data (e.g. "ns.getServer('home')" )
* @param {string=} fileName - (default "/Temp/{commandhash}-data.txt") The name of the file to which data will be written to disk by a temporary process
* @param {args=} args - args to be passed in as arguments to command being run as a new script.
* @param {bool=} verbose - (default false) If set to true, the evaluation result of the command is printed to the terminal
*/
export async function runCommand(ns, command, fileName, args = [], verbose = false, maxRetries = 5, retryDelayMs = 50) {
checkNsInstance(ns, '"runCommand"');
if (!verbose) disableLogs(ns, ['run']);
return await runCommand_Custom(ns, ns.run, command, fileName, args, verbose, maxRetries, retryDelayMs);
}
const _cachedExports = [];
/** @param {NS} ns - The nestcript instance passed to your script's main entry point
* @returns {string[]} The set of all funciton names exported by this file. */
function getExports(ns) {
if (_cachedExports.length > 0) return _cachedExports;
const scriptHelpersRows = ns.read(getFilePath('helpers.js')).split("\n");
for (const row of scriptHelpersRows) {
if (!row.startsWith("export")) continue;
const funcNameStart = row.indexOf("function") + "function".length + 1;
const funcNameEnd = row.indexOf("(", funcNameStart);
_cachedExports.push(row.substring(funcNameStart, funcNameEnd));
}
return _cachedExports;
}
/**
* An advanced version of runCommand that lets you pass your own "isAlive" test to reduce RAM requirements (e.g. to avoid referencing ns.isRunning)
* Importing incurs 0 GB RAM (assuming fnRun, fnWrite are implemented using another ns function you already reference elsewhere like ns.exec)
* @param {NS} ns - The nestcript instance passed to your script's main entry point
* @param {function} fnRun - A single-argument function used to start the new sript, e.g. `ns.run` or `(f,...args) => ns.exec(f, "home", ...args)`
* @param {string} command - The ns command that should be invoked to get the desired data (e.g. "ns.getServer('home')" )
* @param {string=} fileName - (default "/Temp/{commandhash}-data.txt") The name of the file to which data will be written to disk by a temporary process
* @param {args=} args - args to be passed in as arguments to command being run as a new script.
**/
export async function runCommand_Custom(ns, fnRun, command, fileName, args = [], verbose = false, maxRetries = 5, retryDelayMs = 50) {
checkNsInstance(ns, '"runCommand_Custom"');
if (!Array.isArray(args)) throw new Error(`args specified were a ${typeof args}, but an array is required.`);
if (!verbose) disableLogs(ns, ['sleep']);
// Auto-import any helpers that the temp script attempts to use
const required = getExports(ns).filter(e => command.includes(`${e}(`));
let script = (required.length > 0 ? `import { ${required.join(", ")} } from 'helpers.js'\n` : '') +
`export async function main(ns) { ${command} }`;
fileName = fileName || `/Temp/${hashCode(command)}-command.js`;
if (verbose)
log(ns, `INFO: Using a temporary script (${fileName}) to execute the command:` +
`\n ${command}\nWith the following arguments: ${JSON.stringify(args)}`);
// It's possible for the file to be deleted while we're trying to execute it, so even wrap writing the file in a retry
return await autoRetry(ns, async () => {
// To improve performance, don't re-write the temp script if it's already in place with the correct contents.
const oldContents = ns.read(fileName);
if (oldContents != script) {
if (oldContents) // Create some noise if temp scripts are being created with the same name but different contents
ns.tprint(`WARNING: Had to overwrite temp script ${fileName}\nOld Contents:\n${oldContents}\nNew Contents:\n${script}` +
`\nThis warning is generated as part of an effort to switch over to using only 'immutable' temp scripts. ` +
`Please paste a screenshot in Discord at https://discord.com/channels/415207508303544321/935667531111342200`);
ns.write(fileName, script, "w");
// Wait for the script to appear and be readable (game can be finicky on actually completing the write)
await autoRetry(ns, () => ns.read(fileName), c => c == script, () => `Temporary script ${fileName} is not available, ` +
`despite having written it. (Did a competing process delete or overwrite it?)`, maxRetries, retryDelayMs, undefined, verbose, verbose);
}
// Run the script, now that we're sure it is in place
return fnRun(fileName, 1 /* Always 1 thread */, ...args);
}, pid => pid !== 0,
() => `The temp script was not run (likely due to insufficient RAM).` +
`\n Script: ${fileName}\n Args: ${JSON.stringify(args)}\n Command: ${command}` +
`\nThe script that ran this will likely recover and try again later once you have more free ram.`,
maxRetries, retryDelayMs, undefined, verbose, verbose);
}
/**
* Wait for a process id to complete running
* Importing incurs a maximum of 0.1 GB RAM (for ns.isRunning)
* @param {NS} ns - The nestcript instance passed to your script's main entry point
* @param {int} pid - The process id to monitor
* @param {bool=} verbose - (default false) If set to true, pid and result of command are logged.
**/
export async function waitForProcessToComplete(ns, pid, verbose) {
checkNsInstance(ns, '"waitForProcessToComplete"');
if (!verbose) disableLogs(ns, ['isRunning']);
return await waitForProcessToComplete_Custom(ns, ns.isRunning, pid, verbose);
}
/**
* An advanced version of waitForProcessToComplete that lets you pass your own "isAlive" test to reduce RAM requirements (e.g. to avoid referencing ns.isRunning)
* Importing incurs 0 GB RAM (assuming fnIsAlive is implemented using another ns function you already reference elsewhere like ns.ps)
* @param {NS} ns - The nestcript instance passed to your script's main entry point
* @param {(pid: number) => Promise<boolean>} fnIsAlive - A single-argument function used to start the new sript, e.g. `ns.isRunning` or `pid => ns.ps("home").some(process => process.pid === pid)`
**/
export async function waitForProcessToComplete_Custom(ns, fnIsAlive, pid, verbose) {
checkNsInstance(ns, '"waitForProcessToComplete_Custom"');
if (!verbose) disableLogs(ns, ['sleep']);
// Wait for the PID to stop running (cheaper than e.g. deleting (rm) a possibly pre-existing file and waiting for it to be recreated)
let start = Date.now();
let sleepMs = 1;
let done = false;
for (var retries = 0; retries < 1000; retries++) {
if (!(await fnIsAlive(pid))) {
done = true;
break; // Script is done running
}
if (verbose && retries % 100 === 0) ns.print(`Waiting for pid ${pid} to complete... (${formatDuration(Date.now() - start)})`);
await ns.sleep(sleepMs);
sleepMs = Math.min(sleepMs * 2, 200);
}
// Make sure that the process has shut down and we haven't just stopped retrying
if (!done) {
let errorMessage = `run-command pid ${pid} is running much longer than expected. Max retries exceeded.`;
ns.print(errorMessage);
throw new Error(errorMessage);
}
}
/** If the argument is an Error instance, returns it as is, otherwise, returns a new Error instance. */
function asError(error) {
return error instanceof Error ? error : new Error(typeof error === 'string' ? error : JSON.stringify(error));
}
/** Helper to retry something that failed temporarily (can happen when e.g. we temporarily don't have enough RAM to run)
* @param {NS} ns - The nestcript instance passed to your script's main entry point */
export async function autoRetry(ns, fnFunctionThatMayFail, fnSuccessCondition, errorContext = "Success condition not met",
maxRetries = 5, initialRetryDelayMs = 50, backoffRate = 3, verbose = false, tprintFatalErrors = true) {
checkNsInstance(ns, '"autoRetry"');
let retryDelayMs = initialRetryDelayMs, attempts = 0;
while (attempts++ <= maxRetries) {
try {
const result = await fnFunctionThatMayFail()
const error = typeof errorContext === 'string' ? errorContext : errorContext();
if (!fnSuccessCondition(result))
throw asError(error);
return result;
}
catch (error) {
const fatal = attempts >= maxRetries;
log(ns, `${fatal ? 'FAIL' : 'INFO'}: Attempt ${attempts} of ${maxRetries} failed` +
(fatal ? `: ${typeof error === 'string' ? error : error.message || JSON.stringify(error)}` : `. Trying again in ${retryDelayMs}ms...`),
tprintFatalErrors && fatal, !verbose ? undefined : (fatal ? 'error' : 'info'))
if (fatal) throw asError(error);
await ns.sleep(retryDelayMs);
retryDelayMs *= backoffRate;
}
}
}
/** Helper to log a message, and optionally also tprint it and toast it
* @param {NS} ns - The nestcript instance passed to your script's main entry point */
export function log(ns, message = "", alsoPrintToTerminal = false, toastStyle = "", maxToastLength = Number.MAX_SAFE_INTEGER) {
checkNsInstance(ns, '"log"');
ns.print(message);
if (toastStyle) ns.toast(message.length <= maxToastLength ? message : message.substring(0, maxToastLength - 3) + "...", toastStyle);
if (alsoPrintToTerminal) {
ns.tprint(message);
// TODO: Find a way write things logged to the terminal to a "permanent" terminal log file, preferably without this becoming an async function.
// Perhaps we copy logs to a port so that a separate script can optionally pop and append them to a file.
//ns.write("log.terminal.txt", message + '\n', 'a'); // Note: we should get away with not awaiting this promise since it's not a script file
}
return message;
}
/** Helper to get a list of all hostnames on the network
* @param {NS} ns - The nestcript instance passed to your script's main entry point */
export function scanAllServers(ns) {
checkNsInstance(ns, '"scanAllServers"');
let discoveredHosts = []; // Hosts (a.k.a. servers) we have scanned
let hostsToScan = ["home"]; // Hosts we know about, but have no yet scanned
let infiniteLoopProtection = 9999; // In case you mess with this code, this should save you from getting stuck
while (hostsToScan.length > 0 && infiniteLoopProtection-- > 0) { // Loop until the list of hosts to scan is empty
let hostName = hostsToScan.pop(); // Get the next host to be scanned
discoveredHosts.push(hostName); // Mark this host as "scanned"
for (const connectedHost of ns.scan(hostName)) // "scan" (list all hosts connected to this one)
if (!discoveredHosts.includes(connectedHost) && !hostsToScan.includes(connectedHost)) // If we haven't found this host
hostsToScan.push(connectedHost); // Add it to the queue of hosts to be scanned
}
return discoveredHosts; // The list of scanned hosts should now be the set of all hosts in the game!
}
/** @param {NS} ns
* Get a dictionary of active source files, taking into account the current active bitnode as well (optionally disabled). **/
export async function getActiveSourceFiles(ns, includeLevelsFromCurrentBitnode = true) {
return await getActiveSourceFiles_Custom(ns, getNsDataThroughFile, includeLevelsFromCurrentBitnode);
}
/** @param {NS} ns
* @param {(ns: NS, command: string, fName?: string, args?: any, verbose?: any, maxRetries?: number, retryDelayMs?: number) => Promise<any>} fnGetNsDataThroughFile
* getActiveSourceFiles Helper that allows the user to pass in their chosen implementation of getNsDataThroughFile to minimize RAM usage **/
export async function getActiveSourceFiles_Custom(ns, fnGetNsDataThroughFile, includeLevelsFromCurrentBitnode = true) {
checkNsInstance(ns, '"getActiveSourceFiles"');
// Find out what source files the user has unlocked
let dictSourceFiles;
try {
dictSourceFiles = await fnGetNsDataThroughFile(ns,
`Object.fromEntries(ns.singularity.getOwnedSourceFiles().map(sf => [sf.n, sf.lvl]))`,
'/Temp/owned-source-files.txt');
} catch { dictSourceFiles = {}; } // If this fails (e.g. low RAM), return an empty dictionary
// If the user is currently in a given bitnode, they will have its features unlocked
if (includeLevelsFromCurrentBitnode) {
try {
const bitNodeN = (await fnGetNsDataThroughFile(ns, 'ns.getPlayer()', '/Temp/player-info.txt')).bitNodeN;
dictSourceFiles[bitNodeN] = Math.max(3, dictSourceFiles[bitNodeN] || 0);
} catch { /* We are expected to be fault-tolerant in low-ram conditions */ }
}
return dictSourceFiles;
}
/** @param {NS} ns
* Return bitnode multiplers, or null if they cannot be accessed. **/
export async function tryGetBitNodeMultipliers(ns) {
return await tryGetBitNodeMultipliers_Custom(ns, getNsDataThroughFile);
}
/** @param {NS} ns
* tryGetBitNodeMultipliers Helper that allows the user to pass in their chosen implementation of getNsDataThroughFile to minimize RAM usage **/
export async function tryGetBitNodeMultipliers_Custom(ns, fnGetNsDataThroughFile) {
checkNsInstance(ns, '"tryGetBitNodeMultipliers"');
let canGetBitNodeMultipliers = false;
try { canGetBitNodeMultipliers = 5 in (await getActiveSourceFiles_Custom(ns, fnGetNsDataThroughFile)); } catch { }
if (!canGetBitNodeMultipliers) return null;
try { return await fnGetNsDataThroughFile(ns, 'ns.getBitNodeMultipliers()', '/Temp/bitnode-multipliers.txt'); } catch { }
return null;
}
/** @param {NS} ns
* Returns the number of instances of the current script running on the specified host. **/
export async function instanceCount(ns, onHost = "home", warn = true, tailOtherInstances = true) {
checkNsInstance(ns, '"alreadyRunning"');
const scriptName = ns.getScriptName();
const others = await getNsDataThroughFile(ns, 'ns.ps(ns.args[0]).filter(p => p.filename == ns.args[1]).map(p => p.pid)',
'/Temp/ps-other-instances.txt', [onHost, scriptName]);
if (others.length >= 2) {
if (warn)
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)
others.slice(0, others.length - 1).forEach(pid => ns.tail(pid));
}
return others.length;
}
let cachedStockSymbols = null; // Cache of stock symbols since these never change
/** Helper function to get all stock symbols, or null if you do not have TIX api access.
* Caches symbols the first time they are successfully requested, since symbols never change.
* @param {NS} ns */
export async function getStockSymbols(ns) {
cachedStockSymbols ??= await getNsDataThroughFile(ns,
`(() => { try { return ns.stock.getSymbols(); } catch { return null; } })()`,
'/Temp/stock-symbols.txt');
return cachedStockSymbols;
}
/** Helper function to get the total value of stocks using as little RAM as possible.
* @param {NS} ns */
export async function getStocksValue(ns) {
let stockSymbols = await getStockSymbols(ns);
if (stockSymbols == null) return 0; // No TIX API Access
const helper = async (fn) => await getNsDataThroughFile(ns,
`Object.fromEntries(ns.args.map(sym => [sym, ns.stock.${fn}(sym)]))`, `/Temp/stock-${fn}.txt`, stockSymbols);
const askPrices = await helper('getAskPrice');
const bidPrices = await helper('getBidPrice');
const positions = await helper('getPosition');
return stockSymbols.map(sym => ({ sym, pos: positions[sym], ask: askPrices[sym], bid: bidPrices[sym] }))
.reduce((total, stk) => total + (stk.pos[0] * stk.bid) /* Long Value */ + stk.pos[2] * (stk.pos[3] * 2 - stk.ask) /* Short Value */
// Subtract commission only if we have one or more shares (this is money we won't get when we sell our position)
// If for some crazy reason we have shares both in the short and long position, we'll have to pay the commission twice (two separate sales)
- 100000 * (Math.sign(stk.pos[0]) + Math.sign(stk.pos[2])), 0);
}
/** @param {NS} ns
* Returns a helpful error message if we forgot to pass the ns instance to a function */
export function checkNsInstance(ns, fnName = "this function") {
if (ns === undefined || !ns.print) throw new Error(`The first argument to ${fnName} should be a 'ns' instance.`);
return ns;
}
/** A helper to parse the command line arguments with a bunch of extra features, such as
* - Loading a persistent defaults override from a local config file named after the script.
* - Rendering "--help" output without all scripts having to explicitly specify it
* @param {NS} ns
* @param {[string, string | number | boolean | string[]][]} argsSchema - Specification of possible command line args. **/
export function getConfiguration(ns, argsSchema) {
checkNsInstance(ns, '"getConfig"');
const scriptName = ns.getScriptName();
// If the user has a local config file, override the defaults in the argsSchema
const confName = `${scriptName}.config.txt`;
const overrides = ns.read(confName);
const overriddenSchema = overrides ? [...argsSchema] : argsSchema; // Clone the original args schema
if (overrides) {
try {
let parsedOverrides = JSON.parse(overrides); // Expect a parsable dict or array of 2-element arrays like args schema
if (Array.isArray(parsedOverrides)) parsedOverrides = Object.fromEntries(parsedOverrides);
log(ns, `INFO: Applying ${Object.keys(parsedOverrides).length} overriding default arguments from "${confName}"...`);
for (const key in parsedOverrides) {
const override = parsedOverrides[key];
const matchIndex = overriddenSchema.findIndex(o => o[0] == key);
const match = matchIndex === -1 ? null : overriddenSchema[matchIndex];
if (!match)
throw new Error(`Unrecognized key "${key}" does not match of this script's options: ` + JSON.stringify(argsSchema.map(a => a[0])));
else if (override === undefined)
throw new Error(`The key "${key}" appeared in the config with no value. Some value must be provided. Try null?`);
else if (match && JSON.stringify(match[1]) != JSON.stringify(override)) {
if (typeof (match[1]) !== typeof (override))
log(ns, `WARNING: The "${confName}" overriding "${key}" value: ${JSON.stringify(override)} has a different type (${typeof override}) than the ` +
`current default value ${JSON.stringify(match[1])} (${typeof match[1]}). The resulting behaviour may be unpredictable.`, false, 'warning');
else
log(ns, `INFO: Overriding "${key}" value: ${JSON.stringify(match[1])} -> ${JSON.stringify(override)}`);
overriddenSchema[matchIndex] = { ...match }; // Clone the (previously shallow-copied) object at this position of the new argsSchema
overriddenSchema[matchIndex][1] = override; // Update the value of the clone.
}
}
} catch (err) {
log(ns, `ERROR: There's something wrong with your config file "${confName}", it cannot be loaded.` +
`\nThe error encountered was: ${(typeof err === 'string' ? err : err.message || JSON.stringify(err))}` +
`\nYour config file should either be a dictionary e.g.: { "string-opt": "value", "num-opt": 123, "array-opt": ["one", "two"] }` +
`\nor an array of dict entries (2-element arrays) e.g.: [ ["string-opt", "value"], ["num-opt", 123], ["array-opt", ["one", "two"]] ]` +
`\n"${confName}" contains:\n${overrides}`, true, 'error', 80);
return null;
}
}
// Return the result of using the in-game args parser to combine the defaults with the command line args provided
try {
const finalOptions = ns.flags(overriddenSchema);
log(ns, `INFO: Running ${scriptName} with the following settings:` + Object.keys(finalOptions).filter(a => a != "_").map(a =>
`\n ${a.length == 1 ? "-" : "--"}${a} = ${finalOptions[a] === null ? "null" : JSON.stringify(finalOptions[a])}`).join("") +
`\nrun ${scriptName} --help to get more information about these options.`)
return finalOptions;
} catch (err) { // Detect if the user passed invalid arguments, and return help text
const error = ns.args.includes("help") || ns.args.includes("--help") ? null : // Detect if the user explictly asked for help and suppress the error
(typeof err === 'string' ? err : err.message || JSON.stringify(err));
// Try to parse documentation about each argument from the source code's comments
const source = ns.read(scriptName).split("\n");
let argsRow = 1 + source.findIndex(row => row.includes("argsSchema ="));
const optionDescriptions = {}
while (argsRow && argsRow < source.length) {
const nextArgRow = source[argsRow++].trim();
if (nextArgRow.length == 0) continue;
if (nextArgRow[0] == "]" || nextArgRow.includes(";")) break; // We've reached the end of the args schema
const commentSplit = nextArgRow.split("//").map(e => e.trim());
if (commentSplit.length != 2) continue; // This row doesn't appear to be in the format: [option...], // Comment
const optionSplit = commentSplit[0].split("'"); // Expect something like: ['name', someDefault]. All we need is the name
if (optionSplit.length < 2) continue;
optionDescriptions[optionSplit[1]] = commentSplit[1];
}
log(ns, (error ? `ERROR: There was an error parsing the script arguments provided: ${error}\n` : 'INFO: ') +
`${scriptName} possible arguments:` + argsSchema.map(a => `\n ${a[0].length == 1 ? " -" : "--"}${a[0].padEnd(30)} ` +
`Default: ${(a[1] === null ? "null" : JSON.stringify(a[1])).padEnd(10)}` +
(a[0] in optionDescriptions ? ` // ${optionDescriptions[a[0]]}` : '')).join("") + '\n' +
`\nTip: All argument names, and some values support auto-complete. Hit the <tab> key to autocomplete or see possible options.` +
`\nTip: Array arguments are populated by specifying the argument multiple times, e.g.:` +
`\n run ${scriptName} --arrayArg first --arrayArg second --arrayArg third to run the script with arrayArg=[first, second, third]` +
(!overrides ? `\nTip: You can override the default values by creating a config file named "${confName}" containing e.g.: { "arg-name": "preferredValue" }`
: overrides && !error ? `\nNote: The default values are being modified by overrides in your local "${confName}":\n${overrides}`
: `\nThis error may have been caused by your local overriding "${confName}" (especially if you changed the types of any options):\n${overrides}`), true);
return null; // Caller should handle null and shut down elegantly.
}
}
/** In order to pass in args to pass along to the startup/completion script, they may have to be quoted, when given as
* parameters to this script, but those quotes will have to be stripped when passing these along to a subsequent script as raw strings.
* @param {string[]} args - The the array-argument passed to the script.
* @returns {string[]} The the array-argument unescaped (or deserialized if a single argument starting with '[' was supplied]). */
export function unEscapeArrayArgs(args) {
// For convenience, also support args as a single stringified array
if (args.length == 1 && args[0].startsWith("[")) return JSON.parse(args[0]);
// Otherwise, args wrapped in quotes should have those quotes removed.
const escapeChars = ['"', "'", "`"];
return args.map(arg => escapeChars.some(c => arg.startsWith(c) && arg.endsWith(c)) ? arg.slice(1, -1) : arg);
}