bitburner/scripts/hacknet-manager.js

108 lines
4.6 KiB
JavaScript
Raw Normal View History

2022-04-20 11:32:10 -04:00
import {ArgParser} from '/scripts/lib/arg-parser';
2022-03-15 22:05:34 -04:00
import {Logger} from '/scripts/lib/logger';
2022-04-20 11:32:10 -04:00
import {toCurrency} from '/scripts/lib/utils';
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.
2022-04-20 11:32:10 -04:00
* Strategy is to buy a new node when ever we can & then resort to the cheapest upgrade. If auto-scale is on a new
* server will be purchased when it becomes the cheapest option.
*
* @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');
2022-04-20 11:32:10 -04:00
const argParser = new ArgParser('hacknet-manager.js', 'Buy, upgrade & manage Hacknet nodes automatically. Tail for live updates.', [
{name: 'limit', desc: 'Limit the number of nodes the manager will buy, defaults to 8 or the current number of nodes', optional: true, default: 8},
{name: 'autoLimit', desc: 'Automatically increase the node limit when there is nothing to do', flags: ['-a', '--auto-limit'], default: false},
{name: 'balance', desc: 'Prevent spending bellow point', flags: ['-b', '--balance'], default: false},
{name: 'sleep', desc: 'Amount of time to wait between purchases, defaults to 1 (second)', flags: ['-s', '--sleep'], default: 1}
2022-03-15 22:05:34 -04:00
]);
2022-04-20 11:32:10 -04:00
let nodeCount = ns.hacknet.numNodes();
const args = argParser.parse(ns.args);
if(nodeCount > args['limit']) args['limit'] = nodeCount;
const logger = new Logger(ns, [() => `Hacknet Manager: ${nodeCount}/${args['limit']}`]);
2022-02-04 16:17:48 -05: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']));
2022-02-04 16:17:48 -05:00
2022-04-20 11:32:10 -04:00
// Main loop
// noinspection InfiniteLoopJS
while(true) {
const balance = ns.getServerMoneyAvailable('home');
const newNodeCost = ns.hacknet.getPurchaseNodeCost();
2022-02-04 11:49:05 -05:00
2022-04-20 11:32:10 -04:00
// Check if we should wait to buy a node
if(nodeCount < args['limit'] && balance - newNodeCost >= args['balance']) {
nodeCount++;
ns.hacknet.purchaseNode();
logger.log(`Node ${nodeCount} - Purchased - ${toCurrency(newNodeCost)}`);
} else {
// Create an ordered list of nodes by their cheapest upgrade
const upgrades = Array(nodeCount).fill(null)
.map((ignore, i) => ({ // Gather information
index: i,
cacheCost: ns.hacknet.getCacheUpgradeCost(i, 1),
coreCost: ns.hacknet.getCoreUpgradeCost(i, 1),
levelCost: ns.hacknet.getLevelUpgradeCost(i, 1),
ramCost: ns.hacknet.getRamUpgradeCost(i, 1),
...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,1 )
};
} 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, 1)
};
} 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, 1)
};
} else {
node.bestUpgrade = {
name: 'level',
cost: node.levelCost,
purchase: () => ns.hacknet.upgradeLevel(node.index, 1)
};
}
2022-04-20 11:32:10 -04:00
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-04-20 11:32:10 -04:00
// Apply the cheapest upgrade/purchase
if((!upgrades.length || newNodeCost < upgrades[0].bestUpgrade.cost) && args['autoLimit'] && nodeCount >= args['limit']) {
args['limit'] = Math.max(nodeCount, args['limit']) + 1;
logger.log(`Increasing node limit to ${args['limit']}`);
} else if(upgrades.length && upgrades[0].bestUpgrade.cost != Infinity) {
logger.log(`Node ${upgrades[0].index} - ${upgrades[0].bestUpgrade.name} ${upgrades[0][upgrades[0].bestUpgrade.name] + 1} - ${toCurrency(upgrades[0].bestUpgrade.cost)}`);
upgrades[0].bestUpgrade.purchase();
}
2022-03-15 20:41:23 -04:00
}
2022-04-20 11:32:10 -04:00
// Wait & then check again
await ns.sleep(args['sleep'] * 1000);
2022-02-04 11:49:05 -05:00
}
2022-02-10 12:12:29 -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 new Array(10).fill(null).map((ignore, i) => Math.pow(i, 2).toString());
}