forked from alainbryden/bitburner-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remove-worst-server.js
31 lines (30 loc) · 1.55 KB
/
remove-worst-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
import { getNsDataThroughFile, runCommand } from './helpers.js'
/** @param {NS} ns
* Remove the worst server we own (RAM) **/
export async function main(ns) {
let worstServerName = null;
let worstServerRam = Math.pow(2, 20);
let purchasedServers = await getNsDataThroughFile(ns, 'ns.getPurchasedServers()', '/Temp/purchased-servers.txt');
if (purchasedServers.length == 0) {
ns.tprint("Nothing to delete - you have purchased no servers.");
return;
}
purchasedServers.forEach(serverName => {
let ram = ns.getServerMaxRam(serverName);
if (ram < worstServerRam) {
worstServerName = serverName;
worstServerRam = ram;
}
});
if (worstServerName == null) {
ns.tprint("Nothing to delete - all " + purchasedServers.length + " servers have the maximum " + worstServerRam + " GB of RAM");
return;
}
// Flag the server for deletion with a file - daemon should check for this and stop scheduling against it.
await runCommand(ns, `await ns.scp("/Flags/deleting.txt", "${worstServerName}")`, '/Temp/flag-server-for-deletion.js');
var success = await getNsDataThroughFile(ns, `ns.deleteServer("${worstServerName}")`, '/Temp/try-delete-server-result.txt');
if (success)
ns.tprint("Deleted " + worstServerName + " which had only " + worstServerRam + " GB of RAM. " + (purchasedServers.length - 1) + " servers remain.");
else
ns.tprint("Tried to delete " + worstServerName + " with " + worstServerRam + " GB RAM, but it failed (scripts still running)");
}