diff --git a/client/src/dojo/queries.ts b/client/src/dojo/queries.ts index 2657e67a0..b0334ecad 100644 --- a/client/src/dojo/queries.ts +++ b/client/src/dojo/queries.ts @@ -28,18 +28,26 @@ export const syncPosition = async ( 30_000, ); }; -export const syncBuildingQty = async ( + +export const addToSubscriptionTwoKeyModelbyRealmEntityId = async ( client: ToriiClient, components: Component[], - entityID: string, + entityID: string[], ) => { await getEntities( client, { - Keys: { - keys: [entityID, undefined], - pattern_matching: "FixedLen" as PatternMatching, - models: ["s0_eternum-BuildingQuantityv2"], + Composite: { + operator: "Or", + clauses: [ + ...entityID.map((id) => ({ + Keys: { + keys: [id, undefined], + pattern_matching: "VariableLen" as PatternMatching, + models: ["s0_eternum-BuildingQuantityv2"], + }, + })), + ], }, }, components, @@ -49,12 +57,11 @@ export const syncBuildingQty = async ( ); }; -export const addToSubscriptionBuildingQty = async ( +export const addToSubscriptionOneKeyModelbyRealmEntityId = async ( client: ToriiClient, components: Component[], entityID: string[], ) => { - const start = performance.now(); await getEntities( client, { @@ -63,9 +70,9 @@ export const addToSubscriptionBuildingQty = async ( clauses: [ ...entityID.map((id) => ({ Keys: { - keys: [id, undefined], + keys: [id], pattern_matching: "VariableLen" as PatternMatching, - models: ["s0_eternum-BuildingQuantityv2"], + models: ["s0_eternum-ArrivalTime", "s0_eternum-OwnedResourcesTracker"], }, })), ], @@ -76,8 +83,6 @@ export const addToSubscriptionBuildingQty = async ( [], 30_000, ); - const end = performance.now(); - console.log("AddToSubscription Building qty", end - start); }; export const addToSubscription = async ( diff --git a/client/src/dojo/setup.ts b/client/src/dojo/setup.ts index 8f1f1db6a..9e7b34e9e 100644 --- a/client/src/dojo/setup.ts +++ b/client/src/dojo/setup.ts @@ -114,16 +114,13 @@ export async function setup({ ...config }: DojoConfig) { }, ]; - const CompositeStart = performance.now(); await getEntities( network.toriiClient, { Composite: { operator: "Or", clauses: configClauses } }, network.contractComponents as any, ); - const CompositeEnd = performance.now(); // fetch all existing entities from torii - const SingleKeyStart = performance.now(); await getEntities( network.toriiClient, { @@ -143,7 +140,7 @@ export async function setup({ ...config }: DojoConfig) { "s0_eternum-Army", "s0_eternum-Structure", "s0_eternum-Battle", - // Probably load this w/ market + "s0_eternum-EntityOwner", ], }, }, @@ -153,9 +150,7 @@ export async function setup({ ...config }: DojoConfig) { 40_000, false, ); - const SingleKeyEnd = performance.now(); - const DoubleKeyStart = performance.now(); await getEntities( network.toriiClient, { @@ -171,11 +166,8 @@ export async function setup({ ...config }: DojoConfig) { 40_000, false, ); - const DoubleKeyEnd = performance.now(); - const SyncStart = performance.now(); const sync = await syncEntitiesDebounced(network.toriiClient, network.contractComponents as any, [], false); - const SyncEnd = performance.now(); configManager.setDojo(components); @@ -207,11 +199,6 @@ export async function setup({ ...config }: DojoConfig) { false, ); - console.log("CompositeEnd", CompositeEnd - CompositeStart); - console.log("SyncEnd", SyncEnd - SyncStart); - console.log("SingleKeyEnd", SingleKeyEnd - SingleKeyStart); - console.log("DoubleKeyEnd", DoubleKeyEnd - DoubleKeyStart); - return { network, components, diff --git a/client/src/hooks/helpers/use-resource-arrivals.tsx b/client/src/hooks/helpers/use-resource-arrivals.tsx index 21e8ccc1a..9dd7396bd 100644 --- a/client/src/hooks/helpers/use-resource-arrivals.tsx +++ b/client/src/hooks/helpers/use-resource-arrivals.tsx @@ -59,9 +59,10 @@ const usePlayerArrivals = () => { ]; const getArrivalsWithResourceOnPosition = useCallback((positions: Position[]) => { - return positions.flatMap((position) => { + const arrivals = positions.flatMap((position) => { return Array.from(runQuery([HasValue(Position, { x: position.x, y: position.y }), ...queryFragments])); }); + return arrivals; }, []); const createArrivalInfo = useCallback( diff --git a/client/src/hooks/store/useWorldLoading.tsx b/client/src/hooks/store/useWorldLoading.tsx index a7f2bedb1..f46ace82c 100644 --- a/client/src/hooks/store/useWorldLoading.tsx +++ b/client/src/hooks/store/useWorldLoading.tsx @@ -4,13 +4,17 @@ import { create } from "zustand"; interface WorldState { isWorldLoading: boolean; isMarketLoading: boolean; + isStructuresLoading: boolean; setWorldLoading: (loading: boolean) => void; setMarketLoading: (loading: boolean) => void; + setStructuresLoading: (loading: boolean) => void; } export const useWorldStore = create((set) => ({ isWorldLoading: true, isMarketLoading: true, + isStructuresLoading: true, setWorldLoading: (loading: boolean) => set({ isWorldLoading: loading }), setMarketLoading: (loading: boolean) => set({ isMarketLoading: loading }), + setStructuresLoading: (loading: boolean) => set({ isStructuresLoading: loading }), })); diff --git a/client/src/ui/layouts/World.tsx b/client/src/ui/layouts/World.tsx index 3511530e0..21c769b63 100644 --- a/client/src/ui/layouts/World.tsx +++ b/client/src/ui/layouts/World.tsx @@ -3,20 +3,24 @@ import { lazy, Suspense, useEffect, useMemo, useState } from "react"; import { Redirect } from "wouter"; import useUIStore from "../../hooks/store/useUIStore"; -import { addMarketSubscription, addToSubscription, addToSubscriptionBuildingQty, syncBuildingQty } from "@/dojo/queries"; +import { + addMarketSubscription, + addToSubscription, + addToSubscriptionOneKeyModelbyRealmEntityId, + addToSubscriptionTwoKeyModelbyRealmEntityId, +} 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"; -import { ADMIN_BANK_ENTITY_ID, BuildingType } from "@bibliothecadao/eternum"; +import { ADMIN_BANK_ENTITY_ID } from "@bibliothecadao/eternum"; import { getComponentValue } from "@dojoengine/recs"; import { getEntityIdFromKeys } from "@dojoengine/utils"; import { env } from "../../../env"; import { IS_MOBILE } from "../config"; import { LoadingOroborus } from "../modules/loading-oroborus"; import { LoadingScreen } from "../modules/LoadingScreen"; -import { useBuildings } from "@/hooks/helpers/use-buildings"; // Lazy load components const SelectedArmy = lazy(() => @@ -117,12 +121,6 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { [structures, subscriptions], ); - // const { getBuildings } = useBuildings(); - // filteredStructures.forEach((structure) => { - // console.log("Buildings", getBuildings(structure.position.x, structure.position.y)); - // }); - - // console.log("FilteredStructures", filteredStructures); useEffect(() => { if ( !structureEntityId || @@ -133,13 +131,6 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { return; } - // syncBuildingQty(dojo.network.toriiClient, dojo.network.contractComponents as any, structureEntityId.toString()); - // filteredStructures.forEach((structure) => { - // syncBuildingQty(dojo.network.toriiClient, dojo.network.contractComponents as any, structure.entity_id.toString()); - // }); - - - const position = getComponentValue( dojo.setup.components.Position, getEntityIdFromKeys([BigInt(structureEntityId)]), @@ -155,9 +146,13 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { const fetch = async () => { try { - const start = performance.now(); await Promise.all([ - addToSubscriptionBuildingQty( + addToSubscriptionOneKeyModelbyRealmEntityId( + dojo.network.toriiClient, + dojo.network.contractComponents as any, + [...filteredStructures.map((structure) => structure.entity_id.toString())], + ), + addToSubscriptionTwoKeyModelbyRealmEntityId( dojo.network.toriiClient, dojo.network.contractComponents as any, [...filteredStructures.map((structure) => structure.entity_id.toString())], @@ -179,14 +174,11 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { ), addMarketSubscription(dojo.network.toriiClient, dojo.network.contractComponents as any), ]); - const end = performance.now(); - console.log("FetchEnd", end - start); } catch (error) { console.error("Fetch failed", error); } finally { setWorldLoading(false); setMarketLoading(false); - console.log("Done fetching") } }; diff --git a/client/src/ui/modules/world-structures/WorldStructuresMenu.tsx b/client/src/ui/modules/world-structures/WorldStructuresMenu.tsx index 830290638..7473c53e0 100644 --- a/client/src/ui/modules/world-structures/WorldStructuresMenu.tsx +++ b/client/src/ui/modules/world-structures/WorldStructuresMenu.tsx @@ -7,6 +7,7 @@ import { useFragmentMines } from "@/hooks/helpers/useFragmentMines"; import { useGuilds } from "@/hooks/helpers/useGuilds"; import { useHyperstructureProgress, useHyperstructures } from "@/hooks/helpers/useHyperstructures"; import { useResourceBalance } from "@/hooks/helpers/useResources"; +import { useWorldStore } from "@/hooks/store/useWorldLoading"; import { FragmentMinePanel } from "@/ui/components/fragmentMines/FragmentMinePanel"; import { HintSection } from "@/ui/components/hints/HintModal"; import { DisplayedAccess, HyperstructurePanel } from "@/ui/components/hyperstructures/HyperstructurePanel"; @@ -16,16 +17,9 @@ import { ViewOnMapIcon } from "@/ui/components/military/ArmyManagementCard"; import { Checkbox } from "@/ui/elements/Checkbox"; import { HintModalButton } from "@/ui/elements/HintModalButton"; import { ResourceIcon } from "@/ui/elements/ResourceIcon"; -import { currencyFormat, currencyIntlFormat, divideByPrecision, getEntityIdFromKeys } from "@/ui/utils/utils"; -import { - BattleSide, - ContractAddress, - HYPERSTRUCTURE_CONFIG_ID, - ID, - ResourcesIds, - findResourceById, -} from "@bibliothecadao/eternum"; -import { Metadata, getComponentValue } from "@dojoengine/recs"; +import { currencyFormat, currencyIntlFormat, divideByPrecision } from "@/ui/utils/utils"; +import { BattleSide, ContractAddress, ID, ResourcesIds, findResourceById } from "@bibliothecadao/eternum"; +import { Metadata } from "@dojoengine/recs"; import { S } from "@dojoengine/recs/dist/types-3444e4c1"; import { getEntities } from "@dojoengine/state"; import { ToriiClient } from "@dojoengine/torii-wasm"; @@ -39,10 +33,18 @@ export const WorldStructuresMenu = ({ className }: { className?: string }) => { network: { toriiClient, contractComponents }, } = useDojo(); + const isStructuresLoading = useWorldStore((state) => state.isStructuresLoading); + const setStructuresLoading = useWorldStore((state) => state.setStructuresLoading); + useEffect(() => { const fetchData = async () => { try { - await fetchHyperstructureData(toriiClient, contractComponents as any); + await fetchHyperstructureData( + toriiClient, + contractComponents as any, + isStructuresLoading, + setStructuresLoading, + ); } catch (error) { console.error("Failed to fetch hyperstructure data:", error); } @@ -140,6 +142,14 @@ export const WorldStructuresMenu = ({ className }: { className?: string }) => { [selectedTab, hyperstructures, fragmentMines, showOnlyMine, account.address, myHyperstructures], ); + if (isStructuresLoading) { + return ( +
+
Loading structures...
+
+ ); + } + return ( <> @@ -333,16 +343,17 @@ const EntityHeader = ({ entity }: { entity: any }) => { ); }; -const fetchHyperstructureData = async (client: ToriiClient, components: Component[]) => { - const hyperstructureConfig = getComponentValue( - (components as any).HyperstructureResourceConfig, - getEntityIdFromKeys([HYPERSTRUCTURE_CONFIG_ID, 4n]), - ); - - if (hyperstructureConfig) { +const fetchHyperstructureData = async ( + client: ToriiClient, + components: Component[], + isStructuresLoading: boolean, + setStructuresLoading: (loading: boolean) => void, +) => { + if (!isStructuresLoading) { return; } + console.log("Fetching hyperstructure data"); await getEntities( client, { @@ -363,13 +374,6 @@ const fetchHyperstructureData = async (client: ToriiClient, components: Componen models: ["s0_eternum-Epoch", "s0_eternum-Progress"], }, }, - { - Keys: { - keys: [HYPERSTRUCTURE_CONFIG_ID.toString(), undefined], - pattern_matching: "VariableLen", - models: [], - }, - }, ], }, }, @@ -378,5 +382,7 @@ const fetchHyperstructureData = async (client: ToriiClient, components: Componen [], 40_000, false, - ); + ).finally(() => { + setStructuresLoading(false); + }); };