1
1
import { useEffect , useState } from "react" ;
2
2
import "./App.css" ;
3
- import { init } from "@dojoengine/sdk" ;
4
-
3
+ import { ParsedEntity , init } from "@dojoengine/sdk" ;
5
4
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" ;
45
6
46
7
const db = await init < Schema > ( {
47
8
rpcUrl : dojoConfig . rpcUrl ,
@@ -52,13 +13,16 @@ const db = await init<Schema>({
52
13
} ) ;
53
14
54
15
function App ( ) {
16
+ const [ entities , setEntities ] = useState < ParsedEntity < Schema > [ ] > ( [ ] ) ;
17
+
55
18
useEffect ( ( ) => {
56
19
let unsubscribe : ( ( ) => void ) | undefined ;
57
20
58
21
const subscribe = async ( ) => {
59
22
const subscription = await db . subscribeEntityQuery (
60
23
{
61
24
dojo_starter : {
25
+ Position : true ,
62
26
Moves : true ,
63
27
} ,
64
28
} ,
@@ -68,8 +32,18 @@ function App() {
68
32
"Error setting up entity sync:" ,
69
33
response . error
70
34
) ;
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
+ } ) ;
73
47
}
74
48
}
75
49
) ;
@@ -82,21 +56,18 @@ function App() {
82
56
return ( ) => {
83
57
if ( unsubscribe ) {
84
58
unsubscribe ( ) ;
85
- console . log ( "Sync unsubscribed" ) ;
86
59
}
87
60
} ;
88
61
} , [ ] ) ;
89
62
90
63
useEffect ( ( ) => {
91
64
const fetchEntities = async ( ) => {
92
65
try {
93
- const entities = await db . getEntities (
66
+ await db . getEntities (
94
67
{
95
68
dojo_starter : {
96
- Moves : {
97
- $ : {
98
- where : { last_direction : { $eq : "Left" } } ,
99
- } ,
69
+ Position : {
70
+ $ : { } ,
100
71
} ,
101
72
} ,
102
73
} ,
@@ -109,21 +80,67 @@ function App() {
109
80
return ;
110
81
}
111
82
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
+ } ) ;
112
99
}
113
100
}
114
101
) ;
115
- // console.log("Queried entities:", entities);
116
102
} catch ( error ) {
117
103
console . error ( "Error querying entities:" , error ) ;
118
104
}
119
105
} ;
120
106
121
107
fetchEntities ( ) ;
122
108
} , [ ] ) ;
109
+
123
110
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 >
127
144
) ;
128
145
}
129
146
0 commit comments