2023-11-03 18:42:37 -03:00
|
|
|
/*
|
|
|
|
* TODO:
|
|
|
|
* The current solution is not optimal, as it's preferable to buy new nodes, even if upgrading is cheaper.
|
|
|
|
* TODO:
|
|
|
|
* It may be helpful to calculate the money per second of each node on average, (total mps / total nodes) and compare
|
|
|
|
* it to the time it would take to purchase the next cheapest upgrade, and if the average mps is greater than the
|
|
|
|
* negative mps of the upgrade cost, then buy a new node.
|
|
|
|
* TODO:
|
|
|
|
* Potential Ideas:
|
|
|
|
* Find a way to calculate how much mps an individual upgrade would give, and compare it to the average mps of the node.
|
|
|
|
* ns.hacknet.getNodeStats(0).production
|
|
|
|
* If the upgrade mps is greater than the average mps, then buy the upgrade.
|
|
|
|
* If the upgrade mps is less than the average mps, then buy a new node.
|
|
|
|
*/
|
|
|
|
|
2023-11-06 20:20:36 -04:00
|
|
|
enum UpgradeType {
|
|
|
|
level = "level", ram = "ram", core = "code",
|
2023-11-03 18:42:37 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
export async function main(ns: NS) {
|
|
|
|
let timeout: number = <number> ns.args[0]
|
|
|
|
|
|
|
|
let nodes = ns.hacknet.numNodes()
|
2023-11-06 20:20:36 -04:00
|
|
|
// If there are no nodes, buyUpgrade one
|
2023-11-03 20:17:24 -03:00
|
|
|
if (nodes === 0) {
|
|
|
|
ns.hacknet.purchaseNode()
|
|
|
|
nodes = 1
|
|
|
|
}
|
|
|
|
|
2023-11-06 20:20:36 -04:00
|
|
|
let costs: { type: UpgradeType, cost: number }[] = []
|
2023-11-03 18:42:37 -03:00
|
|
|
while (true) {
|
|
|
|
costs = []
|
|
|
|
// Go through each node and get the cheapest upgrade
|
|
|
|
for (let i = 0; i < nodes; i++) {
|
|
|
|
costs.push(getCheapestCost(ns, i))
|
|
|
|
}
|
2023-11-06 20:20:36 -04:00
|
|
|
// Get the cheapest upgrade object
|
|
|
|
let cheapest = costs.reduce((prev, curr) => prev.cost < curr.cost ? prev : curr)
|
|
|
|
|
|
|
|
// Nodes have a lot more value, so only need node price to be 1/10th the cost of the cheapest upgrade
|
|
|
|
if (ns.hacknet.getPurchaseNodeCost() / 10 < cheapest.cost) {
|
|
|
|
await buyNode(ns, ns.hacknet.getPurchaseNodeCost(), timeout)
|
|
|
|
} else {
|
|
|
|
await buyUpgrade(ns, cheapest.type, cheapest.cost, costs.indexOf(cheapest), timeout)
|
2023-11-03 18:42:37 -03:00
|
|
|
}
|
|
|
|
// Make sure that the number of nodes is up-to-date
|
|
|
|
nodes = ns.hacknet.numNodes()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-06 20:20:36 -04:00
|
|
|
* Wait until the player has enough money to buyUpgrade something
|
2023-11-03 18:42:37 -03:00
|
|
|
* @param ns Global NS object
|
|
|
|
* @param money Amount of money to wait for
|
|
|
|
* @param timeout=-1 Number of seconds to wait before timing out.
|
|
|
|
* @throws Error if the timeout is reached
|
|
|
|
* @note This function will wait forever by default
|
|
|
|
*/
|
|
|
|
async function waitUntilMoney(ns: NS, money: number, timeout: number = -1) {
|
2023-11-03 21:57:09 -03:00
|
|
|
while (ns.getServerMoneyAvailable("home") < money) {
|
2023-11-03 18:42:37 -03:00
|
|
|
await ns.sleep(1000)
|
|
|
|
if (timeout == 0) {
|
2023-11-03 21:57:09 -03:00
|
|
|
throw new Error("Timed out waiting for money")
|
2023-11-03 18:42:37 -03:00
|
|
|
} else if (timeout > -1) {
|
|
|
|
timeout--
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 20:20:36 -04:00
|
|
|
async function buyUpgrade(ns: NS, type: UpgradeType, cost: number, node: number, timeout: number) {
|
2023-11-03 18:42:37 -03:00
|
|
|
await waitUntilMoney(ns, cost, timeout)
|
|
|
|
switch (type) {
|
2023-11-06 20:20:36 -04:00
|
|
|
case UpgradeType.level:
|
2023-11-03 18:42:37 -03:00
|
|
|
ns.hacknet.upgradeLevel(node)
|
|
|
|
break
|
2023-11-06 20:20:36 -04:00
|
|
|
case UpgradeType.ram:
|
2023-11-03 18:42:37 -03:00
|
|
|
ns.hacknet.upgradeRam(node)
|
|
|
|
break
|
2023-11-06 20:20:36 -04:00
|
|
|
case UpgradeType.core:
|
2023-11-03 18:42:37 -03:00
|
|
|
ns.hacknet.upgradeCore(node)
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-06 20:20:36 -04:00
|
|
|
async function buyNode(ns: NS, cost: number, timeout: number) {
|
|
|
|
await waitUntilMoney(ns, cost, timeout)
|
|
|
|
ns.hacknet.purchaseNode()
|
|
|
|
}
|
|
|
|
|
2023-11-03 18:42:37 -03:00
|
|
|
function getCheapestCost(ns: NS, node: number) {
|
|
|
|
let levelCost = ns.hacknet.getLevelUpgradeCost(node)
|
|
|
|
let ramCost = ns.hacknet.getRamUpgradeCost(node)
|
|
|
|
let coreCost = ns.hacknet.getCoreUpgradeCost(node)
|
2023-11-06 20:20:36 -04:00
|
|
|
let cheapest = Math.min(levelCost, ramCost, coreCost)
|
2023-11-03 18:42:37 -03:00
|
|
|
switch (cheapest) {
|
|
|
|
case levelCost:
|
2023-11-06 20:20:36 -04:00
|
|
|
return { type: UpgradeType.level, cost: levelCost }
|
2023-11-03 18:42:37 -03:00
|
|
|
case ramCost:
|
2023-11-06 20:20:36 -04:00
|
|
|
return { type: UpgradeType.ram, cost: ramCost }
|
2023-11-03 18:42:37 -03:00
|
|
|
case coreCost:
|
2023-11-06 20:20:36 -04:00
|
|
|
return { type: UpgradeType.core, cost: coreCost }
|
2023-11-03 18:42:37 -03:00
|
|
|
}
|
|
|
|
}
|