diff --git a/README.md b/README.md index 424b7a3..9d199fa 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,7 @@ NOTE: All user scripts to be synced with the game is found under `src/`. ## TODO -- Fix the fucking ezgame.formulas.hacking.growTime(). calculateIntelligenceBonus() is throwing errors when calculating intelligence. Clearly my intelligence value is 0. +- `ezgame.analyze` This is a template for a viteburner project. It is a simple example of how to use Viteburner. diff --git a/src/ezgame.ts b/src/ezgame.ts deleted file mode 100644 index 973b30d..0000000 --- a/src/ezgame.ts +++ /dev/null @@ -1,5 +0,0 @@ -import { formulas } from "./ezgame/formulas"; - -export const ezgame = { - formulas, -}; diff --git a/src/utils/analyze.ts b/src/ezgame/analyze.ts similarity index 98% rename from src/utils/analyze.ts rename to src/ezgame/analyze.ts index 1ea4f0d..7bd75fd 100644 --- a/src/utils/analyze.ts +++ b/src/ezgame/analyze.ts @@ -1,5 +1,6 @@ import { NS, Server } from "@ns"; +// TODO: incomplete // define a new interface for the new analysis results interface ServerAnalysis { hostname: string; diff --git a/src/ezgame/formulas.ts b/src/ezgame/formulas.ts deleted file mode 100644 index a8020f2..0000000 --- a/src/ezgame/formulas.ts +++ /dev/null @@ -1,15 +0,0 @@ -import { gang } from "./formulas/gang"; -import { hacking } from "./formulas/hacking"; -import { hacknetNodes } from "./formulas/hacknet-nodes"; -import { hacknetServers } from "./formulas/hacknet-servers"; -import { reputation } from "./formulas/reputation"; -import { skills } from "./formulas/skills"; - -export const formulas = { - gang, - hacking, - hacknetNodes, - hacknetServers, - reputation, - skills, -}; diff --git a/src/ezgame/formulas/index.ts b/src/ezgame/formulas/index.ts new file mode 100644 index 0000000..c23e89e --- /dev/null +++ b/src/ezgame/formulas/index.ts @@ -0,0 +1,15 @@ +import { gang } from "./gang"; +import { hacking } from "./hacking"; +import { hacknetNodes } from "./hacknet-nodes"; +import { hacknetServers } from "./hacknet-servers"; +import { reputation } from "./reputation"; +import { skills } from "./skills"; + +export const formulas = { + gang, + hacking, + hacknetNodes, + hacknetServers, + reputation, + skills, +}; diff --git a/src/ezgame/formulas/test.ts b/src/ezgame/formulas/test.ts index 237a4e4..f7fed2f 100644 --- a/src/ezgame/formulas/test.ts +++ b/src/ezgame/formulas/test.ts @@ -1,5 +1,5 @@ import { ezgame } from "@/ezgame"; -import { getNormalServer } from "@/utils/get-normal-server"; +import { getNormalServer } from "@/ezgame/get-normal-server"; import { utils } from "@/utils/utils"; import { NS } from "@ns"; diff --git a/src/utils/get-normal-server.ts b/src/ezgame/get-normal-server.ts similarity index 100% rename from src/utils/get-normal-server.ts rename to src/ezgame/get-normal-server.ts diff --git a/src/ezgame/index.ts b/src/ezgame/index.ts new file mode 100644 index 0000000..f373285 --- /dev/null +++ b/src/ezgame/index.ts @@ -0,0 +1,5 @@ +import { formulas } from "./formulas"; + +export const ezgame = { + formulas, +}; diff --git a/src/utils/kill.ts b/src/ezgame/kill.ts similarity index 100% rename from src/utils/kill.ts rename to src/ezgame/kill.ts diff --git a/src/utils/killall.ts b/src/ezgame/killall.ts similarity index 95% rename from src/utils/killall.ts rename to src/ezgame/killall.ts index 21d0bfd..c359f96 100644 --- a/src/utils/killall.ts +++ b/src/ezgame/killall.ts @@ -1,5 +1,5 @@ import { NS } from "@ns"; -import { scan } from "./scan"; +import { scan } from "../ezgame/scan"; export const killall = (ns: NS): void => { const allHosts = scan(ns); diff --git a/src/utils/nuke.ts b/src/ezgame/nuke.ts similarity index 97% rename from src/utils/nuke.ts rename to src/ezgame/nuke.ts index 8a8df6b..15fc03a 100644 --- a/src/utils/nuke.ts +++ b/src/ezgame/nuke.ts @@ -1,5 +1,5 @@ import { NS } from "@ns"; -import { scan } from "./scan"; +import { scan } from "../ezgame/scan"; // get root access to all the servers that we can // then return a list of all the servers we have root access to diff --git a/src/utils/scan.ts b/src/ezgame/scan.ts similarity index 100% rename from src/utils/scan.ts rename to src/ezgame/scan.ts diff --git a/src/utils/script-cleanup.ts b/src/ezgame/script-cleanup.ts similarity index 95% rename from src/utils/script-cleanup.ts rename to src/ezgame/script-cleanup.ts index 7c98250..4142b97 100644 --- a/src/utils/script-cleanup.ts +++ b/src/ezgame/script-cleanup.ts @@ -1,5 +1,5 @@ import { NS } from "@ns"; -import { scan } from "./scan"; +import { scan } from "../ezgame/scan"; export const scriptCleanup = (ns: NS) => { // get all hosts with root access diff --git a/src/utils/script-propagator.ts b/src/ezgame/script-propagator.ts similarity index 95% rename from src/utils/script-propagator.ts rename to src/ezgame/script-propagator.ts index 0d96bec..e4d177d 100644 --- a/src/utils/script-propagator.ts +++ b/src/ezgame/script-propagator.ts @@ -1,5 +1,5 @@ import { NS } from "@ns"; -import { scan } from "./scan"; +import { scan } from "../ezgame/scan"; export const scriptPropagator = (ns: NS) => { // get all hosts with root access diff --git a/src/utils/server/server-buy-max.ts b/src/ezgame/server/buy-max.ts similarity index 95% rename from src/utils/server/server-buy-max.ts rename to src/ezgame/server/buy-max.ts index 218da1f..05e1606 100644 --- a/src/utils/server/server-buy-max.ts +++ b/src/ezgame/server/buy-max.ts @@ -1,6 +1,6 @@ import { NS } from "@ns"; -export const serverBuyMax = (ns: NS, serverNamePrefix: string) => { +export const buyMax = (ns: NS, serverNamePrefix: string) => { // get the maximum number of servers that can be purchased const maxServers = ns.cloud.getServerLimit(); @@ -64,5 +64,5 @@ export const serverBuyMax = (ns: NS, serverNamePrefix: string) => { export const main = (ns: NS) => { const serverNamePrefix = "worker"; - serverBuyMax(ns, serverNamePrefix); + buyMax(ns, serverNamePrefix); }; diff --git a/src/utils/server/server-delete-all.ts b/src/ezgame/server/delete-all.ts similarity index 93% rename from src/utils/server/server-delete-all.ts rename to src/ezgame/server/delete-all.ts index 2e17fc8..037d6fa 100644 --- a/src/utils/server/server-delete-all.ts +++ b/src/ezgame/server/delete-all.ts @@ -1,7 +1,7 @@ import { NS } from "@ns"; import { kill } from "../kill"; -export const serverDeleteAll = (ns: NS) => { +export const deleteAll = (ns: NS) => { // get a list of all the purchased servers const serverNames: string[] = ns.cloud.getServerNames(); @@ -31,5 +31,5 @@ export const serverDeleteAll = (ns: NS) => { }; export const main = (ns: NS) => { - serverDeleteAll(ns); + deleteAll(ns); }; diff --git a/src/utils/startall.ts b/src/ezgame/startall.ts similarity index 98% rename from src/utils/startall.ts rename to src/ezgame/startall.ts index be696c7..47f3054 100644 --- a/src/utils/startall.ts +++ b/src/ezgame/startall.ts @@ -1,5 +1,5 @@ import { NS } from "@ns"; -import { scan } from "./scan"; +import { scan } from "../ezgame/scan"; interface StartOptions { threads?: number; // explicit thread count diff --git a/src/test.ts b/src/test.ts index cbd4f09..8a7a17b 100644 --- a/src/test.ts +++ b/src/test.ts @@ -1,7 +1,10 @@ import { NS } from "@ns"; -import { format } from "./utils/format-number"; +import { ezgame } from "./ezgame"; +import { getNormalServer } from "./ezgame/get-normal-server"; +import { utils } from "./utils"; export const main = (ns: NS) => { // console.log(ns.formulas.hacking.growTime(getNormalServer(ns, "n00dles"), ns.getPlayer())); - console.log(format.number(ns.cloud.getServerCost(2048))); + console.log(utils.format.number(ns.cloud.getServerCost(2048))); + console.log(utils.format.date(ezgame.formulas.hacking.growTime(getNormalServer(ns, "n00dles"), ns.getPlayer()))); }; diff --git a/src/utils/format-number.ts b/src/utils/format.ts similarity index 92% rename from src/utils/format-number.ts rename to src/utils/format.ts index 2c20ace..ee6fa45 100644 --- a/src/utils/format-number.ts +++ b/src/utils/format.ts @@ -268,6 +268,32 @@ function parseBigNumber(str: string): number { return parseFloat(str.substring(0, str.length - 1) + "e" + suffixIndex * 3); } +/** + * Format the date/time. + * If the date is today, show the time. Otherwise, show the date in "day month year" format. + * @param dateString The date string to format + * @returns Formatted date/time string + */ +function formatDate(dateString: string): string { + const date = new Date(dateString); + const today = new Date(); + + // save today date at midnight for comparison + const todayMidnight = new Date(today.getFullYear(), today.getMonth(), today.getDate()); + + // check if the date is today + if ( + date >= todayMidnight && + date < new Date(todayMidnight.getTime() + 86400000 /* milliseconds in a day sheeesh */) + ) { + // show time if today + return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); + } else { + // "day month year" format + return date.toLocaleDateString(undefined, { day: "numeric", month: "long", year: "numeric" }); + } +} + /** Namespace for all number formatting functions */ export const format = { bytes: formatBytes, @@ -304,4 +330,5 @@ export const format = { sleeveSynchro: formatSleeveSynchro, corpStat: formatCorpStat, parseBigNumber: parseBigNumber, + formatDate, }; diff --git a/src/utils/index.ts b/src/utils/index.ts new file mode 100644 index 0000000..2313923 --- /dev/null +++ b/src/utils/index.ts @@ -0,0 +1,5 @@ +import { format } from "./format"; + +export const utils = { + format, +}; diff --git a/src/utils/utils.ts b/src/utils/utils.ts index 493ec87..51012d8 100644 --- a/src/utils/utils.ts +++ b/src/utils/utils.ts @@ -1,9 +1,6 @@ import { Server } from "@ns"; export const utils = { - utilityFormatDate: formatDate, - utilityFormatSize: formatSize, - utilityUnformatSize: unformatSize, utilityExtractFilename: extractFilename, clampNumber, clampInteger, @@ -13,60 +10,6 @@ export const utils = { isDarknetServer, }; -/** - * Format the date/time. - * If the date is today, show the time. Otherwise, show the date in "day month year" format. - * @param dateString The date string to format - * @returns Formatted date/time string - */ -function formatDate(dateString: string): string { - const date = new Date(dateString); - const today = new Date(); - - // save today date at midnight for comparison - const todayMidnight = new Date(today.getFullYear(), today.getMonth(), today.getDate()); - - // check if the date is today - if ( - date >= todayMidnight && - date < new Date(todayMidnight.getTime() + 86400000 /* milliseconds in a day sheeesh */) - ) { - // show time if today - return date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" }); - } else { - // "day month year" format - return date.toLocaleDateString(undefined, { day: "numeric", month: "long", year: "numeric" }); - } -} - -/** - * Format bytes into human-readable size - * @param bytes Number of bytes to format - * @returns Formatted size string - */ -function formatSize(bytes: number): string { - if (bytes === 0) return "0 Bytes"; - - const k = 1024; - const sizes = ["Bytes", "KB", "MB", "GB"]; - const i = Math.floor(Math.log(bytes) / Math.log(k)); - - // tf is pow - return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + " " + sizes[i]; -} - -/** - * Convert a formatted size string back to bytes - * @param formattedSize Formatted size string (must be in the same format produced by `utilityFormatSize()`) - * @returns Number of bytes - */ -function unformatSize(formattedSize: string): number { - const sizes = ["Bytes", "KB", "MB", "GB"]; - const [value, unit] = formattedSize.split(" "); - const index = sizes.indexOf(unit); - return parseFloat(value) * Math.pow(1024, index); -} - /** * Extract filename stem and extension from a file path * @param filePath The file path to parse