From 5989d8e8f4feb6e261fddab6428317536e451402 Mon Sep 17 00:00:00 2001 From: tedison Date: Wed, 11 Dec 2024 21:19:11 -0500 Subject: [PATCH] do sync --- client/package.json | 3 +- client/src/dojo/queries.ts | 251 +++++++++++++++----------------- client/src/dojo/setup.ts | 64 ++++---- client/src/ui/layouts/World.tsx | 20 ++- pnpm-lock.yaml | 3 + 5 files changed, 171 insertions(+), 170 deletions(-) diff --git a/client/package.json b/client/package.json index d69af3fb3..30c37b8d0 100644 --- a/client/package.json +++ b/client/package.json @@ -20,6 +20,7 @@ "@cartridge/connector": "0.5.5", "@cartridge/controller": "0.5.5", "@dojoengine/core": "1.0.1", + "@dojoengine/torii-wasm": "1.0.1", "@dojoengine/create-burner": "1.0.1", "@dojoengine/react": "1.0.1", "@dojoengine/recs": "^2.0.13", @@ -104,4 +105,4 @@ "vitest": "^2.0.5", "workbox-window": "^7.3.0" } -} \ No newline at end of file +} diff --git a/client/src/dojo/queries.ts b/client/src/dojo/queries.ts index ae132c035..53d327a86 100644 --- a/client/src/dojo/queries.ts +++ b/client/src/dojo/queries.ts @@ -2,158 +2,149 @@ import { Component, Metadata, Schema } from "@dojoengine/recs"; import { setEntities } from "@dojoengine/state"; -import { Clause, EntityKeysClause, Subscription, ToriiClient } from "@dojoengine/torii-client"; +import { Clause, EntityKeysClause, PatternMatching, Subscription, ToriiClient } from "@dojoengine/torii-client"; // on hexception -> fetch below queries based on entityID -// background sync after load -> +// background sync after load -> export const getEntities = async ( - client: ToriiClient, - clause: Clause | undefined, - components: Component[], - limit: number = 100, - logging: boolean = false + client: ToriiClient, + clause: Clause | undefined, + components: Component[], + limit: number = 100, + logging: boolean = false, ) => { - if (logging) console.log("Starting getEntities"); - let offset = 0; - let continueFetching = true; + if (logging) console.log("Starting getEntities"); + let offset = 0; + let continueFetching = true; - while (continueFetching) { - const entities = await client.getEntities({ - limit, - offset, - clause, - dont_include_hashed_keys: false, - }); + while (continueFetching) { + const entities = await client.getEntities({ + limit, + offset, + clause, + dont_include_hashed_keys: false, + }); - console.log("entities", entities); + console.log("entities", entities); - if (logging) console.log(`Fetched ${entities} entities`); + if (logging) console.log(`Fetched ${entities} entities`); - setEntities(entities, components, logging); + setEntities(entities, components, logging); - if (Object.keys(entities).length < limit) { - continueFetching = false; - } else { - offset += limit; - } + if (Object.keys(entities).length < limit) { + continueFetching = false; + } else { + offset += limit; } + } }; - - export const syncEntitiesEternum = async ( - client: ToriiClient, - components: Component[], - entityKeyClause: EntityKeysClause[], - logging: boolean = true + client: ToriiClient, + components: Component[], + entityKeyClause: EntityKeysClause[], + logging: boolean = true, ) => { - if (logging) console.log("Starting syncEntities"); - return await client.onEntityUpdated( - entityKeyClause, - (fetchedEntities: any, data: any) => { - if (logging) console.log("Entity updated", fetchedEntities); - - setEntities({ [fetchedEntities]: data }, components, logging); - } - ); + if (logging) console.log("Starting syncEntities"); + return await client.onEntityUpdated(entityKeyClause, (fetchedEntities: any, data: any) => { + if (logging) console.log("Entity updated", fetchedEntities); + + setEntities({ [fetchedEntities]: data }, components, logging); + }); }; -export const addToSubscription = async (client: ToriiClient, subscription: Subscription, components: Component[], entityID: string, position?: {x: number, y: number}) => { - - console.log("position", subscription); - - await getEntities(client, { ...entityQueryOneKey(entityID), Keys: { ...entityQueryOneKey(entityID).Keys, pattern_matching: "FixedLen" } }, components, 1000, false); - - await getEntities(client, { ...entityQueryTwoKey(entityID), Keys: { ...entityQueryTwoKey(entityID).Keys, pattern_matching: "FixedLen" } }, components, 1000, false); - - await getEntities(client, { ...entityQueryThreeKey(entityID), Keys: { ...entityQueryThreeKey(entityID).Keys, pattern_matching: "FixedLen" } }, components, 1000, false); - - - await getEntities(client, { - Keys: { - keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined], - pattern_matching: 'FixedLen', - models: [ - ], - }, - }, components, 1000, false); - - // await client.updateEntitySubscription(subscription, [ - // { ...entityQueryOneKey(entityID), Keys: { ...entityQueryOneKey(entityID).Keys, pattern_matching: "FixedLen" } }, - // ]); - - // await client.updateEntitySubscription(subscription, [ - // { ...entityQueryTwoKey(entityID), Keys: { ...entityQueryTwoKey(entityID).Keys, pattern_matching: "FixedLen" } }, - // ]); - - // await client.updateEntitySubscription(subscription, [ - // { ...entityQueryThreeKey(entityID), Keys: { ...entityQueryThreeKey(entityID).Keys, pattern_matching: "FixedLen" } }, - // ]); - - try { - await client.updateEntitySubscription(subscription, [ - { ...entityQueryOneKey(entityID), Keys: { ...entityQueryOneKey(entityID).Keys, pattern_matching: "FixedLen" } }, - { ...entityQueryTwoKey(entityID), Keys: { ...entityQueryTwoKey(entityID).Keys, pattern_matching: "FixedLen" } }, - { ...entityQueryThreeKey(entityID), Keys: { ...entityQueryThreeKey(entityID).Keys, pattern_matching: "FixedLen" } }, - { - Keys: { - keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined], - pattern_matching: 'FixedLen', - models: [ - ], - }, - }, - ]); - } catch (error) { - console.error("Error updating entity subscription:", error); - } -} +export const addToSubscription = async ( + client: ToriiClient, + syncObject: { sync: Subscription; clauses: EntityKeysClause[] }, + components: Component[], + entityID: string, + position?: { x: number; y: number }, +) => { + console.log("position", syncObject); + + await getEntities(client, { ...(entityQueryOneKey(entityID) as Clause) }, components, 1000, false); + + await getEntities(client, { ...(entityQueryTwoKey(entityID) as Clause) }, components, 1000, false); + + await getEntities(client, { ...(entityQueryThreeKey(entityID) as Clause) }, components, 1000, false); + + await getEntities( + client, + { + Keys: { + keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined], + pattern_matching: "FixedLen", + models: [], + }, + }, + components, + 1000, + false, + ); + const positionClause: EntityKeysClause = { + Keys: { + keys: [String(position?.x || 0), String(position?.y || 0), undefined, undefined], + pattern_matching: "FixedLen" as PatternMatching, + models: [], + }, + }; + + const newSubscriptions = [ + { ...entityQueryOneKey(entityID) }, + { ...entityQueryTwoKey(entityID) }, + { ...entityQueryThreeKey(entityID) }, + { ...entityQueryFourKey(position?.x || 0, position?.y || 0) }, + positionClause, + ...syncObject.clauses, + ]; + + try { + await client.updateEntitySubscription(syncObject.sync, newSubscriptions); + } catch (error) { + console.log("error", error); + } + + syncObject.clauses = newSubscriptions; +}; const entityQueryOneKey = (entityID: string) => { - return { - Keys: { - keys: [entityID], - pattern_matching: 'FixedLen', - models: [], - }, - } -} + return { + Keys: { + keys: [entityID], + pattern_matching: "FixedLen", + models: [], + }, + } as EntityKeysClause; +}; const entityQueryTwoKey = (entityID: string) => { - return { - Keys: { - keys: [entityID, undefined], - pattern_matching: 'FixedLen', - models: [ - ], - }, - } -} + return { + Keys: { + keys: [entityID, undefined], + pattern_matching: "FixedLen", + models: [], + }, + } as EntityKeysClause; +}; const entityQueryThreeKey = (entityID: string) => { - return { - Keys: { - keys: [entityID, undefined, undefined], - pattern_matching: 'FixedLen', - models: [ - ], - }, - } -} - -const entityQueryFourKey = ( x: number, y: number) => { - return { - Keys: { - keys: [String(x), String(y), undefined, undefined], - pattern_matching: 'FixedLen', - models: [ - ], - }, - } -} - - - + return { + Keys: { + keys: [entityID, undefined, undefined], + pattern_matching: "FixedLen", + models: [], + }, + } as EntityKeysClause; +}; +const entityQueryFourKey = (x: number, y: number) => { + return { + Keys: { + keys: [String(x), String(y), undefined, undefined], + pattern_matching: "FixedLen", + models: [], + }, + } as EntityKeysClause; +}; diff --git a/client/src/dojo/setup.ts b/client/src/dojo/setup.ts index 0a7156289..24db060dd 100644 --- a/client/src/dojo/setup.ts +++ b/client/src/dojo/setup.ts @@ -1,6 +1,7 @@ import { WORLD_CONFIG_ID } from "@bibliothecadao/eternum"; import { DojoConfig } from "@dojoengine/core"; import { getSyncEntities, getSyncEvents } from "@dojoengine/state"; +import { Clause } from "@dojoengine/torii-client"; import { createClientComponents } from "./createClientComponents"; import { createSystemCalls } from "./createSystemCalls"; import { ClientConfigManager } from "./modelManager/ConfigManager"; @@ -118,44 +119,45 @@ export async function setup({ ...config }: DojoConfig) { }, }; - // fetch all existing entities from torii - const sync = await getSyncEntities( - network.toriiClient, - network.contractComponents as any, - + const clauses: Clause[] = [ + { + Keys: { + keys: [undefined], + pattern_matching: "FixedLen", + models: [], + }, + }, + { + Keys: { + keys: [WORLD_CONFIG_ID.toString(), undefined], + pattern_matching: "VariableLen", + models: [], + }, + }, { - Composite: { - operator: "Or", - clauses: [ - { - Keys: { - keys: [undefined], - pattern_matching: "FixedLen", - models: [], - }, - }, - { - Keys: { - keys: [WORLD_CONFIG_ID.toString(), undefined], - pattern_matching: "VariableLen", - models: [], - }, - }, - { - Keys: { - keys: [WORLD_CONFIG_ID.toString()], - pattern_matching: "VariableLen", - models: [], - }, - }, - ], + Keys: { + keys: [WORLD_CONFIG_ID.toString()], + pattern_matching: "VariableLen", + models: [], }, }, + ]; + // fetch all existing entities from torii + const sync = await getSyncEntities( + network.toriiClient, + network.contractComponents as any, + { Composite: { operator: "Or", clauses: [...clauses] } }, [], 1000, + true, ); + const syncObject = { + sync, + clauses: [...clauses], + }; + const eventSync = getSyncEvents( network.toriiClient, network.contractComponents.events as any, @@ -172,7 +174,7 @@ export async function setup({ ...config }: DojoConfig) { network, components, systemCalls, - sync, + syncObject, eventSync, }; } diff --git a/client/src/ui/layouts/World.tsx b/client/src/ui/layouts/World.tsx index 8bfbee1c9..dcf279a95 100644 --- a/client/src/ui/layouts/World.tsx +++ b/client/src/ui/layouts/World.tsx @@ -9,11 +9,12 @@ import { useStructureEntityId } from "@/hooks/helpers/useStructureEntityId"; import { useFetchBlockchainData } from "@/hooks/store/useBlockchainStore"; import { useWorldStore } from "@/hooks/store/useWorldLoading"; import { useComponentValue } from "@dojoengine/react"; +import { EntityKeysClause, Subscription } from "@dojoengine/torii-client"; import { getEntityIdFromKeys } from "@dojoengine/utils"; import { env } from "../../../env"; import { IS_MOBILE } from "../config"; -import { LoadingScreen } from "../modules/LoadingScreen"; import { LoadingOroborus } from "../modules/loading-oroborus"; +import { LoadingScreen } from "../modules/LoadingScreen"; // Lazy load components const SelectedArmy = lazy(() => @@ -83,8 +84,6 @@ const MiniMapNavigation = lazy(() => import("../modules/navigation/MiniMapNavigation").then((module) => ({ default: module.MiniMapNavigation })), ); - - export const World = ({ backgroundImage }: { backgroundImage: string }) => { const showBlankOverlay = useUIStore((state) => state.showBlankOverlay); const isLoadingScreenEnabled = useUIStore((state) => state.isLoadingScreenEnabled); @@ -105,14 +104,19 @@ export const World = ({ backgroundImage }: { backgroundImage: string }) => { const dojo = useDojo(); const structureEntityId = useUIStore((state) => state.structureEntityId); - const position = useComponentValue(dojo.setup.components.Position, getEntityIdFromKeys([BigInt(structureEntityId)])) + const position = useComponentValue(dojo.setup.components.Position, getEntityIdFromKeys([BigInt(structureEntityId)])); useEffect(() => { - const fetch = async () => { - await addToSubscription(dojo.setup.network.toriiClient, dojo.setup.sync, dojo.setup.network.contractComponents as any, structureEntityId.toString(), {x: position?.x || 0, y: position?.y || 0}); - } setWorldLoading(true); - console.log("world loading", worldLoading); + const fetch = async () => { + await addToSubscription( + dojo.setup.network.toriiClient, + dojo.setup.syncObject as { sync: Subscription; clauses: EntityKeysClause[] }, + dojo.setup.network.contractComponents as any, + structureEntityId.toString(), + { x: position?.x || 0, y: position?.y || 0 }, + ); + }; fetch(); console.log("world loading", worldLoading); setWorldLoading(false); diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 033831bc9..0c237c5ab 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -154,6 +154,9 @@ importers: '@dojoengine/torii-client': specifier: 1.0.1 version: 1.0.1 + '@dojoengine/torii-wasm': + specifier: 1.0.1 + version: 1.0.1 '@dojoengine/utils': specifier: 1.0.1 version: 1.0.1(starknet@6.11.0(encoding@0.1.13))(typescript@5.6.3)(zod@3.23.8)