-
Notifications
You must be signed in to change notification settings - Fork 0
/
buyServer.js
41 lines (38 loc) · 1.34 KB
/
buyServer.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/** @param {NS} ns **/
export async function main(ns) {
let myMoney = ns.getServerMoneyAvailable("home");
let maxServerLvl = Math.log2(ns.getPurchasedServerMaxRam());
ns.tprint("Current Money: " + trimBigNumber(ns, myMoney));
if (ns.args.length == 0) {
printUsageGuide(ns,maxServerLvl);
}
else if (ns.args.length > 1) {
throw "Wrong usage, only need 0/1 argument: [none]/<ramLevel>";
}
else {
const ramLvl = ns.args[0];
if (ramLvl > maxServerLvl || ramLvl < 1) {
throw "Wrong usage, ramLvl between [1,"+maxServerLvl+"]";
}
ns.purchaseServer("pServ", Math.pow(2, ramLvl));
myMoney = ns.getServerMoneyAvailable("home");
ns.tprint("Money After buying server: " + trimBigNumber(ns, myMoney));
}
}
function generateServerPriceArr(ns,maxServerLvl) {
let serverPriceArr = []
for (let ramLvl = 1; ramLvl < maxServerLvl+1; ramLvl++) {
let moneyNeed = ns.getPurchasedServerCost(Math.pow(2, ramLvl));
serverPriceArr.push(moneyNeed);
}
return serverPriceArr;
}
function printUsageGuide(ns,maxServerLvl) {
let arr = generateServerPriceArr(ns,maxServerLvl);
let rowString = arr.map((it) => { return " " + trimBigNumber(ns, it) });
ns.tprint("Price for serverLvl 1-10: " + rowString.slice(0, 10));
ns.tprint("Price for serverLvl 11-20:" + rowString.slice(10));
}
function trimBigNumber(ns, realPrice) {
return ns.nFormat(realPrice, "($0.00a)");
}