Skip to content

Commit

Permalink
Merge branch 'next' into enh/react-package
Browse files Browse the repository at this point in the history
  • Loading branch information
bob0005 committed Jan 16, 2025
2 parents 358c1d8 + 3aafe70 commit c9256b9
Show file tree
Hide file tree
Showing 33 changed files with 519 additions and 516 deletions.
6 changes: 3 additions & 3 deletions client/apps/game/src/ui/components/bank/add-liquidity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Button from "@/ui/elements/button";
import { ResourceCost } from "@/ui/elements/resource-cost";
import { divideByPrecision, multiplyByPrecision } from "@/ui/utils/utils";
import { ContractAddress, ID, MarketManager, ResourcesIds, getBalance, resources } from "@bibliothecadao/eternum";
import { useDojo, useIsResourcesLocked, usePlayerStructures, useUIStore } from "@bibliothecadao/react";
import { useDojo, useIsStructureResourcesLocked, usePlayerStructures, useUIStore } from "@bibliothecadao/react";
import { useEffect, useMemo, useState } from "react";

const AddLiquidity = ({
Expand Down Expand Up @@ -64,8 +64,8 @@ const AddLiquidity = ({
const hasEnough =
lordsBalance >= multiplyByPrecision(lordsAmount) && resourceBalance >= multiplyByPrecision(resourceAmount);

const isBankResourcesLocked = useIsResourcesLocked(bankEntityId);
const isMyResourcesLocked = useIsResourcesLocked(entityId);
const isBankResourcesLocked = useIsStructureResourcesLocked(bankEntityId);
const isMyResourcesLocked = useIsStructureResourcesLocked(entityId);
const isNotZero = lordsAmount > 0 && resourceAmount > 0;
const canAdd = hasEnough && isNotZero && !isBankResourcesLocked && !isMyResourcesLocked;

Expand Down
11 changes: 5 additions & 6 deletions client/apps/game/src/ui/components/bank/swap.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ContractAddress,
DONKEY_ENTITY_TYPE,
getBalance,
getStructure,
ID,
MarketManager,
RESOURCE_TIERS,
Expand All @@ -20,8 +21,7 @@ import {
import {
soundSelector,
useDojo,
useIsResourcesLocked,
useStructures,
useIsStructureResourcesLocked,
useTravel,
useUiSounds,
useUIStore,
Expand Down Expand Up @@ -54,10 +54,9 @@ export const ResourceSwap = ({
const [resourceAmount, setResourceAmount] = useState(0);
const [canCarry, setCanCarry] = useState(false);
const [openConfirmation, setOpenConfirmation] = useState(false);
const { getStructureByEntityId } = useStructures();

const bankProtector = useMemo(() => {
const structure = getStructureByEntityId(bankEntityId);
const structure = getStructure(bankEntityId, ContractAddress(account.address), setup.components);
return structure?.protector;
}, [bankEntityId]);

Expand Down Expand Up @@ -97,8 +96,8 @@ export const ResourceSwap = ({
return multiplyByPrecision(amount) <= balance;
}, [isBuyResource, lordsAmount, resourceAmount, resourceBalance, lordsBalance, ownerFee]);

const isBankResourcesLocked = useIsResourcesLocked(bankEntityId);
const isMyResourcesLocked = useIsResourcesLocked(entityId);
const isBankResourcesLocked = useIsStructureResourcesLocked(bankEntityId);
const isMyResourcesLocked = useIsStructureResourcesLocked(entityId);
const amountsBiggerThanZero = lordsAmount > 0 && resourceAmount > 0;

const canSwap = useMemo(
Expand Down
9 changes: 6 additions & 3 deletions client/apps/game/src/ui/components/entities/entity.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import {
ContractAddress,
EntityType,
formatTime,
getArmy,
getEntityInfo,
getEntityName,
getResourcesFromBalance,
} from "@bibliothecadao/eternum";
import { useDojo, useGetArmyByEntityId, useNextBlockTimestamp, useUIStore } from "@bibliothecadao/react";
import { useDojo, useNextBlockTimestamp, useUIStore } from "@bibliothecadao/react";
import { useComponentValue } from "@dojoengine/react";
import clsx from "clsx";
import React, { useMemo } from "react";
Expand Down Expand Up @@ -39,7 +40,6 @@ export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
const components = dojo.setup.components;

const { nextBlockTimestamp } = useNextBlockTimestamp();
const { getArmy } = useGetArmyByEntityId();

const weight = useComponentValue(dojo.setup.components.Weight, getEntityIdFromKeys([BigInt(arrival.entityId)]));

Expand All @@ -54,7 +54,10 @@ export const EntityArrival = ({ arrival, ...props }: EntityProps) => {
return getResourcesFromBalance(arrival.entityId, currentDefaultTick, components);
}, [weight]);

const army = useMemo(() => getArmy(arrival.entityId), [arrival.entityId, entity.resources]);
const army = useMemo(
() => getArmy(arrival.entityId, ContractAddress(dojo.account.account.address), components),
[arrival.entityId, entity.resources],
);

const renderEntityStatus = useMemo(() => {
return nextBlockTimestamp ? (
Expand Down
16 changes: 13 additions & 3 deletions client/apps/game/src/ui/components/hyperstructures/co-owners.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,15 @@ import { SelectAddress } from "@/ui/elements/select-address";
import { SortButton, SortInterface } from "@/ui/elements/sort-button";
import { SortPanel } from "@/ui/elements/sort-panel";
import { displayAddress } from "@/ui/utils/utils";
import { ContractAddress, formatTime, getAddressName, HYPERSTRUCTURE_CONFIG_ID, ID } from "@bibliothecadao/eternum";
import { useDojo, useNextBlockTimestamp, usePlayers, useStructureByEntityId, useUIStore } from "@bibliothecadao/react";
import {
ContractAddress,
formatTime,
getAddressName,
getStructure,
HYPERSTRUCTURE_CONFIG_ID,
ID,
} from "@bibliothecadao/eternum";
import { useDojo, useNextBlockTimestamp, usePlayers, useUIStore } from "@bibliothecadao/react";
import { useComponentValue } from "@dojoengine/react";
import { getComponentValue } from "@dojoengine/recs";
import { getEntityIdFromKeys } from "@dojoengine/utils";
Expand Down Expand Up @@ -75,7 +82,10 @@ const CoOwnersRows = ({
}
}, [hyperstructure, hyperstructureConfig, nextBlockTimestamp]);

const structure = useStructureByEntityId(hyperstructureEntityId);
const structure = useMemo(
() => getStructure(hyperstructureEntityId, ContractAddress(account.address), components),
[hyperstructureEntityId, account.address, components],
);

const sortingParams = useMemo(() => {
return [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Button from "@/ui/elements/button";
import { getEntityIdFromKeys } from "@/ui/utils/utils";
import { ArrivalInfo, BattleManager, ID, Resource, ResourceInventoryManager } from "@bibliothecadao/eternum";
import { soundSelector, useDojo, useStructureByEntityId, useUiSounds, useUIStore } from "@bibliothecadao/react";
import { ArrivalInfo, BattleManager, ContractAddress, getStructure, ID, Resource, ResourceInventoryManager } from "@bibliothecadao/eternum";
import { soundSelector, useDojo, useUiSounds, useUIStore } from "@bibliothecadao/react";
import { useComponentValue } from "@dojoengine/react";
import { useMemo, useState } from "react";

Expand All @@ -18,7 +18,13 @@ export const DepositResources = ({ arrival, resources, armyInBattle }: DepositRe
// stone as proxy for depoisiting resources
const { play: playDeposit } = useUiSounds(soundSelector.addStone);

const structureAtPosition = useStructureByEntityId(arrival.recipientEntityId || 0);
const structureAtPosition = useMemo(() => {
return getStructure(
arrival.recipientEntityId || 0,
ContractAddress(dojo.account.account.address),
dojo.setup.components,
);
}, [arrival.recipientEntityId, dojo.account.account.address, dojo.setup.components]);

const battleInProgress = useMemo(() => {
if (!structureAtPosition || !structureAtPosition.protector || structureAtPosition.protector.battle_id === 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,16 @@ import { ResourceIcon } from "@/ui/elements/resource-icon";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/ui/elements/tabs";
import { getTotalTroops } from "@/ui/modules/military/battle-view/battle-history";
import { currencyFormat, formatNumber, formatStringNumber } from "@/ui/utils/utils";
import { ArmyInfo, configManager, ContractAddress, ID, ResourcesIds } from "@bibliothecadao/eternum";
import {
Position,
useDojo,
useGetArmyByEntityId,
useGuilds,
useIsStructureImmune,
useNextBlockTimestamp,
useQuery,
useStructureAtPosition,
useStructureImmunityTimer,
useUIStore,
} from "@bibliothecadao/react";
ArmyInfo,
configManager,
ContractAddress,
getArmy,
getStructureAtPosition,
ID,
ResourcesIds,
} from "@bibliothecadao/eternum";
import { Position, useDojo, useGuilds, useQuery, useUIStore } from "@bibliothecadao/react";
import { useComponentValue } from "@dojoengine/react";
import { getEntityIdFromKeys } from "@dojoengine/utils";
import clsx from "clsx";
Expand All @@ -37,22 +34,30 @@ export const StructureCard = ({
position: Position;
ownArmySelected: ArmyInfo | undefined;
}) => {
const dojo = useDojo();

const [showMergeTroopsPopup, setShowMergeTroopsPopup] = useState<boolean>(false);

const setPreviewBuilding = useUIStore((state) => state.setPreviewBuilding);

const { nextBlockTimestamp } = useNextBlockTimestamp();

const { handleUrlChange } = useQuery();

const { getGuildFromPlayerAddress } = useGuilds();

const structure = useStructureAtPosition(position.getContract());

const playerGuild = getGuildFromPlayerAddress(ContractAddress(structure?.owner.address || 0n));
const structure = useMemo(
() =>
getStructureAtPosition(
position.getContract(),
ContractAddress(dojo.account.account.address),
dojo.setup.components,
),
[position, dojo.account.account.address, dojo.setup.components],
);

const isImmune = useIsStructureImmune(structure, nextBlockTimestamp || 0);
const timer = useStructureImmunityTimer(structure, nextBlockTimestamp || 0);
const playerGuild = useMemo(
() => getGuildFromPlayerAddress(ContractAddress(structure?.owner.address || 0n)),
[structure?.owner.address],
);

const goToHexView = () => {
const url = position.toHexLocationUrl();
Expand All @@ -61,7 +66,7 @@ export const StructureCard = ({
};

return (
Boolean(structure) && (
structure && (
<div className={`px-2 py-2 ${className}`}>
<div className="ml-2">
<Button
Expand Down Expand Up @@ -93,7 +98,7 @@ export const StructureCard = ({
setShowMergeTroopsPopup={setShowMergeTroopsPopup}
showButtons={true}
/>
<ImmunityTimer isImmune={isImmune} timer={timer} className="w-[27rem]" />
<ImmunityTimer structure={structure} className="w-[27rem]" />
</>
)}
{showMergeTroopsPopup && (
Expand Down Expand Up @@ -205,13 +210,13 @@ const TroopExchange = ({
const {
setup: {
account: { account },
components: { Army, Protector },
components,
systemCalls: { army_merge_troops, create_army },
network: { world },
},
} = useDojo();

const { getArmy } = useGetArmyByEntityId();
const { Army, Protector } = components;

const maxTroopCountPerArmy = configManager.getTroopConfig().maxTroopCount;

Expand Down Expand Up @@ -262,11 +267,19 @@ const TroopExchange = ({

const [transferDirection, setTransferDirection] = useState<"to" | "from">("to");

const getArmyWithAddress = (armyId: ID) => getArmy(armyId, ContractAddress(account.address), components);

const protectorArmy = useMemo(
() => takerArmy || getArmyWithAddress(protector?.army_id || 0),
[takerArmy, protector?.army_id],
);
const giverArmy = useMemo(() => getArmyWithAddress(giverArmyEntityId), [giverArmyEntityId]);

const mergeTroops = async () => {
setLoading(true);

const fromArmy = transferDirection === "to" ? getArmy(giverArmyEntityId) : takerArmy || getArmy(protector!.army_id);
const toArmy = transferDirection === "to" ? takerArmy || getArmy(protector!.army_id) : getArmy(giverArmyEntityId);
const fromArmy = transferDirection === "to" ? giverArmy : protectorArmy;
const toArmy = transferDirection === "to" ? protectorArmy : giverArmy;
const transferedTroops = {
knight_count: troopsGiven[ResourcesIds.Knight] * BigInt(configManager.getResourcePrecision()),
paladin_count: troopsGiven[ResourcesIds.Paladin] * BigInt(configManager.getResourcePrecision()),
Expand Down Expand Up @@ -312,7 +325,7 @@ const TroopExchange = ({
<div className="w-[60%] mr-1 bg-gold/20">
<p className="pt-2 pb-1 text-center">{giverArmyName}</p>
<ArmyCapacity
army={transferDirection === "to" ? getArmy(giverArmyEntityId) : takerArmy || getArmy(protector!.army_id)}
army={transferDirection === "to" ? giverArmy : protectorArmy}
className="flex justify-center"
deductedTroops={Object.values(troopsGiven).reduce((a, b) => a + b, 0n)}
/>
Expand Down
33 changes: 27 additions & 6 deletions client/apps/game/src/ui/components/trading/market-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,23 @@ import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@
import { Tabs } from "@/ui/elements/tab";
import { formatTimeDifference } from "@/ui/modules/military/battle-view/battle-progress";
import { currencyFormat, getEntityIdFromKeys } from "@/ui/utils/utils";
import { BattleManager, ID, ResourcesIds, configManager } from "@bibliothecadao/eternum";
import {
useArmyByArmyEntityId,
BattleManager,
ContractAddress,
ID,
ResourcesIds,
configManager,
getArmy,
getStructureAtPosition,
} from "@bibliothecadao/eternum";
import {
useBank,
useBattlesAtPosition,
useDojo,
useMarketStore,
useModalStore,
usePlayerStructures,
useSetMarket,
useStructureByPosition,
useUIStore,
} from "@bibliothecadao/react";
import { useComponentValue } from "@dojoengine/react";
Expand Down Expand Up @@ -69,8 +75,15 @@ export const MarketModal = () => {

const currentBlockTimestamp = useUIStore.getState().nextBlockTimestamp || 0;

const getStructure = useStructureByPosition();
const bankStructure = getStructure(bank?.position || { x: 0, y: 0 });
const bankStructure = useMemo(
() =>
getStructureAtPosition(
bank?.position || { x: 0, y: 0 },
ContractAddress(dojo.account.account.address),
dojo.setup.components,
),
[bank?.position, dojo.account.account.address, dojo.setup.components],
);

const battleEntityId = useMemo(() => {
if (battles.length === 0) return null;
Expand Down Expand Up @@ -107,7 +120,15 @@ export const MarketModal = () => {
getEntityIdFromKeys([BigInt(bank?.entityId!), BigInt(ResourcesIds.Lords)]),
)?.balance || 0n;

const bankArmy = useArmyByArmyEntityId(bankStructure?.protector?.entity_id || 0);
const bankArmy = useMemo(
() =>
getArmy(
bankStructure?.protector?.entity_id || 0,
ContractAddress(dojo.account.account.address),
dojo.setup.components,
),
[bankStructure?.protector?.entity_id, dojo.account.account.address, dojo.setup.components],
);

// get updated army for when a battle starts: we need to have the updated component to have the correct battle_id
const armyInfo = useMemo(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import {
soundSelector,
useDojo,
useIsResourcesLocked,
useIsStructureResourcesLocked,
useNextBlockTimestamp,
useResourceManager,
useTravel,
Expand Down Expand Up @@ -117,7 +117,7 @@ export const MarketOrderPanel = memo(
.sort((a, b) => b.ratio - a.ratio);
}, [resourceAskOffers, resourceId]);

const isResourcesLocked = useIsResourcesLocked(entityId);
const isResourcesLocked = useIsStructureResourcesLocked(entityId);

return (
<div className="order-book-selector grid grid-cols-2 gap-4 p-4 h-full">
Expand Down Expand Up @@ -268,7 +268,7 @@ const OrderRow = memo(
[entityId, updateBalance],
);

const isMakerResourcesLocked = useIsResourcesLocked(offer.makerId);
const isMakerResourcesLocked = useIsStructureResourcesLocked(offer.makerId);

const [confirmOrderModal, setConfirmOrderModal] = useState(false);

Expand Down
Loading

0 comments on commit c9256b9

Please sign in to comment.