Added threading options for run() and exec() commands

This commit is contained in:
Daniel Xie 2017-06-10 22:45:06 -05:00
parent c55b677bc2
commit f7bf83e4d9
3 changed files with 36 additions and 16 deletions

@ -165,11 +165,11 @@ CONSTANTS = {
"kill [script] Stops a script that is running on the current machine<br>" + "kill [script] Stops a script that is running on the current machine<br>" +
"killall Stops all running scripts on the current machine<br>" + "killall Stops all running scripts on the current machine<br>" +
"ls Displays all programs and scripts on the machine<br>" + "ls Displays all programs and scripts on the machine<br>" +
"mem [script] Displays the amount of RAM the script requires to run<br>" + "mem [script] [-t] [n] Displays the amount of RAM the script requires to run with n threads<br>" +
"nano [script] Text editor - Open up and edit a script<br>" + "nano [script] Text editor - Open up and edit a script<br>" +
"ps Display all scripts that are currently running<br>" + "ps Display all scripts that are currently running<br>" +
"rm Delete a script/program from the machine. (WARNING: Permanent)<br>" + "rm Delete a script/program from the machine. (WARNING: Permanent)<br>" +
"run [script/program] Execute a program or a script<br>" + "run [name] [-t] [n] Execute a program or a script with n threads<br>" +
"scan Displays all available network connections<br>" + "scan Displays all available network connections<br>" +
"scan-analyze [depth] Displays hacking-related information for all servers up to <i>depth</i> nodes away<br>" + "scan-analyze [depth] Displays hacking-related information for all servers up to <i>depth</i> nodes away<br>" +
"scp [script] [server] Copies a script to a destination server (specified by ip or hostname)<br>" + "scp [script] [server] Copies a script to a destination server (specified by ip or hostname)<br>" +
@ -652,6 +652,8 @@ CONSTANTS = {
"-You can now see what an Augmentation does and its price even while its locked<br><br>", "-You can now see what an Augmentation does and its price even while its locked<br><br>",
LatestUpdate: LatestUpdate:
"v0.20.2<br>" +
""
"v0.20.1<br>" + "v0.20.1<br>" +
"-Fixed bug where sometimes scripts would crash without showing the error<br>" + "-Fixed bug where sometimes scripts would crash without showing the error<br>" +
"-Added Deepscan programs to Dark Web<br>" + "-Added Deepscan programs to Dark Web<br>" +

@ -6,8 +6,6 @@
//wait for that promise to finish before continuing //wait for that promise to finish before continuing
function evaluate(exp, workerScript) { function evaluate(exp, workerScript) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var threads = workerScript.scriptRef.threads;
if (isNaN(threads) || threads < 1) {threads = 1;}
var env = workerScript.env; var env = workerScript.env;
if (env.stopFlag) {return reject(workerScript);} if (env.stopFlag) {return reject(workerScript);}
if (exp == null) { if (exp == null) {
@ -260,18 +258,29 @@ function evaluate(exp, workerScript) {
reject(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 && exp.args.length != 2) {
return reject(makeRuntimeRejectMsg(workerScript, "run() call has incorrect number of arguments. Takes 1 argument")); return reject(makeRuntimeRejectMsg(workerScript, "run() call has incorrect number of arguments. Takes 1 or 2 arguments"));
} }
var scriptNamePromise = evaluate(exp.args[0], workerScript); var argPromises = exp.args.map(function(arg) {
scriptNamePromise.then(function(scriptname) { return evaluate(arg, workerScript);
});
Promise.all(argPromises).then(function(args) {
if (env.stopFlag) {return reject(workerScript);} if (env.stopFlag) {return reject(workerScript);}
var scriptname = args[0];
var threads = 1;
if (exp.args.length == 2) {
threads = args[1];
}
if (isNaN(threads) || threads < 1) {
return reject(makeRuntimeRejectMsg(workerScript, "Invalid argument for thread count passed into run(). Must be numeric and greater than 0"));
}
var scriptServer = getServer(workerScript.serverIp); var scriptServer = getServer(workerScript.serverIp);
if (scriptServer == null) { if (scriptServer == null) {
return reject(makeRuntimeRejectMsg(workerScript, "Could not find server. This is a bug in the game. Report to game dev")); return reject(makeRuntimeRejectMsg(workerScript, "Could not find server. This is a bug in the game. Report to game dev"));
} }
var runScriptPromise = runScriptFromScript(scriptServer, scriptname, workerScript); var runScriptPromise = runScriptFromScript(scriptServer, scriptname, workerScript, threads);
return runScriptPromise; return runScriptPromise;
}).then(function(res) { }).then(function(res) {
resolve(res); resolve(res);
@ -279,7 +288,7 @@ function evaluate(exp, workerScript) {
reject(e); reject(e);
}); });
} else if (exp.func.value == "exec") { } else if (exp.func.value == "exec") {
if (exp.args.length != 2) { if (exp.args.length != 2 && exp.args.length != 3) {
return reject(makeRuntimeRejectMsg(workerScript, "exec() call has incorrect number of arguments. Takes 2 arguments")); return reject(makeRuntimeRejectMsg(workerScript, "exec() call has incorrect number of arguments. Takes 2 arguments"));
} }
var argPromises = exp.args.map(function(arg) { var argPromises = exp.args.map(function(arg) {
@ -288,12 +297,19 @@ function evaluate(exp, workerScript) {
Promise.all(argPromises).then(function(args) { Promise.all(argPromises).then(function(args) {
if (env.stopFlag) {return reject(workerScript);} if (env.stopFlag) {return reject(workerScript);}
var threads = 1;
if (exp.args.length == 3) {
threads = args[2];
}
if (isNaN(threads) || threads < 1) {
return reject(makeRuntimeRejectMsg(workerScript, "Invalid argument for thread count passed into exec(). Must be numeric and greater than 0"));
}
var server = getServer(args[1]); var server = getServer(args[1]);
if (server == null) { if (server == null) {
return reject(makeRuntimeRejectMsg(workerScript, "Invalid hostname/ip passed into exec() command: " + args[1])); return reject(makeRuntimeRejectMsg(workerScript, "Invalid hostname/ip passed into exec() command: " + args[1]));
} }
return runScriptFromScript(server, args[0], workerScript); return runScriptFromScript(server, args[0], workerScript, threads);
}).then(function(res) { }).then(function(res) {
resolve(res); resolve(res);
}).catch(function(e) { }).catch(function(e) {
@ -925,7 +941,7 @@ function apply_op(op, a, b) {
} }
//Run a script from inside a script using run() command //Run a script from inside a script using run() command
function runScriptFromScript(server, scriptname, workerScript) { function runScriptFromScript(server, scriptname, workerScript, threads=1) {
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var env = workerScript.env; var env = workerScript.env;
if (env.stopFlag) {reject(workerScript); return;} if (env.stopFlag) {reject(workerScript); return;}
@ -944,6 +960,7 @@ function runScriptFromScript(server, scriptname, workerScript) {
if (server.scripts[i].filename == scriptname) { if (server.scripts[i].filename == scriptname) {
//Check for admin rights and that there is enough RAM availble to run //Check for admin rights and that there is enough RAM availble to run
var ramUsage = server.scripts[i].ramUsage; var ramUsage = server.scripts[i].ramUsage;
ramUsage = ramUsage * threads * Math.pow(1.02, threads-1);
var ramAvailable = server.maxRam - server.ramUsed; var ramAvailable = server.maxRam - server.ramUsed;
if (server.hasAdminRights == false) { if (server.hasAdminRights == false) {
@ -951,13 +968,14 @@ function runScriptFromScript(server, scriptname, workerScript) {
resolve(false); resolve(false);
return; return;
} else if (ramUsage > ramAvailable){ } else if (ramUsage > ramAvailable){
workerScript.scriptRef.log("Cannot run script " + scriptname + " on " + server.hostname + " because there is not enough available RAM!"); workerScript.scriptRef.log("Cannot run script " + scriptname + "(t=" + threads + ") on " + server.hostname + " because there is not enough available RAM!");
resolve(false); resolve(false);
return; return;
} else { } else {
//Able to run script //Able to run script
workerScript.scriptRef.log("Running script: " + scriptname + " on " + server.hostname + ". May take a few seconds to start up..."); workerScript.scriptRef.log("Running script: " + scriptname + " on " + server.hostname + " with " + threads + " threads. May take a few seconds to start up...");
var script = server.scripts[i]; var script = server.scripts[i];
script.threads = threads;
server.runningScripts.push(script.filename); //Push onto runningScripts server.runningScripts.push(script.filename); //Push onto runningScripts
addWorkerScript(script, server); addWorkerScript(script, server);
resolve(true); resolve(true);

@ -507,7 +507,7 @@ var Engine = {
updateSkillLevelsCounter: 10, //Only update skill levels every 2 seconds. Might improve performance updateSkillLevelsCounter: 10, //Only update skill levels every 2 seconds. Might improve performance
updateDisplays: 3, //Update displays such as Active Scripts display and character display updateDisplays: 3, //Update displays such as Active Scripts display and character display
createProgramNotifications: 10, //Checks whether any programs can be created and notifies createProgramNotifications: 10, //Checks whether any programs can be created and notifies
checkFactionInvitations: 250, //Check whether you qualify for any faction invitations every 5 minutes checkFactionInvitations: 100, //Check whether you qualify for any faction invitations every 5 minutes
passiveFactionGrowth: 600, passiveFactionGrowth: 600,
messages: 300, messages: 300,
}, },
@ -567,7 +567,7 @@ var Engine = {
var randFaction = invitedFactions[Math.floor(Math.random() * invitedFactions.length)]; var randFaction = invitedFactions[Math.floor(Math.random() * invitedFactions.length)];
inviteToFaction(randFaction); inviteToFaction(randFaction);
} }
Engine.Counters.checkFactionInvitations = 250; Engine.Counters.checkFactionInvitations = 100;
} }
if (Engine.Counters.passiveFactionGrowth <= 0) { if (Engine.Counters.passiveFactionGrowth <= 0) {