From f9ed45a895c3a9bf126faaeaac5b3a13fcfe7914 Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Sat, 8 Jan 2022 11:29:00 -0500 Subject: [PATCH] Pass server to terminal actions that end later Adds the current server object to the finishAction handler so that if the player changes during the progress he'll hit the original server with the command. --- src/Terminal/ITerminal.tsx | 17 +++++---- src/Terminal/Terminal.ts | 61 +++++++++++++++++--------------- src/Terminal/commands/analyze.ts | 2 +- 3 files changed, 43 insertions(+), 37 deletions(-) diff --git a/src/Terminal/ITerminal.tsx b/src/Terminal/ITerminal.tsx index 840cbd65c..6c7518096 100644 --- a/src/Terminal/ITerminal.tsx +++ b/src/Terminal/ITerminal.tsx @@ -5,6 +5,7 @@ import { IPlayer } from "../PersonObjects/IPlayer"; import { IRouter } from "../ui/Router"; import { Settings } from "../Settings/Settings"; import { formatTime } from "../utils/helpers/formatTime"; +import { BaseServer } from "../Server/BaseServer"; export class Output { text: string; @@ -43,11 +44,13 @@ export class TTimer { time: number; timeLeft: number; action: "h" | "b" | "a" | "g" | "w"; + server?: BaseServer; - constructor(time: number, action: "h" | "b" | "a" | "g" | "w") { + constructor(time: number, action: "h" | "b" | "a" | "g" | "w", server?: BaseServer) { this.time = time; this.timeLeft = time; this.action = action; + this.server = server; } } @@ -74,16 +77,16 @@ export interface ITerminal { warn(s: string): void; clear(): void; - startAnalyze(): void; + startAnalyze(player: IPlayer): void; startBackdoor(player: IPlayer): void; startHack(player: IPlayer): void; startGrow(player: IPlayer): void; startWeaken(player: IPlayer): void; - finishHack(router: IRouter, player: IPlayer, cancelled?: boolean): void; - finishGrow(player: IPlayer, cancelled?: boolean): void; - finishWeaken(player: IPlayer, cancelled?: boolean): void; - finishBackdoor(router: IRouter, player: IPlayer, cancelled?: boolean): void; - finishAnalyze(player: IPlayer, cancelled?: boolean): void; + finishHack(router: IRouter, player: IPlayer, server: BaseServer, cancelled?: boolean): void; + finishGrow(player: IPlayer, server: BaseServer, cancelled?: boolean): void; + finishWeaken(player: IPlayer, server: BaseServer, cancelled?: boolean): void; + finishBackdoor(router: IRouter, player: IPlayer, server: BaseServer, cancelled?: boolean): void; + finishAnalyze(player: IPlayer, server: BaseServer, cancelled?: boolean): void; finishAction(router: IRouter, player: IPlayer, cancelled?: boolean): void; getFilepath(filename: string): string; getFile(player: IPlayer, filename: string): Script | TextFile | string | null; diff --git a/src/Terminal/Terminal.ts b/src/Terminal/Terminal.ts index de857e2bd..a317151d7 100644 --- a/src/Terminal/Terminal.ts +++ b/src/Terminal/Terminal.ts @@ -137,7 +137,7 @@ export class Terminal implements ITerminal { return; } if (!(server instanceof Server)) throw new Error("server should be normal server"); - this.startAction(calculateHackingTime(server, player) / 4, "h"); + this.startAction(calculateHackingTime(server, player) / 4, "h", server); } startGrow(player: IPlayer): void { @@ -147,7 +147,7 @@ export class Terminal implements ITerminal { return; } if (!(server instanceof Server)) throw new Error("server should be normal server"); - this.startAction(calculateGrowTime(server, player) / 16, "g"); + this.startAction(calculateGrowTime(server, player) / 16, "g", server); } startWeaken(player: IPlayer): void { const server = player.getCurrentServer(); @@ -156,7 +156,7 @@ export class Terminal implements ITerminal { return; } if (!(server instanceof Server)) throw new Error("server should be normal server"); - this.startAction(calculateWeakenTime(server, player) / 16, "w"); + this.startAction(calculateWeakenTime(server, player) / 16, "w", server); } startBackdoor(player: IPlayer): void { @@ -167,22 +167,23 @@ export class Terminal implements ITerminal { return; } if (!(server instanceof Server)) throw new Error("server should be normal server"); - this.startAction(calculateHackingTime(server, player) / 4, "b"); + this.startAction(calculateHackingTime(server, player) / 4, "b", server); } - startAnalyze(): void { + startAnalyze(player: IPlayer): void { this.print("Analyzing system..."); - this.startAction(1, "a"); + const server = player.getCurrentServer(); + this.startAction(1, "a", server); } - startAction(n: number, action: "h" | "b" | "a" | "g" | "w"): void { - this.action = new TTimer(n, action); + startAction(n: number, action: "h" | "b" | "a" | "g" | "w", server?: BaseServer): void { + this.action = new TTimer(n, action, server); } // Complete the hack/analyze command - finishHack(router: IRouter, player: IPlayer, cancelled = false): void { + finishHack(router: IRouter, player: IPlayer, server: BaseServer, cancelled = false): void { if (cancelled) return; - const server = player.getCurrentServer(); + if (server instanceof HacknetServer) { this.error("Cannot hack this kind of server"); return; @@ -218,25 +219,25 @@ export class Terminal implements ITerminal { const newSec = server.hackDifficulty; this.print( - `Hack successful! Gained ${numeralWrapper.formatMoney(moneyGained)} and ${numeralWrapper.formatExp( + `Hack successful on '${server.hostname}'! Gained ${numeralWrapper.formatMoney(moneyGained)} and ${numeralWrapper.formatExp( expGainedOnSuccess, )} hacking exp`, ); this.print( - `Security increased from ${numeralWrapper.formatSecurity(oldSec)} to ${numeralWrapper.formatSecurity(newSec)}`, + `Security increased on '${server.hostname}' from ${numeralWrapper.formatSecurity(oldSec)} to ${numeralWrapper.formatSecurity(newSec)}`, ); } else { // Failure player.gainHackingExp(expGainedOnFailure); this.print( - `Failed to hack ${server.hostname}. Gained ${numeralWrapper.formatExp(expGainedOnFailure)} hacking exp`, + `Failed to hack '${server.hostname}'. Gained ${numeralWrapper.formatExp(expGainedOnFailure)} hacking exp`, ); } } - finishGrow(player: IPlayer, cancelled = false): void { + finishGrow(player: IPlayer, server: BaseServer, cancelled = false): void { if (cancelled) return; - const server = player.getCurrentServer(); + if (server instanceof HacknetServer) { this.error("Cannot hack this kind of server"); return; @@ -253,13 +254,13 @@ export class Terminal implements ITerminal { )}. Gained ${numeralWrapper.formatExp(expGain)} hacking exp.`, ); this.print( - `Security increased from ${numeralWrapper.formatSecurity(oldSec)} to ${numeralWrapper.formatSecurity(newSec)}`, + `Security increased on '${server.hostname}' from ${numeralWrapper.formatSecurity(oldSec)} to ${numeralWrapper.formatSecurity(newSec)}`, ); } - finishWeaken(player: IPlayer, cancelled = false): void { + finishWeaken(player: IPlayer, server: BaseServer, cancelled = false): void { if (cancelled) return; - const server = player.getCurrentServer(); + if (server instanceof HacknetServer) { this.error("Cannot hack this kind of server"); return; @@ -270,16 +271,15 @@ export class Terminal implements ITerminal { server.weaken(CONSTANTS.ServerWeakenAmount); const newSec = server.hackDifficulty; this.print( - `Security decreased from ${numeralWrapper.formatSecurity(oldSec)} to ${numeralWrapper.formatSecurity( + `Security decreased on '${server.hostname}' from ${numeralWrapper.formatSecurity(oldSec)} to ${numeralWrapper.formatSecurity( newSec, )} (min: ${numeralWrapper.formatSecurity(server.minDifficulty)})` + ` and Gained ${numeralWrapper.formatExp(expGain)} hacking exp.`, ); } - finishBackdoor(router: IRouter, player: IPlayer, cancelled = false): void { + finishBackdoor(router: IRouter, player: IPlayer, server: BaseServer, cancelled = false): void { if (!cancelled) { - const server = player.getCurrentServer(); if (server instanceof HacknetServer) { this.error("Cannot hack this kind of server"); return; @@ -293,13 +293,12 @@ export class Terminal implements ITerminal { router.toBitVerse(false, false); return; } - this.print("Backdoor successful!"); + this.print(`Backdoor on '${server.hostname}' successful!`); } } - finishAnalyze(player: IPlayer, cancelled = false): void { + finishAnalyze(player: IPlayer, currServ: BaseServer, cancelled = false): void { if (!cancelled) { - const currServ = player.getCurrentServer(); const isHacknet = currServ instanceof HacknetServer; this.print(currServ.hostname + ": "); const org = currServ.organizationName; @@ -338,18 +337,22 @@ export class Terminal implements ITerminal { if (!cancelled) throw new Error("Finish action called when there was no action"); return; } + + if (!this.action.server) throw new Error("Missing action target server"); + this.print(this.getProgressText()); if (this.action.action === "h") { - this.finishHack(router, player, cancelled); + this.finishHack(router, player, this.action.server, cancelled); } else if (this.action.action === "g") { - this.finishGrow(player, cancelled); + this.finishGrow(player, this.action.server, cancelled); } else if (this.action.action === "w") { - this.finishWeaken(player, cancelled); + this.finishWeaken(player, this.action.server, cancelled); } else if (this.action.action === "b") { - this.finishBackdoor(router, player, cancelled); + this.finishBackdoor(router, player, this.action.server, cancelled); } else if (this.action.action === "a") { - this.finishAnalyze(player, cancelled); + this.finishAnalyze(player, this.action.server, cancelled); } + if (cancelled) { this.print("Cancelled"); } diff --git a/src/Terminal/commands/analyze.ts b/src/Terminal/commands/analyze.ts index cd8182353..2544d837f 100644 --- a/src/Terminal/commands/analyze.ts +++ b/src/Terminal/commands/analyze.ts @@ -14,5 +14,5 @@ export function analyze( terminal.error("Incorrect usage of analyze command. Usage: analyze"); return; } - terminal.startAnalyze(); + terminal.startAnalyze(player); }