From 2d9f3856c75f0ba0e0dd47691d9b796ec90aa3f2 Mon Sep 17 00:00:00 2001 From: Matthew Goff Date: Sat, 1 Sep 2018 08:42:56 -0500 Subject: [PATCH] formatNumber -> numeral in more locations --- src/Script.js | 7 +++---- src/Terminal.js | 33 ++++++++++++++++----------------- 2 files changed, 19 insertions(+), 21 deletions(-) mode change 100644 => 100755 src/Script.js mode change 100644 => 100755 src/Terminal.js diff --git a/src/Script.js b/src/Script.js old mode 100644 new mode 100755 index 670ff2cd7..27c60e193 --- a/src/Script.js +++ b/src/Script.js @@ -31,15 +31,14 @@ import {AllServers, processSingleServerGrowth} from "./Server"; import {Settings} from "./Settings"; import {post} from "./ui/postToTerminal"; import {TextFile} from "./TextFile"; - import {parse, Node} from "../utils/acorn"; import {Page, routing} from "./ui/navigationTracking"; +import numeral from "numeral/min/numeral.min"; import {dialogBoxCreate} from "../utils/DialogBox"; import {Reviver, Generic_toJSON, Generic_fromJSON} from "../utils/JSONReviver"; import {compareArrays} from "../utils/helpers/compareArrays"; import {createElement} from "../utils/uiHelpers/createElement"; -import {formatNumber} from "../utils/StringHelperFunctions"; import {getTimestamp} from "../utils/helpers/getTimestamp"; import {roundToTwo} from "../utils/helpers/roundToTwo"; @@ -252,7 +251,7 @@ function updateScriptEditorContent() { var codeCopy = code.repeat(1); var ramUsage = calculateRamUsage(codeCopy); if (ramUsage !== -1) { - scriptEditorRamText.innerText = "RAM: " + formatNumber(ramUsage, 2).toString() + "GB"; + scriptEditorRamText.innerText = "RAM: " + numeral(ramUsage).format('0.00') + " GB"; } else { scriptEditorRamText.innerText = "RAM: Syntax Error"; } @@ -875,7 +874,7 @@ function scriptCalculateOfflineProduction(runningScriptObj) { console.log(runningScriptObj.filename + " called grow() on " + serv.hostname + " " + timesGrown + " times while offline"); runningScriptObj.log("Called grow() on " + serv.hostname + " " + timesGrown + " times while offline"); var growth = processSingleServerGrowth(serv, timesGrown * 450); - runningScriptObj.log(serv.hostname + " grown by " + formatNumber(growth * 100 - 100, 6) + "% from grow() calls made while offline"); + runningScriptObj.log(serv.hostname + " grown by " + numeral(growth * 100 - 100).format('0.000000%') + " from grow() calls made while offline"); } } diff --git a/src/Terminal.js b/src/Terminal.js old mode 100644 new mode 100755 index d87277a60..2b3a569df --- a/src/Terminal.js +++ b/src/Terminal.js @@ -31,9 +31,8 @@ import {Settings} from "./Settings"; import {SpecialServerIps, SpecialServerNames} from "./SpecialServerIps"; import {TextFile, getTextFile} from "./TextFile"; - -import {containsAllStrings, longestCommonStart, - formatNumber} from "../utils/StringHelperFunctions"; +import {containsAllStrings, + longestCommonStart} from "../utils/StringHelperFunctions"; import {Page, routing} from "./ui/navigationTracking"; import {KEY} from "../utils/helpers/keyCodes"; import {addOffset} from "../utils/helpers/addOffset"; @@ -660,11 +659,11 @@ let Terminal = { server.fortify(CONSTANTS.ServerFortifyAmount); - post("Hack successful! Gained $" + formatNumber(moneyGained, 2) + " and " + formatNumber(expGainedOnSuccess, 4) + " hacking EXP"); + post("Hack successful! Gained " + numeral(moneyGained).format('($0,0.00)') + " and " + numeral(expGainedOnSuccess).format('0.0000') + " hacking EXP"); } else { //Failure //Player only gains 25% exp for failure? TODO Can change this later to balance Player.gainHackingExp(expGainedOnFailure) - post("Failed to hack " + server.hostname + ". Gained " + formatNumber(expGainedOnFailure, 4) + " hacking EXP"); + post("Failed to hack " + server.hostname + ". Gained " + numeral(expGainedOnFailure).format('0.0000') + " hacking EXP"); } } @@ -686,10 +685,10 @@ let Terminal = { else {rootAccess = "NO";} post("Root Access: " + rootAccess); post("Required hacking skill: " + Player.getCurrentServer().requiredHackingSkill); - post("Estimated server security level: " + formatNumber(addOffset(Player.getCurrentServer().hackDifficulty, 5), 3)); - post("Estimated chance to hack: " + formatNumber(addOffset(Player.calculateHackingChance() * 100, 5), 2) + "%"); - post("Estimated time to hack: " + formatNumber(addOffset(Player.calculateHackingTime(), 5), 3) + " seconds"); - post("Estimated total money available on server: $" + formatNumber(addOffset(Player.getCurrentServer().moneyAvailable, 5), 2)); + post("Estimated server security level: " + numeral(addOffset(Player.getCurrentServer().hackDifficulty, 5)).format('0.000')); + post("Estimated chance to hack: " + numeral(addOffset(Player.calculateHackingChance(), 5)).format('0.00%')); + post("Estimated time to hack: " + numeral(addOffset(Player.calculateHackingTime(), 5)).format('0.000') + " seconds"); + post("Estimated total money available on server: " + numeral(addOffset(Player.getCurrentServer().moneyAvailable, 5)).format('$0,0.00')); post("Required number of open ports for NUKE: " + Player.getCurrentServer().numOpenPortsRequired); if (Player.getCurrentServer().sshPortOpen) { post("SSH port: Open") @@ -1190,7 +1189,7 @@ let Terminal = { var scriptBaseRamUsage = currServ.scripts[i].ramUsage; var ramUsage = scriptBaseRamUsage * numThreads; - post("This script requires " + formatNumber(ramUsage, 2) + "GB of RAM to run for " + numThreads + " thread(s)"); + post("This script requires " + numeral(ramUsage).format('0.00') + " GB of RAM to run for " + numThreads + " thread(s)"); return; } } @@ -1555,7 +1554,7 @@ let Terminal = { var spacesThread = Array(numSpacesThread+1).join(" "); //Calculate and transform RAM usage - ramUsage = formatNumber(script.scriptRef.ramUsage * script.threads, 2).toString() + "GB"; + ramUsage = numeral(script.scriptRef.ramUsage * script.threads).format('0.00') + " GB"; var entry = [script.filename, spacesScript, script.threads, spacesThread, ramUsage]; post(entry.join("")); @@ -1768,9 +1767,9 @@ let Terminal = { if (commandArray.length != 1) { post("Incorrect usage of free command. Usage: free"); return; } - post("Total: " + formatNumber(Player.getCurrentServer().maxRam, 2) + " GB"); - post("Used: " + formatNumber(Player.getCurrentServer().ramUsed, 2) + " GB"); - post("Available: " + formatNumber(Player.getCurrentServer().maxRam - Player.getCurrentServer().ramUsed, 2) + " GB"); + post("Total: " + numeral(Player.getCurrentServer().maxRam).format('0.00') + " GB"); + post("Used: " + numeral(Player.getCurrentServer().ramUsed,).format('0.00') + " GB"); + post("Available: " + numeral(Player.getCurrentServer().maxRam - Player.getCurrentServer().ramUsed).format('0.00') + " GB"); }, //First called when the "run [program]" command is called. Checks to see if you @@ -1891,9 +1890,9 @@ let Terminal = { post("Server base security level: " + targetServer.baseDifficulty); post("Server current security level: " + targetServer.hackDifficulty); post("Server growth rate: " + targetServer.serverGrowth); - post("Netscript hack() execution time: " + formatNumber(scriptCalculateHackingTime(targetServer), 1) + "s"); - post("Netscript grow() execution time: " + formatNumber(scriptCalculateGrowTime(targetServer)/1000, 1) + "s"); - post("Netscript weaken() execution time: " + formatNumber(scriptCalculateWeakenTime(targetServer)/1000, 1) + "s"); + post("Netscript hack() execution time: " + numeral(scriptCalculateHackingTime(targetServer)).format('0.0') + "s"); + post("Netscript grow() execution time: " + numeral(scriptCalculateGrowTime(targetServer)/1000).format('0.0') + "s"); + post("Netscript weaken() execution time: " + numeral(scriptCalculateWeakenTime(targetServer)/1000).format('0.0') + "s"); }; programHandlers[Programs.AutoLink.name] = () => { post("This executable cannot be run.");