bitburner/scripts/connect.js

70 lines
2.2 KiB
JavaScript
Raw Normal View History

2022-04-20 11:32:10 -04:00
import {ArgParser} from '/scripts/lib/arg-parser';
import {pruneTree, terminal} from '/scripts/lib/utils';
import {scanNetwork} from '/scripts/crawler';
2022-03-11 07:05:34 -05:00
/**
2022-04-20 11:32:10 -04:00
* Create connection string to get from one server to another.
*
* @param {NS} ns - BitBurner API
* @param {string} end - Server to find path to
* @param {string} start - Starting point, defaults to the local server
* @returns {string} - Connection string: "home;connect n00dles;connect CSEC;"
2022-03-15 20:41:23 -04:00
*/
2022-04-20 11:32:10 -04:00
export function connectionString(ns, end, start = ns.getHostname()) {
let path = pathToServer(ns, end, start);
const homeShortcut = path.indexOf('home');
if(homeShortcut) path = path.slice(homeShortcut);
return path.map((p, i) => p == 'home' && i == 0 ? 'home' : 'connect ' + p).join(';');
2022-03-15 20:41:23 -04:00
}
/**
2022-04-20 11:32:10 -04:00
* Find path from one server to another.
*
* @param {NS} ns - BitBurner API
* @param {string} end - Server to find path to
* @param {string} start - Starting point, defaults to the local server
* @returns {string[]} - Path to get to server
*/
export function pathToServer(ns, end, start = ns.getHostname()) {
if(start == end) return [end];
const [ignore, network] = scanNetwork(ns, start);
pruneTree(network, d => d == end);
let current = network, name, path = [start];
while(name = Object.keys(current)[0]) {
path.push(name);
current = current[name];
}
return path;
}
/**
* Connect to a server anywhere in the network without a backdoor.
*
* @param {NS} ns - BitBurner API
2022-03-11 07:05:34 -05:00
*/
export async function main(ns) {
2022-03-11 07:05:34 -05:00
// Setup
ns.disableLog('ALL');
2022-04-20 11:32:10 -04:00
const argParser = new ArgParser('connect.js', 'Connect to a server anywhere in the network without a backdoor.', [
{name: 'server', desc: 'Server to connect to'}
2022-03-11 07:05:34 -05:00
]);
2022-04-20 11:32:10 -04:00
const args = argParser.parse(ns.args);
2022-03-15 20:41:23 -04:00
2022-04-20 11:32:10 -04:00
// Help
if(args['help'] || args['_error'].length)
return ns.tprint(argParser.help(args['help'] ? null : args['_error'][0], args['_command']));
// Run
await terminal(connectionString(ns, args['server']));
}
/**
* BitBurner autocomplete.
*
* @param {{servers: string[], txts: string[], scripts: string[], flags: string[]}} data - Contextual information
* @returns {string[]} - Pool of autocomplete options
*/
export function autocomplete(data) {
return [...data.servers];
2022-03-11 07:05:34 -05:00
}