Skip to content

Commit e342815

Browse files
Merge pull request #252 from dojoengine/fix-entity-sync
fix: entity sync
2 parents 5f81899 + f047fe3 commit e342815

File tree

8 files changed

+112
-120
lines changed

8 files changed

+112
-120
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ jobs:
2020
run: git submodule update --init --recursive
2121

2222
- run: curl -L https://install.dojoengine.org | bash
23-
- run: /home/runner/.config/.dojo/bin/dojoup -v v1.0.0-alpha.3
23+
- run: /home/runner/.config/.dojo/bin/dojoup -v v1.0.0-alpha.5
2424
- run: |
2525
cd examples/dojo-starter
2626
/home/runner/.config/.dojo/bin/sozo build

examples/react/react-app/src/App.tsx

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,7 @@ function App() {
1717
account,
1818
} = useDojo();
1919

20-
useQuerySync(toriiClient, contractComponents as any, [
21-
{
22-
Keys: {
23-
keys: [BigInt(account?.account.address).toString()],
24-
models: [
25-
"dojo_starter-Position",
26-
"dojo_starter-Moves",
27-
"dojo_starter-DirectionsAvailable",
28-
],
29-
pattern_matching: "FixedLen",
30-
},
31-
},
32-
]);
20+
useQuerySync(toriiClient, contractComponents as any, []);
3321

3422
const [clipboardStatus, setClipboardStatus] = useState({
3523
message: "",

examples/react/react-app/src/dojo/createSystemCalls.ts

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -82,28 +82,28 @@ export function createSystemCalls(
8282
]) as Entity;
8383

8484
// Update the state before the transaction
85-
const positionId = uuid();
86-
Position.addOverride(positionId, {
87-
entity: entityId,
88-
value: {
89-
player: BigInt(entityId),
90-
vec: updatePositionWithDirection(
91-
direction,
92-
getComponentValue(Position, entityId) as any
93-
).vec,
94-
},
95-
});
85+
// const positionId = uuid();
86+
// Position.addOverride(positionId, {
87+
// entity: entityId,
88+
// value: {
89+
// player: BigInt(entityId),
90+
// vec: updatePositionWithDirection(
91+
// direction,
92+
// getComponentValue(Position, entityId) as any
93+
// ).vec,
94+
// },
95+
// });
9696

97-
// Update the state before the transaction
98-
const movesId = uuid();
99-
Moves.addOverride(movesId, {
100-
entity: entityId,
101-
value: {
102-
player: BigInt(entityId),
103-
remaining:
104-
(getComponentValue(Moves, entityId)?.remaining || 0) - 1,
105-
},
106-
});
97+
// // Update the state before the transaction
98+
// const movesId = uuid();
99+
// Moves.addOverride(movesId, {
100+
// entity: entityId,
101+
// value: {
102+
// player: BigInt(entityId),
103+
// remaining:
104+
// (getComponentValue(Moves, entityId)?.remaining || 0) - 1,
105+
// },
106+
// });
107107

108108
try {
109109
await client.actions.move({
@@ -127,11 +127,11 @@ export function createSystemCalls(
127127
});
128128
} catch (e) {
129129
console.log(e);
130-
Position.removeOverride(positionId);
131-
Moves.removeOverride(movesId);
130+
// Position.removeOverride(positionId);
131+
// Moves.removeOverride(movesId);
132132
} finally {
133-
Position.removeOverride(positionId);
134-
Moves.removeOverride(movesId);
133+
// Position.removeOverride(positionId);
134+
// Moves.removeOverride(movesId);
135135
}
136136
};
137137

examples/react/react-app/src/dojo/generated/setup.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,5 @@ export async function setup({ ...config }: DojoConfig) {
7272
dojoProvider,
7373
burnerManager,
7474
toriiClient,
75-
// sync,
7675
};
7776
}

packages/react/src/useQuerySync.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Component, Metadata, Schema } from "@dojoengine/recs";
22
import { useCallback, useEffect } from "react";
33
import {
4-
Client,
4+
ToriiClient,
55
EntityKeysClause,
66
Subscription,
77
} from "@dojoengine/torii-client";
@@ -28,7 +28,7 @@ import { getSyncEntities } from "@dojoengine/state";
2828
* ]);
2929
*/
3030
export function useQuerySync<S extends Schema>(
31-
toriiClient: Client,
31+
toriiClient: ToriiClient,
3232
components: Component<S, Metadata, undefined>[],
3333
entityKeyClause: EntityKeysClause[]
3434
) {

packages/state/src/recs/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
} from "@dojoengine/recs";
99
import {
1010
Clause,
11-
Client,
11+
ToriiClient,
1212
EntityKeysClause,
1313
PatternMatching,
1414
} from "@dojoengine/torii-client";
@@ -40,7 +40,7 @@ import { convertValues } from "../utils";
4040
* the batch size of each request.
4141
*/
4242
export const getSyncEntities = async <S extends Schema>(
43-
client: Client,
43+
client: ToriiClient,
4444
components: Component<S, Metadata, undefined>[],
4545
entityKeyClause: EntityKeysClause[],
4646
limit: number = 100
@@ -62,7 +62,7 @@ export const getSyncEntities = async <S extends Schema>(
6262
* This function performs paginated queries to fetch all entities and their components.
6363
*/
6464
export const getEntities = async <S extends Schema>(
65-
client: Client,
65+
client: ToriiClient,
6666
components: Component<S, Metadata, undefined>[],
6767
limit: number = 100
6868
) => {
@@ -109,7 +109,7 @@ export const getEntities = async <S extends Schema>(
109109
* to control the batch size of each request.
110110
*/
111111
export const getEntitiesQuery = async <S extends Schema>(
112-
client: Client,
112+
client: ToriiClient,
113113
components: Component<S, Metadata, undefined>[],
114114
entityKeyClause: EntityKeysClause,
115115
patternMatching: PatternMatching = "FixedLen",
@@ -137,7 +137,7 @@ export const getEntitiesQuery = async <S extends Schema>(
137137
const fetchedEntities = await client.getEntities({
138138
limit,
139139
offset: cursor,
140-
clause,
140+
clause: clause || undefined,
141141
});
142142

143143
setEntities(fetchedEntities, components);
@@ -163,14 +163,14 @@ export const getEntitiesQuery = async <S extends Schema>(
163163
* sync.cancel(); // cancel the subscription
164164
*/
165165
export const syncEntities = async <S extends Schema>(
166-
client: Client,
166+
client: ToriiClient,
167167
components: Component<S, Metadata, undefined>[],
168168
entityKeyClause: EntityKeysClause[]
169169
) => {
170170
return await client.onEntityUpdated(
171171
entityKeyClause,
172-
(fetchedEntities: any) => {
173-
setEntities(fetchedEntities, components);
172+
(fetchedEntities: any, data: any) => {
173+
setEntities({ [fetchedEntities]: data }, components);
174174
}
175175
);
176176
};

packages/state/src/utils/index.ts

Lines changed: 74 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,88 @@
1-
import { Type as RecsType, Schema, ComponentValue } from "@dojoengine/recs";
1+
import { Type as RecsType, Schema } from "@dojoengine/recs";
22

3-
export function convertValues(schema: Schema, values: any): ComponentValue {
4-
return Object.keys(schema).reduce<any>(
5-
(acc: ComponentValue, key: string) => {
6-
if (!acc) {
7-
acc = {}; // Ensure acc is initialized
8-
}
9-
const schemaType = schema[key];
10-
const value = values[key];
3+
export function convertValues(schema: Schema, values: any) {
4+
return Object.keys(schema).reduce<any>((acc, key) => {
5+
if (!acc) {
6+
acc = {};
7+
}
8+
const schemaType = schema[key];
9+
const value = values[key];
1110

12-
if (value === null || value === undefined) {
13-
acc[key] = value;
14-
return acc;
15-
}
11+
if (value === null || value === undefined) {
12+
acc[key] = value;
13+
return acc;
14+
}
1615

17-
if (value.type === "enum") {
18-
acc[key] = value.value.option;
19-
return acc;
20-
}
16+
if (value.type === "enum") {
17+
acc[key] = value.value.option;
18+
return acc;
19+
}
2120

22-
switch (schemaType) {
23-
case RecsType.StringArray:
24-
if (
25-
value.type === "array" &&
26-
value.value[0].type === "enum"
27-
) {
28-
acc[key] = value.value.map(
29-
(item: any) => item.value.option
30-
);
31-
} else {
32-
acc[key] = value.value.map((a: any) => {
33-
try {
34-
return BigInt(a.value);
35-
} catch (error) {
36-
console.warn(
37-
`Failed to convert ${a.value} to BigInt. Using string value instead.`
38-
);
39-
return a.value;
40-
}
41-
});
42-
}
43-
break;
21+
switch (schemaType) {
22+
case RecsType.StringArray:
23+
if (value.type === "array" && value.value[0].type === "enum") {
24+
acc[key] = value.value.map(
25+
(item: any) => item.value.option
26+
);
27+
} else {
28+
acc[key] = value.value.map((a: any) => {
29+
try {
30+
return BigInt(a.value);
31+
} catch (error) {
32+
console.warn(
33+
`Failed to convert ${a.value} to BigInt. Using string value instead.`
34+
);
35+
return a.value;
36+
}
37+
});
38+
}
39+
break;
4440

45-
case RecsType.String:
46-
acc[key] = value.value;
47-
break;
41+
case RecsType.String:
42+
acc[key] = value.value;
43+
break;
4844

49-
case RecsType.BigInt:
50-
try {
51-
acc[key] = BigInt(value.value);
52-
} catch (error) {
53-
console.warn(
54-
`Failed to convert ${value.value} to BigInt. Using string value instead.`
55-
);
45+
case RecsType.BigInt:
46+
try {
47+
acc[key] = BigInt(value.value);
48+
} catch (error) {
49+
console.warn(
50+
`Failed to convert ${value.value} to BigInt. Using string value instead.`
51+
);
5652

57-
acc[key] = BigInt(`0x${value.value}`);
58-
}
59-
break;
53+
acc[key] = BigInt(`0x${value.value}`);
54+
}
55+
break;
6056

61-
case RecsType.Boolean:
62-
acc[key] = value.value;
63-
break;
57+
case RecsType.Boolean:
58+
acc[key] = value.value;
59+
break;
6460

65-
case RecsType.Number:
66-
acc[key] = Number(value.value);
67-
break;
61+
case RecsType.Number:
62+
acc[key] = Number(value.value);
63+
break;
6864

69-
default:
70-
if (
71-
typeof schemaType === "object" &&
72-
typeof value === "object"
73-
) {
65+
default:
66+
if (typeof schemaType === "object" && value.type === "struct") {
67+
if (value.value instanceof Map) {
68+
const structValues = Object.fromEntries(value.value);
69+
acc[key] = convertValues(schemaType, structValues);
70+
} else {
7471
acc[key] = convertValues(schemaType, value.value);
7572
}
76-
break;
77-
}
73+
} else if (
74+
Array.isArray(schemaType) &&
75+
value.type === "array"
76+
) {
77+
acc[key] = value.value.map((item: any) =>
78+
convertValues(schemaType[0], item)
79+
);
80+
} else {
81+
acc[key] = value.value;
82+
}
83+
break;
84+
}
7885

79-
return acc;
80-
},
81-
{}
82-
);
86+
return acc;
87+
}, {});
8388
}

0 commit comments

Comments
 (0)