NETSCRIPT: Add growThreads to formulas, improve docs for hacking functions (#330)

This commit is contained in:
David Walker
2023-02-14 14:38:51 -08:00
committed by GitHub
parent b9a2953fa6
commit ed59f4bfe7
5 changed files with 129 additions and 95 deletions
+13 -8
View File
@@ -297,22 +297,27 @@ export const ns: InternalAPI<NSFull> = {
},
growthAnalyze:
(ctx) =>
(_hostname, _growth, _cores = 1) => {
const hostname = helpers.string(ctx, "hostname", _hostname);
const growth = helpers.number(ctx, "growth", _growth);
(_host, _multiplier, _cores = 1) => {
const host = helpers.string(ctx, "hostname", _host);
const mult = helpers.number(ctx, "multiplier", _multiplier);
const cores = helpers.number(ctx, "cores", _cores);
// Check argument validity
const server = helpers.getServer(ctx, hostname);
const server = helpers.getServer(ctx, host);
if (!(server instanceof Server)) {
helpers.log(ctx, () => "Cannot be executed on this server.");
// Todo 2.3: Make this throw instead of returning 0?
helpers.log(ctx, () => `${host} is not a hackable server. Returning 0.`);
return 0;
}
if (typeof growth !== "number" || isNaN(growth) || growth < 1 || !isFinite(growth)) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: growth must be numeric and >= 1, is ${growth}.`);
if (mult < 1 || !isFinite(mult)) {
throw helpers.makeRuntimeErrorMsg(ctx, `Invalid argument: multiplier must be finite and >= 1, is ${mult}.`);
}
// TODO 2.3: Add assertion function for positive integer, there are a lot of places everywhere that can use this
if (!Number.isInteger(cores) || cores < 1) {
throw helpers.makeRuntimeErrorMsg(ctx, `Cores should be a positive integer. Cores provided: ${cores}`);
}
return numCycleForGrowth(server, Number(growth), cores);
return numCycleForGrowth(server, mult, cores);
},
growthAnalyzeSecurity:
(ctx) =>