From 39ed2c40dd34baf9d45af6a2a89cd6da14c6f38e Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Sun, 6 May 2018 14:22:29 -0400 Subject: [PATCH 1/7] added hacknet multiplier netscript function --- doc/source/netscriptfunctions.rst | 24 ++++++++++++++++++++++++ doc/source/netscripthacknetnodeapi.rst | 23 +++++++++++++++++++++++ src/NetscriptFunctions.js | 17 +++++++++++++++++ 3 files changed, 64 insertions(+) diff --git a/doc/source/netscriptfunctions.rst b/doc/source/netscriptfunctions.rst index 3b0dd2a13..4f76382a8 100644 --- a/doc/source/netscriptfunctions.rst +++ b/doc/source/netscriptfunctions.rst @@ -437,6 +437,30 @@ getHackingMultipliers print(mults.chance); print(mults.growth); +getHacknetMultipliers +^^^^^^^^^^^^^^^^^^^^^ + +.. js:function:: getHacknetMultipliers() + + Returns an object containing the Player's hacknet related multipliers. These multipliers are + returned in integer forms, not percentages (e.g. 1.5 instead of 150%). The object has the following structure:: + + { + production: Player's hacknet production multiplier, + purchaseCost: Player's hacknet purchase cost multiplier, + ramCost: Player's hacknet ram cost multiplier, + coreCost: Player's hacknet core cost multiplier, + levelCost: Player's hacknet level cost multiplier + } + + Example of how this can be used:: + + mults = getHacknetMultipliers(); + print(mults.production); + print(mults.purchaseCost); + + + getServerMoneyAvailable ^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/doc/source/netscripthacknetnodeapi.rst b/doc/source/netscripthacknetnodeapi.rst index 73c0860c1..5fe51b4cc 100644 --- a/doc/source/netscripthacknetnodeapi.rst +++ b/doc/source/netscripthacknetnodeapi.rst @@ -90,6 +90,29 @@ array itself. Returns the cost of upgrading the number of cores of the specified Hacknet Node. Upgrading a Node's number of cores adds one additional core. +Utils +^^^^^ + +.. js:function:: getHacknetMultipliers() + + Returns an object containing the Player's hacknet related multipliers. These multipliers are + returned in integer forms, not percentages (e.g. 1.5 instead of 150%). The object has the following structure:: + + { + production: Player's hacknet production multiplier, + purchaseCost: Player's hacknet purchase cost multiplier, + ramCost: Player's hacknet ram cost multiplier, + coreCost: Player's hacknet core cost multiplier, + levelCost: Player's hacknet level cost multiplier + } + + Example of how this can be used:: + + mults = getHacknetMultipliers(); + print(mults.production); + print(mults.purchaseCost); + + Example(s) ^^^^^^^^^^ diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 799ca02df..eeeab05ee 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -1036,6 +1036,23 @@ function NetscriptFunctions(workerScript) { growth: Player.hacking_grow_mult, }; }, + getHacknetMultipliers : function() { + if (workerScript.checkingRam) { + if (workerScript.loadedFns.getHacknetMultipliers) { + return 0; + } else { + workerScript.loadedFns.getHacknetMultipliers = true; + return CONSTANTS.ScriptGetMultipliersRamCost; + } + } + return { + production: Player.hacknet_node_money_mult, + purchaseCost: Player.hacknet_node_purchase_cost_mult, + ramCost: Player.hacknet_node_ram_cost_mult, + coreCost: Player.hacknet_node_core_cost_mult, + levelCost: Player.hacknet_node_level_cost_mult, + }; + }, getBitNodeMultipliers: function() { if (workerScript.checkingRam) { if (workerScript.loadedFns.getBitNodeMultipliers) { From 92b1050a6855c77ea18d1b4f969d1d38e4fb73f9 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Sun, 6 May 2018 16:27:10 -0400 Subject: [PATCH 2/7] fixed integer form typo --- doc/source/netscriptfunctions.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/netscriptfunctions.rst b/doc/source/netscriptfunctions.rst index 4f76382a8..846af59f5 100644 --- a/doc/source/netscriptfunctions.rst +++ b/doc/source/netscriptfunctions.rst @@ -422,7 +422,7 @@ getHackingMultipliers .. js:function:: getHackingMultipliers() Returns an object containing the Player's hacking related multipliers. These multipliers are - returned in integer forms, not percentages (e.g. 1.5 instead of 150%). The object has the following structure:: + returned in fractional forms, not percentages (e.g. 1.5 instead of 150%). The object has the following structure:: { chance: Player's hacking chance multiplier, @@ -443,7 +443,7 @@ getHacknetMultipliers .. js:function:: getHacknetMultipliers() Returns an object containing the Player's hacknet related multipliers. These multipliers are - returned in integer forms, not percentages (e.g. 1.5 instead of 150%). The object has the following structure:: + returned in fractional forms, not percentages (e.g. 1.5 instead of 150%). The object has the following structure:: { production: Player's hacknet production multiplier, From 7045f5d74e4528f392c1fe073716020ac28c7aee Mon Sep 17 00:00:00 2001 From: James Aguilar <799564+jaguilar@users.noreply.github.com> Date: Sun, 6 May 2018 22:07:14 -0400 Subject: [PATCH 3/7] Fix a bug where a JS script would never exit if killed in a sleep loop. --- src/NetscriptFunctions.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 799ca02df..4bd3b4da3 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -229,6 +229,7 @@ function NetscriptFunctions(workerScript) { workerScript.scriptRef.log("Sleeping for " + time + " milliseconds"); } return netscriptDelay(time, workerScript).then(function() { + if (workerScript.env.stopFlag) {return Promise.reject(workerScript);} return Promise.resolve(true); }); }, From f259a5c17d5a4b9a34a96a73234714f652cf38a6 Mon Sep 17 00:00:00 2001 From: James Aguilar <799564+jaguilar@users.noreply.github.com> Date: Sun, 6 May 2018 22:16:28 -0400 Subject: [PATCH 4/7] More permanent fix for infinite sleeps. --- src/NetscriptFunctions.js | 1 - src/NetscriptWorker.js | 7 +++++++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 4bd3b4da3..799ca02df 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -229,7 +229,6 @@ function NetscriptFunctions(workerScript) { workerScript.scriptRef.log("Sleeping for " + time + " milliseconds"); } return netscriptDelay(time, workerScript).then(function() { - if (workerScript.env.stopFlag) {return Promise.reject(workerScript);} return Promise.resolve(true); }); }, diff --git a/src/NetscriptWorker.js b/src/NetscriptWorker.js index e024d729c..0b5aa2a81 100644 --- a/src/NetscriptWorker.js +++ b/src/NetscriptWorker.js @@ -73,6 +73,13 @@ function startJsScript(workerScript) { // This function unfortunately cannot be an async function, because we don't // know if the original one was, and there's no way to tell. return function (...args) { + // Wrap every netscript function with a check for the stop flag. + // This prevents cases where we never stop because we are only calling + // netscript functions that don't check this. + // This is not a problem for legacy Netscript because it also checks the + // stop flag in the evaluator. + if (workerScript.env.stopFlag) {return Promise.reject(workerScript);} + const msg = "Concurrent calls to Netscript functions not allowed! " + "Did you forget to await hack(), grow(), or some other " + "promise-returning function? (Currently running: %s tried to run: %s)" From a483268f3c0e70c3a56a163ef291b00f13baf9fa Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Sun, 6 May 2018 22:21:36 -0400 Subject: [PATCH 5/7] incomplete programs are more precise --- src/Player.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Player.js b/src/Player.js index a086a5bb0..4113dbe86 100644 --- a/src/Player.js +++ b/src/Player.js @@ -1280,7 +1280,7 @@ PlayerObject.prototype.finishCreateProgramWork = function(cancelled, sing=false) this.getHomeComputer().programs.push(programName); } else { - var perc = Math.floor(this.timeWorkedCreateProgram / this.timeNeededToCompleteWork * 100).toString(); + var perc = (Math.floor(this.timeWorkedCreateProgram / this.timeNeededToCompleteWork * 10000)/100).toString(); var incompleteName = programName + "-" + perc + "%-INC"; this.getHomeComputer().programs.push(incompleteName); } From 577f73dce7cf3e878572018dddbfb00aff53ca0c Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Mon, 7 May 2018 15:56:55 -0400 Subject: [PATCH 6/7] getScriptRam now optionally take hostname/ip or default to localhost --- doc/source/netscriptfunctions.rst | 4 ++-- src/NetscriptFunctions.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/source/netscriptfunctions.rst b/doc/source/netscriptfunctions.rst index 3b0dd2a13..d483e7d63 100644 --- a/doc/source/netscriptfunctions.rst +++ b/doc/source/netscriptfunctions.rst @@ -782,10 +782,10 @@ scriptKill getScriptRam ^^^^^^^^^^^^ -.. js:function:: getScriptRam(scriptname, hostname/ip) +.. js:function:: getScriptRam(scriptname[, hostname/ip]) :param string scriptname: Filename of script. This is case-sensitive. - :param string hostname/ip: Hostname or IP of target server + :param string hostname/ip: Hostname or IP of target server the script is located on. This is optional, If it is not specified then the function will se the current server as the target server. Returns the amount of RAM required to run the specified script on the target server diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 799ca02df..139a23905 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -1994,7 +1994,7 @@ function NetscriptFunctions(workerScript) { } return suc; }, - getScriptRam : function (scriptname, ip) { + getScriptRam : function (scriptname, ip=workerScript.serverIp) { if (workerScript.checkingRam) { if (workerScript.loadedFns.getScriptRam) { return 0; From 529e3fb39a47c24c0e1550a0b11dfbf6f72cab45 Mon Sep 17 00:00:00 2001 From: James Aguilar <799564+jaguilar@users.noreply.github.com> Date: Tue, 8 May 2018 21:40:07 -0400 Subject: [PATCH 7/7] Can't return promise sleep, nobody is awaiting it! --- src/NetscriptWorker.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/NetscriptWorker.js b/src/NetscriptWorker.js index 0b5aa2a81..0fc043d26 100644 --- a/src/NetscriptWorker.js +++ b/src/NetscriptWorker.js @@ -78,7 +78,9 @@ function startJsScript(workerScript) { // netscript functions that don't check this. // This is not a problem for legacy Netscript because it also checks the // stop flag in the evaluator. - if (workerScript.env.stopFlag) {return Promise.reject(workerScript);} + if (workerScript.env.stopFlag) {throw workerScript;} + + if (propName === "sleep") return f(...args); // OK for multiple simultaneous calls to sleep. const msg = "Concurrent calls to Netscript functions not allowed! " + "Did you forget to await hack(), grow(), or some other " + @@ -99,9 +101,9 @@ function startJsScript(workerScript) { } } }; + for (let prop in workerScript.env.vars) { if (typeof workerScript.env.vars[prop] !== "function") continue; - if (prop === "sleep") continue; // OK for multiple simultaneous calls to sleep. workerScript.env.vars[prop] = wrap(prop, workerScript.env.vars[prop]); }