-
Notifications
You must be signed in to change notification settings - Fork 0
/
locatePathToServer.js
41 lines (38 loc) · 991 Bytes
/
locatePathToServer.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
function findPath (ns, target, serverName, serverList, ignore, isFound) {
ignore.push(serverName);
let scanResults = ns.scan(serverName);
for (let server of scanResults) {
if (ignore.includes(server)) {
continue;
}
if (server === target) {
serverList.push(server);
return [serverList, true];
}
serverList.push(server);
[serverList, isFound] = findPath(ns, target, server, serverList, ignore, isFound);
if (isFound) {
return [serverList, isFound];
}
serverList.pop();
}
return [serverList, false];
}
/** @param {NS} ns **/
export async function locateServer(ns,target) {
let startServer = "home";
if (target === undefined) {
ns.alert('Please provide target server');
return;
}
let [results, isFound] = findPath(ns, target, startServer, [], [], false);
if (!isFound) {
ns.alert('Server not found!');
} else {
ns.tprint(results.join(' --> '));
}
}
export async function main(ns) {
let target = ns.args[0];
await locateServer(ns,target);
}