-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbuyserver.js
393 lines (347 loc) · 14.5 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
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import { GetAllServers, FormatMoney, FormatRam, LogMessage } from "utils.js";
import { pctColor, PrintTable, DefaultStyle, ColorPrint } from 'tables.js'
import { GetSitRep } from 'sitrep.js'
let MAX_SERVERS = 25;
// *************************************************************************
// **** IMPORTANT NOTES ON THIS SCRIPT'S RAM USAGE AND POSSIBLE CRASH ******
// *************************************************************************
//
// This script uses Singularity, which is a subset of the API you only get
// access to in endgame (after finishing the game for the first time).
// Before getting acces to this, the RAM cost of those functions is severely
// inflated and will make running this script as-is almost impossible for
// most users unless they have some significant home ram upgrades, AND will
// also crash even if they manage to do so.
// Please search for the term "Singulariy" within this script and comment
// out all the identified blocks if you are experiencing a singularity
// related crash, or if the ram usage of this script is over 17.85GB.
//
// *************************************************************************
// *************************************************************************
// *************************************************************************
/*
USAGES:
buyserver (no paramters) : Shows a price list and RAM amount, up to a power of 30 (everything past the dotted line is home upgrade sizes)
buyserver list : Shows a list of all purchased servers
buyserver <name> <power> : Buys a server of the specified name and size (size here is a power of 2, from 1-20), a confirmation will be shown
buyserver * <power> : Buys servers of the specified size until we either hit the MAX_SERVERS limit, or run out of cash. NO CONFIRMATION.
buyserver loop : Will buy and/or upgrade servers in increasing sizes, using the most cost effective path as possible.
buyserver upgrade : Same as loop, but only does one loop. Will spend up to budget and die.
buyserver delete <name> : Delete the specified server, a confirmation will be shown.
*/
/** @param {NS} ns **/
export async function main(ns) {
ns.disableLog('ALL');
MAX_SERVERS = ns.getPurchasedServerLimit();
// No parameter, we list the menu
if (ns.args[0] == null && ns.args[1] == null) {
const data = [];
const columns = [
{ header: ' Power', width: 10 },
{ header: ' RAM', width: 20 },
{ header: ' Price', width: 10 },
{ header: ' $/GB', width: 10 }
];
const softcap = GetSoftcap(ns);
if (softcap != 1) {
data.push([
{ color: '', text: 'N/A'.padStart(9) },
{ color: 'yellow', text: ' Softcap' },
{ color: 'yellow', text: softcap.toString().padStart(9) },
{ color: '', text: 'N/A'.padStart(9) }
]);
data.push(null);
}
for (let i = 1; i <= 20; i++) {
const ram = Math.pow(2, i);
const cost = ns.getPurchasedServerCost(ram);
if (cost == Infinity) continue;
const color = softcap > 1 && i > 6 ? 'yellow' : 'white'
data.push([
{ color: color, text: i.toString().padStart(9) },
{ color: color, text: FormatRam(ns, ram, 0).padStart(19) },
{ color: color, text: FormatMoney(ns, cost, 1).padStart(9) },
{ color: color, text: FormatMoney(ns, cost / ram, 0).padStart(9) },
]);
if (i == 6 && softcap > 1)
data.push(null);
}
// *************************************************************************
// ****** COMMENT THE FOLLOWING BLOCK IF YOU DO NOT HAVE SINGULARITY *******
// *************************************************************************
if (ns.getServerMaxRam('home') < Math.pow(2, 30)) {
const ram = ns.getServerMaxRam('home');
const cost = ns.singularity.getUpgradeHomeRamCost();
data.push(null);
data.push([
{ color: 'white', text: 'Home'.padStart(9) },
{ color: 'white', text: `${FormatRam(ns, ram, 0)} => ${FormatRam(ns, ram * 2, 0)}`.padStart(19) },
{ color: 'white', text: FormatMoney(ns, cost, 1).padStart(9) },
{ color: 'white', text: FormatMoney(ns, cost / ram, 0).padStart(9) },
]);
}
// *************************************************************************
// *************************************************************************
// *************************************************************************
PrintTable(ns, data, columns, DefaultStyle(), ColorPrint);
return;
}
if (ns.args[0] == 'upgrade' || ns.args[0] == 'loop') {
while (true) {
let budget = GetBudget(ns);
//ns.tprint('budget: ' + FormatMoney(ns, budget));
if (budget > 0) {
await SpendBudgetOnServers(ns, budget);
}
if (ns.args[0] != 'loop') return;
await ns.sleep(10000);
}
}
if (ns.args[0] == 'delete' && ns.args[1] == 'all') {
var resp = await ns.prompt('?! Confirm DELETE of ALL servers ?!');
if (resp == false) {
ns.tprint("Transaction aborted.");
ns.exit();
}
ns.getPurchasedServers().forEach((s) => {
ns.killall(s);
ns.deleteServer(s);
ns.tprint("Server deleted.");
})
return;
}
// User wants to delete a server
if (ns.args[0] == 'delete') {
var resp = await ns.prompt('Confirm DELETE of server named ' + ns.args[1]);
if (resp == false) {
ns.tprint("Transaction aborted.");
ns.exit();
}
ns.killall(ns.args[1]);
ns.deleteServer(ns.args[1]);
ns.tprint("Server deleted.");
return;
}
// User wants the list of owned servers
if (ns.args[0] == 'list') {
var servers = ns.getPurchasedServers();
const data = [];
const columns = [
{ header: ' Name', width: 25 },
{ header: ' RAM', width: 10 }
];
for (var server of servers) {
data.push([
{ color: 'white', text: ' ' + server },
{ color: 'white', text: FormatRam(ns, ns.getServerMaxRam(server)).padStart(9) }
]);
}
PrintTable(ns, data, columns, DefaultStyle(), ColorPrint);
return;
}
// User wants to buy a server
var pow = ns.args[1];
var gb = Math.pow(2, pow);
var existing = ns.scan().filter(s => s.startsWith('crusher'));
if (ns.args[0] == '*') {
while (true) {
ns.tprint('Buying multiple servers (player money= ' + ns.nFormat(ns.getPlayer().money, '0.00a') + ' server cost= ' + ns.nFormat(ns.getPurchasedServerCost(gb), '0.00a') + ')');
let nbServers= ns.getPurchasedServers().length;
while (ns.getPurchasedServerCost(gb) < ns.getPlayer().money && nbServers < 25) {
var found = false;
var serverName = undefined;
for (var i = 1; i <= 25; i++) {
if (!existing.find(p => p == 'crusher-' + i)) {
serverName = 'crusher-' + i;
found = true;
break;
}
}
if (!found) {
ns.tprint('Could not find suitable name, aborting.');
ns.exit();
}
ns.tprint('Buying server ' + serverName);
ns.purchaseServer(serverName, gb);
nbServers++;
existing.push(serverName);
}
await ns.sleep(1000);
}
}
else {
var resp = await ns.prompt('Confirm purchase of server named ' + ns.args[0] + ' with ' + FormatRam(ns, gb) + ' RAM for ' + FormatMoney(ns, ns.getPurchasedServerCost(2 ** pow)));
if (resp == false) {
ns.tprint("Transaction aborted.");
return;
}
ns.tprint('Confirming transaction');
ns.purchaseServer(ns.args[0], gb);
}
}
function GetBudget(ns) {
let sitrep = GetSitRep(ns);
return sitrep.ramBudget ?? ns.getPlayer().money;
}
function GetSoftcap(ns) {
const price6 = ns.getPurchasedServerCost(2 ** 6);
const price7 = ns.getPurchasedServerCost(2 ** 7);
const ratio = price7 / price6 / 2;
return ratio;
}
const PowerFromRam = (ram) => Math.log2(ram);
async function SpendBudgetOnServers(ns, budget = ns.getPlayer().money) {
let beforeRam = ns.getPurchasedServers().reduce((sum, s) => sum + ns.getServerMaxRam(s), 0);
let totalUpgradeCost = 0;
while (true) {
const options = GetAvailableOptions(ns, budget);
if (options.length == 0) {
//ns.tprint('INFO: No valid buy/upgrade options currently available!');
break;
}
//ns.tprint(JSON.stringify(options, null, 2));
const option = options.pop();
switch (option.action) {
case 'buy': {
const serverName = GetNewServerName(ns);
if (serverName == undefined) continue;
ns.purchaseServer(serverName, option.size);
ns.tprint('Buying server ' + serverName + ' (' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
ns.print('Buying server ' + serverName + ' (' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
LogMessage(ns, 'Buying server ' + serverName + ' (' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
break;
}
case 'upgrade': {
const serverName = option.server;
ns.upgradePurchasedServer(serverName, option.size);
// ns.tprint('Upgrading server ' + serverName + ' (from ' + FormatRam(ns, option.oldSize) + ' to ' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
// ns.print('Upgrading server ' + serverName + ' (from ' + FormatRam(ns, option.oldSize) + ' to ' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
LogMessage(ns, 'Upgrading server ' + serverName + ' (from ' + FormatRam(ns, option.oldSize) + ' to ' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
totalUpgradeCost += option.cost;
break;
}
// *************************************************************************
// ****** COMMENT THE FOLLOWING BLOCK IF YOU DO NOT HAVE SINGULARITY *******
// *************************************************************************
case 'home': {
if (ns.singularity.upgradeHomeRam()) {
ns.tprint('Upgrading home RAM (from ' + FormatRam(ns, option.oldSize) + ' to ' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
ns.print('Upgrading home RAM (from ' + FormatRam(ns, option.oldSize) + ' to ' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
LogMessage(ns, 'Upgrading home RAM (from ' + FormatRam(ns, option.oldSize) + ' to ' + FormatRam(ns, option.size) + ' for ' + FormatMoney(ns, option.cost) + ')');
}
else {
ns.tprint('FAIL: ?! Could not upgrade home ram ?!');
}
break;
}
// *************************************************************************
// *************************************************************************
// *************************************************************************
default: {
ns.tprint('FAIL: ?! Invalid buyserver option ?!');
break;
}
}
budget -= option.cost;
//ns.tprint('WARN: Waiting 1s');
//await ns.sleep(0);
}
let afterRam = ns.getPurchasedServers().reduce((sum, s) => sum + ns.getServerMaxRam(s), 0);
if (afterRam > beforeRam) {
ns.tprint('Upgraded server(s) (from ' + FormatRam(ns, beforeRam) + ' to ' + FormatRam(ns, afterRam) + ' for ' + FormatMoney(ns, totalUpgradeCost) + ')');
ns.print('Upgraded server(s) (from ' + FormatRam(ns, beforeRam) + ' to ' + FormatRam(ns, afterRam) + ' for ' + FormatMoney(ns, totalUpgradeCost) + ')');
}
}
// Priotities: Upgrade to softcap, buy to softcap, upgrade lowest
function GetAvailableOptions(ns, budget = ns.getPlayer().money) {
let networkRam = GetAllServers(ns).filter(s => ns.hasRootAccess(s) && ns.getServerMaxRam(s) > 0).reduce((sum, s) => sum + ns.getServerMaxRam(s), 0);
const servers = ns.getPurchasedServers();
const softcap = GetSoftcap(ns);
const options = [];
for (let i = 0; i < ns.getPurchasedServerLimit(); i++) {
const server = servers[i];
const currentSize = !server ? 0 : ns.getServerMaxRam(servers[i]);
if (currentSize == ns.getPurchasedServerMaxRam()) continue;
const softcapSize = softcap > 1 ? 6 : PowerFromRam(ns.getPurchasedServerMaxRam());
if (currentSize == 0) {
//ns.tprint('WARN: New server? softcapSize='+softcapSize);
const affordable = _.range(softcapSize, 1, -1).find(s => ns.getPurchasedServerCost(2 ** s) <= budget);
if (affordable > 0) {
options.push({
index: i,
server: 'N/A',
action: 'buy',
size: 2 ** affordable,
oldSize: 0,
cost: ns.getPurchasedServerCost(2 ** affordable),
costPerGb: ns.getPurchasedServerCost(2 ** affordable) / (2 ** affordable)
});
}
}
else if (currentSize < 2 ** softcapSize) {
//ns.tprint('WARN: Upgrade small one?')
// favor upgrade or buy to softcap
const affordable = _.range(softcapSize, 1, -1).find(s => ns.getPurchasedServerUpgradeCost(server, 2 ** s) <= budget);
if (affordable > PowerFromRam(currentSize)) {
options.push({
index: i,
server: server,
action: 'upgrade',
size: 2 ** affordable,
oldSize: currentSize,
cost: ns.getPurchasedServerUpgradeCost(server, 2 ** affordable),
costPerGb: ns.getPurchasedServerUpgradeCost(server, 2 ** affordable) / (2 ** affordable - currentSize)
});
}
}
else {
//ns.tprint('WARN: Upgrade big one?')
if (ns.getPurchasedServerUpgradeCost(server, currentSize * 2) <= budget) {
options.push({
index: i,
server: server,
action: 'upgrade',
size: currentSize * 2,
oldSize: currentSize,
cost: ns.getPurchasedServerUpgradeCost(server, currentSize * 2),
costPerGb: ns.getPurchasedServerUpgradeCost(server, currentSize * 2) / currentSize
});
}
}
}
// *************************************************************************
// ****** COMMENT THE FOLLOWING BLOCK IF YOU DO NOT HAVE SINGULARITY *******
// *************************************************************************
if (ns.singularity.getUpgradeHomeRamCost() <= budget && ns.getServerMaxRam('home') < Math.pow(2, 30)) {
//ns.tprint('home upg cost is ' + FormatMoney(ns, ns.singularity.getUpgradeHomeRamCost()))
options.push({
index: -1,
action: 'home',
size: ns.getServerMaxRam('home') * 2,
oldSize: ns.getServerMaxRam('home'),
cost: ns.singularity.getUpgradeHomeRamCost(),
costPerGb: ns.singularity.getUpgradeHomeRamCost() / ns.getServerMaxRam('home')
});
}
// *************************************************************************
// *************************************************************************
// *************************************************************************
options.sort(function (a, b) {
if (a.costPerGb != b.costPerGb) return b.costPerGb - a.costPerGb;
if (a.action == 'home') return 1;
if (b.action == 'home') return -1;
return b.index - a.index;
});
//ns.tprint(JSON.stringify(options, null, 2));
return options;
}
function GetNewServerName(ns) {
// Find a name
for (var i = 1; i <= MAX_SERVERS; i++) {
if (!ns.getPurchasedServers().find(p => p == 'crusher-' + i))
return 'crusher-' + i;
}
ns.tprint('ERROR: Could not find suitable name, aborting.');
ns.print('ERROR: Could not find suitable name, aborting.');
return undefined;
}