init commit
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
import { formatMoney } from "formatMoney.js";
|
||||
|
||||
var servers;
|
||||
var money;
|
||||
|
||||
/** @param {NS} ns */
|
||||
function buyServer(ns, ram) {
|
||||
ns.purchaseServer("server-" + servers.length, ram);
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function buyServerWithMaxRAM(ns) {
|
||||
var ram = calculateServerWithMaxRAM(ns);
|
||||
buyServer(ns, ram);
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function calculateServerCost(ns, ram) {
|
||||
ram = Math.pow(2, ram);
|
||||
var cost = ns.getPurchasedServerCost(ram);
|
||||
|
||||
if(cost > money) {
|
||||
ns.tprint("Cannot afford a server with " + ram + " GB of RAM!");
|
||||
return false;
|
||||
} else return cost;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
function calculateServerWithMaxRAM(ns) {
|
||||
var ram = 1;
|
||||
var cost = 0;
|
||||
for(var i = 20; i >= 0; i--) {
|
||||
ram = Math.pow(2, i);
|
||||
if(cost = calculateServerCost(ns, i)) {
|
||||
if(cost <= money) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return ram;
|
||||
}
|
||||
|
||||
/** @param {NS} ns */
|
||||
export async function main(ns) {
|
||||
servers = ns.getPurchasedServers();
|
||||
money = ns.getServerMoneyAvailable("home");
|
||||
|
||||
if(ns.args.length == 0) {
|
||||
buyServerWithMaxRAM(ns);
|
||||
} else if(ns.args[0] == "--max" || ns.args[0] == "-m") {
|
||||
var ram = calculateServerWithMaxRAM(ns);
|
||||
ns.tprint("cost: " + formatMoney(ns.getPurchasedServerCost(ram)) + " | ram: " + ram + " GB");
|
||||
} else if(ns.args.length == 2 && (ns.args[0] == "--ram" || ns.args[0] == "-r")) {
|
||||
if(ns.args[1] == "max") {
|
||||
buyServerWithMaxRAM(ns);
|
||||
} else if(calculateServerCost(ns, ns.args[1])) {
|
||||
buyServer(ns, ns.args[1]);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user