Merge pull request #2470 from MartinFournier/fix/terminal-finishaction-server

Pass server to terminal actions that end later
This commit is contained in:
hydroflame 2022-01-08 12:35:33 -05:00 committed by GitHub
commit 67899a4901
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 43 additions and 37 deletions

@ -5,6 +5,7 @@ import { IPlayer } from "../PersonObjects/IPlayer";
import { IRouter } from "../ui/Router"; import { IRouter } from "../ui/Router";
import { Settings } from "../Settings/Settings"; import { Settings } from "../Settings/Settings";
import { formatTime } from "../utils/helpers/formatTime"; import { formatTime } from "../utils/helpers/formatTime";
import { BaseServer } from "../Server/BaseServer";
export class Output { export class Output {
text: string; text: string;
@ -43,11 +44,13 @@ export class TTimer {
time: number; time: number;
timeLeft: number; timeLeft: number;
action: "h" | "b" | "a" | "g" | "w"; 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.time = time;
this.timeLeft = time; this.timeLeft = time;
this.action = action; this.action = action;
this.server = server;
} }
} }
@ -74,16 +77,16 @@ export interface ITerminal {
warn(s: string): void; warn(s: string): void;
clear(): void; clear(): void;
startAnalyze(): void; startAnalyze(player: IPlayer): void;
startBackdoor(player: IPlayer): void; startBackdoor(player: IPlayer): void;
startHack(player: IPlayer): void; startHack(player: IPlayer): void;
startGrow(player: IPlayer): void; startGrow(player: IPlayer): void;
startWeaken(player: IPlayer): void; startWeaken(player: IPlayer): void;
finishHack(router: IRouter, player: IPlayer, cancelled?: boolean): void; finishHack(router: IRouter, player: IPlayer, server: BaseServer, cancelled?: boolean): void;
finishGrow(player: IPlayer, cancelled?: boolean): void; finishGrow(player: IPlayer, server: BaseServer, cancelled?: boolean): void;
finishWeaken(player: IPlayer, cancelled?: boolean): void; finishWeaken(player: IPlayer, server: BaseServer, cancelled?: boolean): void;
finishBackdoor(router: IRouter, player: IPlayer, cancelled?: boolean): void; finishBackdoor(router: IRouter, player: IPlayer, server: BaseServer, cancelled?: boolean): void;
finishAnalyze(player: IPlayer, cancelled?: boolean): void; finishAnalyze(player: IPlayer, server: BaseServer, cancelled?: boolean): void;
finishAction(router: IRouter, player: IPlayer, cancelled?: boolean): void; finishAction(router: IRouter, player: IPlayer, cancelled?: boolean): void;
getFilepath(filename: string): string; getFilepath(filename: string): string;
getFile(player: IPlayer, filename: string): Script | TextFile | string | null; getFile(player: IPlayer, filename: string): Script | TextFile | string | null;

@ -137,7 +137,7 @@ export class Terminal implements ITerminal {
return; return;
} }
if (!(server instanceof Server)) throw new Error("server should be normal server"); 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 { startGrow(player: IPlayer): void {
@ -147,7 +147,7 @@ export class Terminal implements ITerminal {
return; return;
} }
if (!(server instanceof Server)) throw new Error("server should be normal server"); 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 { startWeaken(player: IPlayer): void {
const server = player.getCurrentServer(); const server = player.getCurrentServer();
@ -156,7 +156,7 @@ export class Terminal implements ITerminal {
return; return;
} }
if (!(server instanceof Server)) throw new Error("server should be normal server"); 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 { startBackdoor(player: IPlayer): void {
@ -167,22 +167,23 @@ export class Terminal implements ITerminal {
return; return;
} }
if (!(server instanceof Server)) throw new Error("server should be normal server"); 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.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 { startAction(n: number, action: "h" | "b" | "a" | "g" | "w", server?: BaseServer): void {
this.action = new TTimer(n, action); this.action = new TTimer(n, action, server);
} }
// Complete the hack/analyze command // 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; if (cancelled) return;
const server = player.getCurrentServer();
if (server instanceof HacknetServer) { if (server instanceof HacknetServer) {
this.error("Cannot hack this kind of server"); this.error("Cannot hack this kind of server");
return; return;
@ -218,25 +219,25 @@ export class Terminal implements ITerminal {
const newSec = server.hackDifficulty; const newSec = server.hackDifficulty;
this.print( this.print(
`Hack successful! Gained ${numeralWrapper.formatMoney(moneyGained)} and ${numeralWrapper.formatExp( `Hack successful on '${server.hostname}'! Gained ${numeralWrapper.formatMoney(moneyGained)} and ${numeralWrapper.formatExp(
expGainedOnSuccess, expGainedOnSuccess,
)} hacking exp`, )} hacking exp`,
); );
this.print( 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 { } else {
// Failure // Failure
player.gainHackingExp(expGainedOnFailure); player.gainHackingExp(expGainedOnFailure);
this.print( 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; if (cancelled) return;
const server = player.getCurrentServer();
if (server instanceof HacknetServer) { if (server instanceof HacknetServer) {
this.error("Cannot hack this kind of server"); this.error("Cannot hack this kind of server");
return; return;
@ -253,13 +254,13 @@ export class Terminal implements ITerminal {
)}. Gained ${numeralWrapper.formatExp(expGain)} hacking exp.`, )}. Gained ${numeralWrapper.formatExp(expGain)} hacking exp.`,
); );
this.print( 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; if (cancelled) return;
const server = player.getCurrentServer();
if (server instanceof HacknetServer) { if (server instanceof HacknetServer) {
this.error("Cannot hack this kind of server"); this.error("Cannot hack this kind of server");
return; return;
@ -270,16 +271,15 @@ export class Terminal implements ITerminal {
server.weaken(CONSTANTS.ServerWeakenAmount); server.weaken(CONSTANTS.ServerWeakenAmount);
const newSec = server.hackDifficulty; const newSec = server.hackDifficulty;
this.print( this.print(
`Security decreased from ${numeralWrapper.formatSecurity(oldSec)} to ${numeralWrapper.formatSecurity( `Security decreased on '${server.hostname}' from ${numeralWrapper.formatSecurity(oldSec)} to ${numeralWrapper.formatSecurity(
newSec, newSec,
)} (min: ${numeralWrapper.formatSecurity(server.minDifficulty)})` + )} (min: ${numeralWrapper.formatSecurity(server.minDifficulty)})` +
` and Gained ${numeralWrapper.formatExp(expGain)} hacking exp.`, ` 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) { if (!cancelled) {
const server = player.getCurrentServer();
if (server instanceof HacknetServer) { if (server instanceof HacknetServer) {
this.error("Cannot hack this kind of server"); this.error("Cannot hack this kind of server");
return; return;
@ -293,13 +293,12 @@ export class Terminal implements ITerminal {
router.toBitVerse(false, false); router.toBitVerse(false, false);
return; 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) { if (!cancelled) {
const currServ = player.getCurrentServer();
const isHacknet = currServ instanceof HacknetServer; const isHacknet = currServ instanceof HacknetServer;
this.print(currServ.hostname + ": "); this.print(currServ.hostname + ": ");
const org = currServ.organizationName; 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"); if (!cancelled) throw new Error("Finish action called when there was no action");
return; return;
} }
if (!this.action.server) throw new Error("Missing action target server");
this.print(this.getProgressText()); this.print(this.getProgressText());
if (this.action.action === "h") { if (this.action.action === "h") {
this.finishHack(router, player, cancelled); this.finishHack(router, player, this.action.server, cancelled);
} else if (this.action.action === "g") { } else if (this.action.action === "g") {
this.finishGrow(player, cancelled); this.finishGrow(player, this.action.server, cancelled);
} else if (this.action.action === "w") { } else if (this.action.action === "w") {
this.finishWeaken(player, cancelled); this.finishWeaken(player, this.action.server, cancelled);
} else if (this.action.action === "b") { } 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") { } else if (this.action.action === "a") {
this.finishAnalyze(player, cancelled); this.finishAnalyze(player, this.action.server, cancelled);
} }
if (cancelled) { if (cancelled) {
this.print("Cancelled"); this.print("Cancelled");
} }

@ -14,5 +14,5 @@ export function analyze(
terminal.error("Incorrect usage of analyze command. Usage: analyze"); terminal.error("Incorrect usage of analyze command. Usage: analyze");
return; return;
} }
terminal.startAnalyze(); terminal.startAnalyze(player);
} }