DNET: Add JS object properties as server names; refactor save/load/server storage to support this (#2482)

This commit is contained in:
Michael Ficocelli
2026-02-10 03:13:47 -05:00
committed by GitHub
parent b99e522d1c
commit bab6280735
14 changed files with 213 additions and 120 deletions
+27 -15
View File
@@ -25,7 +25,7 @@ export class RunningScript {
// Map of [key: hostname] -> Hacking data. Used for offline progress calculations.
// Hacking data format: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
dataMap: Record<string, number[]> = {};
dataMap: Map<string, number[]> = new Map();
// Script filename
filename = "default.js" as ScriptFilePath;
@@ -129,27 +129,23 @@ export class RunningScript {
// Update the moneyStolen and numTimesHack maps when hacking
recordHack(hostname: string, moneyGained: number, n = 1): void {
if (this.dataMap[hostname] == null || this.dataMap[hostname].constructor !== Array) {
this.dataMap[hostname] = [0, 0, 0, 0];
}
this.dataMap[hostname][0] += moneyGained;
this.dataMap[hostname][1] += n;
this.initDataMapIfNeeded(hostname);
const [hackMoney, hackCount, growCount, weakenCount] = this.dataMap.get(hostname) ?? [];
this.dataMap.set(hostname, [hackMoney + moneyGained, hackCount + n, growCount, weakenCount]);
}
// Update the grow map when calling grow()
recordGrow(hostname: string, n = 1): void {
if (this.dataMap[hostname] == null || this.dataMap[hostname].constructor !== Array) {
this.dataMap[hostname] = [0, 0, 0, 0];
}
this.dataMap[hostname][2] += n;
this.initDataMapIfNeeded(hostname);
const [hackMoney, hackCount, growCount, weakenCount] = this.dataMap.get(hostname) ?? [];
this.dataMap.set(hostname, [hackMoney, hackCount, growCount + n, weakenCount]);
}
// Update the weaken map when calling weaken() {
recordWeaken(hostname: string, n = 1): void {
if (this.dataMap[hostname] == null || this.dataMap[hostname].constructor !== Array) {
this.dataMap[hostname] = [0, 0, 0, 0];
}
this.dataMap[hostname][3] += n;
this.initDataMapIfNeeded(hostname);
const [hackMoney, hackCount, growCount, weakenCount] = this.dataMap.get(hostname) ?? [];
this.dataMap.set(hostname, [hackMoney, hackCount, growCount, weakenCount + n]);
}
// Serialize the current object to a JSON save state
@@ -157,7 +153,10 @@ export class RunningScript {
// Omit the title if it's a ReactNode, it will be filled in with the default on load.
return Generic_toJSON(
"RunningScript",
this,
{
...this,
dataMap: Object.fromEntries(this.dataMap.entries()),
},
typeof this.title === "string" ? includedProperties : includedPropsNoTitle,
);
}
@@ -165,14 +164,27 @@ export class RunningScript {
// Initializes a RunningScript Object from a JSON save state
static fromJSON(value: IReviverValue): RunningScript {
const runningScript = Generic_fromJSON(RunningScript, value.data, includedProperties);
const validEntries = Object.entries(runningScript.dataMap).filter(isValidDataMapEntry);
runningScript.dataMap = new Map(validEntries);
if (!runningScript.scriptKey) runningScript.scriptKey = scriptKey(runningScript.filename, runningScript.args);
if (!runningScript.title) runningScript.title = `${runningScript.filename} ${runningScript.args.join(" ")}`;
return runningScript;
}
initDataMapIfNeeded(hostname: string) {
if (!this.dataMap.has(hostname)) {
this.dataMap.set(hostname, [0, 0, 0, 0]);
}
}
}
const includedProperties = getKeyList(RunningScript, {
removedKeys: ["logs", "dependencies", "logUpd", "pid", "parent", "tailProps"],
});
const includedPropsNoTitle = includedProperties.filter((x) => x !== "title");
function isValidDataMapEntry(entry: [string, unknown]): entry is [string, number[]] {
const [, value] = entry;
return Array.isArray(value) && value.length === 4 && value.every((v) => typeof v === "number");
}
constructorsForReviver.RunningScript = RunningScript;
+33 -41
View File
@@ -32,29 +32,25 @@ export function scriptCalculateOfflineProduction(
//Data map: [MoneyStolen, NumTimesHacked, NumTimesGrown, NumTimesWeaken]
// Grow
for (const hostname of Object.keys(runningScript.dataMap)) {
if (Object.hasOwn(runningScript.dataMap, hostname)) {
if (runningScript.dataMap[hostname][2] == 0 || runningScript.dataMap[hostname][2] == null) {
continue;
}
const server = GetServer(hostname);
if (server == null) {
continue;
}
const timesGrown = Math.round(
((0.5 * runningScript.dataMap[hostname][2]) / runningScript.onlineRunningTime) * timePassed,
);
runningScript.log(`Called on ${server.hostname} ${timesGrown} times while offline`);
const host = GetServer(runningScript.server);
if (host === null) {
throw new Error("getServer of null key?");
}
if (!(server instanceof Server)) {
throw new Error("trying to grow a non-normal server");
}
const growth = processSingleServerGrowth(server, timesGrown, host.cpuCores);
runningScript.log(`'${server.hostname}' grown by ${formatPercent(growth - 1, 6)} while offline`);
for (const [hostname, [, , growCount]] of runningScript.dataMap.entries()) {
if (growCount == 0 || growCount == null) {
continue;
}
const server = GetServer(hostname);
if (server == null) {
continue;
}
const timesGrown = Math.round(((0.5 * growCount) / runningScript.onlineRunningTime) * timePassed);
runningScript.log(`Called on ${server.hostname} ${timesGrown} times while offline`);
const host = GetServer(runningScript.server);
if (host === null) {
throw new Error("getServer of null key?");
}
if (!(server instanceof Server)) {
throw new Error("trying to grow a non-normal server");
}
const growth = processSingleServerGrowth(server, timesGrown, host.cpuCores);
runningScript.log(`'${server.hostname}' grown by ${formatPercent(growth - 1, 6)} while offline`);
}
// Offline EXP gain
@@ -76,26 +72,22 @@ export function scriptCalculateOfflineProduction(
runningScript.offlineMoneyMade += moneyGain;
// Weaken
for (const hostname of Object.keys(runningScript.dataMap)) {
if (Object.hasOwn(runningScript.dataMap, hostname)) {
if (runningScript.dataMap[hostname][3] == 0 || runningScript.dataMap[hostname][3] == null) {
continue;
}
const serv = GetServer(hostname);
if (serv == null) {
continue;
}
if (!(serv instanceof Server)) throw new Error("trying to weaken a non-normal server");
const host = GetServer(runningScript.server);
if (host === null) throw new Error("getServer of null key?");
const timesWeakened = Math.round(
((0.5 * runningScript.dataMap[hostname][3]) / runningScript.onlineRunningTime) * timePassed,
);
runningScript.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`);
const weakenAmount = getWeakenEffect(runningScript.threads, host.cpuCores);
serv.weaken(weakenAmount * timesWeakened);
for (const [hostname, [, , , weakenCount]] of runningScript.dataMap.entries()) {
if (weakenCount == 0 || weakenCount == null) {
continue;
}
const serv = GetServer(hostname);
if (serv == null) {
continue;
}
if (!(serv instanceof Server)) throw new Error("trying to weaken a non-normal server");
const host = GetServer(runningScript.server);
if (host === null) throw new Error("getServer of null key?");
const timesWeakened = Math.round(((0.5 * weakenCount) / runningScript.onlineRunningTime) * timePassed);
runningScript.log(`Called weaken() on ${serv.hostname} ${timesWeakened} times while offline`);
const weakenAmount = getWeakenEffect(runningScript.threads, host.cpuCores);
serv.weaken(weakenAmount * timesWeakened);
}
}