From d3fa9f8c6eef99c9401c4f3f2bc3d318f7188680 Mon Sep 17 00:00:00 2001 From: Daniel Xie Date: Mon, 5 Jun 2017 20:34:00 -0500 Subject: [PATCH] Slight rebalancing. Added kill() and killall() commands --- src/Constants.js | 19 +++++++++++-- src/NetscriptEvaluator.js | 58 +++++++++++++++++++++++++++++++++++++-- src/Script.js | 2 +- src/Terminal.js | 11 +++++--- src/engine.js | 2 +- 5 files changed, 81 insertions(+), 11 deletions(-) diff --git a/src/Constants.js b/src/Constants.js index b4bbc6f2d..637bf5e5a 100644 --- a/src/Constants.js +++ b/src/Constants.js @@ -40,7 +40,7 @@ CONSTANTS = { ScriptWhileRamCost: 0.2, ScriptForRamCost: 0.2, ScriptIfRamCost: 0.1, - ScriptHackRamCost: 0.15, + ScriptHackRamCost: 0.1, ScriptGrowRamCost: 0.15, ScriptWeakenRamCost: 0.15, ScriptNukeRamCost: 0.05, @@ -53,7 +53,7 @@ CONSTANTS = { ScriptExecRamCost: 1.1, ScriptScpRamCost: 0.5, ScriptHasRootAccessRamCost: 0.05, - ScriptGetHostnameRamCost: 0.1, + ScriptGetHostnameRamCost: 0.05, ScriptGetHackingLevelRamCost: 0.05, ScriptGetServerMoneyRamCost: 0.1, ScriptGetServerSecurityRamCost: 0.1, @@ -335,6 +335,18 @@ CONSTANTS = { "second argument is a string with the hostname or IP of the 'target server' on which to run the script. The specified script must exist on the target server. Returns " + "true if the script is successfully started, and false otherwise. Does NOT work while offline
" + "Example: exec('generic-hack.script', 'foodnstuff');
The example above will try to launch the script 'generic-hack.script' on the 'foodnstuff' server.

" + + "kill(script, [hostname/ip])
Kills a script on a server. The first argument must be a string with the name of the script. The name is case-sensitive. " + + "The second argument must be a string with the hostname or IP of the target server. The function will try to kill the specified script on the target server. " + + "The second argument is optional. If it is omitted, then the function will try to kill the specified script on the current server (the server running " + + "the script that calls this function). If the script is found on the specified server and is running, then it will be killed and this function " + + "will return true. Otherwise, this function will return false.
" + + "Example: kill('foo.script', 'foodnstuff');
" + + "Example: kill('foo.script');
" + + "The first example above will look for a script called 'foo.script' on the 'foodnstuff' server. If the script exists and is running, then it will " + + "be killed and the function will return true. Otherwise false will be returned. The second example above will do the same thing, except on the " + + "current server (the server running the script that calls the kill() function).

" + + "killall(hostname/ip)
Kills all running scripts on the specified server. This function takes a single argument which " + + "must be a string containing the hostname or IP of the target server. This function will always return true.

" + "scp(script, hostname/ip)
Copies a script to another server. The first argument is a string with the filename of the script " + "to be copied. The second argument is a string with the hostname or IP of the destination server. Returns true if the script is successfully " + "copied over and false otherwise.
Example: scp('hack-template.script', 'foodnstuff');

" + @@ -520,6 +532,7 @@ CONSTANTS = { "such as a variable assignment, a function call, a binary operator, getting a variable's value, etc. used to take up to several seconds, " + "now each one should only take 750 milliseconds).
" + "-Percentage money stolen when hacking lowered to compensate for faster script speeds
" + + "-Slightly lowered the runtime of weaken()
" + "-Lowered base growth rate by 25%(which affects amount of money gained from grow())
" + "-Hacking experience granted by grow() halved" + "-Weaken() is now 10% faster, but only grants 3 base hacking exp upon completion instead of 5
" + @@ -528,6 +541,8 @@ CONSTANTS = { "-Added getServerRequiredHackingLevel(server) Netscript command.
" + "-Added fileExists(file, [server]) Netscript command, which is used to check if a script/program exists on a " + "specified server
" + + "-Added killall Terminal command. Kills all running scripts on the current machine
" + + "-Added kill() and killall() Netscript commands. Used to kill scripts on specified machines. See Netscript documentation
" + "v0.19.7
" + "-Added changelog to Options menu
" + "-Bug fix with autocompletion (wasn't working properly for capitalized filenames/programs

" + diff --git a/src/NetscriptEvaluator.js b/src/NetscriptEvaluator.js index 70085c306..9a5eace83 100644 --- a/src/NetscriptEvaluator.js +++ b/src/NetscriptEvaluator.js @@ -257,9 +257,61 @@ function evaluate(exp, workerScript) { reject(e); }); } else if (exp.func.value == "kill") { + if (exp.args.length != 1 && exp.args.length != 2) { + return reject(makeRuntimeRejectMsg(workerScript, "kill() call has incorrect number of arguments. Takes 1 or 2 arguments")); + } + var argPromises = exp.args.map(function(arg) { + return evaluate(arg, workerScript); + }); + var filename = ""; + Promise.all(argPromises).then(function(args) { + if (env.stopFlag) {return reject(workerScript);} + filename = args[0]; + if (exp.args.length == 2) { + return Promise.resolve(workerScript.serverIp); + } else { + return evaluate(exp.args[1], workerScript); + } + }).then(function(ip) { + var server = getServer(ip); + if (server == null) { + workerScript.scriptRef.log("kill() failed. Invalid IP or hostname passed in: " + ip); + return reject(makeRuntimeRejectMsg(workerScript, "Invalid IP or hostname passed into kill() command")); + } + + for (var i = 0; i < server.runningScripts.length; ++i) { + if (filename == server.runningScripts[i].filename) { + killWorkerScript(filename, server.ip); + workerScript.scriptRef.log("Killing " + scriptName + ". May take up to a few minutes for the scripts to die..."); + return resolve(true); + } + } + workerScript.scriptRef.log("kill() failed. No such script "+ scriptName + " on " + server.hostname); + return resolve(false); + }).catch(function(e) { + reject(e); + }); } else if (exp.func.value == "killall") { - + if (exp.args.length != 1) { + return reject(makeRuntimeRejectMsg(workerScript, "killall() call has incorrect number of arguments. Takes 1 argument")); + } + var ipPromise = evaluate(exp.args[0], workerScript); + ipPromise.then(function(ip) { + if (env.stopFlag) {return reject(workerScript);} + var server = getServer(ip); + if (server == null) { + workerScript.scriptRef.log("killall() failed. Invalid IP or hostname passed in: " + ip); + return reject(makeRuntimeRejectMsg(workerScript, "Invalid IP or hostname passed into killall() command")); + } + workerScript.scriptRef.log("killall(): Killing all scrips on " + server.hostname); + for (var i = server.runningScripts.length; i >= 0; --i) { + killWorkerScript(server.runningScripts[i], server.ip); + } + resolve(true); + }, function(e) { + reject(e); + }); } else if (exp.func.value == "scp") { if (exp.args.length != 2) { return reject(makeRuntimeRejectMsg(workerScript, "scp() call has incorrect number of arguments. Takes 2 arguments")); @@ -921,7 +973,7 @@ function scriptCalculatePercentMoneyHacked(server) { function scriptCalculateGrowTime(server) { var difficultyMult = server.requiredHackingSkill * server.hackDifficulty; var skillFactor = (2.5 * difficultyMult + 500) / (Player.hacking_skill + 50); - var growTime = skillFactor * Player.hacking_speed_mult * 17; //This is in seconds + var growTime = skillFactor * Player.hacking_speed_mult * 16; //This is in seconds return growTime * 1000; } @@ -929,6 +981,6 @@ function scriptCalculateGrowTime(server) { function scriptCalculateWeakenTime(server) { var difficultyMult = server.requiredHackingSkill * server.hackDifficulty; var skillFactor = (2.5 * difficultyMult + 500) / (Player.hacking_skill + 50); - var weakenTime = skillFactor * Player.hacking_speed_mult * 45; //This is in seconds + var weakenTime = skillFactor * Player.hacking_speed_mult * 40; //This is in seconds return weakenTime * 1000; } \ No newline at end of file diff --git a/src/Script.js b/src/Script.js index 84c785732..e25905e99 100644 --- a/src/Script.js +++ b/src/Script.js @@ -204,7 +204,7 @@ Script.prototype.reset = function() { //Updates how much RAM the script uses when it is running. Script.prototype.updateRamUsage = function() { - var baseRam = 1.5; + var baseRam = 1.4; var codeCopy = this.code.repeat(1); codeCopy = codeCopy.replace(/\s/g,''); //Remove all whitespace diff --git a/src/Terminal.js b/src/Terminal.js index a13953b1b..143a9796f 100644 --- a/src/Terminal.js +++ b/src/Terminal.js @@ -412,11 +412,14 @@ var Terminal = { //Replace all extra whitespace in command with a single space command = command.replace(/\s\s+/g, ' '); - Terminal.commandHistory.push(command); - if (Terminal.commandHistory.length > 50) { - Terminal.commandHistory.splice(0, 1); + + if (Terminal.commandHistory[Terminal.commandHistory.length-1] != command) { + Terminal.commandHistory.push(command); + if (Terminal.commandHistory.length > 50) { + Terminal.commandHistory.splice(0, 1); + } + Terminal.commandHistoryIndex = Terminal.commandHistory.length; } - Terminal.commandHistoryIndex = Terminal.commandHistory.length; //Process any aliases command = substituteAliases(command); diff --git a/src/engine.js b/src/engine.js index 7fe9248a7..bdf07f116 100644 --- a/src/engine.js +++ b/src/engine.js @@ -1042,7 +1042,7 @@ var Engine = { return false; }); relaySmtpALink.addEventListener("click", function() { - Player.startCreateProgramWork(Programs.RelaySMTPProgram. CONSTANTS.MillisecondsPer2Hours, 250); + Player.startCreateProgramWork(Programs.RelaySMTPProgram, CONSTANTS.MillisecondsPer2Hours, 250); return false; }); httpWormALink.addEventListener("click", function() {