From 4d9439a007fb27d163e35895e449e3a5b9efbc41 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Mon, 3 May 2021 01:07:46 -0400 Subject: [PATCH] Added getServerMaxRam and getServerUsedRam, deprecated getServerRam --- .../netscript/basicfunctions/getServerMaxRam.rst | 15 +++++++++++++++ .../netscript/basicfunctions/getServerRam.rst | 2 ++ .../netscript/basicfunctions/getServerUsedRam.rst | 15 +++++++++++++++ doc/source/netscript/netscriptfunctions.rst | 8 +++++++- src/Netscript/RamCostGenerator.ts | 4 ++++ src/NetscriptFunctions.js | 12 ++++++++++++ test/Netscript/DynamicRamCalculationTests.js | 10 ++++++++++ test/Netscript/StaticRamCalculationTests.js | 10 ++++++++++ 8 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 doc/source/netscript/basicfunctions/getServerMaxRam.rst create mode 100644 doc/source/netscript/basicfunctions/getServerUsedRam.rst diff --git a/doc/source/netscript/basicfunctions/getServerMaxRam.rst b/doc/source/netscript/basicfunctions/getServerMaxRam.rst new file mode 100644 index 000000000..d895c494e --- /dev/null +++ b/doc/source/netscript/basicfunctions/getServerMaxRam.rst @@ -0,0 +1,15 @@ +getServerMaxRam() Netscript Function +==================================== + +.. js:function:: getServerMaxRam(hostname) + + :RAM cost: 0.05 GB + :param string hostname: Hostname of target server. + :returns: Total ram available on that server. In GB. + + Example: + + .. code-block:: javascript + + maxRam = getServerMaxRam("helios"); // returns: 16 + print("helios has "+maxRam + "GB"); diff --git a/doc/source/netscript/basicfunctions/getServerRam.rst b/doc/source/netscript/basicfunctions/getServerRam.rst index 40e46df42..70d3ce032 100644 --- a/doc/source/netscript/basicfunctions/getServerRam.rst +++ b/doc/source/netscript/basicfunctions/getServerRam.rst @@ -3,6 +3,8 @@ getServerRam() Netscript Function .. js:function:: getServerRam(hostname) + .. warning:: This function is deprecated. + :RAM cost: 0.1 GB :param string hostname: Hostname of target server. :returns: An array of 2 number, first number is the total RAM, second the diff --git a/doc/source/netscript/basicfunctions/getServerUsedRam.rst b/doc/source/netscript/basicfunctions/getServerUsedRam.rst new file mode 100644 index 000000000..878389cb0 --- /dev/null +++ b/doc/source/netscript/basicfunctions/getServerUsedRam.rst @@ -0,0 +1,15 @@ +getServerUsedRam() Netscript Function +===================================== + +.. js:function:: getServerUsedRam(hostname) + + :RAM cost: 0.05 GB + :param string hostname: Hostname of target server. + :returns: Used ram on that server. In GB. + + Example: + + .. code-block:: javascript + + usedRam = getServerUsedRam("harakiri-sushi"); // returns: 5.6 + print("harakiri-sushi uses "+usedRam + "GB"); diff --git a/doc/source/netscript/netscriptfunctions.rst b/doc/source/netscript/netscriptfunctions.rst index d0cb3ce37..deea623f9 100644 --- a/doc/source/netscript/netscriptfunctions.rst +++ b/doc/source/netscript/netscriptfunctions.rst @@ -54,7 +54,8 @@ This includes information such as function signatures, what they do, and their r getServerMinSecurityLevel() getServerRequiredHackingLevel() getServerNumPortsRequired() - getServerRam() + getServerMaxRam() + getServerUsedRam() serverExists() fileExists() isRunning() @@ -90,3 +91,8 @@ This includes information such as function signatures, what they do, and their r wget() getFavorToDonate() flags() + +.. toctree:: + :caption: Deprecated: + + getServerRam() \ No newline at end of file diff --git a/src/Netscript/RamCostGenerator.ts b/src/Netscript/RamCostGenerator.ts index 9c9d3bb87..326419d77 100644 --- a/src/Netscript/RamCostGenerator.ts +++ b/src/Netscript/RamCostGenerator.ts @@ -23,6 +23,8 @@ export const RamCostConstants: IMap = { ScriptGetHackingLevelRamCost: 0.05, ScriptGetMultipliersRamCost: 4.0, ScriptGetServerRamCost: 0.1, + ScriptGetServerMaxRam: 0.05, + ScriptGetServerUsedRam: 0.05, ScriptFileExistsRamCost: 0.1, ScriptIsRunningRamCost: 0.1, ScriptHacknetNodesRamCost: 4.0, @@ -122,6 +124,8 @@ export const RamCosts: IMap = { getServerGrowth: () => RamCostConstants.ScriptGetServerRamCost, getServerNumPortsRequired: () => RamCostConstants.ScriptGetServerRamCost, getServerRam: () => RamCostConstants.ScriptGetServerRamCost, + getServerMaxRam: () => RamCostConstants.ScriptGetServerMaxRam, + getServerUsedRam: () => RamCostConstants.ScriptGetServerUsedRam, serverExists: () => RamCostConstants.ScriptGetServerRamCost, fileExists: () => RamCostConstants.ScriptFileExistsRamCost, isRunning: () => RamCostConstants.ScriptIsRunningRamCost, diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index b8758a7c8..41d47e99f 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -1709,6 +1709,18 @@ function NetscriptFunctions(workerScript) { workerScript.log("getServerRam", `returned [${formatNumber(server.maxRam, 2)}GB, ${formatNumber(server.ramUsed, 2)}GB]`); return [server.maxRam, server.ramUsed]; }, + getServerMaxRam: function(ip) { + updateDynamicRam("getServerMaxRam", getRamCost("getServerMaxRam")); + const server = safeGetServer(ip, "getServerMaxRam"); + workerScript.log("getServerMaxRam", `returned ${formatNumber(server.maxRam, 2)}GB`); + return server.maxRam; + }, + getServerUsedRam: function(ip) { + updateDynamicRam("getServerUsedRam", getRamCost("getServerUsedRam")); + const server = safeGetServer(ip, "getServerUsedRam"); + workerScript.log("getServerUsedRam", `returned ${formatNumber(server.ramUsed, 2)}GB`); + return server.ramUsed; + }, serverExists: function(ip) { updateDynamicRam("serverExists", getRamCost("serverExists")); return (getServer(ip) !== null); diff --git a/test/Netscript/DynamicRamCalculationTests.js b/test/Netscript/DynamicRamCalculationTests.js index 95d5040bb..5552508f0 100644 --- a/test/Netscript/DynamicRamCalculationTests.js +++ b/test/Netscript/DynamicRamCalculationTests.js @@ -394,6 +394,16 @@ describe("Netscript Dynamic RAM Calculation/Generation Tests", function() { await testNonzeroDynamicRamCost(f); }); + it("getServerMaxRam()", async function() { + const f = ["getServerMaxRam"]; + await testNonzeroDynamicRamCost(f); + }); + + it("getServerUsedRam()", async function() { + const f = ["getServerUsedRam"]; + await testNonzeroDynamicRamCost(f); + }); + it("serverExists()", async function() { const f = ["serverExists"]; await testNonzeroDynamicRamCost(f); diff --git a/test/Netscript/StaticRamCalculationTests.js b/test/Netscript/StaticRamCalculationTests.js index a883e3b71..bd40a32c3 100644 --- a/test/Netscript/StaticRamCalculationTests.js +++ b/test/Netscript/StaticRamCalculationTests.js @@ -284,6 +284,16 @@ describe("Netscript Static RAM Calculation/Generation Tests", function() { await expectNonZeroRamCost(f); }); + it("getServerMaxRam()", async function() { + const f = ["getServerMaxRam"]; + await expectNonZeroRamCost(f); + }); + + it("getServerUsedRam()", async function() { + const f = ["getServerUsedRam"]; + await expectNonZeroRamCost(f); + }); + it("serverExists()", async function() { const f = ["serverExists"]; await expectNonZeroRamCost(f);