bitburner/scripts/hacknet-manager.js

95 lines
4.0 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';
2022-02-10 12:10:26 -05:00
2022-02-04 17:06:44 -05:00
/**
2022-03-09 14:06:14 -05:00
* Buy, upgrade & manage Hacknet nodes automatically.
* @params ns {NS} - BitBurner API
2022-02-04 17:06:44 -05:00
*/
2022-02-04 11:49:05 -05:00
export async function main(ns) {
2022-03-09 14:06:14 -05:00
// Setup
2022-02-10 12:10:26 -05:00
ns.disableLog('ALL');
const argParser = new ArgParser('hacknet-manager.js', 'Buy, upgrade & manage Hacknet nodes automatically. Tail for live updates.', null, [
2022-03-09 14:06:14 -05:00
{name: 'limit', desc: 'Limit the number of nodes the manager will buy, defaults to 8', optional: true, default: 8, type: 'num'},
{name: 'autoLimit', desc: 'Automatically increase the node limit when there is nothing to do', flags: ['-a', '--auto-limit'], type: 'bool'},
2022-03-15 20:41:23 -04:00
{name: 'balance', desc: 'Prevent spending bellow point', flags: ['-b', '--balance'], type: 'num'},
{name: 'sleep', desc: 'Amount of time to wait between purchases, defaults to 1 (second)', flags: ['-s', '--sleep'], default: 1, type: 'num'}
2022-03-15 22:05:34 -04:00
]);
2022-02-04 16:17:48 -05:00
2022-03-15 20:41:23 -04:00
try {
// Run
const args = argParser.parse(ns.args);
let nodeCount = ns.hacknet.numNodes();
2022-03-15 22:05:34 -04:00
const logger = new Logger(ns, [() => `Hacknet Manager: ${nodeCount}/${args['limit']}`]);
2022-03-15 20:41:23 -04:00
while(true) {
const balance = ns.getServerMoneyAvailable('home');
2022-02-04 16:17:48 -05:00
2022-03-15 20:41:23 -04:00
// Check if we should buy a new node
if(nodeCount < args['limit'] && balance - ns.hacknet.getPurchaseNodeCost() >= args['balance']) {
nodeCount++;
ns.hacknet.purchaseNode();
logger.log(`Buying Node ${nodeCount}`);
} else {
// Create an ordered list of nodes by their cheapest upgrade
const upgrades = Array(nodeCount).fill(null)
2022-03-15 20:41:23 -04:00
.map((ignore, i) => ({ // Gather information
index: i,
cacheCost: ns.hacknet.getCacheUpgradeCost(i),
coreCost: ns.hacknet.getCoreUpgradeCost(i),
levelCost: ns.hacknet.getLevelUpgradeCost(i),
ramCost: ns.hacknet.getRamUpgradeCost(i),
...ns.hacknet.getNodeStats(i)
})).map(node => { // Figure out cheapest upgrade
if(node.cacheCost != 0 && node.cacheCost != Infinity && node.cacheCost <= node.coreCost && node.cacheCost <= node.levelCost && node.cacheCost <= node.ramCost) {
node.bestUpgrade = {
name: 'cache',
cost: node.cacheCost,
purchase: () => ns.hacknet.upgradeCache(node.index)
};
} else if(node.coreCost != 0 && node.coreCost != Infinity && node.coreCost <= node.cacheCost && node.coreCost <= node.levelCost && node.coreCost <= node.ramCost) {
node.bestUpgrade = {
name: 'cores',
cost: node.coreCost,
purchase: () => ns.hacknet.upgradeCore(node.index)
};
} else if(node.ramCost != 0 && node.ramCost != Infinity && node.ramCost <= node.cacheCost && node.ramCost <= node.levelCost && node.ramCost <= node.coreCost) {
node.bestUpgrade = {
name: 'ram',
cost: node.ramCost,
purchase: () => ns.hacknet.upgradeRam(node.index)
};
} else {
node.bestUpgrade = {
name: 'level',
cost: node.levelCost,
purchase: () => ns.hacknet.upgradeLevel(node.index)
};
}
return node;
}).sort((a, b) => { // Sort by cheapest upgrade
if(a.bestUpgrade.cost > b.bestUpgrade.cost) return 1;
if(a.bestUpgrade.cost < b.bestUpgrade.cost) return -1;
return 0;
});
2022-02-04 11:49:05 -05:00
2022-03-15 20:41:23 -04:00
// Apply the cheapest upgrade
if(upgrades.length) {
if(args['autoLimit'] && nodeCount >= args['limit'] && upgrades[0].bestUpgrade.cost == Infinity) {
2022-04-02 13:04:06 -04:00
args['limit'] = Math.max(nodeCount, args['limit']) + 1;
logger.log(`Increasing node limit to ${args['limit']}`);
} else if(balance - upgrades[0].bestUpgrade.cost >= args['balance']) {
const cost = Math.round(upgrades[0].bestUpgrade.cost * 100) / 100;
logger.log(`Node ${upgrades[0].index} - ${upgrades[0].bestUpgrade.name} ${upgrades[0][upgrades[0].bestUpgrade.name] + 1} - $${cost}`);
upgrades[0].bestUpgrade.purchase();
}
2022-03-15 20:41:23 -04:00
}
2022-02-04 14:29:15 -05:00
}
2022-02-04 11:49:05 -05:00
2022-03-15 20:41:23 -04:00
// Check again in 1s
await ns.sleep(args['sleep'] * 1000);
}
} catch(err) {
if(err instanceof ArgError) return ns.tprint(argParser.help(err.message));
throw err;
2022-02-04 11:49:05 -05:00
}
2022-02-10 12:12:29 -05:00
}