From 19fa295d333c568491c58a7dea21aadae7f4218f Mon Sep 17 00:00:00 2001 From: bal7hazar Date: Thu, 12 Dec 2024 10:40:07 +0100 Subject: [PATCH 1/8] =?UTF-8?q?=E2=9C=A8=20Add=20sub=20to=20all=20player?= =?UTF-8?q?=20structures?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/ui/layouts/World.tsx | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/client/src/ui/layouts/World.tsx b/client/src/ui/layouts/World.tsx index 3c74b3ba2..cae57ef46 100644 --- a/client/src/ui/layouts/World.tsx +++ b/client/src/ui/layouts/World.tsx @@ -5,6 +5,7 @@ import useUIStore from "../../hooks/store/useUIStore"; import { addToSubscription } from "@/dojo/queries"; import { useDojo } from "@/hooks/context/DojoContext"; +import { PlayerStructure, useEntities } from "@/hooks/helpers/useEntities"; import { useStructureEntityId } from "@/hooks/helpers/useStructureEntityId"; import { useFetchBlockchainData } from "@/hooks/store/useBlockchainStore"; import { useWorldStore } from "@/hooks/store/useWorldLoading"; @@ -103,6 +104,10 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { const dojo = useDojo(); const structureEntityId = useUIStore((state) => state.structureEntityId); + + const { playerStructures } = useEntities(); + const structures = playerStructures(); + const position = useComponentValue(dojo.setup.components.Position, getEntityIdFromKeys([BigInt(structureEntityId)])); const [lastFetchTimestamp, setLastFetchTimestamp] = useState(Date.now()); @@ -111,11 +116,15 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { setWorldLoading(true); const fetch = async () => { try { - await addToSubscription( - dojo.network.toriiClient, - dojo.network.contractComponents as any, - structureEntityId.toString(), - { x: position?.x || 0, y: position?.y || 0 }, + await Promise.all( + structures.map((structure: PlayerStructure) => + addToSubscription( + dojo.network.toriiClient, + dojo.network.contractComponents as any, + structure.entity_id.toString(), + { x: structure.position.x, y: structure.position.y }, + ), + ), ); } catch (error) { console.error("Fetch failed", error); @@ -136,7 +145,7 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { }; fetch(); - }, [structureEntityId, setWorldLoading]); + }, [setWorldLoading]); return (
Date: Thu, 12 Dec 2024 16:51:34 +0700 Subject: [PATCH 2/8] fix(social): get name from address directly --- client/src/hooks/helpers/useEntities.tsx | 8 +++++++- client/src/ui/modules/social/PlayerId.tsx | 6 ++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/client/src/hooks/helpers/useEntities.tsx b/client/src/hooks/helpers/useEntities.tsx index 701ee1900..f567d6a38 100644 --- a/client/src/hooks/helpers/useEntities.tsx +++ b/client/src/hooks/helpers/useEntities.tsx @@ -277,6 +277,12 @@ export const useEntitiesUtils = () => { } }; + const getAddressName = (address: ContractAddress) => { + const addressName = getComponentValue(AddressName, getEntityIdFromKeys([BigInt(address)])); + + return addressName ? shortString.decodeShortString(addressName.name.toString()) : undefined; + }; + const getAddressNameFromEntity = (entityId: ID) => { const address = getPlayerAddressFromEntity(entityId); if (!address) return; @@ -293,5 +299,5 @@ export const useEntitiesUtils = () => { : undefined; }; - return { getEntityName, getEntityInfo, getAddressNameFromEntity, getPlayerAddressFromEntity }; + return { getEntityName, getEntityInfo, getAddressName, getAddressNameFromEntity, getPlayerAddressFromEntity }; }; diff --git a/client/src/ui/modules/social/PlayerId.tsx b/client/src/ui/modules/social/PlayerId.tsx index 2317890a6..b71c22812 100644 --- a/client/src/ui/modules/social/PlayerId.tsx +++ b/client/src/ui/modules/social/PlayerId.tsx @@ -68,7 +68,7 @@ export const PlayerId = ({ const { getEntityName } = useEntitiesUtils(); - const { getAddressNameFromEntity } = useEntitiesUtils(); + const { getAddressName } = useEntitiesUtils(); const playerEntityId = useMemo(() => { if (!selectedPlayer) return; @@ -83,9 +83,7 @@ export const PlayerId = ({ const playerName = useMemo(() => { if (!selectedPlayer) return; - if (!playerEntityId) return; - - const playerName = getAddressNameFromEntity(playerEntityId); + const playerName = getAddressName(selectedPlayer); return playerName; }, [selectedPlayer, playerEntityId]); From 99d204235ea728fa6270655eee2e61195c77c5ac Mon Sep 17 00:00:00 2001 From: Nasr Date: Thu, 12 Dec 2024 16:54:11 +0700 Subject: [PATCH 3/8] chore: add has not settled realm yet --- client/src/ui/modules/social/PlayerId.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/client/src/ui/modules/social/PlayerId.tsx b/client/src/ui/modules/social/PlayerId.tsx index b71c22812..f67635aad 100644 --- a/client/src/ui/modules/social/PlayerId.tsx +++ b/client/src/ui/modules/social/PlayerId.tsx @@ -138,10 +138,10 @@ export const PlayerId = ({
- {hasBeenPlayingFor ? `Joined ${hasBeenPlayingFor} ago` : "No player selected"} + {hasBeenPlayingFor ? `Joined ${hasBeenPlayingFor} ago` : "Has not settled a realm yet"}
-
{playerEntityId ? `Player ID: ${playerEntityId}` : "No player selected"}
+
{playerEntityId ? `Player ID: ${playerEntityId}` : ""}
From f23882ecb6aac74a4ec59ec7b31d1374cfaa57b3 Mon Sep 17 00:00:00 2001 From: Nasr Date: Thu, 12 Dec 2024 16:56:14 +0700 Subject: [PATCH 4/8] undefined if no realm found --- client/src/ui/modules/social/PlayerId.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/ui/modules/social/PlayerId.tsx b/client/src/ui/modules/social/PlayerId.tsx index f67635aad..1507716fe 100644 --- a/client/src/ui/modules/social/PlayerId.tsx +++ b/client/src/ui/modules/social/PlayerId.tsx @@ -94,7 +94,7 @@ export const PlayerId = ({ SettleRealmData, Array.from(runQuery([HasValue(SettleRealmData, { owner_address: selectedPlayer })]))[0], ); - return formatTime((useUIStore.getState()?.nextBlockTimestamp ?? 0) - (realmSettleData?.timestamp ?? 0)); + return realmSettleData ? formatTime((useUIStore.getState()?.nextBlockTimestamp ?? 0) - (realmSettleData?.timestamp ?? 0)) : undefined; }, [selectedPlayer, playerEntityId]); const playerStructures = useMemo(() => { From b8df024bd74a8411cc6a828eb7a629bd98e4ab4f Mon Sep 17 00:00:00 2001 From: Nasr Date: Thu, 12 Dec 2024 16:59:13 +0700 Subject: [PATCH 5/8] pretty --- client/src/ui/modules/social/PlayerId.tsx | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/client/src/ui/modules/social/PlayerId.tsx b/client/src/ui/modules/social/PlayerId.tsx index 1507716fe..06ec39532 100644 --- a/client/src/ui/modules/social/PlayerId.tsx +++ b/client/src/ui/modules/social/PlayerId.tsx @@ -94,7 +94,9 @@ export const PlayerId = ({ SettleRealmData, Array.from(runQuery([HasValue(SettleRealmData, { owner_address: selectedPlayer })]))[0], ); - return realmSettleData ? formatTime((useUIStore.getState()?.nextBlockTimestamp ?? 0) - (realmSettleData?.timestamp ?? 0)) : undefined; + return realmSettleData + ? formatTime((useUIStore.getState()?.nextBlockTimestamp ?? 0) - (realmSettleData?.timestamp ?? 0)) + : undefined; }, [selectedPlayer, playerEntityId]); const playerStructures = useMemo(() => { From 395b4a450a6b5e9c28aa7e4362f5fddcaf3e5f16 Mon Sep 17 00:00:00 2001 From: 0x1337 Date: Thu, 12 Dec 2024 17:19:55 +0700 Subject: [PATCH 6/8] fix: trade history crash --- .../trading/MarketTradingHistory.tsx | 2 +- .../components/trading/TradeHistoryEvent.tsx | 30 ++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/client/src/ui/components/trading/MarketTradingHistory.tsx b/client/src/ui/components/trading/MarketTradingHistory.tsx index 8cf8e6941..1bf6bb2c7 100644 --- a/client/src/ui/components/trading/MarketTradingHistory.tsx +++ b/client/src/ui/components/trading/MarketTradingHistory.tsx @@ -124,7 +124,7 @@ export const MarketTradingHistory = () => { {filteredTradeEvents .sort((a, b) => b.event.eventTime.getTime() - a.event.eventTime.getTime()) .filter((trade) => - selectedResourceId + trade.event.resourceGiven && trade.event.resourceTaken && selectedResourceId ? trade.event.resourceGiven.resourceId === selectedResourceId || trade.event.resourceTaken.resourceId === selectedResourceId : true, diff --git a/client/src/ui/components/trading/TradeHistoryEvent.tsx b/client/src/ui/components/trading/TradeHistoryEvent.tsx index bfafd8577..ad8c08ed4 100644 --- a/client/src/ui/components/trading/TradeHistoryEvent.tsx +++ b/client/src/ui/components/trading/TradeHistoryEvent.tsx @@ -25,6 +25,13 @@ export const TradeHistoryRowHeader = () => { export const TradeHistoryEvent = ({ trade }: { trade: TradeEvent }) => { const { getAddressNameFromEntity } = useEntitiesUtils(); + + const resourceTaken = trade.event.resourceTaken; + const resourceGiven = trade.event.resourceGiven; + if (!resourceTaken || !resourceGiven) { + return null; + } + const price = getLordsPricePerResource(trade.event.resourceGiven, trade.event.resourceTaken); const taker = getAddressNameFromEntity(trade.event.takerId); @@ -37,12 +44,12 @@ export const TradeHistoryEvent = ({ trade }: { trade: TradeEvent }) => {
{taker}
{"bought"}
- -
{`${currencyIntlFormat(divideByPrecision(trade.event.resourceTaken.amount), 2)} for ${currencyIntlFormat( - divideByPrecision(trade.event.resourceGiven.amount), + +
{`${currencyIntlFormat(divideByPrecision(resourceTaken.amount), 2)} for ${currencyIntlFormat( + divideByPrecision(resourceGiven.amount), 2, )}`}
- +
{currencyIntlFormat(Number(price), 2)} @@ -52,9 +59,7 @@ export const TradeHistoryEvent = ({ trade }: { trade: TradeEvent }) => { resource={ ResourcesIds[ Number( - trade.event.resourceTaken.resourceId === ResourcesIds.Lords - ? trade.event.resourceGiven.resourceId - : trade.event.resourceTaken.resourceId, + resourceTaken.resourceId === ResourcesIds.Lords ? resourceGiven.resourceId : resourceTaken.resourceId, ) ] } @@ -66,8 +71,13 @@ export const TradeHistoryEvent = ({ trade }: { trade: TradeEvent }) => { }; const getLordsPricePerResource = (resourceA: Resource, resourceB: Resource): number => { - const lordsResource = resourceA.resourceId === ResourcesIds.Lords ? resourceA : resourceB; - const otherResource = resourceA.resourceId === ResourcesIds.Lords ? resourceB : resourceA; + try { + const lordsResource = resourceA.resourceId === ResourcesIds.Lords ? resourceA : resourceB; + const otherResource = resourceA.resourceId === ResourcesIds.Lords ? resourceB : resourceA; - return Number(lordsResource.amount) / Number(otherResource.amount); + return Number(lordsResource.amount) / Number(otherResource.amount); + } catch (e) { + console.error(e); + return 0; + } }; From fab0065bf78b3a21cf274e8c7278d7a432c1ffd6 Mon Sep 17 00:00:00 2001 From: bal7hazar Date: Thu, 12 Dec 2024 11:22:02 +0100 Subject: [PATCH 7/8] =?UTF-8?q?=F0=9F=90=9B=20Fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- client/src/ui/layouts/World.tsx | 50 ++++++++++++++++++++++++--------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/client/src/ui/layouts/World.tsx b/client/src/ui/layouts/World.tsx index cae57ef46..b3c9507ec 100644 --- a/client/src/ui/layouts/World.tsx +++ b/client/src/ui/layouts/World.tsx @@ -1,5 +1,5 @@ import { Leva } from "leva"; -import { lazy, Suspense, useEffect, useState } from "react"; +import { lazy, Suspense, useEffect, useMemo, useState } from "react"; import { Redirect } from "wouter"; import useUIStore from "../../hooks/store/useUIStore"; @@ -85,6 +85,7 @@ const MiniMapNavigation = lazy(() => ); export const World = ({ backgroundImage }: { backgroundImage: string }) => { + const [subscriptions, setSubscriptions] = useState<{ [entity: string]: boolean }>({}); const showBlankOverlay = useUIStore((state) => state.showBlankOverlay); const isLoadingScreenEnabled = useUIStore((state) => state.isLoadingScreenEnabled); @@ -108,16 +109,48 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { const { playerStructures } = useEntities(); const structures = playerStructures(); + const filteredStructures = useMemo( + () => structures.filter((structure: PlayerStructure) => !subscriptions[structure.entity_id.toString()]), + [structures, subscriptions], + ); + const position = useComponentValue(dojo.setup.components.Position, getEntityIdFromKeys([BigInt(structureEntityId)])); - const [lastFetchTimestamp, setLastFetchTimestamp] = useState(Date.now()); + useEffect(() => { + if (!structureEntityId || subscriptions[structureEntityId.toString()]) return; + setWorldLoading(true); + setSubscriptions((prev) => ({ ...prev, [structureEntityId.toString()]: true })); + const fetch = async () => { + try { + await addToSubscription( + dojo.network.toriiClient, + dojo.network.contractComponents as any, + structureEntityId.toString(), + { x: position?.x || 0, y: position?.y || 0 }, + ); + } catch (error) { + console.error("Fetch failed", error); + } finally { + setWorldLoading(false); + } + + console.log("world loading", worldLoading); + }; + + fetch(); + }, [structureEntityId, subscriptions, setWorldLoading, setSubscriptions]); useEffect(() => { + if (filteredStructures.length === 0) return; setWorldLoading(true); + setSubscriptions((prev) => ({ + ...prev, + ...Object.fromEntries(filteredStructures.map((structure) => [structure.entity_id.toString(), true])), + })); const fetch = async () => { try { await Promise.all( - structures.map((structure: PlayerStructure) => + filteredStructures.map((structure: PlayerStructure) => addToSubscription( dojo.network.toriiClient, dojo.network.contractComponents as any, @@ -132,20 +165,11 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { setWorldLoading(false); } - // const currentTime = Date.now(); - // setLastFetchTimestamp((prevEndTime) => { - // if (prevEndTime === null) return currentTime; - // if (currentTime - prevEndTime >= 3000) { - // setWorldLoading(false); - // } - // return currentTime; - // }); - console.log("world loading", worldLoading); }; fetch(); - }, [setWorldLoading]); + }, [filteredStructures, setWorldLoading, setSubscriptions]); return (
Date: Thu, 12 Dec 2024 21:22:34 +1100 Subject: [PATCH 8/8] config --- client/package.json | 14 +++++++------- client/src/dojo/setup.ts | 20 +++++++------------- client/src/three/scenes/Worldmap.ts | 1 - 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/client/package.json b/client/package.json index 4694c92b9..2676ed0ae 100644 --- a/client/package.json +++ b/client/package.json @@ -19,14 +19,14 @@ "@bibliothecadao/eternum": "workspace:^", "@cartridge/connector": "0.5.5", "@cartridge/controller": "0.5.5", - "@dojoengine/core": "1.0.3-alpha.0", - "@dojoengine/torii-wasm": "1.0.3-alpha.0", - "@dojoengine/create-burner": "1.0.3-alpha.0", - "@dojoengine/react": "1.0.3-alpha.0", + "@dojoengine/core": "1.0.3-alpha.1", + "@dojoengine/torii-wasm": "1.0.3-alpha.1", + "@dojoengine/create-burner": "1.0.3-alpha.1", + "@dojoengine/react": "1.0.3-alpha.1", "@dojoengine/recs": "^2.0.13", - "@dojoengine/state": "1.0.3-alpha.0", - "@dojoengine/torii-client": "1.0.3-alpha.0", - "@dojoengine/utils": "1.0.3-alpha.0", + "@dojoengine/state": "1.0.3-alpha.1", + "@dojoengine/torii-client": "1.0.3-alpha.1", + "@dojoengine/utils": "1.0.3-alpha.1", "@headlessui/react": "^1.7.18", "@latticexyz/utils": "^2.0.0-next.12", "@radix-ui/react-collapsible": "^1.1.1", diff --git a/client/src/dojo/setup.ts b/client/src/dojo/setup.ts index 90b6305a1..f0807df8d 100644 --- a/client/src/dojo/setup.ts +++ b/client/src/dojo/setup.ts @@ -1,7 +1,6 @@ import { BUILDING_CATEGORY_POPULATION_CONFIG_ID, - HYPERSTRUCTURE_CONFIG_ID, - WORLD_CONFIG_ID, + WORLD_CONFIG_ID } from "@bibliothecadao/eternum"; import { DojoConfig } from "@dojoengine/core"; import { getSyncEntities, getSyncEvents, syncEntities } from "@dojoengine/state"; @@ -30,7 +29,7 @@ export async function setup({ ...config }: DojoConfig) { }, { Keys: { - keys: [BUILDING_CATEGORY_POPULATION_CONFIG_ID.toString(), undefined], + keys: [WORLD_CONFIG_ID.toString(), undefined, undefined], pattern_matching: "FixedLen", models: [], }, @@ -38,24 +37,19 @@ export async function setup({ ...config }: DojoConfig) { { Keys: { keys: [WORLD_CONFIG_ID.toString(), undefined], - pattern_matching: "VariableLen", - models: [], - }, - }, - { - Keys: { - keys: [WORLD_CONFIG_ID.toString()], - pattern_matching: "VariableLen", + pattern_matching: "FixedLen", models: [], }, }, { Keys: { - keys: [HYPERSTRUCTURE_CONFIG_ID.toString()], - pattern_matching: "VariableLen", + keys: [BUILDING_CATEGORY_POPULATION_CONFIG_ID.toString(), undefined], + pattern_matching: "FixedLen", models: [], }, }, + + ]; // fetch all existing entities from torii diff --git a/client/src/three/scenes/Worldmap.ts b/client/src/three/scenes/Worldmap.ts index 01a95c660..f67a2dbc4 100644 --- a/client/src/three/scenes/Worldmap.ts +++ b/client/src/three/scenes/Worldmap.ts @@ -479,7 +479,6 @@ export default class WorldmapScene extends HexagonScene { } removeCachedMatricesAroundColRow(col: number, row: number) { - console.log("removing cache", col, row); for (let i = -this.renderChunkSize.width / 2; i <= this.renderChunkSize.width / 2; i += 10) { for (let j = -this.renderChunkSize.width / 2; j <= this.renderChunkSize.height / 2; j += 10) { if (i === 0 && j === 0) {