Refactor hacknet logic

This commit is contained in:
Isaac Shoebottom 2023-11-06 20:20:36 -04:00
parent fad1c1a7c4
commit 83ba1af0cc

View File

@ -13,37 +13,35 @@
* If the upgrade mps is less than the average mps, then buy a new node. * If the upgrade mps is less than the average mps, then buy a new node.
*/ */
enum Type { enum UpgradeType {
newNode = "node", level = "level", ram = "ram", core = "code", level = "level", ram = "ram", core = "code",
} }
export async function main(ns: NS) { export async function main(ns: NS) {
let timeout: number = <number> ns.args[0] let timeout: number = <number> ns.args[0]
let nodes = ns.hacknet.numNodes() let nodes = ns.hacknet.numNodes()
// If there are no nodes, buy one // If there are no nodes, buyUpgrade one
if (nodes === 0) { if (nodes === 0) {
ns.hacknet.purchaseNode() ns.hacknet.purchaseNode()
nodes = 1 nodes = 1
} }
let costs: { type: Type, cost: number }[] = [] let costs: { type: UpgradeType, cost: number }[] = []
while (true) { while (true) {
costs = [] costs = []
// Go through each node and get the cheapest upgrade // Go through each node and get the cheapest upgrade
for (let i = 0; i < nodes; i++) { for (let i = 0; i < nodes; i++) {
costs.push(getCheapestCost(ns, i)) costs.push(getCheapestCost(ns, i))
} }
// Buy the cheapest upgrade from all nodes // Get the cheapest upgrade object
let cheapest = Math.min(...costs.map(c => c.cost)) let cheapest = costs.reduce((prev, curr) => prev.cost < curr.cost ? prev : curr)
// Find the index of the cheapest cost in the list
let index = costs.findIndex(c => c.cost === cheapest) // Nodes have a lot more value, so only need node price to be 1/10th the cost of the cheapest upgrade
// Wait to buy the cheapest upgrade if (ns.hacknet.getPurchaseNodeCost() / 10 < cheapest.cost) {
try { await buyNode(ns, ns.hacknet.getPurchaseNodeCost(), timeout)
await buy(ns, costs[index].type, costs[index].cost, index, timeout) } else {
} catch (e) { await buyUpgrade(ns, cheapest.type, cheapest.cost, costs.indexOf(cheapest), timeout)
ns.tprint(e)
ns.exit()
} }
// Make sure that the number of nodes is up-to-date // Make sure that the number of nodes is up-to-date
nodes = ns.hacknet.numNodes() nodes = ns.hacknet.numNodes()
@ -51,7 +49,7 @@ export async function main(ns: NS) {
} }
/** /**
* Wait until the player has enough money to buy something * Wait until the player has enough money to buyUpgrade something
* @param ns Global NS object * @param ns Global NS object
* @param money Amount of money to wait for * @param money Amount of money to wait for
* @param timeout=-1 Number of seconds to wait before timing out. * @param timeout=-1 Number of seconds to wait before timing out.
@ -69,38 +67,37 @@ async function waitUntilMoney(ns: NS, money: number, timeout: number = -1) {
} }
} }
async function buy(ns: NS, type: Type, cost: number, node: number, timeout: number) { async function buyUpgrade(ns: NS, type: UpgradeType, cost: number, node: number, timeout: number) {
await waitUntilMoney(ns, cost, timeout) await waitUntilMoney(ns, cost, timeout)
switch (type) { switch (type) {
case Type.newNode: case UpgradeType.level:
ns.hacknet.purchaseNode()
break
case Type.level:
ns.hacknet.upgradeLevel(node) ns.hacknet.upgradeLevel(node)
break break
case Type.ram: case UpgradeType.ram:
ns.hacknet.upgradeRam(node) ns.hacknet.upgradeRam(node)
break break
case Type.core: case UpgradeType.core:
ns.hacknet.upgradeCore(node) ns.hacknet.upgradeCore(node)
break break
} }
} }
async function buyNode(ns: NS, cost: number, timeout: number) {
await waitUntilMoney(ns, cost, timeout)
ns.hacknet.purchaseNode()
}
function getCheapestCost(ns: NS, node: number) { function getCheapestCost(ns: NS, node: number) {
let nodeCost = ns.hacknet.getPurchaseNodeCost()
let levelCost = ns.hacknet.getLevelUpgradeCost(node) let levelCost = ns.hacknet.getLevelUpgradeCost(node)
let ramCost = ns.hacknet.getRamUpgradeCost(node) let ramCost = ns.hacknet.getRamUpgradeCost(node)
let coreCost = ns.hacknet.getCoreUpgradeCost(node) let coreCost = ns.hacknet.getCoreUpgradeCost(node)
let cheapest = Math.min(nodeCost, levelCost, ramCost, coreCost) let cheapest = Math.min(levelCost, ramCost, coreCost)
switch (cheapest) { switch (cheapest) {
case nodeCost:
return { type: Type.newNode, cost: nodeCost }
case levelCost: case levelCost:
return { type: Type.level, cost: levelCost } return { type: UpgradeType.level, cost: levelCost }
case ramCost: case ramCost:
return { type: Type.ram, cost: ramCost } return { type: UpgradeType.ram, cost: ramCost }
case coreCost: case coreCost:
return { type: Type.core, cost: coreCost } return { type: UpgradeType.core, cost: coreCost }
} }
} }