forked from faangbait/phoenix-os-bitburner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.serverextras.so.js
45 lines (39 loc) · 1.15 KB
/
lib.serverextras.so.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
/**
* If a server is provided, returns a route to that server.
* If no server is provided, returns a list of servers.
*
* @param {import(".").NS} ns
* @param {string} [server=undefined]
* @return {string[]}
*/
export function getAllServers(ns, server=undefined) {
const server_list = new Set();
let route = [];
const scanNode = (parent, server, target, route) => {
const children = ns.scan(server);
for (let child of children) {
if (!server_list.has(child)) {
server_list.add(child);
}
if (parent === child) {
continue;
}
if (child === target) {
route.unshift(child);
route.unshift(server);
return true;
}
if (scanNode(server, child, target, route)) {
route.unshift(server);
return true;
}
}
return false;
};
scanNode('', 'home', server, route);
if (server) {
return "connect " + route.join("; connect ") + "; backdoor";
} else {
return Array.from(server_list.keys());
}
}