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; }); } diff --git a/src/NetscriptFunctions.ts b/src/NetscriptFunctions.ts index 856b514ae..af42f2465 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; @@ -615,9 +612,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; @@ -686,7 +680,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 64fc34497..c14de02af 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();