From cc909bfc1a2998121692549b2a4a34495a5c24b2 Mon Sep 17 00:00:00 2001 From: Bob Date: Mon, 23 Sep 2024 16:18:01 -0400 Subject: [PATCH] cap buy and sell orders by resource and lords balance --- .../components/trading/MarketOrderPanel.tsx | 87 ++++++++++++++----- 1 file changed, 66 insertions(+), 21 deletions(-) diff --git a/client/src/ui/components/trading/MarketOrderPanel.tsx b/client/src/ui/components/trading/MarketOrderPanel.tsx index eb678179a..1627f43d4 100644 --- a/client/src/ui/components/trading/MarketOrderPanel.tsx +++ b/client/src/ui/components/trading/MarketOrderPanel.tsx @@ -1,4 +1,5 @@ import { BattleManager } from "@/dojo/modelManager/BattleManager"; +import { ProductionManager } from "@/dojo/modelManager/ProductionManager"; import { useDojo } from "@/hooks/context/DojoContext"; import { useRealm } from "@/hooks/helpers/useRealm"; import { useProductionManager } from "@/hooks/helpers/useResources"; @@ -20,7 +21,7 @@ import { findResourceById, } from "@bibliothecadao/eternum"; import clsx from "clsx"; -import { useMemo, useState } from "react"; +import { useEffect, useMemo, useState } from "react"; import { ConfirmationPopup } from "../bank/ConfirmationPopup"; export const MarketResource = ({ @@ -154,6 +155,8 @@ const MarketOrders = ({ offers: MarketInterface[]; isOwnStructureInBattle: boolean; }) => { + const [updateBalance, setUpdateBalance] = useState(false); + const lowestPrice = useMemo(() => { const price = offers.reduce((acc, offer) => (offer.perLords < acc ? offer.perLords : acc), Infinity); return price === Infinity ? 0 : price; @@ -185,7 +188,14 @@ const MarketOrders = ({ }`} > {offers.map((offer, index) => ( - + ))} {isOwnStructureInBattle && (
@@ -221,15 +231,27 @@ const OrderRowHeader = ({ resourceId, isBuy }: { resourceId?: number; isBuy: boo ); }; -const OrderRow = ({ offer, entityId, isBuy }: { offer: MarketInterface; entityId: ID; isBuy: boolean }) => { +const OrderRow = ({ + offer, + entityId, + isBuy, + updateBalance, + setUpdateBalance, +}: { + offer: MarketInterface; + entityId: ID; + isBuy: boolean; + updateBalance: boolean; + setUpdateBalance: (value: boolean) => void; +}) => { const { computeTravelTime } = useTravel(); const dojo = useDojo(); - const { - account: { account }, - setup: { - systemCalls: { cancel_order, accept_partial_order }, - }, - } = useDojo(); + + const lordsManager = new ProductionManager(dojo.setup, entityId, ResourcesIds.Lords); + const lordsBalance = useMemo(() => Number(lordsManager.getResource()?.balance || 0n), [updateBalance]); + + const resourceManager = new ProductionManager(dojo.setup, entityId, offer.makerGets[0].resourceId); + const resourceBalance = useMemo(() => Number(resourceManager.getResource()?.balance || 0n), [updateBalance]); const { getRealmAddressName } = useRealm(); @@ -241,12 +263,6 @@ const OrderRow = ({ offer, entityId, isBuy }: { offer: MarketInterface; entityId return battleManager.isBattleOngoing(nextBlockTimestamp!) && !battleManager.isSiege(nextBlockTimestamp!); }, [offer, nextBlockTimestamp]); - const [inputValue, setInputValue] = useState(() => { - return isBuy - ? offer.makerGets[0].amount / EternumGlobalConfig.resources.resourcePrecision - : offer.takerGets[0].amount / EternumGlobalConfig.resources.resourcePrecision; - }); - const [confirmOrderModal, setConfirmOrderModal] = useState(false); const travelTime = useMemo( @@ -282,6 +298,28 @@ const OrderRow = ({ offer, entityId, isBuy }: { offer: MarketInterface; entityId const currentDefaultTick = useUIStore((state) => state.currentDefaultTick); + const resourceBalanceRatio = useMemo( + () => (resourceBalance < getsDisplayNumber ? resourceBalance / getsDisplayNumber : 1), + [resourceBalance, getsDisplayNumber], + ); + const lordsBalanceRatio = useMemo( + () => (lordsBalance < getTotalLords ? lordsBalance / getTotalLords : 1), + [lordsBalance, getTotalLords], + ); + const [inputValue, setInputValue] = useState(() => { + return isBuy + ? (offer.makerGets[0].amount / EternumGlobalConfig.resources.resourcePrecision) * resourceBalanceRatio + : (offer.takerGets[0].amount / EternumGlobalConfig.resources.resourcePrecision) * lordsBalanceRatio; + }); + + useEffect(() => { + setInputValue( + isBuy + ? (offer.makerGets[0].amount / EternumGlobalConfig.resources.resourcePrecision) * resourceBalanceRatio + : (offer.takerGets[0].amount / EternumGlobalConfig.resources.resourcePrecision) * lordsBalanceRatio, + ); + }, [resourceBalanceRatio, lordsBalanceRatio]); + const calculatedResourceAmount = useMemo(() => { return inputValue * EternumGlobalConfig.resources.resourcePrecision; }, [inputValue, getsDisplay, getTotalLords]); @@ -325,8 +363,8 @@ const OrderRow = ({ offer, entityId, isBuy }: { offer: MarketInterface; entityId setLoading(true); setConfirmOrderModal(false); - await accept_partial_order({ - signer: account, + await dojo.setup.systemCalls.accept_partial_order({ + signer: dojo.account.account, taker_id: entityId, trade_id: offer.tradeId, maker_gives_resources: [offer.takerGets[0].resourceId, offer.takerGets[0].amount], @@ -336,6 +374,7 @@ const OrderRow = ({ offer, entityId, isBuy }: { offer: MarketInterface; entityId } catch (error) { console.error("Failed to accept order", error); } finally { + setUpdateBalance(!updateBalance); setLoading(false); } }; @@ -382,8 +421,8 @@ const OrderRow = ({ offer, entityId, isBuy }: { offer: MarketInterface; entityId