-
Notifications
You must be signed in to change notification settings - Fork 0
/
buy-single-server.js
79 lines (63 loc) · 1.96 KB
/
buy-single-server.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/** @param {NS} ns **/
export async function main(ns) {
const defaultRam = 13; // 2 ^ 13 = 8192
let currentRam = ns.args[0] || defaultRam;
let target = ns.args[1] || "joesguns";
let serverRam = 1;
for (let iteration = 1; iteration <= currentRam; iteration++) {
serverRam = serverRam * 2;
}
const serverCost = ns.getPurchasedServerCost(serverRam);
const serverLimit = ns.getPurchasedServerLimit();
const hckScript = ["hack.js", "hake.js", "grow.js", "weaken.js"];
const hckScriptRam = ns.getScriptRam("hack.js");
const wknScriptRam = ns.getScriptRam("weaken.js");
const totalHckThread = Math.round(serverRam / 2 / hckScriptRam);
const totalWknThread = Math.round(serverRam / 2 / wknScriptRam) - 1;
let suffix = {
ram: "GB",
money: "k",
};
let divider = {
ram: 1,
money: 1000000,
};
if (serverRam >= 1e3 && serverRam < 1e6) {
suffix.ram = "TB";
divider.ram = 1e3;
}
if (serverRam >= 1e6 && serverRam < 1e9) {
suffix.ram = "PB";
divider.ram = 1e6;
}
if (serverCost >= 1e6 && serverCost < 1e9) {
suffix.money = "m";
divider.money = 1e6;
}
if (serverCost >= 1e9 && serverCost < 1e12) {
suffix.money = "b";
divider.money = 1e9;
}
ns.tprint(`Server Ram: ${serverRam / divider.ram}${suffix.ram}`);
ns.tprint(`Server Cost: ${serverCost / divider.money}${suffix.money}`);
let i = 1;
if (ns.getServerMoneyAvailable("home") < serverCost) {
ns.tprint("Not Enough Money");
return;
}
ns.tprint("Start Buying");
if (ns.getServerMoneyAvailable("home") >= serverCost) {
while (i <= serverLimit) {
const serverName = "p" + i;
if (!ns.serverExists(serverName)) {
ns.purchaseServer(serverName, serverRam);
await ns.scp(hckScript, "home", serverName);
ns.exec("hack.js", serverName, totalHckThread, target);
ns.exec("weaken.js", serverName, totalWknThread, target);
break;
}
i++;
}
}
ns.tprint("Finish Buying");
}