NETSCRIPT: Rework script ram updates (#408)

This commit is contained in:
Snarling
2023-03-05 22:39:42 -05:00
committed by GitHub
parent 14aafbe0a3
commit 759f86d6e5
24 changed files with 171 additions and 177 deletions
+4 -8
View File
@@ -33,6 +33,7 @@ import { BaseServer } from "../Server/BaseServer";
import { dialogBoxCreate } from "../ui/React/DialogBox";
import { checkEnum } from "../utils/helpers/enum";
import { RamCostConstants } from "./RamCostGenerator";
import { PositiveInteger } from "../types";
export const helpers = {
string,
@@ -158,12 +159,12 @@ function number(ctx: NetscriptContext, argName: string, v: unknown): number {
}
/** Convert provided value v for argument argName to a positive integer, throwing if it looks like something else. */
function positiveInteger(ctx: NetscriptContext, argName: string, v: unknown): number {
function positiveInteger(ctx: NetscriptContext, argName: string, v: unknown): PositiveInteger {
const n = number(ctx, argName, v);
if (n < 1 || !Number.isInteger(n)) {
throw makeRuntimeErrorMsg(ctx, `${argName} should be a positive integer, was ${n}`, "TYPE");
}
return n;
return n as PositiveInteger;
}
/** Returns args back if it is a ScriptArg[]. Throws an error if it is not. */
@@ -348,11 +349,6 @@ function updateDynamicRam(ctx: NetscriptContext, ramCost: number): void {
if (ws.dynamicLoadedFns[fnName]) return;
ws.dynamicLoadedFns[fnName] = true;
let threads = ws.scriptRef.threads;
if (typeof threads !== "number") {
console.warn(`WorkerScript detected NaN for thread count for ${ws.name} on ${ws.hostname}`);
threads = 1;
}
ws.dynamicRamUsage = Math.min(ws.dynamicRamUsage + ramCost, RamCostConstants.Max);
if (ws.dynamicRamUsage > 1.01 * ws.ramUsage) {
log(ctx, () => "Insufficient static ram available.");
@@ -362,7 +358,7 @@ function updateDynamicRam(ctx: NetscriptContext, ramCost: number): void {
`Dynamic RAM usage calculated to be greater than initial RAM usage.
This is probably because you somehow circumvented the static RAM calculation.
Threads: ${threads}
Threads: ${ws.scriptRef.threads}
Dynamic RAM Usage: ${formatRam(ws.dynamicRamUsage)} per thread
Static RAM Usage: ${formatRam(ws.ramUsage)} per thread