91 lines
2.0 KiB
JavaScript
91 lines
2.0 KiB
JavaScript
/** @param {NS} ns */
|
|
export const IMPORTANT_HOST = [
|
|
// services
|
|
"iron-gym",
|
|
"crush-fitness",
|
|
"powerhouse-fitness",
|
|
"millenium-fitness",
|
|
"snap-fitness",
|
|
"rothman-uni",
|
|
"summit-uni",
|
|
|
|
// factions
|
|
"CSEC",
|
|
"avmnite-02h",
|
|
"I.I.I.I",
|
|
"run4theh111z",
|
|
|
|
// end bitnode
|
|
"w0r1d_d43m0n"
|
|
]
|
|
|
|
class ServerTreeNode {
|
|
constructor(ns, serverName) {
|
|
this.ns = ns
|
|
this.serverName = serverName
|
|
this.children = []
|
|
this.info = ns.getServer(serverName)
|
|
}
|
|
|
|
toObject() {
|
|
return {
|
|
[this.serverName]: {
|
|
"info": this.info,
|
|
"children": this.children.map(x => x.toObject()),
|
|
},
|
|
}
|
|
}
|
|
|
|
toJson() {
|
|
return JSON.stringify(this.toObject())
|
|
}
|
|
|
|
printTable(padding = "", pointer = "", lastNodeOfParent = true) {
|
|
if (this.serverName == "home") {
|
|
this.ns.tprintf("--R-P--LVL---")
|
|
}
|
|
this.ns.tprintf(
|
|
"%s %s %s %4s %s",
|
|
IMPORTANT_HOST.includes(this.serverName) ? "!" : " ",
|
|
this.info.hasAdminRights ? "Y" : "N",
|
|
this.info.openPortCount,
|
|
this.info.requiredHackingSkill,
|
|
padding + pointer + this.serverName
|
|
)
|
|
|
|
// prepare padding for children
|
|
let newPadding = padding
|
|
if (this.serverName != "home") {
|
|
if (lastNodeOfParent) {
|
|
newPadding += " "
|
|
}
|
|
else {
|
|
newPadding += "| "
|
|
}
|
|
}
|
|
|
|
// print children
|
|
for (let i = 0; i < this.children.length; i++) {
|
|
if (i == this.children.length - 1) {
|
|
this.children[i].printTable(newPadding, "└─", true)
|
|
}
|
|
else {
|
|
this.children[i].printTable(newPadding, "├─", false)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
export function getServerTree(ns, serverNameToScan = "home", scannedServerList = ["home"]) {
|
|
const currentNode = new ServerTreeNode(ns, serverNameToScan)
|
|
for (const serverName of ns.scan(serverNameToScan)) {
|
|
// check if we have already scanned this server
|
|
if (!scannedServerList.includes(serverName)) {
|
|
// add to the list to avoid duplicates
|
|
scannedServerList.push(serverName)
|
|
// scan the node and add as a child to the current node
|
|
currentNode.children.push(getServerTree(ns, serverName, scannedServerList))
|
|
}
|
|
}
|
|
return currentNode
|
|
} |