Skip to content

Commit eb0fc22

Browse files
feat: sdk example
1 parent 7b58cd2 commit eb0fc22

File tree

4 files changed

+112
-66
lines changed

4 files changed

+112
-66
lines changed

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

+70-53
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,8 @@
11
import { useEffect, useState } from "react";
22
import "./App.css";
3-
import { init } from "@dojoengine/sdk";
4-
3+
import { ParsedEntity, init } from "@dojoengine/sdk";
54
import { dojoConfig } from "../dojoConfig.ts";
6-
import { Subscription } from "@dojoengine/torii-wasm";
7-
8-
interface Moves {
9-
player: string;
10-
remaining: number;
11-
last_direction: Direction;
12-
can_move: boolean;
13-
}
14-
15-
interface DirectionsAvailable {
16-
player: string;
17-
directions: Direction[];
18-
}
19-
20-
interface Position {
21-
player: string;
22-
vec: Vec2;
23-
}
24-
25-
enum Direction {
26-
None,
27-
Left,
28-
Right,
29-
Up,
30-
Down,
31-
}
32-
33-
interface Vec2 {
34-
x: number;
35-
y: number;
36-
}
37-
38-
type Schema = {
39-
dojo_starter: {
40-
Moves: Moves;
41-
DirectionsAvailable: DirectionsAvailable;
42-
Position: Position;
43-
};
44-
};
5+
import { Schema } from "./bindings.ts";
456

467
const db = await init<Schema>({
478
rpcUrl: dojoConfig.rpcUrl,
@@ -52,13 +13,16 @@ const db = await init<Schema>({
5213
});
5314

5415
function App() {
16+
const [entities, setEntities] = useState<ParsedEntity<Schema>[]>([]);
17+
5518
useEffect(() => {
5619
let unsubscribe: (() => void) | undefined;
5720

5821
const subscribe = async () => {
5922
const subscription = await db.subscribeEntityQuery(
6023
{
6124
dojo_starter: {
25+
Position: true,
6226
Moves: true,
6327
},
6428
},
@@ -68,8 +32,18 @@ function App() {
6832
"Error setting up entity sync:",
6933
response.error
7034
);
71-
} else {
72-
console.log(response);
35+
} else if (
36+
response.data &&
37+
response.data[0].entityId !== "0x0"
38+
) {
39+
setEntities((prevEntities) => {
40+
return prevEntities.map((entity) => {
41+
const newEntity = response.data?.find(
42+
(e) => e.entityId === entity.entityId
43+
);
44+
return newEntity ? newEntity : entity;
45+
});
46+
});
7347
}
7448
}
7549
);
@@ -82,21 +56,18 @@ function App() {
8256
return () => {
8357
if (unsubscribe) {
8458
unsubscribe();
85-
console.log("Sync unsubscribed");
8659
}
8760
};
8861
}, []);
8962

9063
useEffect(() => {
9164
const fetchEntities = async () => {
9265
try {
93-
const entities = await db.getEntities(
66+
await db.getEntities(
9467
{
9568
dojo_starter: {
96-
Moves: {
97-
$: {
98-
where: { last_direction: { $eq: "Left" } },
99-
},
69+
Position: {
70+
$: {},
10071
},
10172
},
10273
},
@@ -109,21 +80,67 @@ function App() {
10980
return;
11081
}
11182
if (resp.data) {
83+
setEntities((prevEntities) => {
84+
const updatedEntities = [...prevEntities];
85+
resp.data?.forEach((newEntity) => {
86+
const index = updatedEntities.findIndex(
87+
(entity) =>
88+
entity.entityId ===
89+
newEntity.entityId
90+
);
91+
if (index !== -1) {
92+
updatedEntities[index] = newEntity;
93+
} else {
94+
updatedEntities.push(newEntity);
95+
}
96+
});
97+
return updatedEntities;
98+
});
11299
}
113100
}
114101
);
115-
// console.log("Queried entities:", entities);
116102
} catch (error) {
117103
console.error("Error querying entities:", error);
118104
}
119105
};
120106

121107
fetchEntities();
122108
}, []);
109+
123110
return (
124-
<>
125-
<button onClick={() => console.log("s")}></button>
126-
</>
111+
<div>
112+
<h1>Game State</h1>
113+
{entities.map((entity) => (
114+
<div key={entity.entityId}>
115+
<h2>Entity {entity.entityId}</h2>
116+
<h3>Position</h3>
117+
<p>
118+
Player:{" "}
119+
{entity.models.dojo_starter.Position?.player ?? "N/A"}
120+
<br />
121+
X: {entity.models.dojo_starter.Position?.vec.x ?? "N/A"}
122+
<br />
123+
Y: {entity.models.dojo_starter.Position?.vec.y ?? "N/A"}
124+
</p>
125+
<h3>Moves</h3>
126+
<p>
127+
Player:{" "}
128+
{entity.models.dojo_starter.Moves?.player ?? "N/A"}
129+
<br />
130+
Can Move:{" "}
131+
{entity.models.dojo_starter.Moves?.can_move?.toString() ??
132+
"N/A"}
133+
<br />
134+
Last Direction:{" "}
135+
{entity.models.dojo_starter.Moves?.last_direction ??
136+
"N/A"}
137+
<br />
138+
Remaining:{" "}
139+
{entity.models.dojo_starter.Moves?.remaining ?? "N/A"}
140+
</p>
141+
</div>
142+
))}
143+
</div>
127144
);
128145
}
129146

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
interface Moves {
2+
player: string;
3+
remaining: number;
4+
last_direction: Direction;
5+
can_move: boolean;
6+
}
7+
8+
interface DirectionsAvailable {
9+
player: string;
10+
directions: Direction[];
11+
}
12+
13+
interface Position {
14+
player: string;
15+
vec: Vec2;
16+
}
17+
18+
enum Direction {
19+
None,
20+
Left,
21+
Right,
22+
Up,
23+
Down,
24+
}
25+
26+
interface Vec2 {
27+
x: number;
28+
y: number;
29+
}
30+
31+
type Schema = {
32+
dojo_starter: {
33+
Moves: Moves;
34+
DirectionsAvailable: DirectionsAvailable;
35+
Position: Position;
36+
};
37+
};
38+
export type { Schema, Moves, DirectionsAvailable, Position, Vec2 };
39+
export { Direction };

packages/sdk/src/index.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ import {
77
} from "./types";
88
import { subscribeEntityQuery } from "./subscribeEntityQuery";
99
import { getEntities } from "./getEntities";
10-
// import { AccountInterface } from "starknet";
11-
// import {
12-
// createWorldProxy,
13-
// WorldContracts,
14-
// ContractDefinition,
15-
// } from "./execute";
10+
11+
export * from "./types";
1612

1713
import { subscribeEventQuery } from "./subscribeEventQuery";
1814

packages/sdk/src/parseEntities.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
SchemaType,
3-
QueryType,
4-
StandardizedQueryResult,
5-
ParsedEntity,
6-
} from "./types";
1+
import { SchemaType, StandardizedQueryResult, ParsedEntity } from "./types";
72
import * as torii from "@dojoengine/torii-client";
83

94
function parseValue(value: torii.Ty): any {
@@ -37,7 +32,6 @@ function parseStruct(
3732

3833
export function parseEntities<T extends SchemaType>(
3934
entities: torii.Entities,
40-
// query?: QueryType<T>,
4135
options?: { logging?: boolean }
4236
): StandardizedQueryResult<T> {
4337
const result: StandardizedQueryResult<T> = [];

0 commit comments

Comments
 (0)