1
- import { useEffect , useState } from "react" ;
1
+ import { useEffect } from "react" ;
2
2
import "./App.css" ;
3
- import { ParsedEntity , SDK } from "@dojoengine/sdk" ;
3
+ import { SDK } from "@dojoengine/sdk" ;
4
4
import { Schema } from "./bindings.ts" ;
5
+ import { useGameState } from "./state.ts" ;
6
+
7
+ import { v4 as uuidv4 } from "uuid" ;
5
8
6
9
function App ( { db } : { db : SDK < Schema > } ) {
7
- const [ entities , setEntities ] = useState < ParsedEntity < Schema > [ ] > ( [ ] ) ;
10
+ const state = useGameState ( ( state ) => state ) ;
11
+ const entities = useGameState ( ( state ) => state . entities ) ;
8
12
9
13
useEffect ( ( ) => {
10
14
let unsubscribe : ( ( ) => void ) | undefined ;
@@ -28,15 +32,7 @@ function App({ db }: { db: SDK<Schema> }) {
28
32
response . data &&
29
33
response . data [ 0 ] . entityId !== "0x0"
30
34
) {
31
- console . log ( response . data ) ;
32
- setEntities ( ( prevEntities ) => {
33
- return prevEntities . map ( ( entity ) => {
34
- const newEntity = response . data ?. find (
35
- ( e ) => e . entityId === entity . entityId
36
- ) ;
37
- return newEntity ? newEntity : entity ;
38
- } ) ;
39
- } ) ;
35
+ state . setEntities ( response . data ) ;
40
36
}
41
37
} ,
42
38
{ logging : true }
@@ -54,8 +50,6 @@ function App({ db }: { db: SDK<Schema> }) {
54
50
} ;
55
51
} , [ db ] ) ;
56
52
57
- console . log ( "entities:" ) ;
58
-
59
53
useEffect ( ( ) => {
60
54
const fetchEntities = async ( ) => {
61
55
try {
@@ -76,23 +70,7 @@ function App({ db }: { db: SDK<Schema> }) {
76
70
return ;
77
71
}
78
72
if ( resp . data ) {
79
- console . log ( resp . data ) ;
80
- setEntities ( ( prevEntities ) => {
81
- const updatedEntities = [ ...prevEntities ] ;
82
- resp . data ?. forEach ( ( newEntity ) => {
83
- const index = updatedEntities . findIndex (
84
- ( entity ) =>
85
- entity . entityId ===
86
- newEntity . entityId
87
- ) ;
88
- if ( index !== - 1 ) {
89
- updatedEntities [ index ] = newEntity ;
90
- } else {
91
- updatedEntities . push ( newEntity ) ;
92
- }
93
- } ) ;
94
- return updatedEntities ;
95
- } ) ;
73
+ state . setEntities ( resp . data ) ;
96
74
}
97
75
}
98
76
) ;
@@ -104,12 +82,45 @@ function App({ db }: { db: SDK<Schema> }) {
104
82
fetchEntities ( ) ;
105
83
} , [ db ] ) ;
106
84
85
+ const optimisticUpdate = async ( ) => {
86
+ const entityId =
87
+ "0x571368d35c8fe136adf81eecf96a72859c43de7efd8fdd3d6f0d17e308df984" ;
88
+
89
+ const transactionId = uuidv4 ( ) ;
90
+
91
+ state . applyOptimisticUpdate ( transactionId , ( draft ) => {
92
+ draft . entities [ entityId ] . models . dojo_starter . Moves ! . remaining = 10 ;
93
+ } ) ;
94
+
95
+ try {
96
+ // Wait for the entity to be updated before full resolving the transaction. Reverts if the condition is not met.
97
+ const updatedEntity = await state . waitForEntityChange (
98
+ entityId ,
99
+ ( entity ) => {
100
+ // Define your specific condition here
101
+ return entity ?. models . dojo_starter . Moves ?. can_move === true ;
102
+ }
103
+ ) ;
104
+
105
+ console . log ( "Entity has been updated to active:" , updatedEntity ) ;
106
+
107
+ console . log ( "Updating entities..." ) ;
108
+ } catch ( error ) {
109
+ console . error ( "Error updating entities:" , error ) ;
110
+ state . revertOptimisticUpdate ( transactionId ) ;
111
+ } finally {
112
+ console . log ( "Updating entities..." ) ;
113
+ state . confirmTransaction ( transactionId ) ;
114
+ }
115
+ } ;
116
+
107
117
return (
108
118
< div >
109
119
< h1 > Game State</ h1 >
110
- { entities . map ( ( entity ) => (
111
- < div key = { entity . entityId } >
112
- < h2 > Entity { entity . entityId } </ h2 >
120
+ < button onClick = { optimisticUpdate } > update</ button >
121
+ { Object . entries ( entities ) . map ( ( [ entityId , entity ] ) => (
122
+ < div key = { entityId } >
123
+ < h2 > Entity { entityId } </ h2 >
113
124
< h3 > Position</ h3 >
114
125
< p >
115
126
Player:{ " " }
0 commit comments