move some stuff

This commit is contained in:
Olivier Gagnon 2021-09-15 20:06:48 -04:00
parent f0f57150aa
commit aba97d2baa
3 changed files with 83 additions and 64 deletions

@ -13,6 +13,8 @@ import { post } from "./ui/postToTerminal";
import { Terminal as TTerminal } from "./Terminal/Terminal"; import { Terminal as TTerminal } from "./Terminal/Terminal";
const NewTerminal = new TTerminal();
import autosize from "autosize"; import autosize from "autosize";
function postVersion() { function postVersion() {
@ -28,14 +30,14 @@ $(document).keydown(function (event) {
// Terminal // Terminal
if (routing.isOn(Page.Terminal)) { if (routing.isOn(Page.Terminal)) {
var terminalInput = document.getElementById("terminal-input-text-box"); var terminalInput = document.getElementById("terminal-input-text-box");
if (terminalInput != null && !event.ctrlKey && !event.shiftKey && !Terminal.contractOpen) { if (terminalInput != null && !event.ctrlKey && !event.shiftKey && !NewTerminal.contractOpen) {
terminalInput.focus(); terminalInput.focus();
} }
if (event.keyCode === KEY.ENTER) { if (event.keyCode === KEY.ENTER) {
event.preventDefault(); // Prevent newline from being entered in Script Editor event.preventDefault(); // Prevent newline from being entered in Script Editor
const command = getTerminalInput(); const command = getTerminalInput();
const dir = Terminal.currDir; const dir = NewTerminal.currDir;
post( post(
"<span class='prompt'>[" + "<span class='prompt'>[" +
(FconfSettings.ENABLE_TIMESTAMPS ? getTimestamp() + " " : "") + (FconfSettings.ENABLE_TIMESTAMPS ? getTimestamp() + " " : "") +
@ -45,7 +47,7 @@ $(document).keydown(function (event) {
if (command.length > 0) { if (command.length > 0) {
Terminal.resetTerminalInput(); // Clear input first Terminal.resetTerminalInput(); // Clear input first
new TTerminal().executeCommands(Engine, Player, command); NewTerminal.executeCommands(Engine, Player, command);
} }
} }
@ -54,7 +56,7 @@ $(document).keydown(function (event) {
// Cancel action // Cancel action
post("Cancelling..."); post("Cancelling...");
Engine._actionInProgress = false; Engine._actionInProgress = false;
Terminal.finishAction(true); NewTerminal.finishAction(true);
} else if (FconfSettings.ENABLE_BASH_HOTKEYS) { } else if (FconfSettings.ENABLE_BASH_HOTKEYS) {
// Dont prevent default so it still copies // Dont prevent default so it still copies
Terminal.resetTerminalInput(); // Clear Terminal Terminal.resetTerminalInput(); // Clear Terminal
@ -63,7 +65,7 @@ $(document).keydown(function (event) {
if (event.keyCode === KEY.L && event.ctrlKey) { if (event.keyCode === KEY.L && event.ctrlKey) {
event.preventDefault(); event.preventDefault();
new TTerminal().executeCommands(Engine, Player, "clear"); // Clear screen NewTerminal.executeCommands(Engine, Player, "clear"); // Clear screen
} }
// Ctrl p same as up arrow // Ctrl p same as up arrow
@ -80,20 +82,20 @@ $(document).keydown(function (event) {
if (terminalInput == null) { if (terminalInput == null) {
return; return;
} }
var i = Terminal.commandHistoryIndex; var i = NewTerminal.commandHistoryIndex;
var len = Terminal.commandHistory.length; var len = NewTerminal.commandHistory.length;
if (len == 0) { if (len == 0) {
return; return;
} }
if (i < 0 || i > len) { if (i < 0 || i > len) {
Terminal.commandHistoryIndex = len; NewTerminal.commandHistoryIndex = len;
} }
if (i != 0) { if (i != 0) {
--Terminal.commandHistoryIndex; --NewTerminal.commandHistoryIndex;
} }
var prevCommand = Terminal.commandHistory[Terminal.commandHistoryIndex]; var prevCommand = NewTerminal.commandHistory[NewTerminal.commandHistoryIndex];
terminalInput.value = prevCommand; terminalInput.value = prevCommand;
setTimeoutRef(function () { setTimeoutRef(function () {
terminalInput.selectionStart = terminalInput.selectionEnd = 10000; terminalInput.selectionStart = terminalInput.selectionEnd = 10000;
@ -111,23 +113,23 @@ $(document).keydown(function (event) {
if (terminalInput == null) { if (terminalInput == null) {
return; return;
} }
var i = Terminal.commandHistoryIndex; var i = NewTerminal.commandHistoryIndex;
var len = Terminal.commandHistory.length; var len = NewTerminal.commandHistory.length;
if (len == 0) { if (len == 0) {
return; return;
} }
if (i < 0 || i > len) { if (i < 0 || i > len) {
Terminal.commandHistoryIndex = len; NewTerminal.commandHistoryIndex = len;
} }
// Latest command, put nothing // Latest command, put nothing
if (i == len || i == len - 1) { if (i == len || i == len - 1) {
Terminal.commandHistoryIndex = len; NewTerminal.commandHistoryIndex = len;
terminalInput.value = ""; terminalInput.value = "";
} else { } else {
++Terminal.commandHistoryIndex; ++NewTerminal.commandHistoryIndex;
var prevCommand = Terminal.commandHistory[Terminal.commandHistoryIndex]; var prevCommand = NewTerminal.commandHistory[NewTerminal.commandHistoryIndex];
terminalInput.value = prevCommand; terminalInput.value = prevCommand;
} }
} }
@ -157,7 +159,7 @@ $(document).keydown(function (event) {
if (index < -1) { if (index < -1) {
index = 0; index = 0;
} }
const allPos = determineAllPossibilitiesForTabCompletion(Player, input, index, Terminal.currDir); const allPos = determineAllPossibilitiesForTabCompletion(Player, input, index, NewTerminal.currDir);
if (allPos.length == 0) { if (allPos.length == 0) {
return; return;
} }
@ -245,7 +247,7 @@ $(document).keydown(function (e) {
terminalCtrlPressed = true; terminalCtrlPressed = true;
} else if (e.shiftKey) { } else if (e.shiftKey) {
shiftKeyPressed = true; shiftKeyPressed = true;
} else if (terminalCtrlPressed || shiftKeyPressed || Terminal.contractOpen) { } else if (terminalCtrlPressed || shiftKeyPressed || NewTerminal.contractOpen) {
// Don't focus // Don't focus
} else { } else {
var inputTextBox = document.getElementById("terminal-input-text-box"); var inputTextBox = document.getElementById("terminal-input-text-box");
@ -271,29 +273,12 @@ $(document).keyup(function (e) {
}); });
let Terminal = { let Terminal = {
// Flags to determine whether the player is currently running a hack or an analyze
hackFlag: false,
backdoorFlag: false,
analyzeFlag: false,
actionStarted: false,
actionTime: 0,
commandHistory: [],
commandHistoryIndex: 0,
// True if a Coding Contract prompt is opened
contractOpen: false,
// Full Path of current directory
// Excludes the trailing forward slash
currDir: "/",
resetTerminalInput: function (keepInput = false) { resetTerminalInput: function (keepInput = false) {
let input = ""; let input = "";
if (keepInput) { if (keepInput) {
input = getTerminalInput(); input = getTerminalInput();
} }
const dir = Terminal.currDir; const dir = NewTerminal.currDir;
if (FconfSettings.WRAP_INPUT) { if (FconfSettings.WRAP_INPUT) {
document.getElementById("terminal-input-td").innerHTML = document.getElementById("terminal-input-td").innerHTML =
`<div id='terminal-input-header' class='prompt'>[${Player.getCurrentServer().hostname} ~${dir}]$ </div>` + `<div id='terminal-input-header' class='prompt'>[${Player.getCurrentServer().hostname} ~${dir}]$ </div>` +

@ -4,6 +4,23 @@ import { IPlayer } from "../PersonObjects/IPlayer";
import { IEngine } from "../IEngine"; import { IEngine } from "../IEngine";
export interface ITerminal { export interface ITerminal {
// Flags to determine whether the player is currently running a hack or an analyze
hackFlag: boolean;
backdoorFlag: boolean;
analyzeFlag: boolean;
actionStarted: boolean;
actionTime: number;
commandHistory: string[];
commandHistoryIndex: number;
// True if a Coding Contract prompt is opened
contractOpen: boolean;
// Full Path of current directory
// Excludes the trailing forward slash
currDir: string;
print(s: string, config?: any): void; print(s: string, config?: any): void;
error(s: string): void; error(s: string): void;

@ -71,6 +71,23 @@ import { wget } from "./commands/wget";
import { clear } from "./commands/clear"; import { clear } from "./commands/clear";
export class Terminal implements ITerminal { export class Terminal implements ITerminal {
// Flags to determine whether the player is currently running a hack or an analyze
hackFlag = false;
backdoorFlag = false;
analyzeFlag = false;
actionStarted = false;
actionTime = 0;
commandHistory: string[] = [];
commandHistoryIndex = 0;
// True if a Coding Contract prompt is opened
contractOpen = false;
// Full Path of current directory
// Excludes the trailing forward slash
currDir = "/";
print(s: string, config?: any): void { print(s: string, config?: any): void {
postContent(s, config); postContent(s, config);
} }
@ -80,30 +97,30 @@ export class Terminal implements ITerminal {
} }
startHack(player: IPlayer): void { startHack(player: IPlayer): void {
OldTerminal.hackFlag = true; this.hackFlag = true;
// Hacking through Terminal should be faster than hacking through a script // Hacking through Terminal should be faster than hacking through a script
OldTerminal.actionTime = calculateHackingTime(player.getCurrentServer(), player) / 4; this.actionTime = calculateHackingTime(player.getCurrentServer(), player) / 4;
this.startAction(); this.startAction();
} }
startBackdoor(player: IPlayer): void { startBackdoor(player: IPlayer): void {
OldTerminal.backdoorFlag = true; this.backdoorFlag = true;
// Backdoor should take the same amount of time as hack // Backdoor should take the same amount of time as hack
OldTerminal.actionTime = calculateHackingTime(player.getCurrentServer(), player) / 4; this.actionTime = calculateHackingTime(player.getCurrentServer(), player) / 4;
this.startAction(); this.startAction();
} }
startAnalyze(): void { startAnalyze(): void {
OldTerminal.analyzeFlag = true; this.analyzeFlag = true;
OldTerminal.actionTime = 1; this.actionTime = 1;
this.print("Analyzing system..."); this.print("Analyzing system...");
this.startAction(); this.startAction();
} }
startAction(): void { startAction(): void {
OldTerminal.actionStarted = true; this.actionStarted = true;
hackProgressPost("Time left:"); hackProgressPost("Time left:");
hackProgressBarPost("["); hackProgressBarPost("[");
@ -135,7 +152,7 @@ export class Terminal implements ITerminal {
player.bitNodeN = 1; player.bitNodeN = 1;
} }
hackWorldDaemon(player.bitNodeN); hackWorldDaemon(player.bitNodeN);
OldTerminal.hackFlag = false; this.hackFlag = false;
return; return;
} }
server.backdoorInstalled = true; server.backdoorInstalled = true;
@ -168,7 +185,7 @@ export class Terminal implements ITerminal {
); );
} }
} }
OldTerminal.hackFlag = false; this.hackFlag = false;
} }
finishBackdoor(player: IPlayer, cancelled = false): void { finishBackdoor(player: IPlayer, cancelled = false): void {
@ -182,13 +199,13 @@ export class Terminal implements ITerminal {
player.bitNodeN = 1; player.bitNodeN = 1;
} }
hackWorldDaemon(player.bitNodeN); hackWorldDaemon(player.bitNodeN);
OldTerminal.backdoorFlag = false; this.backdoorFlag = false;
return; return;
} }
server.backdoorInstalled = true; server.backdoorInstalled = true;
this.print("Backdoor successful!"); this.print("Backdoor successful!");
} }
OldTerminal.backdoorFlag = false; this.backdoorFlag = false;
} }
finishAnalyze(player: IPlayer, cancelled = false): void { finishAnalyze(player: IPlayer, cancelled = false): void {
@ -219,22 +236,22 @@ export class Terminal implements ITerminal {
this.print("HTTP port: " + (currServ.httpPortOpen ? "Open" : "Closed")); this.print("HTTP port: " + (currServ.httpPortOpen ? "Open" : "Closed"));
this.print("SQL port: " + (currServ.sqlPortOpen ? "Open" : "Closed")); this.print("SQL port: " + (currServ.sqlPortOpen ? "Open" : "Closed"));
} }
OldTerminal.analyzeFlag = false; this.analyzeFlag = false;
} }
finishAction(player: IPlayer, cancelled = false): void { finishAction(player: IPlayer, cancelled = false): void {
if (OldTerminal.hackFlag) { if (this.hackFlag) {
this.finishHack(player, cancelled); this.finishHack(player, cancelled);
} else if (OldTerminal.backdoorFlag) { } else if (this.backdoorFlag) {
this.finishBackdoor(player, cancelled); this.finishBackdoor(player, cancelled);
} else if (OldTerminal.analyzeFlag) { } else if (this.analyzeFlag) {
this.finishAnalyze(player, cancelled); this.finishAnalyze(player, cancelled);
} }
// Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal // Rename the progress bar so that the next hacks dont trigger it. Re-enable terminal
$("#hack-progress-bar").attr("id", "old-hack-progress-bar"); $("#hack-progress-bar").attr("id", "old-hack-progress-bar");
$("#hack-progress").attr("id", "old-hack-progress"); $("#hack-progress").attr("id", "old-hack-progress");
OldTerminal.resetTerminalInput(); this.resetTerminalInput();
$("input[class=terminal-input]").prop("disabled", false); $("input[class=terminal-input]").prop("disabled", false);
} }
@ -308,16 +325,16 @@ export class Terminal implements ITerminal {
} }
cwd(): string { cwd(): string {
return OldTerminal.currDir; return this.currDir;
} }
setcwd(dir: string): void { setcwd(dir: string): void {
OldTerminal.currDir = dir; this.currDir = dir;
} }
async runContract(player: IPlayer, contractName: string): Promise<void> { async runContract(player: IPlayer, contractName: string): Promise<void> {
// There's already an opened contract // There's already an opened contract
if (OldTerminal.contractOpen) { if (this.contractOpen) {
return this.error("There's already a Coding Contract in Progress"); return this.error("There's already a Coding Contract in Progress");
} }
@ -327,7 +344,7 @@ export class Terminal implements ITerminal {
return this.error("No such contract"); return this.error("No such contract");
} }
OldTerminal.contractOpen = true; this.contractOpen = true;
const res = await contract.prompt(); const res = await contract.prompt();
switch (res) { switch (res) {
@ -356,7 +373,7 @@ export class Terminal implements ITerminal {
this.print("Contract cancelled"); this.print("Contract cancelled");
break; break;
} }
OldTerminal.contractOpen = false; this.contractOpen = false;
} }
executeScanAnalyzeCommand(player: IPlayer, depth = 1, all = false): void { executeScanAnalyzeCommand(player: IPlayer, depth = 1, all = false): void {
@ -427,7 +444,7 @@ export class Terminal implements ITerminal {
(() => { (() => {
const hostname = links[i].innerHTML.toString(); const hostname = links[i].innerHTML.toString();
links[i].addEventListener("onclick", () => { links[i].addEventListener("onclick", () => {
if (OldTerminal.analyzeFlag || OldTerminal.hackFlag || OldTerminal.backdoorFlag) { if (this.analyzeFlag || this.hackFlag || this.backdoorFlag) {
return; return;
} }
this.connectToServer(player, hostname); this.connectToServer(player, hostname);
@ -459,13 +476,13 @@ export class Terminal implements ITerminal {
commands = commands.replace(/\s\s+/g, " "); // Replace all extra whitespace in command with a single space commands = commands.replace(/\s\s+/g, " "); // Replace all extra whitespace in command with a single space
// Handle Terminal History - multiple commands should be saved as one // Handle Terminal History - multiple commands should be saved as one
if (OldTerminal.commandHistory[OldTerminal.commandHistory.length - 1] != commands) { if (this.commandHistory[this.commandHistory.length - 1] != commands) {
OldTerminal.commandHistory.push(commands); this.commandHistory.push(commands);
if (OldTerminal.commandHistory.length > 50) { if (this.commandHistory.length > 50) {
OldTerminal.commandHistory.splice(0, 1); this.commandHistory.splice(0, 1);
} }
} }
OldTerminal.commandHistoryIndex = OldTerminal.commandHistory.length; this.commandHistoryIndex = this.commandHistory.length;
const allCommands = ParseCommands(commands); const allCommands = ParseCommands(commands);
for (let i = 0; i < allCommands.length; i++) { for (let i = 0; i < allCommands.length; i++) {
@ -474,7 +491,7 @@ export class Terminal implements ITerminal {
} }
executeCommand(engine: IEngine, player: IPlayer, command: string): void { executeCommand(engine: IEngine, player: IPlayer, command: string): void {
if (OldTerminal.hackFlag || OldTerminal.backdoorFlag || OldTerminal.analyzeFlag) { if (this.hackFlag || this.backdoorFlag || this.analyzeFlag) {
this.error(`Cannot execute command (${command}) while an action is in progress`); this.error(`Cannot execute command (${command}) while an action is in progress`);
return; return;
} }