From 6b40482e6c8689fbac97ed0a3e3d9164f37e20e1 Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Tue, 23 May 2017 13:17:37 -0500 Subject: [PATCH] Added hasRootAccess() and getNumHacknetNodes commands to netscript --- src/Constants.js | 4 ++++ src/NetscriptEvaluator.js | 35 ++++++++++++++++++++++++++++++++++- src/Terminal.js | 6 +++--- 3 files changed, 41 insertions(+), 4 deletions(-) diff --git a/src/Constants.js b/src/Constants.js index d188bd544..267bae772 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -274,6 +274,9 @@ CONSTANTS = { "be used to run scripts located on the same server. Returns true if the script is successfully started, and false otherwise. Requires a significant amount " + "of RAM to run this command. Does NOT work while offline
Example: run('hack-foodnstuff.script');
The example above will try and launch the 'hack-foodnstuff.script' script on " + "the current server, if it exists.

" + + "hasRootAccess(hostname/ip)
Returns a boolean (true or false) indicating whether or not the Player has root access to a server. " + + "The argument passed in must be a string with either the hostname or IP of the target server. Does NOT work while offline.
" + + "Example: if (hasRootAccess('foodnstuff') == false) {
    nuke('foodnstuff');
}

" + "getHackingLevel()
Returns the Player's current hacking level. Does NOT work while offline

" + "getServerMoneyAvailable(hostname/ip)
Returns the amount of money available on a server. The argument passed in must be a string with either the " + "hostname or IP of the target server. Does NOT work while offline
Example: getServerMoneyAvailable('foodnstuff');

" + @@ -290,6 +293,7 @@ CONSTANTS = { "};

" + "The example code above will attempt to purchase a new Hacknet Node. If the Hacknet Node is purchased, then it will " + "continuously try to level it up until it is leveled up 10 times.

" + + "getNumHacknetNodes()
returns the number of Hacknet Nodes that the Player owns. Does NOT work while offline

" + "

While loops


" + "A while loop is a control flow statement that repeatedly executes code as long as a condition is met.

" + "while ([cond]) {
    [code]
}


" + diff --git a/src/NetscriptEvaluator.js b/src/NetscriptEvaluator.js index 84f2e941e..dc7938737 100644 --- a/src/NetscriptEvaluator.js +++ b/src/NetscriptEvaluator.js @@ -654,6 +654,27 @@ function evaluate(exp, workerScript) { }, function(e) { reject(e); }); + } else if (exp.func.value == "hasRootAccess") { + if (exp.args.length != 1) { + reject("|"+workerScript.serverIp+"|"+workerScript.name+"|hasRootAccess() call has incorrect number of arguments. Takes 1 argument"); + return; + } + var ipPromise = evaluate(exp.args[0], workerScript); + ipPromise.then(function(ip) { + if (env.stopFlag) {reject(workerScript);} + setTimeout(function() { + var server = getServer(ip); + if (server == null) { + reject("|" + workerScript.serverIp + "|" + workerScript.name + "|Invalid IP or hostname passed into hasRootAccess() command"); + workerScript.scriptRef.log("Cannot hasRootAccess(). Invalid IP or hostname passed in: " + ip); + return; + } + workerScript.scriptRef.log("hasRootAccess() returned " + server.hasAdminRights); + resolve(server.hasAdminRights); + }, CONSTANTS.CodeInstructionRunTime); + }, function(e) { + reject(e); + }); } else if (exp.func.value == "run") { if (exp.args.length != 1) { reject("|"+workerScript.serverIp+"|"+workerScript.name+"|run() call has incorrect number of arguments. Takes 1 argument"); @@ -661,6 +682,7 @@ function evaluate(exp, workerScript) { } var scriptNamePromise = evaluate(exp.args[0], workerScript); scriptNamePromise.then(function(scriptname) { + if (env.stopFlag) {reject(workerScript);} var serverIp = workerScript.serverIp; var scriptServer = AllServers[serverIp]; if (scriptServer == null) { @@ -684,6 +706,7 @@ function evaluate(exp, workerScript) { return; } setTimeout(function() { + if (env.stopFlag) {reject(workerScript);} Player.updateSkillLevels(); workerScript.scriptRef.log("getHackingLevel() returned " + Player.hacking_skill); resolve(Player.hacking_skill); @@ -771,6 +794,16 @@ function evaluate(exp, workerScript) { }, function(e) { reject(e); }); + } else if (exp.func.value == "getNumHacknetNodes") { + if (exp.args.length != 0) { + reject("|"+workerScript.serverIp+"|"+workerScript.name+"|getNumHacknetNodes() call has incorrect number of arguments. Takes 0 arguments"); + return; + } + setTimeout(function() { + if (env.stopFlag) {reject(workerScript);} + workerScript.scriptRef.log("getNumHacknetNodes() returned " + Player.hacknetNodes.length); + resolve(Player.hacknetNodes.length); + }, CONSTANTS.CodeInstructionRunTime); } }, CONSTANTS.CodeInstructionRunTime); }); @@ -990,7 +1023,7 @@ function apply_op(op, a, b) { return x; } switch (op) { - case "+": return num(a) + num(b); + case "+": return a + b; case "-": return num(a) - num(b); case "*": return num(a) * num(b); case "/": return num(a) / div(b); diff --git a/src/Terminal.js b/src/Terminal.js index a72f806d3..7c7d707e0 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -849,9 +849,9 @@ var Terminal = { post("" + serv.hostname + ""); var c = "N"; if (serv.hasAdminRights) {c = "Y";} - post("----Root Access: " + c); - post("----Required hacking skill: " + serv.requiredHackingSkill); - post("----Number open ports required to NUKE: " + serv.numOpenPortsRequired); + post("--Root Access: " + c); + post("--Required hacking skill: " + serv.requiredHackingSkill); + post("--Number open ports required to NUKE: " + serv.numOpenPortsRequired); post(" "); } },