Added hasRootAccess() and getNumHacknetNodes commands to netscript

This commit is contained in:
Daniel Xie 2017-05-23 13:17:37 -05:00
parent ea17334792
commit 6b40482e6c
3 changed files with 41 additions and 4 deletions

@ -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 " + "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 <br>Example: run('hack-foodnstuff.script'); <br> The example above will try and launch the 'hack-foodnstuff.script' script on " + "of RAM to run this command. Does NOT work while offline <br>Example: run('hack-foodnstuff.script'); <br> The example above will try and launch the 'hack-foodnstuff.script' script on " +
"the current server, if it exists. <br><br>" + "the current server, if it exists. <br><br>" +
"<i>hasRootAccess(hostname/ip)</i><br> 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.<br> " +
"Example: if (hasRootAccess('foodnstuff') == false) {<br>&nbsp;&nbsp;&nbsp;&nbsp;nuke('foodnstuff');<br>}<br><br>" +
"<i>getHackingLevel() </i><br> Returns the Player's current hacking level. Does NOT work while offline <br><br> " + "<i>getHackingLevel() </i><br> Returns the Player's current hacking level. Does NOT work while offline <br><br> " +
"<i>getServerMoneyAvailable(hostname/ip)</i><br> Returns the amount of money available on a server. The argument passed in must be a string with either the " + "<i>getServerMoneyAvailable(hostname/ip)</i><br> 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 <br> Example: getServerMoneyAvailable('foodnstuff');<br><br>" + "hostname or IP of the target server. Does NOT work while offline <br> Example: getServerMoneyAvailable('foodnstuff');<br><br>" +
@ -290,6 +293,7 @@ CONSTANTS = {
"};<br><br>" + "};<br><br>" +
"The example code above will attempt to purchase a new Hacknet Node. If the Hacknet Node is purchased, then it will " + "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. <br><br>" + "continuously try to level it up until it is leveled up 10 times. <br><br>" +
"<i>getNumHacknetNodes()</i><br> returns the number of Hacknet Nodes that the Player owns. Does NOT work while offline<br><br>" +
"<u><h1>While loops </h1></u><br>" + "<u><h1>While loops </h1></u><br>" +
"A while loop is a control flow statement that repeatedly executes code as long as a condition is met. <br><br> " + "A while loop is a control flow statement that repeatedly executes code as long as a condition is met. <br><br> " +
"<i>while (<i>[cond]</i>) {<br>&nbsp;&nbsp;&nbsp;&nbsp;<i>[code]</i><br>}</i><br><br>" + "<i>while (<i>[cond]</i>) {<br>&nbsp;&nbsp;&nbsp;&nbsp;<i>[code]</i><br>}</i><br><br>" +

@ -654,6 +654,27 @@ function evaluate(exp, workerScript) {
}, function(e) { }, function(e) {
reject(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") { } else if (exp.func.value == "run") {
if (exp.args.length != 1) { if (exp.args.length != 1) {
reject("|"+workerScript.serverIp+"|"+workerScript.name+"|run() call has incorrect number of arguments. Takes 1 argument"); 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); var scriptNamePromise = evaluate(exp.args[0], workerScript);
scriptNamePromise.then(function(scriptname) { scriptNamePromise.then(function(scriptname) {
if (env.stopFlag) {reject(workerScript);}
var serverIp = workerScript.serverIp; var serverIp = workerScript.serverIp;
var scriptServer = AllServers[serverIp]; var scriptServer = AllServers[serverIp];
if (scriptServer == null) { if (scriptServer == null) {
@ -684,6 +706,7 @@ function evaluate(exp, workerScript) {
return; return;
} }
setTimeout(function() { setTimeout(function() {
if (env.stopFlag) {reject(workerScript);}
Player.updateSkillLevels(); Player.updateSkillLevels();
workerScript.scriptRef.log("getHackingLevel() returned " + Player.hacking_skill); workerScript.scriptRef.log("getHackingLevel() returned " + Player.hacking_skill);
resolve(Player.hacking_skill); resolve(Player.hacking_skill);
@ -771,6 +794,16 @@ function evaluate(exp, workerScript) {
}, function(e) { }, function(e) {
reject(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); }, CONSTANTS.CodeInstructionRunTime);
}); });
@ -990,7 +1023,7 @@ function apply_op(op, a, b) {
return x; return x;
} }
switch (op) { 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) * num(b); case "*": return num(a) * num(b);
case "/": return num(a) / div(b); case "/": return num(a) / div(b);

@ -849,9 +849,9 @@ var Terminal = {
post("<strong>" + serv.hostname + "</strong>"); post("<strong>" + serv.hostname + "</strong>");
var c = "N"; var c = "N";
if (serv.hasAdminRights) {c = "Y";} if (serv.hasAdminRights) {c = "Y";}
post("----Root Access: " + c); post("--Root Access: " + c);
post("----Required hacking skill: " + serv.requiredHackingSkill); post("--Required hacking skill: " + serv.requiredHackingSkill);
post("----Number open ports required to NUKE: " + serv.numOpenPortsRequired); post("--Number open ports required to NUKE: " + serv.numOpenPortsRequired);
post(" "); post(" ");
} }
}, },