Updated node-manger help & param order

This commit is contained in:
Zakary Timson 2022-02-10 03:18:01 +00:00
parent 15d3766b35
commit 62f29285b3

View File

@ -8,7 +8,7 @@ export async function main(ns) {
* message - optional message to add * message - optional message to add
*/ */
function help(message) { function help(message) {
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.tprint(`\n\n${!message ? '' : `${message}\n\n`}Usage: run node-manager.js [OPTION] LIMIT\n\n\tLimit - Limit the number of nodes the script will buy\n\nOptions:\n\tHelp - Displays this help message\n\tBalance - Prevent spending bellow this point\n\n`);
ns.exit(); ns.exit();
} }
@ -19,36 +19,44 @@ export async function main(ns) {
function log(message) { function log(message) {
ns.clearLog(); ns.clearLog();
ns.print('==================================================='); ns.print('===================================================');
ns.print(`🖥️ Node Manager: ${nodeCount}/${LIMIT} Nodes`); ns.print(`🖥️ Node Manager: ${nodeCount}/${limit} Nodes`);
ns.print('==================================================='); ns.print('===================================================');
if(message != null) MESSAGE_HISTORY.push(message); if(message != null) messageHistory.push(message);
MESSAGE_HISTORY.splice(0, MESSAGE_HISTORY.length - HISTORY_LENGTH); messageHistory.splice(0, messageHistory.length - historyLength);
MESSAGE_HISTORY.forEach(m => ns.print(m)); messageHistory.forEach(m => ns.print(m));
} }
// Setup // Setup
ns.disableLog('ALL'); ns.disableLog('ALL');
if(ns.args.length == 0) help('Missing number of nodes');
if(ns.args[0] == 'help') help(); if(ns.args[0] == 'help') help();
if(ns.args[0] == null) help('Missing number of nodes'); const historyLength = 17;
if(isNaN(ns.args[0])) help('First argument must be a number'); const messageHistory = Array(historyLength).fill('');
const HISTORY_LENGTH = 17; let limit, savings, nodeCount = ns.hacknet.numNodes();
const MESSAGE_HISTORY = Array(HISTORY_LENGTH).fill('');
const LIMIT = ns.args[0] < ns.hacknet.maxNumNodes() ? ns.args[0] : ns.hacknet.maxNumNodes(); if(ns.args.length == 1) {
const SAVINGS = ns.args[1] ?? 0; if(isNaN(ns.args[0])) help('Limit must be a number');
let nodeCount = ns.hacknet.numNodes(); limit = ns.args[0];
savings = 0;
} else if(ns.args.length == 2) {
if(isNaN(ns.args[1])) help('Limit must be a number');
limit = ns.args[1];
if(isNaN(ns.args[0])) help('Balance must be a number');
savings = ns.args[0];
}
log(); log();
while(true) { while(true) {
const BALANCE = ns.getServerMoneyAvailable('home'); const balance = ns.getServerMoneyAvailable('home');
// Check if we should buy a new node // Check if we should buy a new node
if(nodeCount < LIMIT && BALANCE - ns.hacknet.getPurchaseNodeCost() > SAVINGS) { if(nodeCount < limit && balance - ns.hacknet.getPurchaseNodeCost() >= savings) {
nodeCount++; nodeCount++;
ns.hacknet.purchaseNode(); ns.hacknet.purchaseNode();
log(`Buying Node ${nodeCount}`); log(`Buying Node ${nodeCount}`);
} else { } else {
// Create an ordered list of nodes by their cheapest upgrade // Create an ordered list of nodes by their cheapest upgrade
const NODES = Array(nodeCount).fill(null) const nodes = Array(nodeCount).fill(null)
.map((ignore, i) => ({ // Gather information .map((ignore, i) => ({ // Gather information
index: i, index: i,
cacheCost: ns.hacknet.getCacheUpgradeCost(i), cacheCost: ns.hacknet.getCacheUpgradeCost(i),
@ -90,10 +98,10 @@ export async function main(ns) {
}); });
// Apply the cheapest upgrade // Apply the cheapest upgrade
if(BALANCE - NODES[0].bestUpgrade.cost > SAVINGS) { if(nodes.length && balance - nodes[0].bestUpgrade.cost >= savings) {
const COST = Math.round(NODES[0].bestUpgrade.cost * 100) / 100; const cost = Math.round(nodes[0].bestUpgrade.cost * 100) / 100;
log(`Upgrading Node ${NODES[0].index} ${NODES[0].bestUpgrade.name}: $${COST}`); log(`Upgrading Node ${nodes[0].index} ${nodes[0].bestUpgrade.name}: $${cost}`);
NODES[0].bestUpgrade.purchase(); nodes[0].bestUpgrade.purchase();
} }
} }