bitburner/scripts/connect.js

40 lines
1.2 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 async function main(ns) {
2022-03-11 07:05:34 -05:00
// 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, path = [];
2022-03-15 20:41:23 -04:00
while(name = Object.keys(current)[0]) {
current = current[name];
path.push(name);
2022-03-15 20:41:23 -04:00
}
await terminal('home; ' + path.map(p => `connect ${p}`).join('; '));
2022-03-11 07:05:34 -05:00
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
}