Skip to content

Commit 026e7f4

Browse files
Merge pull request #264 from dojoengine/fix/sync
fix: sync
2 parents c656e95 + 7c03d77 commit 026e7f4

File tree

80 files changed

+2210
-1572
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

80 files changed

+2210
-1572
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.6
23+
- run: /home/runner/.config/.dojo/bin/dojoup -v v1.0.0-alpha.9
2424
- run: |
2525
cd examples/dojo/dojo-starter
2626
/home/runner/.config/.dojo/bin/sozo build

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import "./App.css";
22
import { useComponentValue, useQuerySync } from "@dojoengine/react";
33
import { Entity } from "@dojoengine/recs";
44
import { useEffect, useState } from "react";
5-
import { Direction } from "./utils";
65
import { getEntityIdFromKeys } from "@dojoengine/utils";
76
import { useDojo } from "./dojo/useDojo";
87

@@ -17,6 +16,7 @@ function App() {
1716
account,
1817
} = useDojo();
1918

19+
// sync entities
2020
useQuerySync(toriiClient, contractComponents as any, []);
2121

2222
const [clipboardStatus, setClipboardStatus] = useState({
@@ -139,7 +139,7 @@ function App() {
139139
<button
140140
onClick={() =>
141141
position && position.vec.y > 0
142-
? move(account.account, Direction.Up)
142+
? move(account.account, { type: "Up" })
143143
: console.log("Reach the borders of the world.")
144144
}
145145
>
@@ -150,21 +150,21 @@ function App() {
150150
<button
151151
onClick={() =>
152152
position && position.vec.x > 0
153-
? move(account.account, Direction.Left)
153+
? move(account.account, { type: "Left" })
154154
: console.log("Reach the borders of the world.")
155155
}
156156
>
157157
Move Left
158158
</button>
159159
<button
160-
onClick={() => move(account.account, Direction.Right)}
160+
onClick={() => move(account.account, { type: "Right" })}
161161
>
162162
Move Right
163163
</button>
164164
</div>
165165
<div>
166166
<button
167-
onClick={() => move(account.account, Direction.Down)}
167+
onClick={() => move(account.account, { type: "Down" })}
168168
>
169169
Move Down
170170
</button>

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

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Account, AccountInterface } from "starknet";
1+
import { Account } from "starknet";
22
import {
33
Entity,
44
Has,
@@ -9,9 +9,9 @@ import {
99
} from "@dojoengine/recs";
1010
import { uuid } from "@latticexyz/utils";
1111
import { ClientComponents } from "./createClientComponents";
12-
import { Direction, updatePositionWithDirection } from "../utils";
1312
import { getEntityIdFromKeys } from "@dojoengine/utils";
1413
import type { IWorld } from "./typescript/contracts.gen";
14+
import { Direction } from "./typescript/models.gen";
1515

1616
export type SystemCalls = ReturnType<typeof createSystemCalls>;
1717

@@ -77,38 +77,10 @@ export function createSystemCalls(
7777
};
7878

7979
const move = async (account: Account, direction: Direction) => {
80-
const entityId = getEntityIdFromKeys([
81-
BigInt(account.address),
82-
]) as Entity;
83-
84-
// 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-
// });
96-
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-
// });
107-
10880
try {
10981
await client.actions.move({
11082
account,
111-
direction: { type: "Left" },
83+
direction,
11284
});
11385

11486
// Wait for the indexer to update the entity
@@ -127,11 +99,6 @@ export function createSystemCalls(
12799
});
128100
} catch (e) {
129101
console.log(e);
130-
// Position.removeOverride(positionId);
131-
// Moves.removeOverride(movesId);
132-
} finally {
133-
// Position.removeOverride(positionId);
134-
// Moves.removeOverride(movesId);
135102
}
136103
};
137104

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { world } from "./world";
77
import { setupWorld } from "./typescript/contracts.gen";
88
import { Account, ArraySignatureType } from "starknet";
99
import { BurnerManager } from "@dojoengine/create-burner";
10-
import { getSyncEvents } from "@dojoengine/state";
10+
import { getSyncEvents, getSyncEntities } from "@dojoengine/state";
1111

1212
export type SetupResult = Awaited<ReturnType<typeof setup>>;
1313

@@ -29,13 +29,21 @@ export async function setup({ ...config }: DojoConfig) {
2929
// create dojo provider
3030
const dojoProvider = new DojoProvider(config.manifest, config.rpcUrl);
3131

32+
// Sync all events
3233
const eventSync = getSyncEvents(
3334
toriiClient,
3435
contractComponents as any,
3536
undefined,
3637
[]
3738
);
3839

40+
// Sync all entities
41+
const sync = await getSyncEntities(
42+
toriiClient,
43+
contractComponents as any,
44+
[]
45+
);
46+
3947
// setup world
4048
const client = await setupWorld(dojoProvider);
4149

@@ -75,5 +83,6 @@ export async function setup({ ...config }: DojoConfig) {
7583
burnerManager,
7684
toriiClient,
7785
eventSync,
86+
sync,
7887
};
7988
}

examples/clients/react/react-app/src/dojo/typescript/contracts.gen.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Generated by dojo-bindgen on Thu, 22 Aug 2024 20:04:33 +0000. Do not modify this file manually.
22
// Import the necessary types from the recs SDK
33
// generate again with `sozo build --typescript`
4-
import { Account, byteArray } from "starknet";
4+
import { Account } from "starknet";
55
import { DojoProvider } from "@dojoengine/core";
66
import * as models from "./models.gen";
77

examples/clients/react/react-app/tsconfig.json

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
11
{
2+
"extends": "../../../../tsconfig.base.json",
23
"compilerOptions": {
3-
"target": "ES2020",
4-
"useDefineForClassFields": true,
5-
"lib": ["ES2020", "DOM", "DOM.Iterable"],
6-
"module": "ESNext",
7-
"skipLibCheck": true,
8-
"moduleResolution": "node",
9-
"allowImportingTsExtensions": true,
10-
"resolveJsonModule": true,
11-
"isolatedModules": true,
12-
"noEmit": true,
13-
"jsx": "react-jsx",
14-
"strict": true,
15-
// "noUnusedLocals": true,
16-
// "noUnusedParameters": true,
17-
"noFallthroughCasesInSwitch": true,
18-
"allowSyntheticDefaultImports": true
4+
"jsx": "react-jsx"
195
},
206
"include": ["src", "dojoConfig.ts"],
217
"references": [

examples/clients/react/react-phaser-example/src/dojo/contractComponents.ts

Lines changed: 0 additions & 41 deletions
This file was deleted.

examples/clients/react/react-phaser-example/src/dojo/createClientComponents.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { overridableComponent } from "@dojoengine/recs";
2-
import { ContractComponents } from "./generated/contractComponents";
2+
import { ContractComponents } from "./typescript/models.gen";
33

44
export type ClientComponents = ReturnType<typeof createClientComponents>;
55

examples/clients/react/react-phaser-example/src/dojo/createNetworkLayer.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { world as recsWorld } from "./world";
2-
import { setup } from "./generated/setup";
2+
33
import { dojoConfig } from "../../dojoConfig";
44
import { createBurner } from "./createBurner";
55
import { Account } from "starknet";
6+
import { setup } from "./setup";
67

78
export type NetworkLayer = Awaited<ReturnType<typeof createNetworkLayer>>;
89

examples/clients/react/react-phaser-example/src/dojo/createSystemCalls.ts

Lines changed: 78 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,102 @@
1-
import { AccountInterface } from "starknet";
1+
import { Account, AccountInterface } from "starknet";
2+
import {
3+
Entity,
4+
Has,
5+
HasValue,
6+
World,
7+
defineSystem,
8+
getComponentValue,
9+
} from "@dojoengine/recs";
10+
import { uuid } from "@latticexyz/utils";
211
import { ClientComponents } from "./createClientComponents";
3-
import { Direction } from "./utils";
4-
import { ContractComponents } from "./generated/contractComponents";
5-
import type { IWorld } from "./generated/generated";
12+
import { getEntityIdFromKeys } from "@dojoengine/utils";
13+
import type { IWorld } from "./typescript/contracts.gen";
14+
import { Direction } from "./typescript/models.gen";
615

716
export type SystemCalls = ReturnType<typeof createSystemCalls>;
817

918
export function createSystemCalls(
1019
{ client }: { client: IWorld },
11-
contractComponents: ContractComponents,
12-
{ Position, Moves }: ClientComponents
20+
{ Position, Moves }: ClientComponents,
21+
world: World
1322
) {
14-
const spawn = async (account: AccountInterface) => {
23+
const spawn = async (account: Account) => {
24+
const entityId = getEntityIdFromKeys([
25+
BigInt(account.address),
26+
]) as Entity;
27+
28+
const movesId = uuid();
29+
Moves.addOverride(movesId, {
30+
entity: entityId,
31+
value: {
32+
player: BigInt(entityId),
33+
remaining:
34+
(getComponentValue(Moves, entityId)?.remaining || 0) + 100,
35+
},
36+
});
37+
38+
const positionId = uuid();
39+
Position.addOverride(positionId, {
40+
entity: entityId,
41+
value: {
42+
player: BigInt(entityId),
43+
vec: {
44+
x: 10 + (getComponentValue(Position, entityId)?.vec.x || 0),
45+
y: 10 + (getComponentValue(Position, entityId)?.vec.y || 0),
46+
},
47+
},
48+
});
49+
1550
try {
1651
await client.actions.spawn({
1752
account,
1853
});
54+
55+
// Wait for the indexer to update the entity
56+
// By doing this we keep the optimistic UI in sync with the actual state
57+
await new Promise<void>((resolve) => {
58+
defineSystem(
59+
world,
60+
[
61+
Has(Moves),
62+
HasValue(Moves, { player: BigInt(account.address) }),
63+
],
64+
() => {
65+
resolve();
66+
}
67+
);
68+
});
1969
} catch (e) {
2070
console.log(e);
71+
Position.removeOverride(positionId);
72+
Moves.removeOverride(movesId);
73+
} finally {
74+
Position.removeOverride(positionId);
75+
Moves.removeOverride(movesId);
2176
}
2277
};
2378

24-
const move = async (account: AccountInterface, direction: Direction) => {
79+
const move = async (account: Account, direction: Direction) => {
2580
try {
2681
await client.actions.move({
2782
account,
2883
direction,
2984
});
85+
86+
// Wait for the indexer to update the entity
87+
// By doing this we keep the optimistic UI in sync with the actual state
88+
await new Promise<void>((resolve) => {
89+
defineSystem(
90+
world,
91+
[
92+
Has(Moves),
93+
HasValue(Moves, { player: BigInt(account.address) }),
94+
],
95+
() => {
96+
resolve();
97+
}
98+
);
99+
});
30100
} catch (e) {
31101
console.log(e);
32102
}

0 commit comments

Comments
 (0)