Added comments

This commit is contained in:
Zakary Timson 2022-02-04 22:06:44 +00:00
parent 6799bf5e23
commit d72cd78bcf

View File

@ -1,11 +1,21 @@
const SAVINGS = 10E6 // Minimum bank account balance
/**
* Manages hacknet nodes, purchasing nodes to reach the desired amount.
* Upgrades (Level, RAM, Cores & Cache) will be automatically purchased.
*/
export async function main(ns) {
/**
* How to use this script
* message - optional message to add
*/
function help(message) {
ns.tprint(`\n\n${!message ? '' : `${message}\n\n`}Usage:\nrun node-manager.js <num>\n\n\tnum - Target number of nodes\n\n`);
ns.tprint(`\n\n${!message ? '' : `${message}\n\n`}Usage:\nrun node-manager.js <num> [savings]\n\n\tnum - Target number of nodes\n\tsavings - Prevent spending bellow this point\n\n`);
ns.exit();
}
/**
* Print header with logs
* message - message to append to logs
*/
function log(message) {
ns.clearLog();
ns.print('===================================================');
@ -17,34 +27,37 @@ export async function main(ns) {
for(let i = MESSAGE_HISTORY.length; i < HISTORY_LENGTH; i++) ns.print('');
}
// Setup
ns.disableLog('ALL');
if(ns.args[0] == 'help') help();
if(ns.args[0] == null) help('Missing number of nodes');
if(isNaN(ns.args[0])) help('First argument must be a number');
const HISTORY_LENGTH = 17;
const MESSAGE_HISTORY = [];
const LIMIT = ns.args[0] < ns.hacknet.maxNumNodes() ? ns.args[0] : ns.hacknet.maxNumNodes();
const SAVINGS = ng.args[1] ?? 0;
let nodeCount = ns.hacknet.numNodes();
log();
while(true) {
const BALANCE = ns.getServerMoneyAvailable('home');
// Check if we should buy a new node
if(nodeCount < LIMIT && BALANCE - ns.hacknet.getPurchaseNodeCost() > SAVINGS) {
nodeCount++;
ns.hacknet.purchaseNode();
log(`Buying Node ${nodeCount}`);
} else {
// Create an ordered list of nodes by their cheapest upgrade
const NODES = Array(nodeCount).fill(null)
.map((ignore, i) => ({
.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 => {
})).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',
@ -71,12 +84,13 @@ export async function main(ns) {
};
}
return node;
}).sort((a, b) => {
}).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;
});
// Apply the cheapest upgrade
if(BALANCE - NODES[0].bestUpgrade.cost > SAVINGS) {
const COST = Math.round(NODES[0].bestUpgrade.cost * 100) / 100;
log(`Upgrading Node ${NODES[0].index} ${NODES[0].bestUpgrade.name}: $${COST}`);
@ -84,6 +98,7 @@ export async function main(ns) {
}
}
// Check again in 1s
await ns.sleep(1000);
}
}