From b0b4961f493dd1ac8aafab15d59c78e8e46dc356 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Tue, 13 Aug 2024 11:57:59 -0400 Subject: [PATCH 1/3] fix: sync --- examples/dojo-starter | 2 +- examples/react/react-app/src/App.tsx | 14 +----- .../react-app/src/dojo/createSystemCalls.ts | 50 +++++++++---------- .../react-app/src/dojo/generated/setup.ts | 1 - packages/react/src/useQuerySync.ts | 4 +- packages/state/src/recs/index.ts | 17 +++---- packages/state/src/utils/index.ts | 24 +++++++-- 7 files changed, 57 insertions(+), 55 deletions(-) diff --git a/examples/dojo-starter b/examples/dojo-starter index 7c01064e..5887882f 160000 --- a/examples/dojo-starter +++ b/examples/dojo-starter @@ -1 +1 @@ -Subproject commit 7c01064e352a5798848090a150a3384409d336ba +Subproject commit 5887882fede43a6805f45869b36a3e3fe2da0a44 diff --git a/examples/react/react-app/src/App.tsx b/examples/react/react-app/src/App.tsx index fd523d5a..da43171f 100644 --- a/examples/react/react-app/src/App.tsx +++ b/examples/react/react-app/src/App.tsx @@ -17,19 +17,7 @@ function App() { account, } = useDojo(); - useQuerySync(toriiClient, contractComponents as any, [ - { - Keys: { - keys: [BigInt(account?.account.address).toString()], - models: [ - "dojo_starter-Position", - "dojo_starter-Moves", - "dojo_starter-DirectionsAvailable", - ], - pattern_matching: "FixedLen", - }, - }, - ]); + useQuerySync(toriiClient, contractComponents as any, []); const [clipboardStatus, setClipboardStatus] = useState({ message: "", diff --git a/examples/react/react-app/src/dojo/createSystemCalls.ts b/examples/react/react-app/src/dojo/createSystemCalls.ts index 71825390..4240ba17 100644 --- a/examples/react/react-app/src/dojo/createSystemCalls.ts +++ b/examples/react/react-app/src/dojo/createSystemCalls.ts @@ -82,28 +82,28 @@ export function createSystemCalls( ]) as Entity; // Update the state before the transaction - const positionId = uuid(); - Position.addOverride(positionId, { - entity: entityId, - value: { - player: BigInt(entityId), - vec: updatePositionWithDirection( - direction, - getComponentValue(Position, entityId) as any - ).vec, - }, - }); + // const positionId = uuid(); + // Position.addOverride(positionId, { + // entity: entityId, + // value: { + // player: BigInt(entityId), + // vec: updatePositionWithDirection( + // direction, + // getComponentValue(Position, entityId) as any + // ).vec, + // }, + // }); - // Update the state before the transaction - const movesId = uuid(); - Moves.addOverride(movesId, { - entity: entityId, - value: { - player: BigInt(entityId), - remaining: - (getComponentValue(Moves, entityId)?.remaining || 0) - 1, - }, - }); + // // Update the state before the transaction + // const movesId = uuid(); + // Moves.addOverride(movesId, { + // entity: entityId, + // value: { + // player: BigInt(entityId), + // remaining: + // (getComponentValue(Moves, entityId)?.remaining || 0) - 1, + // }, + // }); try { await client.actions.move({ @@ -127,11 +127,11 @@ export function createSystemCalls( }); } catch (e) { console.log(e); - Position.removeOverride(positionId); - Moves.removeOverride(movesId); + // Position.removeOverride(positionId); + // Moves.removeOverride(movesId); } finally { - Position.removeOverride(positionId); - Moves.removeOverride(movesId); + // Position.removeOverride(positionId); + // Moves.removeOverride(movesId); } }; diff --git a/examples/react/react-app/src/dojo/generated/setup.ts b/examples/react/react-app/src/dojo/generated/setup.ts index f4c1f093..98c64040 100644 --- a/examples/react/react-app/src/dojo/generated/setup.ts +++ b/examples/react/react-app/src/dojo/generated/setup.ts @@ -72,6 +72,5 @@ export async function setup({ ...config }: DojoConfig) { dojoProvider, burnerManager, toriiClient, - // sync, }; } diff --git a/packages/react/src/useQuerySync.ts b/packages/react/src/useQuerySync.ts index 3eca4fcd..19f79faf 100644 --- a/packages/react/src/useQuerySync.ts +++ b/packages/react/src/useQuerySync.ts @@ -1,7 +1,7 @@ import { Component, Metadata, Schema } from "@dojoengine/recs"; import { useCallback, useEffect } from "react"; import { - Client, + ToriiClient, EntityKeysClause, Subscription, } from "@dojoengine/torii-client"; @@ -28,7 +28,7 @@ import { getSyncEntities } from "@dojoengine/state"; * ]); */ export function useQuerySync( - toriiClient: Client, + toriiClient: ToriiClient, components: Component[], entityKeyClause: EntityKeysClause[] ) { diff --git a/packages/state/src/recs/index.ts b/packages/state/src/recs/index.ts index 5f0ea20f..c4858bb4 100644 --- a/packages/state/src/recs/index.ts +++ b/packages/state/src/recs/index.ts @@ -8,7 +8,7 @@ import { } from "@dojoengine/recs"; import { Clause, - Client, + ToriiClient, EntityKeysClause, PatternMatching, } from "@dojoengine/torii-client"; @@ -40,7 +40,7 @@ import { convertValues } from "../utils"; * the batch size of each request. */ export const getSyncEntities = async ( - client: Client, + client: ToriiClient, components: Component[], entityKeyClause: EntityKeysClause[], limit: number = 100 @@ -62,7 +62,7 @@ export const getSyncEntities = async ( * This function performs paginated queries to fetch all entities and their components. */ export const getEntities = async ( - client: Client, + client: ToriiClient, components: Component[], limit: number = 100 ) => { @@ -109,7 +109,7 @@ export const getEntities = async ( * to control the batch size of each request. */ export const getEntitiesQuery = async ( - client: Client, + client: ToriiClient, components: Component[], entityKeyClause: EntityKeysClause, patternMatching: PatternMatching = "FixedLen", @@ -137,7 +137,7 @@ export const getEntitiesQuery = async ( const fetchedEntities = await client.getEntities({ limit, offset: cursor, - clause, + clause: clause || undefined, }); setEntities(fetchedEntities, components); @@ -163,14 +163,14 @@ export const getEntitiesQuery = async ( * sync.cancel(); // cancel the subscription */ export const syncEntities = async ( - client: Client, + client: ToriiClient, components: Component[], entityKeyClause: EntityKeysClause[] ) => { return await client.onEntityUpdated( entityKeyClause, - (fetchedEntities: any) => { - setEntities(fetchedEntities, components); + (fetchedEntities: any, data: any) => { + setEntities({ [fetchedEntities]: data }, components); } ); }; @@ -184,7 +184,6 @@ export const setEntities = async ( entities: any, components: Component[] ) => { - console.log(entities, components); for (let key in entities) { if (entities.hasOwnProperty(key)) { for (let componentName in entities[key]) { diff --git a/packages/state/src/utils/index.ts b/packages/state/src/utils/index.ts index 96a87e1e..850c0550 100644 --- a/packages/state/src/utils/index.ts +++ b/packages/state/src/utils/index.ts @@ -18,6 +18,11 @@ export function convertValues(schema: Schema, values: any) { return acc; } + // if (value.type === "struct") { + // acc[key] = convertValues(schemaType, value.value); + // return acc; + // } + switch (schemaType) { case RecsType.StringArray: if (value.type === "array" && value.value[0].type === "enum") { @@ -63,11 +68,22 @@ export function convertValues(schema: Schema, values: any) { break; default: - if ( - typeof schemaType === "object" && - typeof value === "object" + if (typeof schemaType === "object" && value.type === "struct") { + if (value.value instanceof Map) { + const structValues = Object.fromEntries(value.value); + acc[key] = convertValues(schemaType, structValues); + } else { + acc[key] = convertValues(schemaType, value.value); + } + } else if ( + Array.isArray(schemaType) && + value.type === "array" ) { - acc[key] = convertValues(schemaType, value.value); + acc[key] = value.value.map((item: any) => + convertValues(schemaType[0], item) + ); + } else { + acc[key] = value.value; } break; } From 4352c6772bc2a846e8af363c411207926c2c3ee3 Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Tue, 13 Aug 2024 11:58:18 -0400 Subject: [PATCH 2/3] feat: ci --- .github/workflows/ci.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 979b8cb1..888f4540 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,7 +20,7 @@ jobs: run: git submodule update --init --recursive - run: curl -L https://install.dojoengine.org | bash - - run: /home/runner/.config/.dojo/bin/dojoup -v v1.0.0-alpha.3 + - run: /home/runner/.config/.dojo/bin/dojoup -v v1.0.0-alpha.5 - run: | cd examples/dojo-starter /home/runner/.config/.dojo/bin/sozo build From bf1b654e43df42a19b179459f377d0e7295abcaa Mon Sep 17 00:00:00 2001 From: ponderingdemocritus Date: Tue, 13 Aug 2024 12:07:40 -0400 Subject: [PATCH 3/3] feat: submodule --- examples/dojo-starter | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/dojo-starter b/examples/dojo-starter index 5887882f..beaf9a95 160000 --- a/examples/dojo-starter +++ b/examples/dojo-starter @@ -1 +1 @@ -Subproject commit 5887882fede43a6805f45869b36a3e3fe2da0a44 +Subproject commit beaf9a95206dc9da3868c185cb3274a7df013547