bitburner/scripts/connect.js

39 lines
1.1 KiB
JavaScript
Raw Normal View History

2022-03-15 20:41:23 -04:00
import {ArgError, ArgParser} from '/scripts/lib/arg-parser';
import {pruneTree, scanNetwork, terminal} from '/scripts/lib/utils';
2022-03-11 07:05:34 -05:00
/**
2022-03-15 20:41:23 -04:00
* BitBurner autocomplete
* @param data {server: string[], txts: string[], scripts: string[], flags: string[]} - Contextual information
* @returns {string[]} - Pool of autocomplete options
*/
export function autocomplete(data) {
return [...data.servers];
}
/**
* Search the network for a device and connect to it.
2022-03-11 07:05:34 -05:00
* @param ns {NS} - BitBurner API
*/
export function main(ns) {
// Setup
ns.disableLog('ALL');
2022-03-15 20:41:23 -04:00
const argParser = new ArgParser('connect.js', 'Search the network for a device and connect to it.', null, [
2022-03-11 07:05:34 -05:00
{name: 'device', desc: 'Device to connect to', default: ns.getHostname(), type: 'string'}
]);
2022-03-15 20:41:23 -04:00
2022-03-11 07:05:34 -05:00
try {
2022-03-15 20:41:23 -04:00
// Run
const args = argParser.parse(ns.args);
const [devices, network] = scanNetwork(ns);
pruneTree(network, d => d == args['device']);
let current = network, name;
while(name = Object.keys(current)[0]) {
terminal(`connect ${name}`);
current = current[name];
}
2022-03-11 07:05:34 -05:00
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
}