2022-02-11 01:08:32 -05:00
|
|
|
class ArgParser {
|
|
|
|
/**
|
|
|
|
* Create a unix-like argument parser to extract flags from the argument list. Can also create help messages.
|
|
|
|
* @param opts - {examples: string[], arguments: {key: string, alias: string, type: string, optional: boolean, desc: string}[], desc: string}
|
|
|
|
*/
|
|
|
|
constructor(opts) {
|
|
|
|
this.examples = opts.examples ?? [];
|
|
|
|
this.arguments = opts.args ?? [];
|
|
|
|
this.description = opts.desc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse the list for arguments & create a dictionary.
|
|
|
|
* @param args {any[]} - Array of arguments
|
|
|
|
* @returns Dictionary of matched flags + unmatched args under 'extra'
|
|
|
|
*/
|
|
|
|
parse(args) {
|
|
|
|
const req = this.arguments.filter(a => !a.optional && !a.skip);
|
|
|
|
const queue = [...args], parsed = {}, extra = [];
|
|
|
|
for(let i = 0; i < queue.length; i++) {
|
|
|
|
if(queue[i][0] != '-') {
|
|
|
|
extra.push(queue[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let value = null, parse = queue[i].slice(queue[i][1] == '-' ? 2 : 1);
|
|
|
|
if(parse.indexOf('=')) {
|
|
|
|
const split = parse.split('=');
|
|
|
|
parse = split[0];
|
|
|
|
value = split[1];
|
|
|
|
}
|
|
|
|
let arg = this.arguments.find(a => a.key == parse) ?? this.arguments.find(a => a.alias == parse);
|
|
|
|
if(!arg) {
|
|
|
|
extra.push(queue[i]);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if(!value) {
|
|
|
|
value = arg.type == 'bool' ? true : queue[i + 1];
|
|
|
|
if(arg.type != 'bool') i++;
|
|
|
|
}
|
|
|
|
parsed[arg.key] = value;
|
|
|
|
}
|
|
|
|
req.forEach((a, i) => parsed[a.key] = extra[i]);
|
|
|
|
extra.splice(0, req.length);
|
|
|
|
return {...parsed, extra};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a help message of the expected paramters & usage.
|
|
|
|
* @param msg {String} - Optional message to display with help
|
|
|
|
*/
|
|
|
|
help(msg) {
|
|
|
|
let message = '\n\n';
|
|
|
|
message += msg ? msg : this.description;
|
|
|
|
if(this.examples.length) message += '\n\nUsage:\t' + this.examples.join('\n\t');
|
|
|
|
const required = this.arguments.filter(a => !a.optional);
|
|
|
|
if(required.length) message += '\n\n\t' + required.map(a => {
|
|
|
|
const padding = 3 - ~~(a.key.length / 8);
|
|
|
|
return `${a.key}${Array(padding).fill('\t').join('')} ${a.desc}`;
|
|
|
|
}).join('\n\t');
|
|
|
|
const optional = this.arguments.filter(a => a.optional);
|
|
|
|
if(optional.length) message += '\n\nOptions:\n\t' + optional.map(a => {
|
|
|
|
const flgs = `${a.alias ? `-${a.alias} ` : ''}--${a.key}${a.type && a.type != 'bool' ? `=${a.type}` : ''}`;
|
|
|
|
const padding = 3 - ~~(flgs.length / 8);
|
|
|
|
return `${flgs}${Array(padding).fill('\t').join('')} ${a.desc}`;
|
|
|
|
}).join('\n\t');
|
|
|
|
return `${message}\n\n`;
|
|
|
|
}
|
|
|
|
}
|
2022-02-10 12:10:26 -05:00
|
|
|
|
2022-02-04 17:06:44 -05:00
|
|
|
/**
|
|
|
|
* Manages hacknet nodes, purchasing nodes to reach the desired amount.
|
|
|
|
* Upgrades (Level, RAM, Cores & Cache) will be automatically purchased.
|
|
|
|
*/
|
2022-02-04 11:49:05 -05:00
|
|
|
export async function main(ns) {
|
2022-02-10 12:10:26 -05:00
|
|
|
ns.disableLog('ALL');
|
2022-02-04 16:17:48 -05:00
|
|
|
|
2022-02-04 17:06:44 -05:00
|
|
|
/**
|
|
|
|
* Print header with logs
|
2022-02-10 12:43:41 -05:00
|
|
|
* @param message - message to append to logs
|
2022-02-04 17:06:44 -05:00
|
|
|
*/
|
2022-02-04 16:17:48 -05:00
|
|
|
function log(message) {
|
|
|
|
ns.clearLog();
|
|
|
|
ns.print('===================================================');
|
2022-02-09 22:18:01 -05:00
|
|
|
ns.print(`🖥️ Node Manager: ${nodeCount}/${limit} Nodes`);
|
2022-02-04 16:17:48 -05:00
|
|
|
ns.print('===================================================');
|
2022-02-09 22:18:01 -05:00
|
|
|
if(message != null) messageHistory.push(message);
|
|
|
|
messageHistory.splice(0, messageHistory.length - historyLength);
|
|
|
|
messageHistory.forEach(m => ns.print(m));
|
2022-02-04 16:17:48 -05:00
|
|
|
}
|
|
|
|
|
2022-02-10 12:10:26 -05:00
|
|
|
// Initilize script arguments
|
|
|
|
const argParser = new ArgParser({
|
2022-02-10 12:12:29 -05:00
|
|
|
desc: 'Buy, upgrade & manage Hacknet nodes automatically. Tail for live updates.',
|
2022-02-10 12:10:26 -05:00
|
|
|
examples: [
|
|
|
|
'run node-manager.js [OPTIONS] LIMIT',
|
|
|
|
'run node-manager.js --balance 1E6 4',
|
|
|
|
'run node-manager.js --help',
|
|
|
|
],
|
|
|
|
args: [
|
|
|
|
{key: 'LIMIT', desc: 'Limit the number of nodes the manager will buy'},
|
|
|
|
{key: 'balance', alias: 'b', type: 'num', optional: true, desc: 'Prevent spending bellow this point'},
|
2022-02-11 01:08:32 -05:00
|
|
|
{key: 'help', alias: 'h', type: 'bool', optional: true, desc: 'Display help message'},
|
2022-02-10 12:10:26 -05:00
|
|
|
]
|
|
|
|
});
|
|
|
|
const args = argParser.parse(ns.args);
|
|
|
|
|
|
|
|
// Check arguments
|
|
|
|
if(args['help']) return ns.tprint(argParser.help());
|
|
|
|
if(!args['LIMIT']) return ns.tprint(argParser.help('Missing LIMIT'));
|
|
|
|
if(isNaN(args['LIMIT'])) return ns.tprint(argParser.help('LIMIT must be a number'));
|
|
|
|
if(!!args['balance'] && isNaN(args['balance'])) return ns.tprint(argParser.help('LIMIT must be a number'));
|
|
|
|
|
2022-02-04 17:06:44 -05:00
|
|
|
// Setup
|
2022-02-09 22:18:01 -05:00
|
|
|
const historyLength = 17;
|
|
|
|
const messageHistory = Array(historyLength).fill('');
|
2022-02-10 12:10:26 -05:00
|
|
|
const limit = args['LIMIT'];
|
|
|
|
const savings = args['balance'] ?? 0
|
2022-02-11 01:08:32 -05:00
|
|
|
let nodeCount = ns.hacknet.numNodes();
|
2022-02-04 11:49:05 -05:00
|
|
|
|
2022-02-04 16:17:48 -05:00
|
|
|
log();
|
2022-02-04 11:49:05 -05:00
|
|
|
while(true) {
|
2022-02-09 22:18:01 -05:00
|
|
|
const balance = ns.getServerMoneyAvailable('home');
|
2022-02-04 11:49:05 -05:00
|
|
|
|
2022-02-04 17:06:44 -05:00
|
|
|
// Check if we should buy a new node
|
2022-02-09 22:18:01 -05:00
|
|
|
if(nodeCount < limit && balance - ns.hacknet.getPurchaseNodeCost() >= savings) {
|
2022-02-04 14:29:15 -05:00
|
|
|
nodeCount++;
|
|
|
|
ns.hacknet.purchaseNode();
|
2022-02-04 16:17:48 -05:00
|
|
|
log(`Buying Node ${nodeCount}`);
|
2022-02-04 14:29:15 -05:00
|
|
|
} else {
|
2022-02-04 17:06:44 -05:00
|
|
|
// Create an ordered list of nodes by their cheapest upgrade
|
2022-02-09 22:18:01 -05:00
|
|
|
const nodes = Array(nodeCount).fill(null)
|
2022-02-04 17:06:44 -05:00
|
|
|
.map((ignore, i) => ({ // Gather information
|
2022-02-04 14:29:15 -05:00
|
|
|
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)
|
2022-02-04 17:06:44 -05:00
|
|
|
})).map(node => { // Figure out cheapest upgrade
|
2022-02-04 14:29:15 -05:00
|
|
|
if(node.cacheCost != 0 && node.cacheCost != Infinity && node.cacheCost <= node.coreCost && node.cacheCost <= node.levelCost && node.cacheCost <= node.ramCost) {
|
|
|
|
node.bestUpgrade = {
|
2022-02-10 12:10:26 -05:00
|
|
|
name: 'cache',
|
2022-02-04 14:29:15 -05:00
|
|
|
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 = {
|
2022-02-10 12:10:26 -05:00
|
|
|
name: 'cores',
|
2022-02-04 14:29:15 -05:00
|
|
|
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 = {
|
2022-02-10 12:10:26 -05:00
|
|
|
name: 'ram',
|
2022-02-04 14:29:15 -05:00
|
|
|
cost: node.ramCost,
|
|
|
|
purchase: () => ns.hacknet.upgradeRam(node.index)
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
node.bestUpgrade = {
|
2022-02-10 12:10:26 -05:00
|
|
|
name: 'level',
|
2022-02-04 14:29:15 -05:00
|
|
|
cost: node.levelCost,
|
|
|
|
purchase: () => ns.hacknet.upgradeLevel(node.index)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
return node;
|
2022-02-04 17:06:44 -05:00
|
|
|
}).sort((a, b) => { // Sort by cheapest upgrade
|
2022-02-04 14:29:15 -05:00
|
|
|
if(a.bestUpgrade.cost > b.bestUpgrade.cost) return 1;
|
|
|
|
if(a.bestUpgrade.cost < b.bestUpgrade.cost) return -1;
|
|
|
|
return 0;
|
|
|
|
});
|
|
|
|
|
2022-02-04 17:06:44 -05:00
|
|
|
// Apply the cheapest upgrade
|
2022-02-09 22:18:01 -05:00
|
|
|
if(nodes.length && balance - nodes[0].bestUpgrade.cost >= savings) {
|
|
|
|
const cost = Math.round(nodes[0].bestUpgrade.cost * 100) / 100;
|
2022-02-10 12:10:26 -05:00
|
|
|
log(`Node ${nodes[0].index} - ${nodes[0].bestUpgrade.name} ${nodes[0][nodes[0].bestUpgrade.name] + 1} - $${cost}`);
|
2022-02-09 22:18:01 -05:00
|
|
|
nodes[0].bestUpgrade.purchase();
|
2022-02-04 14:29:15 -05:00
|
|
|
}
|
|
|
|
}
|
2022-02-04 11:49:05 -05:00
|
|
|
|
2022-02-04 17:06:44 -05:00
|
|
|
// Check again in 1s
|
2022-02-04 14:29:15 -05:00
|
|
|
await ns.sleep(1000);
|
2022-02-04 11:49:05 -05:00
|
|
|
}
|
2022-02-10 12:12:29 -05:00
|
|
|
}
|