Simple Bot that uses math to choose the most profitable upgrade.
仕組み
The bot takes the following steps.
- Checks cost and production growth of all the possible upgrades
- Calculates profitable ratio of each upgrade
- Sorts elements by ratio descending
- Chooses the first element
- Checks if you can purchase upgrade now, そうでない場合, it waits to collect money
- Checks if you can buy a new node in less than 30 秒 (you can customize this time), もしそうなら, it buys it
- 繰り返す…
Profitable ratio = Production growth / upgrade cost
Production growth = Production after upgrade / current production
This guide is based on my previous “Automation for Hacknet Nodes”.
使用方法
Take the following steps.
- Run the terminal
- Type ‘nano hacknet-bot.js’
- Copy and paste the code below into this file
- ファイルを保存します (Ctrl + s)
- Back to the terminal
- Type ‘run hacknet-bot.js’
/** @param {NS} ns **/ 非同期関数メインのエクスポート(ns) { // helpers const getMoney = () => ns.getPlayer().お金; const getProd = (レベル, ram, cores) => (レベル * 1.5) * Math.pow(1.035, ram - 1) * ((cores + 5) / 6); // your production multiplier const PROD_MULTIPLIER = ns.getHacknetMultipliers().生産; // maximum waiting time for collecting money for new node (default 30s) const WAITING_TIME = ns.args[0] || 30; その間 (真実) { const ratios = []; let hacknetProduction = 0; // loop through all nodes for (インデックス = にしてみましょう 0; 索引 < ns.hacknet.numNodes(); インデックス++) { // get current node stats const { レベル, ram, cores, 生産 } = ns.hacknet.getNodeStats(索引); hacknetProduction += production; // get upgrades cost const levelUpgradeCost = ns.hacknet.getLevelUpgradeCost(索引); const ramUpgradeCost = ns.hacknet.getRamUpgradeCost(索引); const coreUpgradeCost = ns.hacknet.getCoreUpgradeCost(索引); // get prod. 成長 / cost ratios const levelUpgradeRatio = ((getProd(レベル + 1, ram, cores) * PROD_MULTIPLIER) - 生産) / levelUpgradeCost; const ramUpgradeRatio = ((getProd(レベル, ram * 2, cores) * PROD_MULTIPLIER) - 生産) / ramUpgradeCost; const coreUpgradeRatio = ((getProd(レベル, ram, cores + 1) * PROD_MULTIPLIER) - 生産) / coreUpgradeCost; // possible upgrades of current node const currentNodeUpgrades = [ {比率: levelUpgradeRatio, 料金: levelUpgradeCost, nodeIndex: 索引, アップグレード: "レベル"}, {比率: ramUpgradeRatio, 料金: ramUpgradeCost, nodeIndex: 索引, アップグレード: "ram"}, {比率: coreUpgradeRatio, 料金: coreUpgradeCost, nodeIndex: 索引, アップグレード: "コア"} ]; // push current node upgrades to all upgrades ratios.push(...currentNodeUpgrades); } // get the most profitable upgrade const { 料金, nodeIndex, アップグレード } = ratios.sort((ある, b) => b.ratio - a.ratio)[0]; // wait until you have the money for upgrade while (getMoney() < 料金) { await ns.sleep(1); } // execute upgrade switch (アップグレード) { 場合 "レベル": await ns.hacknet.upgradeLevel(nodeIndex); 壊す; 場合 "ram": await ns.hacknet.upgradeRam(nodeIndex); 壊す; 場合 "コア": await ns.hacknet.upgradeCore(nodeIndex); 壊す; デフォルト: 続く; } // check if you can purchase new node const purchaseNodeCost = ns.hacknet.getPurchaseNodeCost(); const missingMoneyForNewNode = purchaseNodeCost - getMoney(); もし (missingMoneyForNewNode > 0 && missingMoneyForNewNode < hacknetProduction * WAITING_TIME) { // if you need to wait for purchase node less than WAITING_TIME (in seconds) program waits to collect money while (getMoney() < purchaseNodeCost) { await ns.sleep(1); } ns.hacknet.purchaseNode(); } // sleep 1ms to prevent crash because of infinite loop await ns.sleep(1); } }
You can customize the time of waiting for purchase node (by default 30s) by passing an additional script argument. 例えば:
run hacknet-bot.js 120