added upgrade-all.ts (wip)
This commit is contained in:
parent
fbe84d3c25
commit
e77aed0065
|
|
@ -5,6 +5,7 @@ NOTE: All user scripts to be synced with the game is found under `src/`.
|
|||
## TODO
|
||||
|
||||
- `ezgame.analyze`
|
||||
- `ezgame.cloud.upgradeAll`
|
||||
|
||||
This is a template for a viteburner project. It is a simple example of how to use Viteburner.
|
||||
|
||||
|
|
|
|||
3215
package-lock.json
generated
3215
package-lock.json
generated
File diff suppressed because it is too large
Load diff
|
|
@ -11,6 +11,7 @@
|
|||
"@typescript-eslint/parser": "^5.55.0",
|
||||
"eslint": "^8.36.0",
|
||||
"eslint-config-prettier": "^8.7.0",
|
||||
"eslint-plugin-import": "^2.32.0",
|
||||
"eslint-plugin-prettier": "^4.2.1",
|
||||
"prettier": "^2.8.4",
|
||||
"typescript": "^4.9.5",
|
||||
|
|
|
|||
|
|
@ -24,8 +24,8 @@ export const buyMax = (ns: NS, serverNamePrefix: string) => {
|
|||
// we start with 2^1, then increase the power
|
||||
// once we hit the max, we return the -1 power that was working before
|
||||
let targetRam = 0;
|
||||
// do until 2^67
|
||||
for (let pow = 1; pow <= 67; pow++) {
|
||||
// do until 2^20
|
||||
for (let pow = 1; pow <= 20; pow++) {
|
||||
// calculate the ram based on power
|
||||
const ram = Math.pow(2, pow);
|
||||
// get the cost of the server with the ram
|
||||
9
src/ezgame/cloud/index.ts
Normal file
9
src/ezgame/cloud/index.ts
Normal 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,
|
||||
};
|
||||
81
src/ezgame/cloud/upgrade-all.ts
Normal file
81
src/ezgame/cloud/upgrade-all.ts
Normal 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)}`);
|
||||
}
|
||||
};
|
||||
|
|
@ -1,5 +0,0 @@
|
|||
import { formulas } from "./formulas";
|
||||
|
||||
export const ezgame = {
|
||||
formulas,
|
||||
};
|
||||
|
|
@ -5,11 +5,12 @@ interface ScanOptions {
|
|||
requiredHackingSkill?: number;
|
||||
portsForNuke?: number;
|
||||
ram?: number;
|
||||
purchasedByPlayer?: boolean;
|
||||
}
|
||||
|
||||
export const scan = (ns: NS, options: ScanOptions = {}): string[] => {
|
||||
// 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
|
||||
const allHosts = new Set<string>(["home"]);
|
||||
|
|
@ -29,6 +30,7 @@ export const scan = (ns: NS, options: ScanOptions = {}): string[] => {
|
|||
return false;
|
||||
if (portsForNuke !== undefined && ns.getServerNumPortsRequired(host) > portsForNuke) return false;
|
||||
if (ram !== undefined && ns.getServerMaxRam(host) < ram) return false;
|
||||
if (purchasedByPlayer !== undefined && ns.getServer(host).purchasedByPlayer !== purchasedByPlayer) return false;
|
||||
|
||||
return true;
|
||||
});
|
||||
|
|
|
|||
|
|
@ -27,6 +27,6 @@ export const scriptPropagator = (ns: NS) => {
|
|||
return rootedHosts;
|
||||
};
|
||||
|
||||
export const main = (ns: NS) => {
|
||||
scriptPropagator(ns);
|
||||
};
|
||||
// export const main = (ns: NS) => {
|
||||
// scriptPropagator(ns);
|
||||
// };
|
||||
|
|
|
|||
|
|
@ -53,8 +53,8 @@ export const startall = (ns: NS, scriptName: string, options: StartOptions = {})
|
|||
ns.tprint(`Hosts affected: ${hostsStartedOn}`);
|
||||
};
|
||||
|
||||
export const main = (ns: NS) => {
|
||||
// get the arguments from the command line
|
||||
const args = ns.args as string[];
|
||||
startall(ns, "super.js", { args: args });
|
||||
};
|
||||
// export const main = (ns: NS) => {
|
||||
// // get the arguments from the command line
|
||||
// const args = ns.args as string[];
|
||||
// startall(ns, "super.js", { args: args });
|
||||
// };
|
||||
|
|
|
|||
|
|
@ -1,10 +1,6 @@
|
|||
import { NS } from "@ns";
|
||||
import { ezgame } from "./ezgame";
|
||||
import { getNormalServer } from "./ezgame/get-normal-server";
|
||||
import { utils } from "./utils";
|
||||
import { cloud } from "./ezgame/cloud";
|
||||
|
||||
export const main = (ns: NS) => {
|
||||
// console.log(ns.formulas.hacking.growTime(getNormalServer(ns, "n00dles"), ns.getPlayer()));
|
||||
console.log(utils.format.number(ns.cloud.getServerCost(2048)));
|
||||
console.log(utils.format.date(ezgame.formulas.hacking.growTime(getNormalServer(ns, "n00dles"), ns.getPlayer())));
|
||||
cloud.upgradeAll(ns);
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue