mirror of
https://github.com/bitburner-official/bitburner-src.git
synced 2024-11-19 06:03:50 +01:00
[refactor] Determine program execution from terminal via hashmap instead
of switch statement. Introducing variables within the case block of a switch statement can be problematic and confusing as case statements do not have their own scope, and thus are hoisted to the function level. Thus, with a large enough switch statement, variable declarations could easily be overlooked as they are peppered throughout the cases. This is especially problematic for 'const' and 'let' declarations, which are very concerned about scoping. There is an ESLint rule specifically enabled to catch this behavior. This refactoring is a way to mitigate the behavior, while still allowing for the use of 'const'.
This commit is contained in:
parent
fabc45f496
commit
a09fcd21ca
168
src/Terminal.js
168
src/Terminal.js
@ -1831,97 +1831,116 @@ let Terminal = {
|
||||
if (splitArgs.length > 1) {
|
||||
programName = splitArgs[0];
|
||||
}
|
||||
switch (programName) {
|
||||
case Programs.NukeProgram.name:
|
||||
if (s.hasAdminRights) {
|
||||
|
||||
// TODO: refactor this/these out of Terminal. This logic could reside closer to the Programs themselves.
|
||||
/**
|
||||
* @typedef {function (server=, args=)} ProgramHandler
|
||||
* @param {Server} server The current server the program is being executed against
|
||||
* @param {string[]} args The command line arguments passed in to the program
|
||||
* @returns {void}
|
||||
*/
|
||||
/**
|
||||
* @type {Object.<string, ProgramHandler}
|
||||
*/
|
||||
const programHandlers = {};
|
||||
programHandlers[Programs.NukeProgram.name] = (server) => {
|
||||
if (server.hasAdminRights) {
|
||||
post("You already have root access to this computer. There is no reason to run NUKE.exe");
|
||||
} else {
|
||||
if (s.openPortCount >= Player.getCurrentServer().numOpenPortsRequired) {
|
||||
s.hasAdminRights = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (server.openPortCount >= Player.getCurrentServer().numOpenPortsRequired) {
|
||||
server.hasAdminRights = true;
|
||||
post("NUKE successful! Gained root access to " + Player.getCurrentServer().hostname);
|
||||
//TODO Make this take time rather than be instant
|
||||
} else {
|
||||
// TODO: Make this take time rather than be instant
|
||||
return;
|
||||
}
|
||||
|
||||
post("NUKE unsuccessful. Not enough ports have been opened");
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Programs.BruteSSHProgram.name:
|
||||
if (s.sshPortOpen) {
|
||||
};
|
||||
programHandlers[Programs.BruteSSHProgram.name] = (server) => {
|
||||
if (server.sshPortOpen) {
|
||||
post("SSH Port (22) is already open!");
|
||||
} else {
|
||||
s.sshPortOpen = true;
|
||||
return;
|
||||
}
|
||||
|
||||
server.sshPortOpen = true;
|
||||
post("Opened SSH Port(22)!")
|
||||
++s.openPortCount;
|
||||
}
|
||||
break;
|
||||
case Programs.FTPCrackProgram.name:
|
||||
if (s.ftpPortOpen) {
|
||||
server.openPortCount++;
|
||||
};
|
||||
programHandlers[Programs.FTPCrackProgram.name] = (server) => {
|
||||
if (server.ftpPortOpen) {
|
||||
post("FTP Port (21) is already open!");
|
||||
} else {
|
||||
s.ftpPortOpen = true;
|
||||
return;
|
||||
}
|
||||
|
||||
server.ftpPortOpen = true;
|
||||
post("Opened FTP Port (21)!");
|
||||
++s.openPortCount;
|
||||
}
|
||||
break;
|
||||
case Programs.RelaySMTPProgram.name:
|
||||
if (s.smtpPortOpen) {
|
||||
server.openPortCount++;
|
||||
};
|
||||
programHandlers[Programs.RelaySMTPProgram.name] = (server) => {
|
||||
if (server.smtpPortOpen) {
|
||||
post("SMTP Port (25) is already open!");
|
||||
} else {
|
||||
s.smtpPortOpen = true;
|
||||
return;
|
||||
}
|
||||
|
||||
server.smtpPortOpen = true;
|
||||
post("Opened SMTP Port (25)!");
|
||||
++s.openPortCount;
|
||||
}
|
||||
break;
|
||||
case Programs.HTTPWormProgram.name:
|
||||
if (s.httpPortOpen) {
|
||||
server.openPortCount++;
|
||||
};
|
||||
programHandlers[Programs.HTTPWormProgram.name] = (server) => {
|
||||
if (serv.httpPortOpen) {
|
||||
post("HTTP Port (80) is already open!");
|
||||
} else {
|
||||
s.httpPortOpen = true;
|
||||
return;
|
||||
}
|
||||
|
||||
server.httpPortOpen = true;
|
||||
post("Opened HTTP Port (80)!");
|
||||
++s.openPortCount;
|
||||
}
|
||||
break;
|
||||
case Programs.SQLInjectProgram.name:
|
||||
if (s.sqlPortOpen) {
|
||||
server.openPortCount++;
|
||||
};
|
||||
programHandlers[Programs.SQLInjectProgram.name] = (server) => {
|
||||
if (server.sqlPortOpen) {
|
||||
post("SQL Port (1433) is already open!");
|
||||
} else {
|
||||
s.sqlPortOpen = true;
|
||||
post("Opened SQL Port (1433)!");
|
||||
++s.openPortCount;
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case Programs.ServerProfiler.name:
|
||||
if (splitArgs.length != 2) {
|
||||
|
||||
server.sqlPortOpen = true;
|
||||
post("Opened SQL Port (1433)!");
|
||||
server.openPortCount++;
|
||||
};
|
||||
programHandlers[Programs.ServerProfiler.name] = (server, args) => {
|
||||
if (args.length !== 2) {
|
||||
post("Must pass a server hostname or IP as an argument for ServerProfiler.exe");
|
||||
return;
|
||||
}
|
||||
var serv = getServer(splitArgs[1]);
|
||||
if (serv == null) {
|
||||
|
||||
const targetServer = getServer(args[1]);
|
||||
if (targetServer == null) {
|
||||
post("Invalid server IP/hostname");
|
||||
return;
|
||||
}
|
||||
post(serv.hostname + ":");
|
||||
post("Server base security level: " + serv.baseDifficulty);
|
||||
post("Server current security level: " + serv.hackDifficulty);
|
||||
post("Server growth rate: " + serv.serverGrowth);
|
||||
post("Netscript hack() execution time: " + formatNumber(scriptCalculateHackingTime(serv), 1) + "s");
|
||||
post("Netscript grow() execution time: " + formatNumber(scriptCalculateGrowTime(serv)/1000, 1) + "s");
|
||||
post("Netscript weaken() execution time: " + formatNumber(scriptCalculateWeakenTime(serv)/1000, 1) + "s");
|
||||
break;
|
||||
case Programs.AutoLink.name:
|
||||
post(targetServer.hostname + ":");
|
||||
post("Server base security level: " + targetServer.baseDifficulty);
|
||||
post("Server current security level: " + targetServer.hackDifficulty);
|
||||
post("Server growth rate: " + targetServer.serverGrowth);
|
||||
post("Netscript hack() execution time: " + formatNumber(scriptCalculateHackingTime(targetServer), 1) + "s");
|
||||
post("Netscript grow() execution time: " + formatNumber(scriptCalculateGrowTime(targetServer)/1000, 1) + "s");
|
||||
post("Netscript weaken() execution time: " + formatNumber(scriptCalculateWeakenTime(targetServer)/1000, 1) + "s");
|
||||
};
|
||||
programHandlers[Programs.AutoLink.name] = () => {
|
||||
post("This executable cannot be run.");
|
||||
post("AutoLink.exe lets you automatically connect to other servers when using 'scan-analyze'.");
|
||||
post("When using scan-analyze, click on a server's hostname to connect to it.");
|
||||
break;
|
||||
case Programs.DeepscanV1.name:
|
||||
};
|
||||
programHandlers[Programs.DeepscanV1.name] = () => {
|
||||
post("This executable cannot be run.");
|
||||
post("DeepscanV1.exe lets you run 'scan-analyze' with a depth up to 5.");
|
||||
break;
|
||||
case Programs.DeepscanV2.name:
|
||||
};
|
||||
programHandlers[Programs.DeepscanV2.name] = () => {
|
||||
post("This executable cannot be run.");
|
||||
post("DeepscanV2.exe lets you run 'scan-analyze' with a depth up to 10.");
|
||||
break;
|
||||
case Programs.Flight.name:
|
||||
};
|
||||
programHandlers[Programs.Flight.name] = () => {
|
||||
const fulfilled = Player.augmentations.length >= 30 &&
|
||||
Player.money.gt(1e11) &&
|
||||
((Player.hacking_skill >= 2500)||
|
||||
@ -1941,14 +1960,15 @@ let Terminal = {
|
||||
post("Defense: " + Player.defense + " / 1500");
|
||||
post("Dexterity: " + Player.dexterity + " / 1500");
|
||||
post("Agility: " + Player.agility + " / 1500");
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
post("We will contact you.");
|
||||
post("-- Daedalus --");
|
||||
}
|
||||
break;
|
||||
case Programs.BitFlume.name:
|
||||
var yesBtn = yesNoBoxGetYesButton(),
|
||||
noBtn = yesNoBoxGetNoButton();
|
||||
};
|
||||
programHandlers[Programs.BitFlume.name] = () => {
|
||||
const yesBtn = yesNoBoxGetYesButton();
|
||||
const noBtn = yesNoBoxGetNoButton();
|
||||
yesBtn.innerHTML = "Travel to BitNode Nexus";
|
||||
noBtn.innerHTML = "Cancel";
|
||||
yesBtn.addEventListener("click", function() {
|
||||
@ -1961,12 +1981,14 @@ let Terminal = {
|
||||
yesNoBoxCreate("WARNING: USING THIS PROGRAM WILL CAUSE YOU TO LOSE ALL OF YOUR PROGRESS ON THE CURRENT BITNODE.<br><br>" +
|
||||
"Do you want to travel to the BitNode Nexus? This allows you to reset the current BitNode " +
|
||||
"and select a new one.");
|
||||
};
|
||||
|
||||
break;
|
||||
default:
|
||||
if (!programHandlers.hasOwnProperty(programName)){
|
||||
post("Invalid executable. Cannot be run");
|
||||
return;
|
||||
}
|
||||
|
||||
programHandlers[programName](s, splitArgs);
|
||||
},
|
||||
|
||||
runScript: function(scriptName) {
|
||||
|
Loading…
Reference in New Issue
Block a user