bitburner/scripts/miner.js

77 lines
2.4 KiB
JavaScript
Raw Normal View History

2022-02-10 17:30:11 -05:00
import {ArgParser} from './scripts/lib/arg-parser';
/**
* Hack a server for it's money.
*/
2022-02-04 18:56:22 -05:00
export async function main(ns) {
ns.disableLog('ALL');
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('===================================================');
ns.print(`💎⛏️ Mining: ${target}`);
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
// Initilize script arguments
const argParser = new ArgParser({
desc: 'Weaken, spoof & hack the target in a loop for money.',
examples: [
'run miner.js [OPTIONS] [TARGET]',
'run miner.js --help',
],
args: [
{key: 'TARGET', desc: 'Target to mine. Defaults to localhost'},
{key: 'threads', alias: 't', optional: true, desc: 'Speed up script with more CPU power'},
{key: 'help', alias: 'h', optional: true, desc: 'Display help message'},
]
});
const args = argParser.parse(ns.args);
if(args['help']) return ns.tprint(argParser.help());
// Setup
const historyLength = 15;
const messageHistory = Array(historyLength).fill('');
const threads = args['threads'] || 1;
const target = args['TARGET'] && args['TARGET'] != 'localhost' ? args['TARGET'] : ns.getHostname();
const minSecurity = ns.getServerMinSecurityLevel(target) + 2;
let orgBalance, balance, security;
log();
while(true) {
// Update information
security = await ns.getServerSecurityLevel(target);
balance = await ns.getServerMoneyAvailable(target);
if(orgBalance == null) orgBalance = balance;
2022-02-04 18:56:22 -05:00
2022-02-10 17:30:11 -05:00
// Pick step
if(security > minSecurity) {
log('Attacking Security...');
const w = await ns.weaken(target, {threads});
log(`Security: -${w}`);
} else if(balance <= orgBalance) {
log('Spoofing Balance...');
const g = await ns.grow(target, {threads});
log(`Balance: +$${Math.round((g * balance - balance) * 100) / 100}`);
2022-02-04 18:56:22 -05:00
} else {
2022-02-10 17:30:11 -05:00
log('Hacking Account...');
const h = await ns.hack(target, {threads});
log(`Balance: -$${h}`);
2022-02-04 18:56:22 -05:00
}
}
}
export function autocomplete(data) {
return [...data.servers];
}