added upgrade-all.ts (wip)

This commit is contained in:
Vomitblood 2026-05-04 01:28:24 +08:00
parent fbe84d3c25
commit e77aed0065
12 changed files with 3290 additions and 56 deletions

View file

@ -5,6 +5,7 @@ NOTE: All user scripts to be synced with the game is found under `src/`.
## TODO ## TODO
- `ezgame.analyze` - `ezgame.analyze`
- `ezgame.cloud.upgradeAll`
This is a template for a viteburner project. It is a simple example of how to use Viteburner. This is a template for a viteburner project. It is a simple example of how to use Viteburner.

3215
package-lock.json generated

File diff suppressed because it is too large Load diff

View file

@ -11,6 +11,7 @@
"@typescript-eslint/parser": "^5.55.0", "@typescript-eslint/parser": "^5.55.0",
"eslint": "^8.36.0", "eslint": "^8.36.0",
"eslint-config-prettier": "^8.7.0", "eslint-config-prettier": "^8.7.0",
"eslint-plugin-import": "^2.32.0",
"eslint-plugin-prettier": "^4.2.1", "eslint-plugin-prettier": "^4.2.1",
"prettier": "^2.8.4", "prettier": "^2.8.4",
"typescript": "^4.9.5", "typescript": "^4.9.5",
@ -18,4 +19,4 @@
"viteburner": "^0.5.3" "viteburner": "^0.5.3"
}, },
"license": "MIT" "license": "MIT"
} }

View file

