bitburner-src/src/Terminal/Terminal.ts

834 lines
28 KiB
TypeScript
Raw Normal View History

2021-10-27 02:26:05 +02:00
import { ITerminal, Output, Link, RawOutput, TTimer } from "./ITerminal";
2021-09-17 08:58:02 +02:00
import { IRouter } from "../ui/Router";
2021-09-16 01:50:44 +02:00
import { IPlayer } from "../PersonObjects/IPlayer";
import { HacknetServer } from "../Hacknet/HacknetServer";
import { BaseServer } from "../Server/BaseServer";
2021-09-24 00:47:43 +02:00
import { Server } from "../Server/Server";
2021-09-16 01:50:44 +02:00
import { Programs } from "../Programs/Programs";
import { CodingContractResult } from "../CodingContracts";
2021-09-20 06:14:30 +02:00
import { TerminalEvents, TerminalClearEvents } from "./TerminalEvents";
2021-09-16 01:50:44 +02:00
import { TextFile } from "../TextFile";
import { Script } from "../Script/Script";
2021-09-24 22:34:21 +02:00
import { isScriptFilename } from "../Script/isScriptFilename";
2021-09-16 01:50:44 +02:00
import { CONSTANTS } from "../Constants";
2021-10-07 22:56:01 +02:00
import { GetServer, GetAllServers } from "../Server/AllServers";
2021-09-16 01:50:44 +02:00
import { removeLeadingSlash, isInRootDirectory, evaluateFilePath } from "./DirectoryHelpers";
import { checkIfConnectedToDarkweb } from "../DarkWeb/DarkWeb";
import { iTutorialNextStep, iTutorialSteps, ITutorial } from "../InteractiveTutorial";
2021-10-07 22:56:01 +02:00
import { getServerOnNetwork, processSingleServerGrowth } from "../Server/ServerHelpers";
2021-09-16 01:50:44 +02:00
import { ParseCommand, ParseCommands } from "./Parser";
2021-10-07 23:55:49 +02:00
import { SpecialServers } from "../Server/data/SpecialServers";
import { Settings } from "../Settings/Settings";
2021-09-25 20:42:57 +02:00
import { createProgressBarText } from "../utils/helpers/createProgressBarText";
2021-09-16 01:50:44 +02:00
import {
calculateHackingChance,
calculateHackingExpGain,
calculatePercentMoneyHacked,
calculateHackingTime,
calculateGrowTime,
calculateWeakenTime,
2021-09-16 01:50:44 +02:00
} from "../Hacking";
import { numeralWrapper } from "../ui/numeralFormat";
2021-09-25 20:42:57 +02:00
import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions";
2021-09-16 01:50:44 +02:00
import { alias } from "./commands/alias";
import { analyze } from "./commands/analyze";
import { backdoor } from "./commands/backdoor";
import { buy } from "./commands/buy";
import { cat } from "./commands/cat";
import { cd } from "./commands/cd";
import { check } from "./commands/check";
import { connect } from "./commands/connect";
2021-10-12 04:34:04 +02:00
import { cp } from "./commands/cp";
2021-09-16 01:50:44 +02:00
import { download } from "./commands/download";
import { expr } from "./commands/expr";
import { free } from "./commands/free";
import { grow } from "./commands/grow";
2021-09-16 01:50:44 +02:00
import { hack } from "./commands/hack";
import { help } from "./commands/help";
import { history } from "./commands/history";
2021-09-16 01:50:44 +02:00
import { home } from "./commands/home";
import { hostname } from "./commands/hostname";
import { kill } from "./commands/kill";
import { killall } from "./commands/killall";
import { ls } from "./commands/ls";
import { lscpu } from "./commands/lscpu";
import { mem } from "./commands/mem";
import { mv } from "./commands/mv";
import { nano } from "./commands/nano";
import { ps } from "./commands/ps";
import { rm } from "./commands/rm";
import { run } from "./commands/run";
import { scan } from "./commands/scan";
import { scananalyze } from "./commands/scananalyze";
import { scp } from "./commands/scp";
import { sudov } from "./commands/sudov";
import { tail } from "./commands/tail";
import { top } from "./commands/top";
import { unalias } from "./commands/unalias";
2021-12-17 18:48:34 +01:00
import { vim } from "./commands/vim";
import { weaken } from "./commands/weaken";
2021-09-16 01:50:44 +02:00
import { wget } from "./commands/wget";
2021-12-17 02:07:34 +01:00
import { hash } from "../hash/hash";
import { apr1 } from "./commands/apr1";
2021-09-16 01:50:44 +02:00
export class Terminal implements ITerminal {
2021-09-16 02:06:48 +02:00
// Flags to determine whether the player is currently running a hack or an analyze
2021-09-16 20:43:39 +02:00
action: TTimer | null = null;
2021-09-16 02:06:48 +02:00
commandHistory: string[] = [];
commandHistoryIndex = 0;
2021-12-17 02:07:34 +01:00
outputHistory: (Output | Link | RawOutput)[] = [
new Output(`Bitburner v${CONSTANTS.VersionString} (${hash()})`, "primary"),
];
2021-09-16 08:52:45 +02:00
2021-09-16 02:06:48 +02:00
// True if a Coding Contract prompt is opened
contractOpen = false;
// Full Path of current directory
// Excludes the trailing forward slash
currDir = "/";
2021-09-18 01:43:08 +02:00
process(router: IRouter, player: IPlayer, cycles: number): void {
2021-09-16 20:43:39 +02:00
if (this.action === null) return;
this.action.timeLeft -= (CONSTANTS._idleSpeed * cycles) / 1000;
2021-09-20 06:38:05 +02:00
if (this.action.timeLeft < 0.01) this.finishAction(router, player, false);
2021-09-19 06:46:39 +02:00
TerminalEvents.emit();
2021-09-16 20:43:39 +02:00
}
2021-10-27 02:26:05 +02:00
append(item: Output | Link | RawOutput): void {
this.outputHistory.push(item);
if (this.outputHistory.length > Settings.MaxTerminalCapacity) {
2021-09-25 01:09:19 +02:00
this.outputHistory.splice(0, this.outputHistory.length - Settings.MaxTerminalCapacity);
}
2021-09-19 06:46:39 +02:00
TerminalEvents.emit();
}
2021-09-16 21:49:37 +02:00
print(s: string): void {
this.append(new Output(s, "primary"));
2021-09-16 01:50:44 +02:00
}
2021-10-27 02:26:05 +02:00
printRaw(node: React.ReactNode): void {
this.append(new RawOutput(node));
}
2021-09-16 01:50:44 +02:00
error(s: string): void {
this.append(new Output(s, "error"));
2021-09-16 01:50:44 +02:00
}
2021-11-19 21:44:12 +01:00
success(s: string): void {
this.append(new Output(s, "success"));
}
info(s: string): void {
this.append(new Output(s, "info"));
}
warn(s: string): void {
this.append(new Output(s, "warn"));
}
2021-09-16 01:50:44 +02:00
startHack(player: IPlayer): void {
// Hacking through Terminal should be faster than hacking through a script
2021-09-24 00:47:43 +02:00
const server = player.getCurrentServer();
if (server instanceof HacknetServer) {
this.error("Cannot hack this kind of server");
return;
}
2021-10-07 22:04:04 +02:00
if (!(server instanceof Server)) throw new Error("server should be normal server");
this.startAction(calculateHackingTime(server, player) / 4, "h", server);
2021-09-16 01:50:44 +02:00
}
startGrow(player: IPlayer): void {
const server = player.getCurrentServer();
if (server instanceof HacknetServer) {
this.error("Cannot grow this kind of server");
return;
}
2021-10-07 22:04:04 +02:00
if (!(server instanceof Server)) throw new Error("server should be normal server");
this.startAction(calculateGrowTime(server, player) / 16, "g", server);
}
startWeaken(player: IPlayer): void {
const server = player.getCurrentServer();
if (server instanceof HacknetServer) {
this.error("Cannot weaken this kind of server");
return;
}
2021-10-07 22:04:04 +02:00
if (!(server instanceof Server)) throw new Error("server should be normal server");
this.startAction(calculateWeakenTime(server, player) / 16, "w", server);
}
2021-09-16 01:50:44 +02:00
startBackdoor(player: IPlayer): void {
// Backdoor should take the same amount of time as hack
2021-09-24 00:47:43 +02:00
const server = player.getCurrentServer();
if (server instanceof HacknetServer) {
this.error("Cannot backdoor this kind of server");
return;
}
2021-10-07 22:04:04 +02:00
if (!(server instanceof Server)) throw new Error("server should be normal server");
this.startAction(calculateHackingTime(server, player) / 4, "b", server);
2021-09-16 01:50:44 +02:00
}
startAnalyze(player: IPlayer): void {
2021-09-16 01:50:44 +02:00
this.print("Analyzing system...");
const server = player.getCurrentServer();
this.startAction(1, "a", server);
2021-09-16 01:50:44 +02:00
}
startAction(n: number, action: "h" | "b" | "a" | "g" | "w", server?: BaseServer): void {
this.action = new TTimer(n, action, server);
2021-09-16 01:50:44 +02:00
}
// Complete the hack/analyze command
finishHack(router: IRouter, player: IPlayer, server: BaseServer, cancelled = false): void {
2021-09-16 20:43:39 +02:00
if (cancelled) return;
2021-09-24 00:47:43 +02:00
if (server instanceof HacknetServer) {
this.error("Cannot hack this kind of server");
return;
}
2021-10-07 22:04:04 +02:00
if (!(server instanceof Server)) throw new Error("server should be normal server");
2021-09-16 20:43:39 +02:00
// Calculate whether hack was successful
const hackChance = calculateHackingChance(server, player);
const rand = Math.random();
const expGainedOnSuccess = calculateHackingExpGain(server, player);
const expGainedOnFailure = expGainedOnSuccess / 4;
if (rand < hackChance) {
// Success!
2021-11-29 05:51:01 +01:00
server.backdoorInstalled = true;
2021-10-07 23:55:49 +02:00
if (SpecialServers.WorldDaemon === server.hostname) {
2021-09-18 01:43:08 +02:00
router.toBitVerse(false, false);
2021-09-16 20:43:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
2021-09-16 20:43:39 +02:00
let moneyGained = calculatePercentMoneyHacked(server, player);
moneyGained = Math.floor(server.moneyAvailable * moneyGained);
if (moneyGained <= 0) {
moneyGained = 0;
} // Safety check
server.moneyAvailable -= moneyGained;
2021-10-27 20:18:33 +02:00
player.gainMoney(moneyGained, "hacking");
2021-09-16 20:43:39 +02:00
player.gainHackingExp(expGainedOnSuccess);
player.gainIntelligenceExp(expGainedOnSuccess / CONSTANTS.IntelligenceTerminalHackBaseExpGain);
2021-12-20 20:13:56 +01:00
const oldSec = server.hackDifficulty;
2021-09-16 20:43:39 +02:00
server.fortify(CONSTANTS.ServerFortifyAmount);
2021-12-20 20:13:56 +01:00
const newSec = server.hackDifficulty;
2021-09-16 20:43:39 +02:00
this.print(
2022-01-09 21:22:23 +01:00
`Hack successful on '${server.hostname}'! Gained ${numeralWrapper.formatMoney(
moneyGained,
)} and ${numeralWrapper.formatExp(expGainedOnSuccess)} hacking exp`,
2021-09-16 20:43:39 +02:00
);
2021-12-20 20:13:56 +01:00
this.print(
2022-01-09 21:22:23 +01:00
`Security increased on '${server.hostname}' from ${numeralWrapper.formatSecurity(
oldSec,
)} to ${numeralWrapper.formatSecurity(newSec)}`,
2021-12-20 20:13:56 +01:00
);
2021-09-16 20:43:39 +02:00
} else {
// Failure
player.gainHackingExp(expGainedOnFailure);
this.print(
`Failed to hack '${server.hostname}'. Gained ${numeralWrapper.formatExp(expGainedOnFailure)} hacking exp`,
2021-09-16 20:43:39 +02:00
);
2021-09-16 01:50:44 +02:00
}
}
finishGrow(player: IPlayer, server: BaseServer, cancelled = false): void {
if (cancelled) return;
if (server instanceof HacknetServer) {
this.error("Cannot grow this kind of server");
return;
}
2021-10-07 22:04:04 +02:00
if (!(server instanceof Server)) throw new Error("server should be normal server");
const expGain = calculateHackingExpGain(server, player);
2021-12-20 20:13:56 +01:00
const oldSec = server.hackDifficulty;
2021-11-14 02:53:21 +01:00
const growth = processSingleServerGrowth(server, 25, player, server.cpuCores) - 1;
2021-12-20 20:13:56 +01:00
const newSec = server.hackDifficulty;
2022-01-08 22:57:44 +01:00
player.gainHackingExp(expGain);
this.print(
`Available money on '${server.hostname}' grown by ${numeralWrapper.formatPercentage(
growth,
6,
)}. Gained ${numeralWrapper.formatExp(expGain)} hacking exp.`,
);
2021-12-20 20:13:56 +01:00
this.print(
2022-01-09 21:22:23 +01:00
`Security increased on '${server.hostname}' from ${numeralWrapper.formatSecurity(
oldSec,
)} to ${numeralWrapper.formatSecurity(newSec)}`,
2021-12-20 20:13:56 +01:00
);
}
finishWeaken(player: IPlayer, server: BaseServer, cancelled = false): void {
if (cancelled) return;
if (server instanceof HacknetServer) {
this.error("Cannot weaken this kind of server");
return;
}
2021-10-07 22:04:04 +02:00
if (!(server instanceof Server)) throw new Error("server should be normal server");
const expGain = calculateHackingExpGain(server, player);
2021-12-20 20:13:56 +01:00
const oldSec = server.hackDifficulty;
server.weaken(CONSTANTS.ServerWeakenAmount);
2021-12-20 20:13:56 +01:00
const newSec = server.hackDifficulty;
2022-01-08 22:57:44 +01:00
player.gainHackingExp(expGain);
this.print(
2022-01-09 21:22:23 +01:00
`Security decreased on '${server.hostname}' from ${numeralWrapper.formatSecurity(
oldSec,
)} to ${numeralWrapper.formatSecurity(newSec)} (min: ${numeralWrapper.formatSecurity(server.minDifficulty)})` +
2021-12-20 20:13:56 +01:00
` and Gained ${numeralWrapper.formatExp(expGain)} hacking exp.`,
);
}
finishBackdoor(router: IRouter, player: IPlayer, server: BaseServer, cancelled = false): void {
2021-09-16 01:50:44 +02:00
if (!cancelled) {
2021-09-24 00:47:43 +02:00
if (server instanceof HacknetServer) {
this.error("Cannot hack this kind of server");
return;
}
2021-10-07 22:04:04 +02:00
if (!(server instanceof Server)) throw new Error("server should be normal server");
server.backdoorInstalled = true;
2021-10-07 23:55:49 +02:00
if (SpecialServers.WorldDaemon === server.hostname) {
2021-09-16 01:50:44 +02:00
if (player.bitNodeN == null) {
player.bitNodeN = 1;
}
2021-09-18 01:43:08 +02:00
router.toBitVerse(false, false);
2021-09-16 01:50:44 +02:00
return;
}
this.print(`Backdoor on '${server.hostname}' successful!`);
2021-09-16 01:50:44 +02:00
}
}
finishAnalyze(player: IPlayer, currServ: BaseServer, cancelled = false): void {
2021-09-16 01:50:44 +02:00
if (!cancelled) {
const isHacknet = currServ instanceof HacknetServer;
this.print(currServ.hostname + ": ");
const org = currServ.organizationName;
this.print("Organization name: " + (!isHacknet ? org : "player"));
const hasAdminRights = (!isHacknet && currServ.hasAdminRights) || isHacknet;
this.print("Root Access: " + (hasAdminRights ? "YES" : "NO"));
const canRunScripts = hasAdminRights && currServ.maxRam > 0;
this.print("Can run scripts on this host: " + (canRunScripts ? "YES" : "NO"));
this.print("RAM: " + numeralWrapper.formatRAM(currServ.maxRam));
2021-09-24 00:47:43 +02:00
if (currServ instanceof Server) {
this.print("Backdoor: " + (currServ.backdoorInstalled ? "YES" : "NO"));
2021-09-24 00:47:43 +02:00
const hackingSkill = currServ.requiredHackingSkill;
2022-01-09 21:22:23 +01:00
this.print("Required hacking skill for hack() and backdoor: " + (!isHacknet ? hackingSkill : "N/A"));
2021-09-24 00:47:43 +02:00
const security = currServ.hackDifficulty;
this.print("Server security level: " + (!isHacknet ? numeralWrapper.formatServerSecurity(security) : "N/A"));
const hackingChance = calculateHackingChance(currServ, player);
this.print("Chance to hack: " + (!isHacknet ? numeralWrapper.formatPercentage(hackingChance) : "N/A"));
const hackingTime = calculateHackingTime(currServ, player) * 1000;
this.print("Time to hack: " + (!isHacknet ? convertTimeMsToTimeElapsedString(hackingTime, true) : "N/A"));
}
2021-09-16 01:50:44 +02:00
this.print(
2021-09-24 00:47:43 +02:00
`Total money available on server: ${
2021-10-07 22:04:04 +02:00
currServ instanceof Server ? numeralWrapper.formatMoney(currServ.moneyAvailable) : "N/A"
2021-09-24 00:47:43 +02:00
}`,
2021-09-16 01:50:44 +02:00
);
2021-09-24 00:47:43 +02:00
if (currServ instanceof Server) {
const numPort = currServ.numOpenPortsRequired;
this.print("Required number of open ports for NUKE: " + (!isHacknet ? numPort : "N/A"));
this.print("SSH port: " + (currServ.sshPortOpen ? "Open" : "Closed"));
this.print("FTP port: " + (currServ.ftpPortOpen ? "Open" : "Closed"));
this.print("SMTP port: " + (currServ.smtpPortOpen ? "Open" : "Closed"));
this.print("HTTP port: " + (currServ.httpPortOpen ? "Open" : "Closed"));
this.print("SQL port: " + (currServ.sqlPortOpen ? "Open" : "Closed"));
}
2021-09-16 01:50:44 +02:00
}
}
2021-09-18 01:43:08 +02:00
finishAction(router: IRouter, player: IPlayer, cancelled = false): void {
2021-09-16 20:43:39 +02:00
if (this.action === null) {
if (!cancelled) throw new Error("Finish action called when there was no action");
return;
}
if (!this.action.server) throw new Error("Missing action target server");
2021-09-16 20:43:39 +02:00
this.print(this.getProgressText());
if (this.action.action === "h") {
this.finishHack(router, player, this.action.server, cancelled);
} else if (this.action.action === "g") {
this.finishGrow(player, this.action.server, cancelled);
} else if (this.action.action === "w") {
this.finishWeaken(player, this.action.server, cancelled);
2021-09-16 20:43:39 +02:00
} else if (this.action.action === "b") {
this.finishBackdoor(router, player, this.action.server, cancelled);
2021-09-16 20:43:39 +02:00
} else if (this.action.action === "a") {
this.finishAnalyze(player, this.action.server, cancelled);
2021-09-16 01:50:44 +02:00
}
2021-09-16 20:43:39 +02:00
if (cancelled) {
this.print("Cancelled");
}
this.action = null;
2021-09-19 06:46:39 +02:00
TerminalEvents.emit();
2021-09-16 01:50:44 +02:00
}
getFile(player: IPlayer, filename: string): Script | TextFile | string | null {
if (isScriptFilename(filename)) {
return this.getScript(player, filename);
}
if (filename.endsWith(".lit")) {
return this.getLitFile(player, filename);
}
if (filename.endsWith(".txt")) {
return this.getTextFile(player, filename);
}
return null;
}
getFilepath(filename: string): string {
const path = evaluateFilePath(filename, this.cwd());
if (path == null) {
throw new Error(`Invalid file path specified: ${filename}`);
}
if (isInRootDirectory(path)) {
return removeLeadingSlash(path);
}
return path;
}
getScript(player: IPlayer, filename: string): Script | null {
const s = player.getCurrentServer();
const filepath = this.getFilepath(filename);
for (const script of s.scripts) {
if (filepath === script.filename) {
return script;
}
}
return null;
}
getTextFile(player: IPlayer, filename: string): TextFile | null {
const s = player.getCurrentServer();
const filepath = this.getFilepath(filename);
for (const txt of s.textFiles) {
if (filepath === txt.fn) {
return txt;
}
}
return null;
}
getLitFile(player: IPlayer, filename: string): string | null {
const s = player.getCurrentServer();
const filepath = this.getFilepath(filename);
for (const lit of s.messages) {
if (typeof lit === "string" && filepath === lit) {
return lit;
}
}
return null;
}
cwd(): string {
2021-09-16 02:06:48 +02:00
return this.currDir;
2021-09-16 01:50:44 +02:00
}
setcwd(dir: string): void {
2021-09-16 02:06:48 +02:00
this.currDir = dir;
2021-09-18 21:44:39 +02:00
TerminalEvents.emit();
2021-09-16 01:50:44 +02:00
}
async runContract(player: IPlayer, contractName: string): Promise<void> {
// There's already an opened contract
2021-09-16 02:06:48 +02:00
if (this.contractOpen) {
2021-09-16 01:50:44 +02:00
return this.error("There's already a Coding Contract in Progress");
}
const serv = player.getCurrentServer();
const contract = serv.getContract(contractName);
if (contract == null) {
return this.error("No such contract");
}
2021-09-16 02:06:48 +02:00
this.contractOpen = true;
2021-09-16 01:50:44 +02:00
const res = await contract.prompt();
switch (res) {
case CodingContractResult.Success:
if (contract.reward !== null) {
const reward = player.gainCodingContractReward(contract.reward, contract.getDifficulty());
this.print(`Contract SUCCESS - ${reward}`);
}
serv.removeContract(contract);
break;
case CodingContractResult.Failure:
++contract.tries;
if (contract.tries >= contract.getMaxNumTries()) {
2021-10-27 02:26:05 +02:00
this.error("Contract FAILED - Contract is now self-destructing");
2021-09-16 01:50:44 +02:00
serv.removeContract(contract);
} else {
2021-10-27 02:26:05 +02:00
this.error(`Contract FAILED - ${contract.getMaxNumTries() - contract.tries} tries remaining`);
2021-09-16 01:50:44 +02:00
}
break;
case CodingContractResult.Cancelled:
default:
this.print("Contract cancelled");
break;
}
2021-09-16 02:06:48 +02:00
this.contractOpen = false;
2021-09-16 01:50:44 +02:00
}
executeScanAnalyzeCommand(player: IPlayer, depth = 1, all = false): void {
// TODO Using array as stack for now, can make more efficient
this.print("~~~~~~~~~~ Beginning scan-analyze ~~~~~~~~~~");
this.print(" ");
// Map of all servers to keep track of which have been visited
const visited: {
[key: string]: number | undefined;
} = {};
2021-10-07 22:04:04 +02:00
for (const server of GetAllServers()) {
visited[server.hostname] = 0;
2021-09-16 01:50:44 +02:00
}
const stack: BaseServer[] = [];
const depthQueue: number[] = [0];
const currServ = player.getCurrentServer();
stack.push(currServ);
while (stack.length != 0) {
const s = stack.pop();
if (!s) continue;
const d = depthQueue.pop();
if (d === undefined) continue;
const isHacknet = s instanceof HacknetServer;
if (!all && (s as any).purchasedByPlayer && s.hostname != "home") {
continue; // Purchased server
2021-10-07 22:04:04 +02:00
} else if (visited[s.hostname] || d > depth) {
2021-09-16 01:50:44 +02:00
continue; // Already visited or out-of-depth
} else if (!all && isHacknet) {
continue; // Hacknet Server
} else {
2021-10-07 22:04:04 +02:00
visited[s.hostname] = 1;
2021-09-16 01:50:44 +02:00
}
for (let i = s.serversOnNetwork.length - 1; i >= 0; --i) {
const newS = getServerOnNetwork(s, i);
if (newS === null) continue;
stack.push(newS);
depthQueue.push(d + 1);
}
if (d == 0) {
continue;
} // Don't print current server
const titleDashes = Array((d - 1) * 4 + 1).join("-");
if (player.hasProgram(Programs.AutoLink.name)) {
2021-09-22 17:32:04 +02:00
this.append(new Link(titleDashes, s.hostname));
2021-09-16 01:50:44 +02:00
} else {
2021-09-22 18:56:55 +02:00
this.print(titleDashes + s.hostname);
2021-09-16 01:50:44 +02:00
}
const dashes = titleDashes + "--";
let c = "NO";
if (s.hasAdminRights) {
c = "YES";
}
2021-12-14 00:44:52 +01:00
this.print(
`${dashes}Root Access: ${c}${!isHacknet ? ", Required hacking skill: " + (s as any).requiredHackingSkill : ""}`,
);
2021-12-03 20:17:29 +01:00
2021-09-16 01:50:44 +02:00
if (s.hasOwnProperty("numOpenPortsRequired")) {
2021-12-14 00:44:52 +01:00
this.print(dashes + "Number of open ports required to NUKE: " + (s as any).numOpenPortsRequired);
2021-09-16 01:50:44 +02:00
}
2021-12-14 00:44:52 +01:00
this.print(dashes + "RAM: " + numeralWrapper.formatRAM(s.maxRam));
this.print(" ");
2021-09-16 01:50:44 +02:00
}
}
connectToServer(player: IPlayer, server: string): void {
2021-10-07 22:56:01 +02:00
const serv = GetServer(server);
2021-09-16 01:50:44 +02:00
if (serv == null) {
this.error("Invalid server. Connection failed.");
return;
}
player.getCurrentServer().isConnectedTo = false;
2021-10-07 23:55:49 +02:00
player.currentServer = serv.hostname;
2021-09-16 01:50:44 +02:00
player.getCurrentServer().isConnectedTo = true;
this.print("Connected to " + serv.hostname);
this.setcwd("/");
if (player.getCurrentServer().hostname == "darkweb") {
checkIfConnectedToDarkweb(); // Posts a 'help' message if connecting to dark web
}
}
2021-09-17 08:58:02 +02:00
executeCommands(router: IRouter, player: IPlayer, commands: string): void {
2021-09-16 01:50:44 +02:00
// Sanitize input
commands = commands.trim();
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
2021-09-16 02:06:48 +02:00
if (this.commandHistory[this.commandHistory.length - 1] != commands) {
this.commandHistory.push(commands);
if (this.commandHistory.length > 50) {
this.commandHistory.splice(0, 1);
2021-09-16 01:50:44 +02:00
}
player.terminalCommandHistory = this.commandHistory;
2021-09-16 01:50:44 +02:00
}
2021-09-16 02:06:48 +02:00
this.commandHistoryIndex = this.commandHistory.length;
2021-09-16 01:50:44 +02:00
const allCommands = ParseCommands(commands);
for (let i = 0; i < allCommands.length; i++) {
2021-09-17 08:58:02 +02:00
this.executeCommand(router, player, allCommands[i]);
2021-09-16 01:50:44 +02:00
}
}
2021-09-16 08:52:45 +02:00
clear(): void {
2021-12-17 02:53:44 +01:00
this.outputHistory = [new Output(`Bitburner v${CONSTANTS.VersionString} (${hash()})`, "primary")];
2021-09-18 21:44:39 +02:00
TerminalEvents.emit();
2021-09-20 06:14:30 +02:00
TerminalClearEvents.emit();
2021-09-16 08:52:45 +02:00
}
2021-09-16 20:43:39 +02:00
prestige(): void {
this.action = null;
this.clear();
}
2021-09-17 08:58:02 +02:00
executeCommand(router: IRouter, player: IPlayer, command: string): void {
2021-09-16 20:43:39 +02:00
if (this.action !== null) {
2021-09-16 01:50:44 +02:00
this.error(`Cannot execute command (${command}) while an action is in progress`);
return;
}
// Allow usage of ./
if (command.startsWith("./")) {
command = "run " + command.slice(2);
}
// Only split the first space
const commandArray = ParseCommand(command);
if (commandArray.length == 0) {
return;
}
const s = player.getCurrentServer();
/****************** Interactive Tutorial Terminal Commands ******************/
if (ITutorial.isRunning) {
2021-10-07 22:56:01 +02:00
const n00dlesServ = GetServer("n00dles");
2021-09-16 01:50:44 +02:00
if (n00dlesServ == null) {
throw new Error("Could not get n00dles server");
}
switch (ITutorial.currStep) {
case iTutorialSteps.TerminalHelp:
if (commandArray.length === 1 && commandArray[0] == "help") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalLs:
if (commandArray.length === 1 && commandArray[0] == "ls") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalScan:
if (commandArray.length === 1 && commandArray[0] == "scan") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalScanAnalyze1:
if (commandArray.length == 1 && commandArray[0] == "scan-analyze") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalScanAnalyze2:
if (commandArray.length == 2 && commandArray[0] == "scan-analyze" && commandArray[1] === 2) {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalConnect:
if (commandArray.length == 2) {
2021-10-07 23:55:49 +02:00
if (
commandArray[0] == "connect" &&
(commandArray[1] == "n00dles" || commandArray[1] == n00dlesServ.hostname)
) {
2021-09-16 01:50:44 +02:00
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Wrong command! Try again!");
2021-09-16 01:50:44 +02:00
return;
}
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalAnalyze:
if (commandArray.length === 1 && commandArray[0] === "analyze") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalNuke:
if (commandArray.length == 2 && commandArray[0] == "run" && commandArray[1] == "NUKE.exe") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalManualHack:
if (commandArray.length == 1 && commandArray[0] == "hack") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
2021-12-17 02:53:44 +01:00
case iTutorialSteps.TerminalHackingMechanics:
if (commandArray.length !== 1 || !["grow", "weaken", "hack"].includes(commandArray[0] + "")) {
this.error("Bad command. Please follow the tutorial");
return;
}
break;
2021-09-19 09:13:49 +02:00
case iTutorialSteps.TerminalGoHome:
if (commandArray.length == 1 && commandArray[0] == "home") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 09:13:49 +02:00
return;
}
break;
2021-09-16 01:50:44 +02:00
case iTutorialSteps.TerminalCreateScript:
if (commandArray.length == 2 && commandArray[0] == "nano" && commandArray[1] == "n00dles.script") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalFree:
if (commandArray.length == 1 && commandArray[0] == "free") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.TerminalRunScript:
if (commandArray.length == 2 && commandArray[0] == "run" && commandArray[1] == "n00dles.script") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
case iTutorialSteps.ActiveScriptsToTerminal:
if (commandArray.length == 2 && commandArray[0] == "tail" && commandArray[1] == "n00dles.script") {
iTutorialNextStep();
} else {
2021-10-27 02:26:05 +02:00
this.error("Bad command. Please follow the tutorial");
2021-09-19 06:46:39 +02:00
return;
2021-09-16 01:50:44 +02:00
}
break;
default:
2021-10-27 02:26:05 +02:00
this.error("Please follow the tutorial, or click 'EXIT' if you'd like to skip it");
2021-09-16 01:50:44 +02:00
return;
}
}
/****************** END INTERACTIVE TUTORIAL ******************/
/* Command parser */
const commandName = commandArray[0];
2021-12-03 20:44:32 +01:00
if (typeof commandName === "number" || typeof commandName === "boolean") {
2021-09-16 01:50:44 +02:00
this.error(`Command ${commandArray[0]} not found`);
return;
}
const commands: {
[key: string]: (
terminal: ITerminal,
2021-09-17 08:58:02 +02:00
router: IRouter,
2021-09-16 01:50:44 +02:00
player: IPlayer,
server: BaseServer,
2021-12-03 20:44:32 +01:00
args: (string | number | boolean)[],
2021-09-16 01:50:44 +02:00
) => void;
} = {
"scan-analyze": scananalyze,
2021-09-16 01:50:44 +02:00
alias: alias,
analyze: analyze,
backdoor: backdoor,
buy: buy,
cat: cat,
cd: cd,
check: check,
2021-09-16 08:52:45 +02:00
clear: () => this.clear(),
cls: () => this.clear(),
2021-09-16 01:50:44 +02:00
connect: connect,
2021-10-12 04:34:04 +02:00
cp: cp,
2021-09-16 01:50:44 +02:00
download: download,
expr: expr,
free: free,
grow: grow,
2021-09-16 01:50:44 +02:00
hack: hack,
help: help,
history: history,
2021-09-16 01:50:44 +02:00
home: home,
hostname: hostname,
kill: kill,
killall: killall,
ls: ls,
lscpu: lscpu,
mem: mem,
mv: mv,
nano: nano,
ps: ps,
rm: rm,
run: run,
scan: scan,
scp: scp,
sudov: sudov,
tail: tail,
apr1: apr1,
2021-09-16 01:50:44 +02:00
top: top,
unalias: unalias,
2021-12-17 18:48:34 +01:00
vim: vim,
weaken: weaken,
2021-09-16 01:50:44 +02:00
wget: wget,
};
const f = commands[commandName.toLowerCase()];
if (!f) {
this.error(`Command ${commandArray[0]} not found`);
return;
}
2021-09-17 08:58:02 +02:00
f(this, router, player, s, commandArray.slice(1));
2021-09-16 01:50:44 +02:00
}
2021-09-16 20:43:39 +02:00
getProgressText(): string {
if (this.action === null) throw new Error("trying to get the progress text when there's no action");
return createProgressBarText({
progress: (this.action.time - this.action.timeLeft) / this.action.time,
totalTicks: 50,
});
}
2021-09-16 01:50:44 +02:00
}