Added comments
This commit is contained in:
		@@ -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) {
 | 
					export async function main(ns) {
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * How to use this script
 | 
				
			||||||
 | 
						 * 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>\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();
 | 
							ns.exit();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Print header with logs
 | 
				
			||||||
 | 
						 * message - message to append to logs
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
	function log(message) {
 | 
						function log(message) {
 | 
				
			||||||
		ns.clearLog();
 | 
							ns.clearLog();
 | 
				
			||||||
		ns.print('===================================================');
 | 
							ns.print('===================================================');
 | 
				
			||||||
@@ -17,34 +27,37 @@ export async function main(ns) {
 | 
				
			|||||||
		for(let i = MESSAGE_HISTORY.length; i < HISTORY_LENGTH; i++) ns.print('');
 | 
							for(let i = MESSAGE_HISTORY.length; i < HISTORY_LENGTH; i++) ns.print('');
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Setup
 | 
				
			||||||
	ns.disableLog('ALL');
 | 
						ns.disableLog('ALL');
 | 
				
			||||||
	if(ns.args[0] == 'help') help();
 | 
						if(ns.args[0] == 'help') help();
 | 
				
			||||||
	if(ns.args[0] == null) help('Missing number of nodes');
 | 
						if(ns.args[0] == null) help('Missing number of nodes');
 | 
				
			||||||
	if(isNaN(ns.args[0])) help('First argument must be a number');
 | 
						if(isNaN(ns.args[0])) help('First argument must be a number');
 | 
				
			||||||
	
 | 
					 | 
				
			||||||
	const HISTORY_LENGTH = 17;
 | 
						const HISTORY_LENGTH = 17;
 | 
				
			||||||
	const MESSAGE_HISTORY = [];
 | 
						const MESSAGE_HISTORY = [];
 | 
				
			||||||
	const LIMIT = ns.args[0] < ns.hacknet.maxNumNodes() ? ns.args[0] : ns.hacknet.maxNumNodes();
 | 
						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();
 | 
						let nodeCount = ns.hacknet.numNodes();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	log();
 | 
						log();
 | 
				
			||||||
    while(true) {
 | 
					    while(true) {
 | 
				
			||||||
		const BALANCE = ns.getServerMoneyAvailable('home');
 | 
							const BALANCE = ns.getServerMoneyAvailable('home');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// 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
 | 
				
			||||||
			const NODES = Array(nodeCount).fill(null)
 | 
								const NODES = Array(nodeCount).fill(null)
 | 
				
			||||||
				.map((ignore, i) => ({
 | 
									.map((ignore, i) => ({ // Gather information
 | 
				
			||||||
					index: i,
 | 
										index: i,
 | 
				
			||||||
					cacheCost: ns.hacknet.getCacheUpgradeCost(i),
 | 
										cacheCost: ns.hacknet.getCacheUpgradeCost(i),
 | 
				
			||||||
					coreCost: ns.hacknet.getCoreUpgradeCost(i),
 | 
										coreCost: ns.hacknet.getCoreUpgradeCost(i),
 | 
				
			||||||
					levelCost: ns.hacknet.getLevelUpgradeCost(i),
 | 
										levelCost: ns.hacknet.getLevelUpgradeCost(i),
 | 
				
			||||||
					ramCost: ns.hacknet.getRamUpgradeCost(i),
 | 
										ramCost: ns.hacknet.getRamUpgradeCost(i),
 | 
				
			||||||
					...ns.hacknet.getNodeStats(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) {
 | 
										if(node.cacheCost != 0 && node.cacheCost != Infinity && node.cacheCost <= node.coreCost && node.cacheCost <= node.levelCost && node.cacheCost <= node.ramCost) {
 | 
				
			||||||
						node.bestUpgrade = {
 | 
											node.bestUpgrade = {
 | 
				
			||||||
							name: 'Cache',
 | 
												name: 'Cache',
 | 
				
			||||||
@@ -71,12 +84,13 @@ export async function main(ns) {
 | 
				
			|||||||
						};
 | 
											};
 | 
				
			||||||
					}
 | 
										}
 | 
				
			||||||
					return node;
 | 
										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;
 | 
				
			||||||
					if(a.bestUpgrade.cost < b.bestUpgrade.cost) return -1;
 | 
										if(a.bestUpgrade.cost < b.bestUpgrade.cost) return -1;
 | 
				
			||||||
					return 0;
 | 
										return 0;
 | 
				
			||||||
				});
 | 
									});
 | 
				
			||||||
			
 | 
								
 | 
				
			||||||
 | 
								// Apply the cheapest upgrade
 | 
				
			||||||
			if(BALANCE - NODES[0].bestUpgrade.cost > SAVINGS) {
 | 
								if(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}`);
 | 
				
			||||||
@@ -84,6 +98,7 @@ export async function main(ns) {
 | 
				
			|||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							// Check again in 1s
 | 
				
			||||||
		await ns.sleep(1000);
 | 
							await ns.sleep(1000);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user