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";
const NewTerminal = new TTerminal();
import autosize from "autosize";
function postVersion() {
@ -28,14 +30,14 @@ $(document).keydown(function (event) {
// Terminal
if (routing.isOn(Page.Terminal)) {
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();
}
if (event.keyCode === KEY.ENTER) {
event.preventDefault(); // Prevent newline from being entered in Script Editor
const command = getTerminalInput();
const dir = Terminal.currDir;
const dir = NewTerminal.currDir;
post(
"<span class='prompt'>[" +
(FconfSettings.ENABLE_TIMESTAMPS ? getTimestamp() + " " : "") +
@ -45,7 +47,7 @@ $(document).keydown(function (event) {
if (command.length > 0) {
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
post("Cancelling...");
Engine._actionInProgress = false;
Terminal.finishAction(true);
NewTerminal.finishAction(true);
} else if (FconfSettings.ENABLE_BASH_HOTKEYS) {
// Dont prevent default so it still copies
Terminal.resetTerminalInput(); // Clear Terminal
@ -63,7 +65,7 @@ $(document).keydown(function (event) {
if (event.keyCode === KEY.L && event.ctrlKey) {
event.preventDefault();
new TTerminal().executeCommands(Engine, Player, "clear"); // Clear screen
NewTerminal.executeCommands(Engine, Player, "clear"); // Clear screen
}
// Ctrl p same as up arrow
@ -80,20 +82,20 @@ $(document).keydown(function (event) {
if (terminalInput == null) {
return;
}
var i = Terminal.commandHistoryIndex;
var len = Terminal.commandHistory.length;
var i = NewTerminal.commandHistoryIndex;
var len = NewTerminal.commandHistory.length;
if (len == 0) {
return;
}
if (i < 0 || i > len) {
Terminal.commandHistoryIndex = len;
NewTerminal.commandHistoryIndex = len;
}
if (i != 0) {
--Terminal.commandHistoryIndex;
--NewTerminal.commandHistoryIndex;
}
var prevCommand = Terminal.commandHistory[Terminal.commandHistoryIndex];
var prevCommand = NewTerminal.commandHistory[NewTerminal.commandHistoryIndex];
terminalInput.value = prevCommand;
setTimeoutRef(function () {
terminalInput.selectionStart = terminalInput.selectionEnd = 10000;
@ -111,23 +113,23 @@ $(document).keydown(function (event) {
if (terminalInput == null) {
return;
}
var i = Terminal.commandHistoryIndex;
var len = Terminal.commandHistory.length;
var i = NewTerminal.commandHistoryIndex;
var len = NewTerminal.commandHistory.length;
if (len == 0) {
return;
}
if (i < 0 || i > len) {
Terminal.commandHistoryIndex = len;
NewTerminal.commandHistoryIndex = len;
}
// Latest command, put nothing
if (i == len || i == len - 1) {
Terminal.commandHistoryIndex = len;
NewTerminal.commandHistoryIndex = len;
terminalInput.value = "";
} else {
++Terminal.commandHistoryIndex;
var prevCommand = Terminal.commandHistory[Terminal.commandHistoryIndex];
++NewTerminal.commandHistoryIndex;
var prevCommand = NewTerminal.commandHistory[NewTerminal.commandHistoryIndex];
terminalInput.value = prevCommand;
}
}
@ -157,7 +159,7 @@ $(document).keydown(function (event) {
if (index < -1) {
index = 0;
}
const allPos = determineAllPossibilitiesForTabCompletion(Player, input, index, Terminal.currDir);
const allPos = determineAllPossibilitiesForTabCompletion(Player, input, index, NewTerminal.currDir);
if (allPos.length == 0) {
return;
}
@ -245,7 +247,7 @@ $(document).keydown(function (e) {
terminalCtrlPressed = true;
} else if (e.shiftKey) {
shiftKeyPressed = true;
} else if (terminalCtrlPressed || shiftKeyPressed || Terminal.contractOpen) {
} else if (terminalCtrlPressed || shiftKeyPressed || NewTerminal.contractOpen) {
// Don't focus
} else {
var inputTextBox = document.getElementById("terminal-input-text-box");
@ -271,29 +273,12 @@ $(document).keyup(function (e) {
});
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) {
let input = "";
if (keepInput) {
input = getTerminalInput();
}
const dir = Terminal.currDir;
const dir = NewTerminal.currDir;
if (FconfSettings.WRAP_INPUT) {
document.getElementById("terminal-input-td").innerHTML =
`<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";
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;
error(s: string): void;

@ -71,6 +71,23 @@ import { wget } from "./commands/wget";
import { clear } from "./commands/clear";
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 {
postContent(s, config);
}
@ -80,30 +97,30 @@ export class Terminal implements ITerminal {
}
startHack(player: IPlayer): void {
OldTerminal.hackFlag = true;
this.hackFlag = true;
// 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();
}
startBackdoor(player: IPlayer): void {
OldTerminal.backdoorFlag = true;
this.backdoorFlag = true;
// 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();
}
startAnalyze(): void {
OldTerminal.analyzeFlag = true;
OldTerminal.actionTime = 1;
this.analyzeFlag = true;
this.actionTime = 1;
this.print("Analyzing system...");
this.startAction();
}
startAction(): void {
OldTerminal.actionStarted = true;
this.actionStarted = true;
hackProgressPost("Time left:");
hackProgressBarPost("[");
@ -135,7 +152,7 @@ export class Terminal implements ITerminal {
player.bitNodeN = 1;
}
hackWorldDaemon(player.bitNodeN);
OldTerminal.hackFlag = false;
this.hackFlag = false;
return;
}
server.backdoorInstalled = true;
@ -168,7 +185,7 @@ export class Terminal implements ITerminal {
);
}
}
OldTerminal.hackFlag = false;
this.hackFlag = false;
}
finishBackdoor(player: IPlayer, cancelled = false): void {
@ -182,13 +199,13 @@ export class Terminal implements ITerminal {
player.bitNodeN = 1;
}
hackWorldDaemon(player.bitNodeN);
OldTerminal.backdoorFlag = false;
this.backdoorFlag = false;
return;
}
server.backdoorInstalled = true;
this.print("Backdoor successful!");
}
OldTerminal.backdoorFlag = false;
this.backdoorFlag = false;
}
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("SQL port: " + (currServ.sqlPortOpen ? "Open" : "Closed"));
}
OldTerminal.analyzeFlag = false;
this.analyzeFlag = false;
}
finishAction(player: IPlayer, cancelled = false): void {
if (OldTerminal.hackFlag) {
if (this.hackFlag) {
this.finishHack(player, cancelled);
} else if (OldTerminal.backdoorFlag) {
} else if (this.backdoorFlag) {
this.finishBackdoor(player, cancelled);
} else if (OldTerminal.analyzeFlag) {
} else if (this.analyzeFlag) {
this.finishAnalyze(player, cancelled);
}
// 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").attr("id", "old-hack-progress");
OldTerminal.resetTerminalInput();
this.resetTerminalInput();
$("input[class=terminal-input]").prop("disabled", false);
}
@ -308,16 +325,16 @@ export class Terminal implements ITerminal {
}
cwd(): string {
return OldTerminal.currDir;
return this.currDir;
}
setcwd(dir: string): void {
OldTerminal.currDir = dir;
this.currDir = dir;
}
async runContract(player: IPlayer, contractName: string): Promise<void> {
// There's already an opened contract
if (OldTerminal.contractOpen) {
if (this.contractOpen) {
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");
}
OldTerminal.contractOpen = true;
this.contractOpen = true;
const res = await contract.prompt();
switch (res) {
@ -356,7 +373,7 @@ export class Terminal implements ITerminal {
this.print("Contract cancelled");
break;
}
OldTerminal.contractOpen = false;
this.contractOpen = false;
}
executeScanAnalyzeCommand(player: IPlayer, depth = 1, all = false): void {
@ -427,7 +444,7 @@ export class Terminal implements ITerminal {
(() => {
const hostname = links[i].innerHTML.toString();
links[i].addEventListener("onclick", () => {
if (OldTerminal.analyzeFlag || OldTerminal.hackFlag || OldTerminal.backdoorFlag) {
if (this.analyzeFlag || this.hackFlag || this.backdoorFlag) {
return;
}
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
// Handle Terminal History - multiple commands should be saved as one
if (OldTerminal.commandHistory[OldTerminal.commandHistory.length - 1] != commands) {
OldTerminal.commandHistory.push(commands);
if (OldTerminal.commandHistory.length > 50) {
OldTerminal.commandHistory.splice(0, 1);
if (this.commandHistory[this.commandHistory.length - 1] != commands) {
this.commandHistory.push(commands);
if (this.commandHistory.length > 50) {
this.commandHistory.splice(0, 1);
}
}
OldTerminal.commandHistoryIndex = OldTerminal.commandHistory.length;
this.commandHistoryIndex = this.commandHistory.length;
const allCommands = ParseCommands(commands);
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 {
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`);
return;
}