More cleanup

This commit is contained in:
Zakary Timson 2022-03-15 22:05:34 -04:00
parent d699112d38
commit 3753ae02db
4 changed files with 86 additions and 97 deletions

View File

@ -1,4 +1,5 @@
import {ArgError, ArgParser} from './scripts/lib/arg-parser'; import {ArgError, ArgParser} from '/scripts/lib/arg-parser';
import {Logger} from '/scripts/lib/logger';
/** /**
* Buy, upgrade & manage Hacknet nodes automatically. * Buy, upgrade & manage Hacknet nodes automatically.
@ -17,7 +18,7 @@ export async function main(ns) {
// Run // Run
const args = argParser.parse(ns.args); const args = argParser.parse(ns.args);
let nodeCount = ns.hacknet.numNodes(); let nodeCount = ns.hacknet.numNodes();
const logger = new Logger(ns, () => [() => `Hacknet Manager: ${nodeCount}/${args['limit']}`]); const logger = new Logger(ns, [() => `Hacknet Manager: ${nodeCount}/${args['limit']}`]);
while(true) { while(true) {
const balance = ns.getServerMoneyAvailable('home'); const balance = ns.getServerMoneyAvailable('home');

View File

@ -10,9 +10,8 @@ export class Logger {
constructor(ns, lineFns = []) { constructor(ns, lineFns = []) {
this.ns = ns; this.ns = ns;
this.fns = lineFns; this.fns = lineFns;
this.historyLen -= fns.length * 2; this.historyLen -= lineFns.length * 2;
this.history = Array(this.historyLen).fill(''); this.history = Array(this.historyLen).fill('');
this.log();
} }
/** /**

View File

@ -1,4 +1,14 @@
import {ArgError, ArgParser} from './scripts/lib/arg-parser'; import {ArgError, ArgParser} from '/scripts/lib/arg-parser';
import {Logger} from "/scripts/lib/logger";
/**
* 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];
}
/** /**
* Weaken, Grow, Hack loop to "mine" target machine for money. * Weaken, Grow, Hack loop to "mine" target machine for money.
@ -7,64 +17,44 @@ import {ArgError, ArgParser} from './scripts/lib/arg-parser';
export async function main(ns) { export async function main(ns) {
// Setup // Setup
ns.disableLog('ALL'); ns.disableLog('ALL');
const historyLength = 15;
const messageHistory = Array(historyLength).fill('');
let maxBalance, balance, minSecurity, security;
const argParser = new ArgParser('miner.js', 'Weaken, Grow, Hack loop to "mine" machine for money. Tail for live updates', null, [ const argParser = new ArgParser('miner.js', 'Weaken, Grow, Hack loop to "mine" machine for money. Tail for live updates', null, [
{name: 'device', desc: 'Device to mine, defaults to current machine', optional: true, default: ns.getHostname(), type: 'string'} {name: 'device', desc: 'Device to mine, defaults to current machine', optional: true, default: ns.getHostname(), type: 'string'}
]); ]);
let args;
try { try {
args = argParser.parse(ns.args); // Run
const args = argParser.parse(ns.args);
let maxBalance, balance, minSecurity, security;
maxBalance = await ns.getServerMaxMoney(args['device']); maxBalance = await ns.getServerMaxMoney(args['device']);
balance = await ns.getServerMoneyAvailable(args['device']); balance = await ns.getServerMoneyAvailable(args['device']);
minSecurity = await ns.getServerMinSecurityLevel(args['device']) + 2; minSecurity = await ns.getServerMinSecurityLevel(args['device']) + 2;
security = await ns.getServerSecurityLevel(args['device']); security = await ns.getServerSecurityLevel(args['device']);
} catch(err) { const logger = new Logger(ns, [
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message)); () => `Mining: ${args['device']}`,
throw err; () => `Security: ${Math.round(security)}/${minSecurity}\tBalance: \$${Math.round(balance * 100) / 100}`
} ]);
while(true) {
/**
* Print header with logs
* @param message - message to append to logs
*/
function log(message) {
const sec = `${Math.round(security)}/${minSecurity}`;
ns.clearLog();
ns.print('===================================================');
ns.print(`Mining: ${args['device']}`);
ns.print('===================================================');
ns.print(`Security: ${sec}${sec.length < 6 ? '\t' : ''}\tBalance: $${Math.round(balance * 100) / 100}`);
ns.print('===================================================');
if(message != null) messageHistory.push(message);
messageHistory.splice(0, messageHistory.length - historyLength);
messageHistory.forEach(m => ns.print(m));
}
log();
do {
// Update information // Update information
security = await ns.getServerSecurityLevel(args['device']); security = await ns.getServerSecurityLevel(args['device']);
balance = await ns.getServerMoneyAvailable(args['device']); balance = await ns.getServerMoneyAvailable(args['device']);
// Pick step // Pick step
if(security > minSecurity) { // Weaken if(security > minSecurity) { // Weaken
log('Attacking Security...'); logger.log('Attacking Security...');
const w = await ns.weaken(args['device']); const w = await ns.weaken(args['device']);
log(`Security: -${w}`); logger.log(`Security: -${w}`);
} else if(balance < maxBalance) { // Grow } else if(balance < maxBalance) { // Grow
log('Spoofing Balance...'); logger.log('Spoofing Balance...');
const g = await ns.grow(args['device']); const g = await ns.grow(args['device']);
log(`Balance: +$${Math.round((g * balance - balance) * 100) / 100}`); logger.log(`Balance: +$${Math.round((g * balance - balance) * 100) / 100}`);
} else { // Hack } else { // Hack
log('Hacking Account...'); logger.log('Hacking Account...');
const h = await ns.hack(args['device']); const h = await ns.hack(args['device']);
log(`Balance: -$${h}`); logger.log(`Balance: -$${h}`);
}
}
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
} }
} while(true);
}
export function autocomplete(data) {
return [...data.servers];
} }

