@@ -10,8 +10,7 @@ import {
1010 selectAccounts ,
1111 selectPlayerColor ,
1212 selectPlayer ,
13- selectCurrentPlayerColors ,
14- selectGameCache
13+ selectPieces
1514} from './gameSelectors' ;
1615import defaultPieceSetup from './defaultPieceSetup' ;
1716
@@ -61,6 +60,13 @@ function* initializePlayer({ player }) {
6160 stratego4 . methods . isPlayersTurn . cacheCall ( player , { ...ops } )
6261 )
6362 ) ;
63+
64+ yield put (
65+ setCacheKey (
66+ 'getGamePieces' ,
67+ stratego4 . methods . getGamePieces . cacheCall ( player , { ...ops } )
68+ )
69+ ) ;
6470}
6571
6672/**
@@ -105,81 +111,29 @@ export function* initializePlayerPieces() {
105111 ) ;
106112 } ) ;
107113}
108-
109- /**
110- * Update the piece cache for a player
111- *
112- * @param {Array<string> } pieces - Array of piece rank hashes
113- * @param {string } playerAddress - Address of player to update
114- */
115- export function * updatePieceCache ( pieces , playerAddress ) {
116- const ops = yield defaultTxOps ( ) ;
117- const playerColors = yield select ( selectCurrentPlayerColors ) ;
118- const gameCache = yield select ( selectGameCache ) ;
119- const player = playerColors . find ( player => player . address === playerAddress ) ;
120-
121- if ( ! player ) {
122- // Skip if a player isn't found. Happens when changing to a new game
123- return ;
124- }
125-
126- const gameCacheKey = `getPiece-${ player . color } ` ;
127- const currentPieceCache = gameCache [ gameCacheKey ] || [ ] ;
128-
129- yield put (
130- setCacheKey (
131- gameCacheKey ,
132- pieces . map ( piece => {
133- const args = [ playerAddress , piece , { ...ops } ] ;
134- const cacheKey = stratego4 . generateArgsHash ( args ) ;
135-
136- if ( ! currentPieceCache . includes ( cacheKey ) ) {
137- stratego4 . methods . getPiece . cacheCall ( ...args ) ;
138- }
139-
140- return cacheKey ;
141- } )
142- )
143- ) ;
144- }
145-
146114/**
147115 * Handle when drizzle receives player piece rank hashes
148116 *
149117 * @param {Object } action - redux action
150118 */
151- export function * handleGotPlayerPieces ( action ) {
119+ export function * handleGotGamePieces ( action ) {
152120 const player = yield select ( selectPlayer ) ;
153121 const actionPlayer = action . args [ 0 ] ;
154122 const playerColor = yield select ( selectPlayerColor ) ;
155- const pieces = action . value ;
156- const isAlreadySetup = pieces . length > 0 ;
123+ const gamePieces = yield select ( selectPieces ) ;
157124
158125 const isCurrentPlayer = actionPlayer === player ;
159126
160127 if ( ! playerColor ) {
161128 return ;
162129 }
163130
131+ const pieces = gamePieces [ playerColor ] ;
132+ const isAlreadySetup = pieces . length > 0 ;
133+
164134 if ( isCurrentPlayer && ! isAlreadySetup ) {
165135 yield initializePlayerPieces ( ) ;
166136 }
167-
168- yield updatePieceCache ( pieces , actionPlayer ) ;
169- }
170-
171- /**
172- * Handle when drizzle receives current player addresses
173- *
174- * @param {Object } action - Redux action
175- */
176- export function * handleGotCurrentPlayers ( action ) {
177- const ops = yield defaultTxOps ( ) ;
178- const currentPlayers = action . value ;
179-
180- currentPlayers . forEach ( player =>
181- stratego4 . methods . playerPieces . cacheCall ( player , { ...ops } )
182- ) ;
183137}
184138
185139/**
@@ -189,19 +143,14 @@ export function* handleGotCurrentPlayers(action) {
189143 */
190144export function * handleGotContractVar ( action ) {
191145 switch ( action . variable ) {
192- case 'playerPieces' :
193- yield handleGotPlayerPieces ( action ) ;
194- break ;
195-
196- case 'currentPlayers' :
197- yield handleGotCurrentPlayers ( action ) ;
146+ case 'getGamePieces' :
147+ yield handleGotGamePieces ( action ) ;
198148 break ;
199149
200150 default :
201151 break ;
202152 }
203153}
204-
205154/**
206155 * A player has joined the a game
207156 *
0 commit comments