From 5a45e3584d38b9e64a654491db235920e3b7d7c7 Mon Sep 17 00:00:00 2001 From: Olivier Gagnon Date: Sun, 2 May 2021 01:04:49 -0400 Subject: [PATCH] Formatted time in netscript hgw display in human time, milliFormat now displays milli sometimes --- src/NetscriptFunctions.js | 6 +++--- src/Terminal.jsx | 8 ++++---- test/StringHelperFunctionsTests.ts | 11 +++++++++++ test/index.js | 1 + utils/StringHelperFunctions.ts | 11 +++++++++-- 5 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 test/StringHelperFunctionsTests.ts diff --git a/src/NetscriptFunctions.js b/src/NetscriptFunctions.js index 5816967eb..e257a6452 100644 --- a/src/NetscriptFunctions.js +++ b/src/NetscriptFunctions.js @@ -670,7 +670,7 @@ function NetscriptFunctions(workerScript) { throw makeRuntimeErrorMsg('hack', canHack.msg); } - workerScript.log("hack", `Executing ${ip} in ${hackingTime.toFixed(3)} seconds (t=${threads})`); + workerScript.log("hack", `Executing ${ip} in ${convertTimeMsToTimeElapsedString(hackingTime*1000, true)} (t=${threads})`); return netscriptDelay(hackingTime * 1000, workerScript).then(function() { if (workerScript.env.stopFlag) {return Promise.reject(workerScript);} @@ -937,7 +937,7 @@ function NetscriptFunctions(workerScript) { } var growTime = calculateGrowTime(server, Player); - workerScript.log("grow", `Executing on '${server.hostname}' in ${formatNumber(growTime, 3)} seconds (t=${threads}).`); + workerScript.log("grow", `Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(growTime*1000, true)} (t=${threads}).`); return netscriptDelay(growTime * 1000, workerScript).then(function() { if (workerScript.env.stopFlag) {return Promise.reject(workerScript);} const moneyBefore = server.moneyAvailable <= 0 ? 1 : server.moneyAvailable; @@ -988,7 +988,7 @@ function NetscriptFunctions(workerScript) { } var weakenTime = calculateWeakenTime(server, Player); - workerScript.log("weaken", `Executing on '${server.hostname}' in ${formatNumber(weakenTime, 3)} seconds (t=${threads})`); + workerScript.log("weaken", `Executing on '${server.hostname}' in ${convertTimeMsToTimeElapsedString(weakenTime*1000, true)} (t=${threads})`); return netscriptDelay(weakenTime * 1000, workerScript).then(function() { if (workerScript.env.stopFlag) {return Promise.reject(workerScript);} server.weaken(CONSTANTS.ServerWeakenAmount * threads); diff --git a/src/Terminal.jsx b/src/Terminal.jsx index 6fcf04884..91a670390 100644 --- a/src/Terminal.jsx +++ b/src/Terminal.jsx @@ -583,7 +583,7 @@ let Terminal = { if (!isHacknet) { post("Required hacking skill: " + currServ.requiredHackingSkill); } post("Server security level: " + numeralWrapper.formatServerSecurity(currServ.hackDifficulty)); post("Chance to hack: " + numeralWrapper.formatPercentage(calculateHackingChance(currServ, Player))); - post("Time to hack: " + convertTimeMsToTimeElapsedString(calculateHackingTime(currServ, Player)*1000)); + post("Time to hack: " + convertTimeMsToTimeElapsedString(calculateHackingTime(currServ, Player)*1000, true)); postElement(<>Total money available on server: {Money(currServ.moneyAvailable)}); if (!isHacknet) { post("Required number of open ports for NUKE: " + currServ.numOpenPortsRequired); } @@ -2153,9 +2153,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: ${convertTimeMsToTimeElapsedString(calculateHackingTime(targetServer, Player)*1000)}`); - post(`Netscript grow() execution time: ${convertTimeMsToTimeElapsedString(calculateGrowTime(targetServer, Player)*1000)}`); - post(`Netscript weaken() execution time: ${convertTimeMsToTimeElapsedString(calculateWeakenTime(targetServer, Player)*1000)}`); + post(`Netscript hack() execution time: ${convertTimeMsToTimeElapsedString(calculateHackingTime(targetServer, Player)*1000, true)}`); + post(`Netscript grow() execution time: ${convertTimeMsToTimeElapsedString(calculateGrowTime(targetServer, Player)*1000, true)}`); + post(`Netscript weaken() execution time: ${convertTimeMsToTimeElapsedString(calculateWeakenTime(targetServer, Player)*1000, true)}`); }; programHandlers[Programs.AutoLink.name] = () => { post("This executable cannot be run."); diff --git a/test/StringHelperFunctionsTests.ts b/test/StringHelperFunctionsTests.ts new file mode 100644 index 000000000..2e45f7845 --- /dev/null +++ b/test/StringHelperFunctionsTests.ts @@ -0,0 +1,11 @@ +import { expect } from "chai"; +import { convertTimeMsToTimeElapsedString } from "../utils/StringHelperFunctions"; + +describe("StringHelperFunctions Tests", function() { + expect(convertTimeMsToTimeElapsedString(1000)).to.equal("1 seconds"); + expect(convertTimeMsToTimeElapsedString(5*60*1000+34*1000)).to.equal("5 minutes 34 seconds"); + expect(convertTimeMsToTimeElapsedString(2*60*60*24*1000+5*60*1000+34*1000)).to.equal("2 days 5 minutes 34 seconds"); + expect(convertTimeMsToTimeElapsedString(2*60*60*24*1000+5*60*1000+34*1000, true)).to.equal("2 days 5 minutes 34.000 seconds"); + expect(convertTimeMsToTimeElapsedString(2*60*60*24*1000+5*60*1000+34*1000+123, true)).to.equal("2 days 5 minutes 34.123 seconds"); + expect(convertTimeMsToTimeElapsedString(2*60*60*24*1000+5*60*1000+34*1000+123.888, true)).to.equal("2 days 5 minutes 34.123 seconds"); +}) \ No newline at end of file diff --git a/test/index.js b/test/index.js index 68045825f..3b95de359 100644 --- a/test/index.js +++ b/test/index.js @@ -1,4 +1,5 @@ export * from "./Netscript/DynamicRamCalculationTests"; export * from "./Netscript/StaticRamCalculationTests"; export * from "./StockMarketTests"; +export * from "./StringHelperFunctionsTests"; export * from "./Terminal/DirectoryTests"; diff --git a/utils/StringHelperFunctions.ts b/utils/StringHelperFunctions.ts index effe284d4..90f80fab5 100644 --- a/utils/StringHelperFunctions.ts +++ b/utils/StringHelperFunctions.ts @@ -13,7 +13,8 @@ Converts a date representing time in milliseconds to a string with the format H e.g. 10000 -> "10 seconds" 120000 -> "2 minutes and 0 seconds" */ -function convertTimeMsToTimeElapsedString(time: number): string { +function convertTimeMsToTimeElapsedString(time: number, showMilli=false): string { + time = Math.floor(time); const millisecondsPerSecond = 1000; const secondPerMinute = 60; const minutesPerHours = 60; @@ -33,7 +34,13 @@ function convertTimeMsToTimeElapsedString(time: number): string { const minutes: number = Math.floor(secTruncHours / secondPerMinute); const secTruncMinutes: number = secTruncHours % secondPerMinute; - const seconds: number = secTruncMinutes; + const milliTruncSec: string = (() => { + let str: string = `${time % millisecondsPerSecond}`; + while(str.length < 3) str = "0"+str; + return str; + })() + + const seconds: string = showMilli ? `${secTruncMinutes}.${milliTruncSec}` : `${secTruncMinutes}`; let res = ""; if (days > 0) {res += `${days} days `; }