diff --git a/src/DarkWeb/DarkWeb.tsx b/src/DarkWeb/DarkWeb.tsx index f89fa95e7..1857aece0 100644 --- a/src/DarkWeb/DarkWeb.tsx +++ b/src/DarkWeb/DarkWeb.tsx @@ -72,17 +72,12 @@ export function buyDarkwebItem(itemName: string): void { // buy and push Player.loseMoney(item.price, "other"); - const programsRef = Player.getHomeComputer().programs; - // Remove partially created program if there is one - const existingPartialExeIndex = programsRef.findIndex( - (program) => item?.program && program.startsWith(item?.program), - ); - // findIndex returns -1 if there is no match, we only want to splice on a match - if (existingPartialExeIndex > -1) { - programsRef.splice(existingPartialExeIndex, 1); + Player.getHomeComputer().pushProgram(item.program); + // Cancel if the program is in progress of writing + if (Player.createProgramName === item.program) { + Player.isWorking = false; + Player.resetWorkStatus(); } - // Add the newly bought, full .exe - Player.getHomeComputer().programs.push(item.program); Terminal.print( "You have purchased the " + item.program + " program. The new program can be found on your home computer.", diff --git a/src/NetscriptFunctions/Singularity.ts b/src/NetscriptFunctions/Singularity.ts index 7675ebdfd..28f7dcccd 100644 --- a/src/NetscriptFunctions/Singularity.ts +++ b/src/NetscriptFunctions/Singularity.ts @@ -547,8 +547,14 @@ export function NetscriptSingularity(player: IPlayer, workerScript: WorkerScript return true; } + player.getHomeComputer().pushProgram(item.program); + // Cancel if the program is in progress of writing + if (player.createProgramName === item.program) { + player.isWorking = false; + player.resetWorkStatus(); + } + player.loseMoney(item.price, "other"); - player.getHomeComputer().programs.push(item.program); workerScript.log( "purchaseProgram", () => `You have purchased the '${item.program}' program. The new program can be found on your home computer.`, diff --git a/src/Server/BaseServer.ts b/src/Server/BaseServer.ts index faa0d3c35..378fe640c 100644 --- a/src/Server/BaseServer.ts +++ b/src/Server/BaseServer.ts @@ -248,6 +248,19 @@ export class BaseServer { this.ramUsed = ram; } + pushProgram(program: string): void { + if (this.programs.includes(program)) return; + + // Remove partially created program if there is one + const existingPartialExeIndex = this.programs.findIndex((p) => p.startsWith(program)); + // findIndex returns -1 if there is no match, we only want to splice on a match + if (existingPartialExeIndex > -1) { + this.programs.splice(existingPartialExeIndex, 1); + } + + this.programs.push(program); + } + /** * Write to a script file * Overwrites existing files. Creates new files if the script does not eixst