From 59da79a42799529556f4dc2c45b9aa310c75148e Mon Sep 17 00:00:00 2001 From: Heikki Aitakangas Date: Thu, 6 Jan 2022 02:56:10 +0200 Subject: [PATCH 1/2] Refactor netscriptDelay and script kill interaction Store the Promise reject function instead of resolve in WorkerScript, since it's only used when killing a script that's blocked in delay. And when killing a script, reject the delay Promise with the WorkerScript as cause. --- src/Netscript/WorkerScript.ts | 4 ++-- src/Netscript/killWorkerScript.ts | 4 ++-- src/NetscriptEvaluator.ts | 11 ++++++++--- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/Netscript/WorkerScript.ts b/src/Netscript/WorkerScript.ts index 304cd00bd..307e61731 100644 --- a/src/Netscript/WorkerScript.ts +++ b/src/Netscript/WorkerScript.ts @@ -33,9 +33,9 @@ export class WorkerScript { delay: number | null = null; /** - * Holds the Promise resolve() function for when the script is "blocked" by an async op + * Holds the Promise reject() function while the script is "blocked" by an async op */ - delayResolve?: () => void; + delayReject?: (reason?: any) => void; /** * Stores names of all functions that have logging disabled diff --git a/src/Netscript/killWorkerScript.ts b/src/Netscript/killWorkerScript.ts index 32f1586f8..17a3fd42c 100644 --- a/src/Netscript/killWorkerScript.ts +++ b/src/Netscript/killWorkerScript.ts @@ -138,8 +138,8 @@ function killNetscriptDelay(workerScript: WorkerScript): void { if (workerScript instanceof WorkerScript) { if (workerScript.delay) { clearTimeout(workerScript.delay); - if (workerScript.delayResolve) { - workerScript.delayResolve(); + if (workerScript.delayReject) { + workerScript.delayReject(workerScript); } } } diff --git a/src/NetscriptEvaluator.ts b/src/NetscriptEvaluator.ts index 915bc6534..1fc7a7420 100644 --- a/src/NetscriptEvaluator.ts +++ b/src/NetscriptEvaluator.ts @@ -3,12 +3,17 @@ import { GetServer } from "./Server/AllServers"; import { WorkerScript } from "./Netscript/WorkerScript"; export function netscriptDelay(time: number, workerScript: WorkerScript): Promise { - return new Promise(function (resolve) { + return new Promise(function (resolve, reject) { workerScript.delay = window.setTimeout(() => { workerScript.delay = null; - resolve(); + workerScript.delayReject = undefined; + + if (workerScript.env.stopFlag) + reject(workerScript); + else + resolve(); }, time); - workerScript.delayResolve = resolve; + workerScript.delayReject = reject; }); } From c6ff982b1d53124cbc9f66d5638b24a821ccd158 Mon Sep 17 00:00:00 2001 From: Heikki Aitakangas Date: Sat, 8 Jan 2022 22:04:01 +0200 Subject: [PATCH 2/2] Remove stopFlag checks made redundant by the netscriptDelay change --- src/NetscriptFunctions.ts | 7 ------- src/NetscriptFunctions/Corporation.ts | 9 --------- src/NetscriptFunctions/Singularity.ts | 3 --- src/NetscriptFunctions/Stanek.ts | 3 --- 4 files changed, 22 deletions(-) diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index cfafff2d4..3c67ef6fc 100644 --- a/src/NetscriptFunctions.ts +++ b/src/NetscriptFunctions.ts @@ -342,9 +342,6 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { ); return netscriptDelay(hackingTime * 1000, workerScript).then(function () { - if (workerScript.env.stopFlag) { - return Promise.reject(workerScript); - } const hackChance = calculateHackingChance(server, Player); const rand = Math.random(); let expGainedOnSuccess = calculateHackingExpGain(server, Player) * threads; @@ -614,9 +611,6 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { )} (t=${numeralWrapper.formatThreads(threads)}).`, ); return netscriptDelay(growTime * 1000, workerScript).then(function () { - if (workerScript.env.stopFlag) { - return Promise.reject(workerScript); - } const moneyBefore = server.moneyAvailable <= 0 ? 1 : server.moneyAvailable; processSingleServerGrowth(server, threads, Player, host.cpuCores); const moneyAfter = server.moneyAvailable; @@ -685,7 +679,6 @@ export function NetscriptFunctions(workerScript: WorkerScript): NS { )} (t=${numeralWrapper.formatThreads(threads)})`, ); return netscriptDelay(weakenTime * 1000, workerScript).then(function () { - if (workerScript.env.stopFlag) return Promise.reject(workerScript); const host = GetServer(workerScript.hostname); if (host === null) { workerScript.log("weaken", () => "Server is null, did it die?"); diff --git a/src/NetscriptFunctions/Corporation.ts b/src/NetscriptFunctions/Corporation.ts index 98037706c..67bb16e76 100644 --- a/src/NetscriptFunctions/Corporation.ts +++ b/src/NetscriptFunctions/Corporation.ts @@ -311,9 +311,6 @@ export function NetscriptCorporation( const job = helper.string("assignJob", "job", ajob); const employee = getEmployee(divisionName, cityName, employeeName); return netscriptDelay(1000, workerScript).then(function () { - if (workerScript.env.stopFlag) { - return Promise.reject(workerScript); - } return Promise.resolve(AssignJob(employee, job)); }); }, @@ -344,9 +341,6 @@ export function NetscriptCorporation( (60 * 1000) / (player.hacking_speed_mult * calculateIntelligenceBonus(player.intelligence, 1)), workerScript, ).then(function () { - if (workerScript.env.stopFlag) { - return Promise.reject(workerScript); - } return Promise.resolve(ThrowParty(corporation, office, costPerEmployee)); }); }, @@ -359,9 +353,6 @@ export function NetscriptCorporation( (60 * 1000) / (player.hacking_speed_mult * calculateIntelligenceBonus(player.intelligence, 1)), workerScript, ).then(function () { - if (workerScript.env.stopFlag) { - return Promise.reject(workerScript); - } return Promise.resolve(BuyCoffee(corporation, getDivision(divisionName), getOffice(divisionName, cityName))); }); }, diff --git a/src/NetscriptFunctions/Singularity.ts b/src/NetscriptFunctions/Singularity.ts index f2e2e0542..2f8f3f7ef 100644 --- a/src/NetscriptFunctions/Singularity.ts +++ b/src/NetscriptFunctions/Singularity.ts @@ -611,9 +611,6 @@ export function NetscriptSingularity( ); return netscriptDelay(installTime, workerScript).then(function () { - if (workerScript.env.stopFlag) { - return Promise.reject(workerScript); - } workerScript.log("installBackdoor", () => `Successfully installed backdoor on '${server.hostname}'`); server.backdoorInstalled = true; diff --git a/src/NetscriptFunctions/Stanek.ts b/src/NetscriptFunctions/Stanek.ts index 94dd6d24b..66e084655 100644 --- a/src/NetscriptFunctions/Stanek.ts +++ b/src/NetscriptFunctions/Stanek.ts @@ -38,9 +38,6 @@ export function NetscriptStanek(player: IPlayer, workerScript: WorkerScript, hel if (!fragment) throw helper.makeRuntimeErrorMsg("stanek.charge", `No fragment with root (${rootX}, ${rootY}).`); const time = staneksGift.inBonus() ? 200 : 1000; return netscriptDelay(time, workerScript).then(function () { - if (workerScript.env.stopFlag) { - return Promise.reject(workerScript); - } const charge = staneksGift.charge(player, fragment, workerScript.scriptRef.threads); workerScript.log("stanek.charge", () => `Charged fragment for ${charge} charge.`); return Promise.resolve();