forked from faangbait/lets-play-bitburner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.js
71 lines (59 loc) · 2 KB
/
go.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
import HackableBaseServer from "./if.server.hackable"
import BasePlayer from "./if.player";
import { dpList } from "./lib.utils";
function execHackingScript(ns, servers) {
let home = new HackableBaseServer(ns, "home")
let home_pids = home.pids;
let hacking_scripts = home_pids.filter(p => p.filename.startsWith("sbin.hack"))
let hacking_script;
if (hacking_scripts.length > 1) {
throw "Two hacking scripts are running";
} else {
try { hacking_script = hacking_scripts[0]; } catch {}
}
let command = ns.peek(1);
if (command == "NULL PORT DATA") { command = "sbin.hack.roundrobin.js" }
if (hacking_script) {
if (hacking_script.filename !== command) {
ns.kill(hacking_script.pid);
servers.map(s => s.pids).flat().filter(proc => proc.filename.startsWith("bin.")).forEach(proc => ns.kill(proc.pid));
ns.exec(command, "home");
}
} else {
ns.exec(command, "home");
}
}
/** @param {NS} ns **/
export async function main(ns) {
let player = new BasePlayer(ns, "player");
// player.updateCache().catch(console.error);
player.createEventListener("hacking.level").catch(console.error)
player.createEventListener("faction.membership").catch(console.error)
let servers = [];
let slist = dpList(ns);
for (let s of slist) {
servers.push(new HackableBaseServer(ns, s))
}
for (let server of servers) {
// server.updateCache().catch(console.error)
server.createEventListener("admin").catch(console.error)
server.createEventListener("backdoored").catch(console.error)
}
for (let server of servers) {
await ns.scp(["bin.wk.js", "bin.hk.js", "bin.gr.js"], "home", server.id)
}
while (true) {
for (let server of servers) {
// Gain admin on all servers that we can
if (!server.admin && server.ports.required <= player.ports) {
server.sudo();
}
// Upload files to any server that doesn't have them
if (ns.ls(server.id, "bin.").length == 0) {
await ns.scp(["bin.wk.js", "bin.hk.js", "bin.gr.js"], "home", server.id)
}
}
execHackingScript(ns, servers);
await ns.asleep(10);
}
}