@ -24,8 +24,8 @@ export const buyMax = (ns: NS, serverNamePrefix: string) => {
// we start with 2^1, then increase the power // we start with 2^1, then increase the power
// once we hit the max, we return the -1 power that was working before // once we hit the max, we return the -1 power that was working before
let targetRam = 0; let targetRam = 0;
// do until 2^67 // do until 2^20
for (let pow = 1; pow <= 67; pow++) { for (let pow = 1; pow <= 20; pow++) {
// calculate the ram based on power // calculate the ram based on power
const ram = Math.pow(2, pow); const ram = Math.pow(2, pow);
// get the cost of the server with the ram // get the cost of the server with the ram

View file

@ -0,0 +1,9 @@
import { buyMax } from "./buy-max";
import { deleteAll } from "./delete-all";
import { upgradeAll } from "./upgrade-all";
export const cloud = {
buyMax,
deleteAll,
upgradeAll,
};

View file

@ -0,0 +1,81 @@
import { NS } from "@ns";
import { utils } from "@/utils";
import { scan } from "../scan";
export const upgradeAll = (ns: NS) => {
// get a list of all the purchased servers
const cloudServerNames: string[] = scan(ns, { purchasedByPlayer: true });
// check if there are any servers to upgrade
if (cloudServerNames.length === 0) {
ns.tprint("No servers to upgrade.");
return;
}
// get current money available
const moneyAvailable = ns.getServerMoneyAvailable("home");
// find the maximum ram we can afford for all servers
let targetRam = 0;
for (let pow = 1; pow <= 20; pow++) {
const ram = Math.pow(2, pow);
let totalCost = 0;
for (const serverName of cloudServerNames) {
const upgradeCost = ns.cloud.getServerUpgradeCost(serverName, ram);
if (upgradeCost === -1) {
totalCost = Infinity;
break;
}
totalCost += upgradeCost;
}
if (totalCost <= moneyAvailable) {
targetRam = ram;
} else {
break;
}
}
// calculate the cost of the next tier regardless of whether we can afford current
const nextRam = targetRam === 0 ? 2 : targetRam * 2;
let nextTierCost = 0;
for (const serverName of cloudServerNames) {
const upgradeCost = ns.cloud.getServerUpgradeCost(serverName, nextRam);
if (upgradeCost === -1) {
nextTierCost = -1;
break;
}
nextTierCost += upgradeCost;
}
// check if we found a valid target ram amount
if (targetRam === 0) {
ns.tprint("Cannot afford to upgrade any servers.");
if (nextTierCost === -1) {
ns.tprint(`Next tier: ${utils.format.ram(nextRam)} GB is the maximum.`);
} else {
ns.tprint(`Next tier (${utils.format.ram(nextRam)} GB) will cost: ${utils.format.money(nextTierCost)}`);
}
return;
}
// upgrade the servers to the target ram
let serversUpgraded = 0;
let totalRamUpgraded = 0;
for (const serverName of cloudServerNames) {
const currentRam = ns.getServerMaxRam(serverName);
if (currentRam >= targetRam) continue;
totalRamUpgraded += targetRam - currentRam;
if (ns.cloud.upgradeServer(serverName, targetRam)) {
serversUpgraded++;
}
}
ns.tprint(`Servers upgraded: ${serversUpgraded}`);
ns.tprint(`Total RAM upgraded: ${utils.format.ram(totalRamUpgraded)} GB`);
ns.tprint(`Target RAM: ${utils.format.ram(targetRam)} GB RAM each`);
if (nextTierCost === -1) {
ns.tprint(`Next tier: ${utils.format.ram(nextRam)} GB is the maximum.`);
} else {
ns.tprint(`Next tier (${utils.format.ram(nextRam)} GB) will cost: ${utils.format.money(nextTierCost)}`);
}
};

View file

@ -1,5 +0,0 @@
import { formulas } from "./formulas";
export const ezgame = {
formulas,
};

View file

@ -5,11 +5,12 @@ interface ScanOptions {
requiredHackingSkill?: number; requiredHackingSkill?: number;
portsForNuke?: number; portsForNuke?: number;
ram?: number; ram?: number;
purchasedByPlayer?: boolean;
} }
export const scan = (ns: NS, options: ScanOptions = {}): string[] => { export const scan = (ns: NS, options: ScanOptions = {}): string[] => {
// destructure options // destructure options
const { rootAccess, requiredHackingSkill, portsForNuke, ram } = options; const { rootAccess, requiredHackingSkill, portsForNuke, ram, purchasedByPlayer } = options;
// initialize a new set to store a list of all hosts // initialize a new set to store a list of all hosts
const allHosts = new Set<string>(["home"]); const allHosts = new Set<string>(["home"]);
@ -29,6 +30,7 @@ export const scan = (ns: NS, options: ScanOptions = {}): string[] => {
return false; return false;
if (portsForNuke !== undefined && ns.getServerNumPortsRequired(host) > portsForNuke) return false; if (portsForNuke !== undefined && ns.getServerNumPortsRequired(host) > portsForNuke) return false;
if (ram !== undefined && ns.getServerMaxRam(host) < ram) return false; if (ram !== undefined && ns.getServerMaxRam(host) < ram) return false;
if (purchasedByPlayer !== undefined && ns.getServer(host).purchasedByPlayer !== purchasedByPlayer) return false;
return true; return true;
}); });

View file

@ -27,6 +27,6 @@ export const scriptPropagator = (ns: NS) => {
return rootedHosts; return rootedHosts;
}; };
export const main = (ns: NS) => { // export const main = (ns: NS) => {
scriptPropagator(ns); // scriptPropagator(ns);
}; // };

View file

@ -53,8 +53,8 @@ export const startall = (ns: NS, scriptName: string, options: StartOptions = {})
ns.tprint(`Hosts affected: ${hostsStartedOn}`); ns.tprint(`Hosts affected: ${hostsStartedOn}`);
}; };
export const main = (ns: NS) => { // export const main = (ns: NS) => {
// get the arguments from the command line // // get the arguments from the command line
const args = ns.args as string[]; // const args = ns.args as string[];
startall(ns, "super.js", { args: args }); // startall(ns, "super.js", { args: args });
}; // };

View file

@ -1,10 +1,6 @@
import { NS } from "@ns"; import { NS } from "@ns";
import { ezgame } from "./ezgame"; import { cloud } from "./ezgame/cloud";
import { getNormalServer } from "./ezgame/get-normal-server";
import { utils } from "./utils";
export const main = (ns: NS) => { export const main = (ns: NS) => {
// console.log(ns.formulas.hacking.growTime(getNormalServer(ns, "n00dles"), ns.getPlayer())); cloud.upgradeAll(ns);
console.log(utils.format.number(ns.cloud.getServerCost(2048)));
console.log(utils.format.date(ezgame.formulas.hacking.growTime(getNormalServer(ns, "n00dles"), ns.getPlayer())));
}; };