bitburner/scripts/miner.js

66 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 {Logger} from '/scripts/lib/logger';
import {toCurrency} from '/scripts/lib/utils';
2022-03-15 22:05:34 -04:00
/**
2022-04-20 11:32:10 -04:00
* Hack, Grow, Weaken loop to "mine" a server for money. Tail for live updates.
*
* @params {NS} ns - BitBurner API
2022-02-10 17:30:11 -05:00
*/
2022-02-04 18:56:22 -05:00
export async function main(ns) {
2022-03-09 14:06:14 -05:00
// Setup
2022-02-04 18:56:22 -05:00
ns.disableLog('ALL');
2022-04-20 11:32:10 -04:00
const argParser = new ArgParser('miner.js', 'Hack, Grow, Weaken loop to "mine" a server for money. Tail for live updates.', [
{name: 'server', desc: 'Server to mine, defaults to the local server', optional: true, default: ns.getHostname()}
2022-03-09 14:06:14 -05:00
]);
2022-04-20 11:32:10 -04:00
const args = argParser.parse(ns.args);
let maxBalance, balance, minSecurity, security;
maxBalance = ns.getServerMaxMoney(args['server']);
balance = ns.getServerMoneyAvailable(args['server']);
minSecurity = ns.getServerMinSecurityLevel(args['server']) + 2;
security = ns.getServerSecurityLevel(args['server']);
2022-03-15 22:05:34 -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']));
// Logger
const logger = new Logger(ns, [
() => `Mining: ${args['server']}`,
2022-04-28 17:24:59 -04:00
() => `Security: ${Math.round(security)}/${minSecurity}\tBalance: ${toCurrency(balance)}`
2022-04-20 11:32:10 -04:00
]);
2022-03-15 22:05:34 -04:00
2022-04-20 11:32:10 -04:00
// Main loop
// noinspection InfiniteLoopJS
while(true) {
// Update information
security = ns.getServerSecurityLevel(args['server']);
balance = ns.getServerMoneyAvailable(args['server']);
// Pick step
if(security > minSecurity) { // Weaken
logger.log('Attacking Security...');
const w = await ns.weaken(args['server']);
logger.log(`Security: -${w}`);
} else if(balance < maxBalance) { // Grow
logger.log('Spoofing Balance...');
const g = await ns.grow(args['server']);
logger.log(`Balance: +${toCurrency(g * balance - balance)}`);
} else { // Hack
logger.log('Hacking Account...');
const h = await ns.hack(args['server']);
logger.log(`Balance: -$${h}`);
2022-03-15 22:05:34 -04:00
}
2022-03-09 14:06:14 -05:00
}
2022-02-04 18:56:22 -05:00
}
2022-04-20 11:32:10 -04:00
/**
* 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];
}