bitburner/scripts/miner.js

71 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-03-09 14:06:14 -05:00
import {ArgError, ArgParser} from './scripts/lib/arg-parser';
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-09 14:06:14 -05:00
const historyLength = 15;
const messageHistory = Array(historyLength).fill('');
let maxBalance, balance, minSecurity, security;
2022-03-09 15:12:37 -05:00
const argParser = new ArgParser('miner.js', 'Weaken, Grow, Hack loop to "mine" machine for money.', null, [
{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
]);
let args;
try {
args = argParser.parse(ns.args);
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-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
2022-02-10 17:30:11 -05:00
/**
* Print header with logs
* @param message - message to append to logs
*/
function log(message) {
const sec = `${Math.round(security)}/${minSecurity}`;
ns.clearLog();
ns.print('===================================================');
2022-03-09 15:12:37 -05:00
ns.print(`Mining: ${args['device']}`);
2022-02-10 17:30:11 -05:00
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));
}
2022-02-04 18:56:22 -05:00
2022-02-10 17:30:11 -05:00
log();
2022-03-09 14:06:14 -05:00
do {
2022-02-10 17:30:11 -05:00
// Update information
2022-03-09 15:12:37 -05:00
security = await ns.getServerSecurityLevel(args['device']);
balance = await ns.getServerMoneyAvailable(args['device']);
2022-02-04 18:56:22 -05:00
2022-02-10 17:30:11 -05:00
// Pick step
2022-02-11 01:08:32 -05:00
if(security > minSecurity) { // Weaken
2022-02-10 17:30:11 -05:00
log('Attacking Security...');
2022-03-09 15:12:37 -05:00
const w = await ns.weaken(args['device']);
2022-02-10 17:30:11 -05:00
log(`Security: -${w}`);
2022-03-09 14:06:14 -05:00
} else if(balance < maxBalance) { // Grow
2022-02-10 17:30:11 -05:00
log('Spoofing Balance...');
2022-03-09 15:12:37 -05:00
const g = await ns.grow(args['device']);
2022-02-10 17:30:11 -05:00
log(`Balance: +$${Math.round((g * balance - balance) * 100) / 100}`);
2022-02-11 01:08:32 -05:00
} else { // Hack
2022-02-10 17:30:11 -05:00
log('Hacking Account...');
2022-03-09 15:12:37 -05:00
const h = await ns.hack(args['device']);
2022-02-10 17:30:11 -05:00
log(`Balance: -$${h}`);
2022-02-04 18:56:22 -05:00
}
2022-03-09 14:06:14 -05:00
} while(true);
2022-02-04 18:56:22 -05:00
}
export function autocomplete(data) {
return [...data.servers];
}