View File

@ -15,25 +15,6 @@ export function autocomplete(data) {
* @param ns {NS} - BitBurner API * @param ns {NS} - BitBurner API
*/ */
export async function main(ns) { export async function main(ns) {
// Setup
ns.disableLog('ALL');
const argParser = new ArgParser('network-graph.js', 'Scan the network for devices and display as an ASCII tree:\n home\n ├─ n00dles (ROOTED)\n | └─ max-hardware (80|1)\n | └─ neo-net (50|1)\n ├─ foodnstuff (ROOTED)\n └─ sigma-cosmetics (ROOTED)', null, [
{name: 'device', desc: 'Point to start scan from, defaults to current machine', optional: true, default: ns.getHostname(), type: 'string'},
{name: 'depth', desc: 'Depth to scan to, defaults is 3', flags: ['-d', '--depth'], default: Infinity, type: 'num'},
{name: 'filter', desc: 'Filter to device matching name', flags: ['-f', '--filter'], type: 'string'},
{name: 'regex', desc: 'Filter to devices matching pattern', flags: ['-e', '--regex'], type: 'string'},
{name: 'rooted', desc: 'Filter to devices that have been rooted', flags: ['-r', '--rooted'], type: 'bool'},
{name: 'notRooted', desc: 'Filter to devices that have not been rooted', flags: ['-n', '--not-rooted'], type: 'bool'},
{name: 'verbose', desc: 'Display the required hack level & number of ports to root: (level|port)', flags: ['-v', '--verbose'], type: 'bool'},
]);
let args;
try {
args = argParser.parse(ns.args);
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
/** /**
* Iterate tree & print to screen * Iterate tree & print to screen
* @param tree {Object} - Tree to parse * @param tree {Object} - Tree to parse
@ -51,7 +32,21 @@ export async function main(ns) {
}); });
} }
// Setup
ns.disableLog('ALL');
const argParser = new ArgParser('network-graph.js', 'Scan the network for devices and display as an ASCII tree:\n home\n ├─ n00dles (ROOTED)\n | └─ max-hardware (80|1)\n | └─ neo-net (50|1)\n ├─ foodnstuff (ROOTED)\n └─ sigma-cosmetics (ROOTED)', null, [
{name: 'device', desc: 'Point to start scan from, defaults to current machine', optional: true, default: ns.getHostname(), type: 'string'},
{name: 'depth', desc: 'Depth to scan to, defaults is 3', flags: ['-d', '--depth'], default: Infinity, type: 'num'},
{name: 'filter', desc: 'Filter to device matching name', flags: ['-f', '--filter'], type: 'string'},
{name: 'regex', desc: 'Filter to devices matching pattern', flags: ['-e', '--regex'], type: 'string'},
{name: 'rooted', desc: 'Filter to devices that have been rooted', flags: ['-r', '--rooted'], type: 'bool'},
{name: 'notRooted', desc: 'Filter to devices that have not been rooted', flags: ['-n', '--not-rooted'], type: 'bool'},
{name: 'verbose', desc: 'Display the required hack level & number of ports to root: (level|port)', flags: ['-v', '--verbose'], type: 'bool'},
]);
try {
// Run // Run
const args = argParser.parse(ns.args);
const [devices, network] = scanNetwork(ns, args['device'], args['depth']); const [devices, network] = scanNetwork(ns, args['device'], args['depth']);
const stats = devices.reduce((acc, d) => ({...acc, [d]: ns.getServer(d)}), {}); const stats = devices.reduce((acc, d) => ({...acc, [d]: ns.getServer(d)}), {});
if(args['regex']) pruneTree(network, d => RegExp(args['regex']).test(d)); // Regex flag if(args['regex']) pruneTree(network, d => RegExp(args['regex']).test(d)); // Regex flag
@ -61,4 +56,8 @@ export async function main(ns) {
ns.tprint(args['device']); ns.tprint(args['device']);
render(network, args['verbose'] ? stats : null); render(network, args['verbose'] ? stats : null);
ns.tprint(''); ns.tprint('');
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
} }