From 68a436cb77ff77bb0437b956f00bace6e64ffc27 Mon Sep 17 00:00:00 2001 From: catloversg <152669316+catloversg@users.noreply.github.com> Date: Sun, 5 Jan 2025 07:34:48 +0700 Subject: [PATCH] UI: Disable buttons when player cannot buy things in tech vendor (#1881) * UI: Disable buttons when player cannot buy things in tech vendor * Tweak reachMaxCore warning comment --------- Co-authored-by: David Walker --- src/Locations/ui/CoresButton.tsx | 27 ++++++++++++++++-------- src/Locations/ui/PurchaseServerModal.tsx | 2 +- src/Locations/ui/RamButton.tsx | 21 ++++++++++-------- src/Locations/ui/TechVendorLocation.tsx | 20 +++++++++++------- src/Locations/ui/TorButton.tsx | 8 +++---- src/Server/ServerPurchases.ts | 7 +++++- 6 files changed, 52 insertions(+), 33 deletions(-) diff --git a/src/Locations/ui/CoresButton.tsx b/src/Locations/ui/CoresButton.tsx index 9e0c082f4..563bcb556 100644 --- a/src/Locations/ui/CoresButton.tsx +++ b/src/Locations/ui/CoresButton.tsx @@ -14,16 +14,18 @@ interface IProps { export function CoresButton(props: IProps): React.ReactElement { const homeComputer = Player.getHomeComputer(); - const maxCores = Player.bitNodeOptions.restrictHomePCUpgrade || homeComputer.cpuCores >= 8; - if (maxCores) { - return ; - } + const reachMaxCore = Player.bitNodeOptions.restrictHomePCUpgrade || homeComputer.cpuCores >= 8; const cost = Player.getUpgradeHomeCoresCost(); function buy(): void { - if (maxCores) return; - if (!Player.canAfford(cost)) return; + // Do NOT reuse reachMaxCore - it is cached (and possibly stale) at button creation time + if (Player.bitNodeOptions.restrictHomePCUpgrade || homeComputer.cpuCores >= 8) { + return; + } + if (!Player.canAfford(cost)) { + return; + } Player.loseMoney(cost, "servers"); homeComputer.cpuCores++; props.rerender(); @@ -37,9 +39,16 @@ export function CoresButton(props: IProps): React.ReactElement { "Cores increase the effectiveness of grow() and weaken() on 'home'"
- diff --git a/src/Locations/ui/PurchaseServerModal.tsx b/src/Locations/ui/PurchaseServerModal.tsx index 0b62d8060..76ffede26 100644 --- a/src/Locations/ui/PurchaseServerModal.tsx +++ b/src/Locations/ui/PurchaseServerModal.tsx @@ -21,7 +21,7 @@ export function PurchaseServerModal(props: IProps): React.ReactElement { const [hostname, setHostname] = useState(""); function tryToPurchaseServer(): void { - purchaseServer(hostname, props.ram, props.cost); + purchaseServer(hostname, props.ram); props.onClose(); } diff --git a/src/Locations/ui/RamButton.tsx b/src/Locations/ui/RamButton.tsx index b7e5510d4..0a0ba3b22 100644 --- a/src/Locations/ui/RamButton.tsx +++ b/src/Locations/ui/RamButton.tsx @@ -19,12 +19,9 @@ interface IProps { export function RamButton(props: IProps): React.ReactElement { const homeComputer = Player.getHomeComputer(); - if ( + const reachMaxRam = (Player.bitNodeOptions.restrictHomePCUpgrade && homeComputer.maxRam >= 128) || - homeComputer.maxRam >= ServerConstants.HomeComputerMaxRam - ) { - return ; - } + homeComputer.maxRam >= ServerConstants.HomeComputerMaxRam; const cost = Player.getUpgradeHomeRamCost(); @@ -47,10 +44,16 @@ export function RamButton(props: IProps): React.ReactElement { "More RAM means more scripts on 'home'"
- diff --git a/src/Locations/ui/TechVendorLocation.tsx b/src/Locations/ui/TechVendorLocation.tsx index 5328bd989..a07418926 100644 --- a/src/Locations/ui/TechVendorLocation.tsx +++ b/src/Locations/ui/TechVendorLocation.tsx @@ -12,23 +12,24 @@ import { RamButton } from "./RamButton"; import { TorButton } from "./TorButton"; import { CoresButton } from "./CoresButton"; -import { getPurchaseServerCost } from "../../Server/ServerPurchases"; +import { getPurchaseServerCost, getPurchaseServerLimit, getPurchaseServerMaxRam } from "../../Server/ServerPurchases"; import { Money } from "../../ui/React/Money"; import { Player } from "@player"; import { PurchaseServerModal } from "./PurchaseServerModal"; import { formatRam } from "../../ui/formatNumber"; import { Box } from "@mui/material"; -import { useRerender } from "../../ui/React/hooks"; +import { useCycleRerender } from "../../ui/React/hooks"; function ServerButton(props: { ram: number }): React.ReactElement { const [open, setOpen] = useState(false); const cost = getPurchaseServerCost(props.ram); + const reachLimitOfPrivateServer = Player.purchasedServers.length >= getPurchaseServerLimit(); return ( <> - setOpen(false)} ram={props.ram} cost={cost} /> @@ -36,11 +37,14 @@ function ServerButton(props: { ram: number }): React.ReactElement { } export function TechVendorLocation(props: { loc: Location }): React.ReactElement { - const rerender = useRerender(1000); + const rerender = useCycleRerender(); const purchaseServerButtons: React.ReactNode[] = []; - for (let i = props.loc.techVendorMinRam; i <= props.loc.techVendorMaxRam; i *= 2) { - purchaseServerButtons.push(); + for (let ram = props.loc.techVendorMinRam; ram <= props.loc.techVendorMaxRam; ram *= 2) { + if (ram > getPurchaseServerMaxRam()) { + break; + } + purchaseServerButtons.push(); } return ( diff --git a/src/Locations/ui/TorButton.tsx b/src/Locations/ui/TorButton.tsx index 3d6889114..1329cd45f 100644 --- a/src/Locations/ui/TorButton.tsx +++ b/src/Locations/ui/TorButton.tsx @@ -46,14 +46,12 @@ export function TorButton(props: IProps): React.ReactElement { props.rerender(); } - if (Player.hasTorRouter()) { - return ; - } + const hasTorRouter = Player.hasTorRouter(); return ( - ); } diff --git a/src/Server/ServerPurchases.ts b/src/Server/ServerPurchases.ts index 62ad79714..0e763c247 100644 --- a/src/Server/ServerPurchases.ts +++ b/src/Server/ServerPurchases.ts @@ -101,7 +101,12 @@ export function getPurchaseServerMaxRam(): number { } // Manually purchase a server (NOT through Netscript) -export function purchaseServer(hostname: string, ram: number, cost: number): void { +export function purchaseServer(hostname: string, ram: number): void { + const cost = getPurchaseServerCost(ram); + if (cost === Infinity) { + return; + } + //Check if player has enough money if (!Player.canAfford(cost)) { dialogBoxCreate("You don't have enough money to purchase this server!");