bitburner/scripts/miner.js

61 lines
2.1 KiB
JavaScript
Raw Normal View History

2022-03-15 22:05:34 -04:00
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];
}
2022-02-10 17:30:11 -05:00
/**
2022-03-09 14:06:14 -05:00
* Weaken, Grow, Hack loop to "mine" target machine for money.
* @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-03-15 20:41:23 -04:00
const argParser = new ArgParser('miner.js', 'Weaken, Grow, Hack loop to "mine" machine for money. Tail for live updates', null, [
2022-03-09 15:12:37 -05:00
{name: 'device', desc: 'Device to mine, defaults to current machine', optional: true, default: ns.getHostname(), type: 'string'}
2022-03-09 14:06:14 -05:00
]);
2022-03-15 22:05:34 -04:00
2022-03-09 14:06:14 -05:00
try {
2022-03-15 22:05:34 -04:00
// Run
const args = argParser.parse(ns.args);
let maxBalance, balance, minSecurity, security;
2022-03-09 15:12:37 -05:00
maxBalance = await ns.getServerMaxMoney(args['device']);
balance = await ns.getServerMoneyAvailable(args['device']);
minSecurity = await ns.getServerMinSecurityLevel(args['device']) + 2;
security = await ns.getServerSecurityLevel(args['device']);
2022-03-15 22:05:34 -04:00
const logger = new Logger(ns, [
() => `Mining: ${args['device']}`,
() => `Security: ${Math.round(security)}/${minSecurity}\tBalance: \$${Math.round(balance * 100) / 100}`
]);
while(true) {
// Update information
security = await ns.getServerSecurityLevel(args['device']);
balance = await ns.getServerMoneyAvailable(args['device']);
// Pick step
if(security > minSecurity) { // Weaken
logger.log('Attacking Security...');
const w = await ns.weaken(args['device']);
logger.log(`Security: -${w}`);
} else if(balance < maxBalance) { // Grow
logger.log('Spoofing Balance...');
const g = await ns.grow(args['device']);
logger.log(`Balance: +$${Math.round((g * balance - balance) * 100) / 100}`);
} else { // Hack
logger.log('Hacking Account...');
const h = await ns.hack(args['device']);
logger.log(`Balance: -$${h}`);
}
}
2022-03-09 14:06:14 -05:00
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
}
2022-02-04 18:56:22 -05:00
}