From 7ddb66c60193efeb7cd71064cb1daea1f4a145aa Mon Sep 17 00:00:00 2001 From: Martin Fournier Date: Sat, 8 Jan 2022 06:30:22 -0500 Subject: [PATCH] Use wasted hashes on "Sell for Money" --- src/Hacknet/HacknetHelpers.tsx | 16 +++++++++++++++- src/Hacknet/HashManager.ts | 10 +++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/Hacknet/HacknetHelpers.tsx b/src/Hacknet/HacknetHelpers.tsx index 2e4a29354..e2870a6cb 100644 --- a/src/Hacknet/HacknetHelpers.tsx +++ b/src/Hacknet/HacknetHelpers.tsx @@ -421,7 +421,21 @@ function processAllHacknetServerEarnings(player: IPlayer, numCycles: number): nu hashes += h; } - player.hashManager.storeHashes(hashes); + const wastedHashes = player.hashManager.storeHashes(hashes); + if (wastedHashes > 0) { + // Currently set to cap in at 1% of the capacity so it's probably still more efficient to run a script to use the hashes + const maxHashesToConvert = player.hashManager.capacity * 0.01; + const hashesToConvertToCash = Math.min(wastedHashes, maxHashesToConvert); + + const upgrade = HashUpgrades["Sell for Money"]; + if (upgrade === null) throw new Error("Could not get the hash upgrade"); + if (!upgrade.cost) throw new Error("Upgrade is not properly configured"); + + const multiplier = Math.floor(hashesToConvertToCash / upgrade.cost); + if (multiplier > 0) { + player.gainMoney(upgrade.value * multiplier, "hacknet"); + } + } return hashes; } diff --git a/src/Hacknet/HashManager.ts b/src/Hacknet/HashManager.ts index a2fe238f8..aa2cba24d 100644 --- a/src/Hacknet/HashManager.ts +++ b/src/Hacknet/HashManager.ts @@ -113,9 +113,17 @@ export class HashManager { this.hashes += cost; } - storeHashes(numHashes: number): void { + /** + * Stores the given hashes, capping at capacity + * @param numHashes The number of hashes to increment + * @returns The number of wasted hashes (over capacity) + */ + storeHashes(numHashes: number): number { this.hashes += numHashes; + let wastedHashes = this.hashes; this.hashes = Math.min(this.hashes, this.capacity); + wastedHashes -= this.hashes; + return wastedHashes; } updateCapacity(newCap: number): void {