Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** @param {NS} ns */
- export async function main(ns) {
- // Store padding as a variable so we can keep track of the longest server name encountered.
- let padding = 0;
- /** This function constructs the tree */
- function getTreeFromHostname(parent = "", hostname = "home") {
- // get all children of the host
- let children = ns.scan(hostname).filter(child => child !== parent);
- // set padding if this is the longest server name observed
- padding = Math.max(padding, hostname.length + 1);
- // Return hostname, plus recursive trees for all children
- return [hostname, ...children.map(child => getTreeFromHostname(hostname, child))];
- }
- /** This function returns a string representation of a tree. If no tree provided, it will make a new one using getTreeFromHostname. */
- function printWithChildren(tree = getTreeFromHostname(), level = 0) {
- //start a new line and add initial padding based on the level
- let output = "\n" + " ".repeat(level * 3);
- //Parse the current level of the tree into a hostname and its child trees
- let [host, ...childTrees] = tree;
- output += `${(host + ":").padEnd(padding, "-")} Rooted: ${ns.hasRootAccess(host) ? "Yes" : "No "} Backdoored: ${ns.getServer(host).backdoorInstalled ? "Yes" : "No "}`;
- if (childTrees) childTrees.forEach(childTree => output += printWithChildren(childTree, level + 1));
- return output;
- }
- // All main needs to actually do is call the print function, which will grab the whole tree on its own due to default parameters.
- ns.tprint(printWithChildren());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement