From e7aa977eb0b12582f00230d13a0ba3173cf7d960 Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Sat, 15 Jan 2022 06:26:58 -0500 Subject: [PATCH] Persist terminal command history in game save Copy the terminal commands into the player object so that they are saved between game reloads. Adds a 'history' command to display history, and a 'history -c' command to clear it from both the current terminal & the player's save. --- src/PersonObjects/IPlayer.ts | 1 + src/PersonObjects/Player/PlayerObject.ts | 2 ++ src/Terminal/HelpText.ts | 7 ++++++ src/Terminal/Terminal.ts | 3 +++ src/Terminal/commands/history.ts | 27 ++++++++++++++++++++++++ src/Terminal/ui/TerminalInput.tsx | 6 ++++++ 6 files changed, 46 insertions(+) create mode 100644 src/Terminal/commands/history.ts diff --git a/src/PersonObjects/IPlayer.ts b/src/PersonObjects/IPlayer.ts index 3138e5c9d..7b5053adc 100644 --- a/src/PersonObjects/IPlayer.ts +++ b/src/PersonObjects/IPlayer.ts @@ -72,6 +72,7 @@ export interface IPlayer { sourceFiles: IPlayerOwnedSourceFile[]; exploits: Exploit[]; achievements: PlayerAchievement[]; + terminalCommandHistory: string[]; lastUpdate: number; totalPlaytime: number; diff --git a/src/PersonObjects/Player/PlayerObject.ts b/src/PersonObjects/Player/PlayerObject.ts index b2b859eaa..c03569225 100644 --- a/src/PersonObjects/Player/PlayerObject.ts +++ b/src/PersonObjects/Player/PlayerObject.ts @@ -77,6 +77,7 @@ export class PlayerObject implements IPlayer { sourceFiles: IPlayerOwnedSourceFile[]; exploits: Exploit[]; achievements: PlayerAchievement[]; + terminalCommandHistory: string[]; lastUpdate: number; totalPlaytime: number; @@ -471,6 +472,7 @@ export class PlayerObject implements IPlayer { this.exploits = []; this.achievements = []; + this.terminalCommandHistory = []; this.init = generalMethods.init; this.prestigeAugmentation = generalMethods.prestigeAugmentation; diff --git a/src/Terminal/HelpText.ts b/src/Terminal/HelpText.ts index a1d86591e..ba19f49c8 100644 --- a/src/Terminal/HelpText.ts +++ b/src/Terminal/HelpText.ts @@ -21,6 +21,7 @@ export const TerminalHelpText: string[] = [ " grow Spoof money in a servers bank account, increasing the amount available.", " hack Hack the current machine", " help [command] Display this help text, or the help text for a command", + " history [-c] Display the terminal history", " home Connect to home computer", " hostname Displays the hostname of the machine", " kill [script/pid] [args...] Stops the specified script on the current server ", @@ -255,6 +256,12 @@ export const HelpTexts: IMap = { " help scan-analyze", " ", ], + history: [ + "Usage: history [-c]", + " ", + "Without arguments, displays the terminal command history. To clear the history, pass in the '-c' argument.", + " ", + ], home: [ "Usage: home", " ", "Connect to your home computer. This will work no matter what server you are currently connected to.", " ", ], diff --git a/src/Terminal/Terminal.ts b/src/Terminal/Terminal.ts index 806e79fa7..2a64dc88f 100644 --- a/src/Terminal/Terminal.ts +++ b/src/Terminal/Terminal.ts @@ -48,6 +48,7 @@ import { free } from "./commands/free"; import { grow } from "./commands/grow"; import { hack } from "./commands/hack"; import { help } from "./commands/help"; +import { history } from "./commands/history"; import { home } from "./commands/home"; import { hostname } from "./commands/hostname"; import { kill } from "./commands/kill"; @@ -576,6 +577,7 @@ export class Terminal implements ITerminal { if (this.commandHistory.length > 50) { this.commandHistory.splice(0, 1); } + player.terminalCommandHistory = this.commandHistory; } this.commandHistoryIndex = this.commandHistory.length; const allCommands = ParseCommands(commands); @@ -785,6 +787,7 @@ export class Terminal implements ITerminal { grow: grow, hack: hack, help: help, + history: history, home: home, hostname: hostname, kill: kill, diff --git a/src/Terminal/commands/history.ts b/src/Terminal/commands/history.ts new file mode 100644 index 000000000..32a51f162 --- /dev/null +++ b/src/Terminal/commands/history.ts @@ -0,0 +1,27 @@ +import { ITerminal } from "../ITerminal"; +import { IRouter } from "../../ui/Router"; +import { IPlayer } from "../../PersonObjects/IPlayer"; +import { BaseServer } from "../../Server/BaseServer"; + +export function history( + terminal: ITerminal, + router: IRouter, + player: IPlayer, + server: BaseServer, + args: (string | number | boolean)[], +): void { + if (args.length === 0) { + terminal.commandHistory.forEach((command, index) => { + terminal.print(`${index.toString().padStart(2)} ${command}`); + }); + return; + } + const arg = args[0] + ""; + if (arg === "-c" || arg === "--clear") { + player.terminalCommandHistory = []; + terminal.commandHistory = []; + terminal.commandHistoryIndex = 1; + } else { + terminal.error("Incorrect usage of history command. usage: history [-c]"); + } +} diff --git a/src/Terminal/ui/TerminalInput.tsx b/src/Terminal/ui/TerminalInput.tsx index dc0a4ab86..bf9e1f400 100644 --- a/src/Terminal/ui/TerminalInput.tsx +++ b/src/Terminal/ui/TerminalInput.tsx @@ -52,6 +52,12 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React const [possibilities, setPossibilities] = useState([]); const classes = useStyles(); + // If we have no data in the current terminal history, let's initialize it from the player save + if (terminal.commandHistory.length === 0 && player.terminalCommandHistory.length > 0) { + terminal.commandHistory = player.terminalCommandHistory; + terminal.commandHistoryIndex = terminal.commandHistory.length; + } + // Need to run after state updates, for example if we need to move cursor // *after* we modify input useEffect(() => {