-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathrace.js
105 lines (82 loc) · 3.11 KB
/
race.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
/** @param {NS} ns */
export async function main(ns) {
ns.disableLog('ALL');
let start = performance.now();
let pids = RunScript(ns, 'hack.script', 'foodnstuff', Infinity)
await WaitPids(ns, pids);
pids = RunScript(ns, 'hack.script', 'sigma-cosmetics', Infinity)
await WaitPids(ns, pids);
while (ns.getPlayer().skills.hacking < 200 && ns.getServerSecurityLevel('joesguns') != ns.getServerMinSecurityLevel('joesguns')) {
let pids = RunScript(ns, 'weaken.script', 'joesguns', Infinity)
await WaitPids(ns, pids);
}
while (ns.getPlayer().skills.hacking < 200) {
let pids = RunScript(ns, 'grow.script', 'joesguns', Infinity)
await WaitPids(ns, pids);
}
let elapsed = performance.now() - start;
ns.tprint('WARN: Got to 200 hacking in ' + ns.tFormat(elapsed));
}
function RunScript(ns, scriptName, target, threads) {
// Find all servers
const allServers = GetAllServers(ns);
// Sort by maximum memory
allServers.sort((a, b) => ns.getServerMaxRam(b) - ns.getServerMaxRam(a));
// Find script RAM usage
const ramPerThread = ns.getScriptRam(scriptName);
// Find usable servers
const usableServers = allServers.filter(p => ns.hasRootAccess(p) && ns.getServerMaxRam(p) > 0);
// Fired threads counter
let fired = 0;
const pids = [];
const MIN_HOME_RAM= 120;
for (const server of usableServers) {
// Determin how many threads we can run on target server for the given script
let availableRam = ns.getServerMaxRam(server) - ns.getServerUsedRam(server);
if (server == 'home') {
availableRam -= MIN_HOME_RAM;
if (availableRam < 0) availableRam = 0;
}
let possibleThreads = Math.floor(availableRam / ramPerThread);
// Check if server is already at max capacity
if (possibleThreads <= 0)
continue;
// Lower thread count if we are over target
if (possibleThreads > threads - fired)
possibleThreads = threads - fired;
// Copy script to the server if it's not the current
if (server != ns.getHostname())
ns.scp(scriptName, server);
// Fire the script with as many threads as possible
ns.print('INFO: Starting script ' + scriptName + ' on ' + server + ' with ' + possibleThreads + ' threads');
let pid = ns.exec(scriptName, server, possibleThreads, target);
if (pid == 0)
ns.print('WARN: Could not start script ' + scriptName + ' on ' + server + ' with ' + possibleThreads + ' threads');
else {
fired += possibleThreads;
pids.push(pid);
}
if (fired >= threads) break;
}
if (fired == 0) {
ns.print('FAIL: Not enough memory to launch a single thread of ' + scriptName + ' (out of memory on all servers!)');
}
if (fired != threads) {
ns.print('FAIL: There wasn\'t enough ram to run ' + threads + ' threads of ' + scriptName + ' (fired: ' + fired + ').');
}
return pids;
}
export async function WaitPids(ns, pids) {
if (!Array.isArray(pids)) pids = [pids];
while (pids.some(p => ns.getRunningScript(p) != undefined)) { await ns.sleep(5); }
}
// Iterative network scan
export function GetAllServers(ns) {
let servers = ['home'];
for (const server of servers) {
const found = ns.scan(server);
if (server != 'home') found.splice(0, 1);
servers.push(...found);
}
return servers;
}