Update node-manager.js

This commit is contained in:
Zakary Timson 2022-02-04 19:29:15 +00:00
parent 308b422c75
commit 11f85f5749

View File

@ -1,45 +1,71 @@
export function sortByProp(prop, reverse = false) {
return function(a, b) {
if(a[prop] > b[prop]) return reverse ? -1 : 1;
if(a[prop] < b[prop]) return reverse ? 1: -1;
return 0;
};
}
const SAVINGS = 10E6 // Minimum bank account balance
export async function main(ns) {
ns.disableLog('ALL');
const LIMIT = ns.args[0] || 8;
let limit = ns.args[0] || 8;
limit = limit < ns.hacknet.maxNumNodes() ? limit : ns.hacknet.maxNumNodes();
let nodeCount = ns.hacknet.numNodes();
ns.print('===================================================');
ns.print(`🖥️ Node Manager: ${nodeCount}`);
ns.print(`🖥️ Node Manager: ${limit > nodeCount ? limit : nodeCount} Nodes`);
ns.print('===================================================');
while(true) {
let limit = LIMIT < ns.hacknet.maxNumNodes() ? LIMIT : ns.hacknet.maxNumNodes();
while(nodeCount < limit) {
const res = ns.hacknet.purchaseNode();
if(res == -1) break;
nodeCount++;
ns.print(`Purchased Node: ${nodeCount}`);
const BALANCE = ns.getServerMoneyAvailable('home');
if(nodeCount < limit && BALANCE - ns.hacknet.getPurchaseNodeCost() > SAVINGS) {
nodeCount++;
ns.hacknet.purchaseNode();
ns.print(`Buying Node ${nodeCount}`);
} else {
const NODES = Array(nodeCount).fill(null)
.map((ignore, i) => ({
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 => {
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: 'Core',
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) => {
if(a.bestUpgrade.cost > b.bestUpgrade.cost) return 1;
if(a.bestUpgrade.cost < b.bestUpgrade.cost) return -1;
return 0;
});
if(BALANCE - NODES[0].bestUpgrade.cost > SAVINGS) {
const COST = Math.round(NODES[0].bestUpgrade.cost * 100) / 100;
ns.print(`Upgrading Node ${NODES[0].index} ${NODES[0].bestUpgrade.name}: $${COST}`);
NODES[0].bestUpgrade.purchase();
}
}
const NODES = Array(nodeCount).fill(null)
.map((ignore, i) => ({index: i, ...ns.hacknet.getNodeStats(i)}));
NODES.sort(sortByProp('level')).forEach(n => {
const s = ns.hacknet.upgradeLevel(n.index, 1);
if(s) ns.print(`Purchased Level for: ${n.index}`);
});
NODES.sort(sortByProp('ram')).forEach(n => {
const s = ns.hacknet.upgradeRam(n.index, 1);
if(s) ns.print(`Purchased RAM for: ${n.index}`);
});
NODES.sort(sortByProp('cores')).forEach(n => {
const s = ns.hacknet.upgradeCore(n.index, 1);
if(s) ns.print(`Purchased CPU for: ${n.index}`);
});
await ns.sleep(10000);
await ns.sleep(1000);
}
}