Library - ScanUtils // Infinite-depth scan, infinite-depth connect.

ScanUtils:

The main library itself! As a library, it is not designed to be run on its' own, but imported into other scripts to use its' functions for full effect. In order to use this library, you MUST run buildNetwork(ns) to retrieve a network tree var which you can supply to its' other functions. I probably could have and should have made these scopeless vars, but sue me.

I recommend saving this to /lib/scanUtils.js for the sake of consistency, and because the following two use cases implement it that way.

export async function main(ns) { ns.tprint("scanUtils.js is a library, and has no functionality."); } /** Builds the list of all Servers on a network. * @param {NS} ns * @return {array} multi-dimensional array of the network, with only first-layer element being home. */ export async function buildNetwork(ns) { let home = new Server("home", [], []); ns.print("Home name: " + home.name); await recurseNetwork(ns, ["home"], home); return home; } /** * @param {NS} ns */ async function recurseNetwork(ns, coveredGround, host){ let listHosts = ns.scan(host.name); for(let i = 0; i < listHosts.length; i++){ if(coveredGround.includes(listHosts)){

continue;

}

coveredGround.push(listHosts);

let foundServer = new Server(listHosts, [], host.path.slice())

host.addServer(foundServer);

foundServer.addPath(host.name);

recurseNetwork(ns, coveredGround, foundServer);

}

}

/**

* Transforms the Server it receives into a one-dimensional list of all of its' networked Servers as SlimServers.

*/

export function listNetwork(server){

return recurseList(server, []);

}

function recurseList(server, list){

list.push(server.slimify());

for(let i = 0; i < server.network.length; i++){

recurseList(server.network, list)

}

return list

}

/**

* A Server, which has a name (hostname), a network (connections), and a path (chain of connections to get there).

*/

function Server(name, network, path){

this.name = name;

this.network = network;

this.path = path;

this.addServer = function (server) {

network.push(server);

}

this.addPath = function(server) {

path.push(server);

}

this.slimify = function() {

return new SlimServer(this.name, this.path);

}

}

/**

* A Server without a contained network for clean viewing.

*/

function SlimServer(name, path){

this.name = name;

this.path = path;

}

HyperScan:

The first thing I wrote when I made the library! Because who needs upgrades to scan-analyze when you have a trans girl with too much time on her hands and a willingness to stay up until 4 AM on the night before a big crunch at work?

This works pretty much exactly like scan-analyze, but has infinite depth. That means that you'll see every single server on the network, along with the associated data! It even gives it a nice little spacing effect to show which servers are accessed by chaining connections.

While before I recommended the /lib/scanUtils.js path for consistency, here I recommend that path because otherwise this won't work-as-written! Still, it's an easy fix, just change the import. I also recommend aliasing this (perhaps as "hyperscan"?) for ease of use and to fit in a bit better with other terminal commands.

import * as scanUtils from "/lib/scanUtils.js" /** @param {NS} ns */ export async function main(ns) { let home = await scanUtils.buildNetwork(ns); let network = scanUtils.listNetwork(home); let output = "\n HYPERSCAN\n=============\n"; for(let i = 0; i < network.length; i++){ output += "\n"; let spacing = "" for(let x = 0; x < network.path.length; x++){

spacing += " ";

}

output += spacing + "- " + network.name + ":\n";

//output += spacing + " Full path: "; //Commented due to hyperconnect handling this for us!

for(let x = 0; x < network.path.length; x++){

output += network.path[x] + " > ";

}

output += network.name + "\n";

output += spacing + " -- Root Access: " + (ns.hasRootAccess(network.name) ? "YES" : "NO")

+ ", Required hacking skill: " + (ns.getServerRequiredHackingLevel(network.name)) + "\n";

output += spacing + " -- Number of open ports required to NUKE: "

+ (ns.getServerNumPortsRequired(network.name)) + "\n";

output += spacing + " -- RAM: " + ns.formatRam(ns.getServerMaxRam(network.name)) + "\n";

output += "\n";

}

ns.tprint(output);

}

HyperConnect:

My first (and hopefully last -- I hate the DOM with a passion only someone who has worked in JQuery in the past feels) foray into meta-manipulation of the terminal. As a result, this script is also a little more fragile than it otherwise would be! Definitely make sure you're running it from the terminal.

This script takes a server name as a parameter and automatically constructs a chain of connect commands to get you to that server, which is then automatically executed! For similar reasons to HyperScan, I recommend aliasing it.

import * as scanUtils from "/lib/scanUtils.js"; /** @param {NS} ns */ export async function main(ns) { let home = await scanUtils.buildNetwork(ns); let net = scanUtils.listNetwork(home); let path = "ERR"; for(let i = 0; i < net.length; i++){ if(net["name"] == ns.args[0]){

path = net["path"];

break;

}

}

if(path == "ERR"){

ns.tprint(ns.args[0] + " was not found on the network.");

ns.exit()

}

let command = "";

for(let i = 0; i < path.length; i++){

command += "connect " + path + "; ";

}

command += "connect " + ns.args[0];

const terminalInput = document.getElementById("terminal-input");

terminalInput.value = command;

const handler = Object.keys(terminalInput)[1];

terminalInput[handler].onChange({target:terminalInput});

terminalInput[handler].onKeyDown({key:'Enter',preventDefault:()=>null});

}

More...?

Watch this space! I still have ideas for what I can use the ScanUtils library for, and if I get around to actually making them I'll post them! Next on the chopping block is of course an automatic coding contract finder. Perhaps even solver!

If you have any questions, comments, concerns, or recommendations, please let me know!

Source: https://steamcommunity.com/sharedfiles/filedetails/?id=2998305291					

More Bitburner guilds