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.
This commit is contained in:
Martin Fournier 2022-01-15 06:26:58 -05:00
parent 07fe3c1906
commit e7aa977eb0
6 changed files with 46 additions and 0 deletions

@ -72,6 +72,7 @@ export interface IPlayer {
sourceFiles: IPlayerOwnedSourceFile[]; sourceFiles: IPlayerOwnedSourceFile[];
exploits: Exploit[]; exploits: Exploit[];
achievements: PlayerAchievement[]; achievements: PlayerAchievement[];
terminalCommandHistory: string[];
lastUpdate: number; lastUpdate: number;
totalPlaytime: number; totalPlaytime: number;

@ -77,6 +77,7 @@ export class PlayerObject implements IPlayer {
sourceFiles: IPlayerOwnedSourceFile[]; sourceFiles: IPlayerOwnedSourceFile[];
exploits: Exploit[]; exploits: Exploit[];
achievements: PlayerAchievement[]; achievements: PlayerAchievement[];
terminalCommandHistory: string[];
lastUpdate: number; lastUpdate: number;
totalPlaytime: number; totalPlaytime: number;
@ -471,6 +472,7 @@ export class PlayerObject implements IPlayer {
this.exploits = []; this.exploits = [];
this.achievements = []; this.achievements = [];
this.terminalCommandHistory = [];
this.init = generalMethods.init; this.init = generalMethods.init;
this.prestigeAugmentation = generalMethods.prestigeAugmentation; this.prestigeAugmentation = generalMethods.prestigeAugmentation;

@ -21,6 +21,7 @@ export const TerminalHelpText: string[] = [
" grow Spoof money in a servers bank account, increasing the amount available.", " grow Spoof money in a servers bank account, increasing the amount available.",
" hack Hack the current machine", " hack Hack the current machine",
" help [command] Display this help text, or the help text for a command", " help [command] Display this help text, or the help text for a command",
" history [-c] Display the terminal history",
" home Connect to home computer", " home Connect to home computer",
" hostname Displays the hostname of the machine", " hostname Displays the hostname of the machine",
" kill [script/pid] [args...] Stops the specified script on the current server ", " kill [script/pid] [args...] Stops the specified script on the current server ",
@ -255,6 +256,12 @@ export const HelpTexts: IMap<string[]> = {
" help scan-analyze", " help scan-analyze",
" ", " ",
], ],
history: [
"Usage: history [-c]",
" ",
"Without arguments, displays the terminal command history. To clear the history, pass in the '-c' argument.",
" ",
],
home: [ home: [
"Usage: home", " ", "Connect to your home computer. This will work no matter what server you are currently connected to.", " ", "Usage: home", " ", "Connect to your home computer. This will work no matter what server you are currently connected to.", " ",
], ],

@ -48,6 +48,7 @@ import { free } from "./commands/free";
import { grow } from "./commands/grow"; import { grow } from "./commands/grow";
import { hack } from "./commands/hack"; import { hack } from "./commands/hack";
import { help } from "./commands/help"; import { help } from "./commands/help";
import { history } from "./commands/history";
import { home } from "./commands/home"; import { home } from "./commands/home";
import { hostname } from "./commands/hostname"; import { hostname } from "./commands/hostname";
import { kill } from "./commands/kill"; import { kill } from "./commands/kill";
@ -576,6 +577,7 @@ export class Terminal implements ITerminal {
if (this.commandHistory.length > 50) { if (this.commandHistory.length > 50) {
this.commandHistory.splice(0, 1); this.commandHistory.splice(0, 1);
} }
player.terminalCommandHistory = this.commandHistory;
} }
this.commandHistoryIndex = this.commandHistory.length; this.commandHistoryIndex = this.commandHistory.length;
const allCommands = ParseCommands(commands); const allCommands = ParseCommands(commands);
@ -785,6 +787,7 @@ export class Terminal implements ITerminal {
grow: grow, grow: grow,
hack: hack, hack: hack,
help: help, help: help,
history: history,
home: home, home: home,
hostname: hostname, hostname: hostname,
kill: kill, kill: kill,

@ -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]");
}
}

@ -52,6 +52,12 @@ export function TerminalInput({ terminal, router, player }: IProps): React.React
const [possibilities, setPossibilities] = useState<string[]>([]); const [possibilities, setPossibilities] = useState<string[]>([]);
const classes = useStyles(); 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 // Need to run after state updates, for example if we need to move cursor
// *after* we modify input // *after* we modify input
useEffect(() => { useEffect(() => {