terminal parses boolean exactly.

This commit is contained in:
Olivier Gagnon 2021-12-03 14:44:32 -05:00
parent 99ed9b25a3
commit 13b08d7cc8
40 changed files with 67 additions and 55 deletions

@ -87,7 +87,7 @@ export function scriptCalculateOfflineProduction(runningScript: RunningScript):
//designated server, and false otherwise
export function findRunningScript(
filename: string,
args: (string | number)[],
args: (string | number | boolean)[],
server: BaseServer,
): RunningScript | null {
for (let i = 0; i < server.runningScripts.length; ++i) {

@ -31,7 +31,7 @@ export function ParseCommands(commands: string): string[] {
return out;
}
export function ParseCommand(command: string): (string | number)[] {
export function ParseCommand(command: string): (string | number | boolean)[] {
// This will be used to keep track of whether we're in a quote. This is for situations
// like the alias command:
// alias run="run NUKE.exe"
@ -101,6 +101,10 @@ export function ParseCommand(command: string): (string | number)[] {
// If this is a number, convert it from a string to number
if (isNumber(arg)) {
args.push(parseFloat(arg));
} else if (arg === "true") {
args.push(true);
} else if (arg === "false") {
args.push(false);
} else {
args.push(arg);
}

@ -718,7 +718,7 @@ export class Terminal implements ITerminal {
/****************** END INTERACTIVE TUTORIAL ******************/
/* Command parser */
const commandName = commandArray[0];
if (typeof commandName === "number") {
if (typeof commandName === "number" || typeof commandName === "boolean") {
this.error(`Command ${commandArray[0]} not found`);
return;
}
@ -729,7 +729,7 @@ export class Terminal implements ITerminal {
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
) => void;
} = {
"scan-analyze": scananalyze,

@ -9,7 +9,7 @@ export function alias(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length === 0) {
printAliases();

@ -8,7 +8,7 @@ export function analyze(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of analyze command. Usage: analyze");

@ -10,7 +10,7 @@ export function backdoor(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of backdoor command. Usage: backdoor");

@ -11,7 +11,7 @@ export function buy(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (!GetServer(SpecialServers.DarkWeb)) {
terminal.error(

@ -10,7 +10,7 @@ export function cat(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 1) {
terminal.error("Incorrect usage of cat command. Usage: cat [file]");

@ -10,7 +10,7 @@ export function cd(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length > 1) {
terminal.error("Incorrect number of arguments. Usage: cd [dir]");

@ -10,7 +10,7 @@ export function check(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length < 1) {
terminal.error("Incorrect number of arguments. Usage: check [script] [arg1] [arg2]...");

@ -9,7 +9,7 @@ export function connect(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
// Disconnect from current server in terminal and connect to new one
if (args.length !== 1) {

@ -9,7 +9,7 @@ export function cp(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
try {
if (args.length !== 2) {

@ -11,7 +11,7 @@ export function download(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
try {
if (args.length !== 1) {
@ -24,7 +24,7 @@ export function download(
const matchEnding = fn.length == 1 || fn === "*.*" ? null : fn.slice(1); // Treat *.* the same as *
const zip = new JSZip();
// Helper function to zip any file contents whose name matches the pattern
let zipFiles = (fileNames: string[], fileContents: string[]) => {
const zipFiles = (fileNames: string[], fileContents: string[]) => {
for (let i = 0; i < fileContents.length; ++i) {
let name = fileNames[i];
if (name.startsWith("/")) name = name.slice(1);
@ -34,13 +34,18 @@ export function download(
};
// In the case of script files, we pull from the server.scripts array
if (!matchEnding || isScriptFilename(matchEnding))
zipFiles(server.scripts.map(s => s.filename), server.scripts.map(s => s.code));
zipFiles(
server.scripts.map((s) => s.filename),
server.scripts.map((s) => s.code),
);
// In the case of text files, we pull from the server.scripts array
if (!matchEnding || matchEnding.endsWith(".txt"))
zipFiles(server.textFiles.map(s => s.fn), server.textFiles.map(s => s.text));
zipFiles(
server.textFiles.map((s) => s.fn),
server.textFiles.map((s) => s.text),
);
// Return an error if no files matched, rather than an empty zip folder
if (Object.keys(zip.files).length == 0)
return terminal.error(`No files match the pattern ${fn}`);
if (Object.keys(zip.files).length == 0) return terminal.error(`No files match the pattern ${fn}`);
const zipFn = `bitburner${isScriptFilename(fn) ? "Scripts" : fn === "*.txt" ? "Texts" : "Files"}.zip`;
zip.generateAsync({ type: "blob" }).then((content: any) => FileSaver.saveAs(content, zipFn));
return;

@ -8,7 +8,7 @@ export function expr(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length === 0) {
terminal.error("Incorrect usage of expr command. Usage: expr [math expression]");

@ -9,7 +9,7 @@ export function free(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of free command. Usage: free");

@ -9,7 +9,7 @@ export function grow(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of grow command. Usage: grow");

@ -9,7 +9,7 @@ export function hack(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of hack command. Usage: hack");

@ -9,7 +9,7 @@ export function help(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0 && args.length !== 1) {
terminal.error("Incorrect usage of help command. Usage: help");
@ -18,7 +18,7 @@ export function help(
if (args.length === 0) {
TerminalHelpText.forEach((line) => terminal.print(line));
} else {
const cmd = args[0];
const cmd = args[0] + "";
const txt = HelpTexts[cmd];
if (txt == null) {
terminal.error("No help topics match '" + cmd + "'");

@ -8,7 +8,7 @@ export function home(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of home command. Usage: home");

@ -8,7 +8,7 @@ export function hostname(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of hostname command. Usage: hostname");

@ -9,13 +9,16 @@ export function kill(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
try {
if (args.length < 1) {
terminal.error("Incorrect usage of kill command. Usage: kill [scriptname] [arg1] [arg2]...");
return;
}
if (typeof args[0] === "boolean") {
return;
}
// Kill by PID
if (typeof args[0] === "number") {

@ -10,7 +10,7 @@ export function ls(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
const numArgs = args.length;
function incorrectUsage(): void {

@ -9,7 +9,7 @@ export function mem(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
try {
if (args.length !== 1 && args.length !== 3) {

@ -11,7 +11,7 @@ export function mv(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 2) {
terminal.error(`Incorrect number of arguments. Usage: mv [src] [dest]`);

@ -10,7 +10,7 @@ export function nano(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 1) {
terminal.error("Incorrect usage of nano command. Usage: nano [scriptname]");

@ -8,7 +8,7 @@ export function ps(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of ps command. Usage: ps");

@ -9,7 +9,7 @@ export function rm(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 1) {
terminal.error("Incorrect number of arguments. Usage: rm [program/script]");

@ -11,7 +11,7 @@ export function run(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
// Run a program or a script
if (args.length < 1) {

@ -9,7 +9,7 @@ export function runProgram(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length < 1) {
return;

@ -14,7 +14,7 @@ export function runScript(
router: IRouter,
player: IPlayer,
server: BaseServer,
commandArgs: (string | number)[],
commandArgs: (string | number | boolean)[],
): void {
if (commandArgs.length < 1) {
terminal.error(

@ -9,7 +9,7 @@ export function scan(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of netstat/scan command. Usage: netstat/scan");

@ -9,7 +9,7 @@ export function scananalyze(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length === 0) {
terminal.executeScanAnalyzeCommand(player, 1);

@ -10,7 +10,7 @@ export function scp(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
try {
if (args.length !== 2) {

@ -8,7 +8,7 @@ export function sudov(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect number of arguments. Usage: sudov");

@ -12,7 +12,7 @@ export function tail(
router: IRouter,
player: IPlayer,
server: BaseServer,
commandArray: (string | number)[],
commandArray: (string | number | boolean)[],
): void {
try {
if (commandArray.length < 1) {
@ -67,7 +67,7 @@ export function tail(
// if there's no candidate then we just don't know.
terminal.error("No such script exists.");
} else {
} else if (typeof commandArray[0] === "number") {
const runningScript = findRunningScriptByPid(commandArray[0], server);
if (runningScript == null) {
terminal.error("No such script exists");

@ -10,7 +10,7 @@ export function top(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of top command. Usage: top");

@ -9,7 +9,7 @@ export function unalias(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 1) {
terminal.error("Incorrect usage of unalias name. Usage: unalias [alias]");

@ -9,7 +9,7 @@ export function weaken(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 0) {
terminal.error("Incorrect usage of weaken command. Usage: weaken");

@ -9,7 +9,7 @@ export function wget(
router: IRouter,
player: IPlayer,
server: BaseServer,
args: (string | number)[],
args: (string | number | boolean)[],
): void {
if (args.length !== 2) {
terminal.error("Incorrect usage of wget command. Usage: wget [url] [target file]");

@ -265,13 +265,6 @@ export async function determineAllPossibilitiesForTabCompletion(
return allPos;
}
if (isCommand("kill") || isCommand("tail") || isCommand("mem") || isCommand("check")) {
addAllScripts();
addAllDirectories();
return allPos;
}
if (isCommand("nano")) {
addAllScripts();
addAllTextFiles();
@ -292,7 +285,7 @@ export async function determineAllPossibilitiesForTabCompletion(
}
async function scriptAutocomplete(): Promise<string[] | undefined> {
if (!isCommand("run")) return;
if (!isCommand("run") && !isCommand("tail") && !isCommand("kill")) return;
const commands = ParseCommands(input);
if (commands.length === 0) return;
const command = ParseCommand(commands[commands.length - 1]);
@ -348,6 +341,13 @@ export async function determineAllPossibilitiesForTabCompletion(
addAllDirectories();
}
if (isCommand("kill") || isCommand("tail") || isCommand("mem") || isCommand("check")) {
addAllScripts();
addAllDirectories();
return allPos;
}
if (isCommand("cat")) {
addAllMessages();
addAllLitFiles();