@@ -8,115 +8,123 @@ import {
8
8
getEvents ,
9
9
setComponentsFromEvents ,
10
10
} from "@dojoengine/utils" ;
11
- import { ContractComponents } from "./defineContractComponents" ;
12
11
import type { IWorld } from "./defineContractSystems" ;
12
+ import { ContractComponents } from "./defineContractComponents" ;
13
13
14
14
export type SystemCalls = ReturnType < typeof systems > ;
15
15
16
16
export function systems ( {
17
17
client,
18
18
clientModels : { Position, Moves } ,
19
+ contractComponents,
19
20
} : {
20
21
client : IWorld ;
21
22
clientModels : ClientComponents ;
23
+ contractComponents : ContractComponents ;
22
24
} ) {
23
- const spawn = async ( account : AccountInterface ) => {
24
- const entityId = getEntityIdFromKeys ( [
25
- BigInt ( account . address ) ,
26
- ] ) as Entity ;
27
-
28
- const positionId = uuid ( ) ;
29
- Position . addOverride ( positionId , {
30
- entity : entityId ,
31
- value : { player : BigInt ( entityId ) , vec : { x : 10 , y : 10 } } ,
32
- } ) ;
25
+ function actions ( ) {
26
+ const spawn = async ( account : AccountInterface ) => {
27
+ const entityId = getEntityIdFromKeys ( [
28
+ BigInt ( account . address ) ,
29
+ ] ) as Entity ;
33
30
34
- const movesId = uuid ( ) ;
35
- Moves . addOverride ( movesId , {
36
- entity : entityId ,
37
- value : {
38
- player : BigInt ( entityId ) ,
39
- remaining : 100 ,
40
- last_direction : 0 ,
41
- } ,
42
- } ) ;
31
+ const positionId = uuid ( ) ;
32
+ Position . addOverride ( positionId , {
33
+ entity : entityId ,
34
+ value : { player : BigInt ( entityId ) , vec : { x : 10 , y : 10 } } ,
35
+ } ) ;
43
36
44
- try {
45
- const { transaction_hash } = await client . actions . spawn ( {
46
- account,
37
+ const movesId = uuid ( ) ;
38
+ Moves . addOverride ( movesId , {
39
+ entity : entityId ,
40
+ value : {
41
+ player : BigInt ( entityId ) ,
42
+ remaining : 100 ,
43
+ last_direction : 0 ,
44
+ } ,
47
45
} ) ;
48
46
49
- setComponentsFromEvents (
50
- contractComponents ,
51
- getEvents (
52
- await account . waitForTransaction ( transaction_hash , {
53
- retryInterval : 100 ,
54
- } )
55
- )
56
- ) ;
57
- } catch ( e ) {
58
- console . log ( e ) ;
59
- Position . removeOverride ( positionId ) ;
60
- Moves . removeOverride ( movesId ) ;
61
- } finally {
62
- Position . removeOverride ( positionId ) ;
63
- Moves . removeOverride ( movesId ) ;
64
- }
65
- } ;
47
+ try {
48
+ const { transaction_hash } = await client . actions . spawn ( {
49
+ account,
50
+ } ) ;
51
+ // setComponentsFromEvents(
52
+ // contractComponents,
53
+ // getEvents(
54
+ // await account.waitForTransaction(transaction_hash, {
55
+ // retryInterval: 100,
56
+ // })
57
+ // )
58
+ // );
59
+ } catch ( e ) {
60
+ console . log ( e ) ;
61
+ Position . removeOverride ( positionId ) ;
62
+ Moves . removeOverride ( movesId ) ;
63
+ } finally {
64
+ Position . removeOverride ( positionId ) ;
65
+ Moves . removeOverride ( movesId ) ;
66
+ }
67
+ } ;
66
68
67
- const move = async ( account : AccountInterface , direction : Direction ) => {
68
- const entityId = getEntityIdFromKeys ( [
69
- BigInt ( account . address ) ,
70
- ] ) as Entity ;
69
+ const move = async (
70
+ account : AccountInterface ,
71
+ direction : Direction
72
+ ) => {
73
+ const entityId = getEntityIdFromKeys ( [
74
+ BigInt ( account . address ) ,
75
+ ] ) as Entity ;
71
76
72
- const positionId = uuid ( ) ;
73
- Position . addOverride ( positionId , {
74
- entity : entityId ,
75
- value : {
76
- player : BigInt ( entityId ) ,
77
- vec : updatePositionWithDirection (
78
- direction ,
79
- getComponentValue ( Position , entityId ) as any
80
- ) . vec ,
81
- } ,
82
- } ) ;
83
-
84
- const movesId = uuid ( ) ;
85
- Moves . addOverride ( movesId , {
86
- entity : entityId ,
87
- value : {
88
- player : BigInt ( entityId ) ,
89
- remaining :
90
- ( getComponentValue ( Moves , entityId ) ?. remaining || 0 ) - 1 ,
91
- } ,
92
- } ) ;
77
+ console . log ( direction , getComponentValue ( Position , entityId ) ) ;
78
+ const positionId = uuid ( ) ;
79
+ Position . addOverride ( positionId , {
80
+ entity : entityId ,
81
+ value : {
82
+ player : BigInt ( entityId ) ,
83
+ vec : updatePositionWithDirection (
84
+ direction ,
85
+ getComponentValue ( Position , entityId ) as any
86
+ ) . vec ,
87
+ } ,
88
+ } ) ;
93
89
94
- try {
95
- const { transaction_hash } = await client . actions . move ( {
96
- account,
97
- direction,
90
+ const movesId = uuid ( ) ;
91
+ Moves . addOverride ( movesId , {
92
+ entity : entityId ,
93
+ value : {
94
+ player : BigInt ( entityId ) ,
95
+ remaining :
96
+ ( getComponentValue ( Moves , entityId ) ?. remaining || 0 ) -
97
+ 1 ,
98
+ } ,
98
99
} ) ;
99
100
100
- setComponentsFromEvents (
101
- contractComponents ,
102
- getEvents (
103
- await account . waitForTransaction ( transaction_hash , {
104
- retryInterval : 100 ,
105
- } )
106
- )
107
- ) ;
108
- } catch ( e ) {
109
- console . log ( e ) ;
110
- Position . removeOverride ( positionId ) ;
111
- Moves . removeOverride ( movesId ) ;
112
- } finally {
113
- Position . removeOverride ( positionId ) ;
114
- Moves . removeOverride ( movesId ) ;
115
- }
116
- } ;
101
+ try {
102
+ const { transaction_hash } = await client . actions . move ( {
103
+ account,
104
+ direction,
105
+ } ) ;
106
+
107
+ setComponentsFromEvents (
108
+ contractComponents ,
109
+ getEvents (
110
+ await account . waitForTransaction ( transaction_hash , {
111
+ retryInterval : 100 ,
112
+ } )
113
+ )
114
+ ) ;
115
+ } catch ( e ) {
116
+ console . error ( e ) ;
117
+ Position . removeOverride ( positionId ) ;
118
+ Moves . removeOverride ( movesId ) ;
119
+ } finally {
120
+ Position . removeOverride ( positionId ) ;
121
+ Moves . removeOverride ( movesId ) ;
122
+ }
123
+ } ;
124
+ return { spawn, move } ;
125
+ }
117
126
118
127
return {
119
- spawn,
120
- move,
128
+ actions : actions ( ) ,
121
129
} ;
122
130
}
0 commit comments