@@ -135,9 +135,6 @@ export function isAtEnd(gameState) {
135
135
136
136
export function teleport ( baseState , targetPos ) {
137
137
let viewPos = baseState . viewPos
138
- if ( targetPos >= baseState . queueLength ) {
139
- return goToEnd ( baseState )
140
- }
141
138
let diff = targetPos - viewPos
142
139
let direction = Math . sign ( diff )
143
140
let moves = baseState . moves
@@ -172,26 +169,6 @@ export function moveForward(baseState) {
172
169
return teleport ( baseState , viewPos + 1 )
173
170
}
174
171
175
- function goToEnd ( baseState ) {
176
- let moves = baseState . moves
177
- let queueLength = baseState . queueLength
178
- let baseBoard = baseState . baseBoard
179
- let historyBoard = baseState . historyBoard
180
- let counting = baseState . state === STATE_COUNTING
181
- for ( let i = baseState . viewPos ; i < moves . length ; i ++ ) {
182
- let move = moves [ i ]
183
- let previousMove = getMove ( moves , i - 1 )
184
- let [ , updated ] = updateBoardState ( baseBoard , previousMove , move , counting )
185
- baseBoard = updated
186
- }
187
- let board = rehydrate ( baseBoard , historyBoard )
188
- return produce ( baseState , ( draft ) => {
189
- draft . board = board
190
- draft . viewPos = queueLength
191
- draft . lastMove = getMove ( moves , queueLength - 1 )
192
- } )
193
- }
194
-
195
172
export function addMove ( baseState , move ) {
196
173
let { action, n} = move
197
174
let { moves, baseBoard, historyBoard, state, queueLength} = baseState
@@ -207,11 +184,16 @@ export function addMove(baseState, move) {
207
184
}
208
185
if ( action === "end" ) {
209
186
let updated = resetCounting ( baseBoard )
187
+ let finalQueueLength = getFinalQueueLength ( baseState )
188
+ let lastMove = finalQueueLength ? moves [ finalQueueLength - 1 ] : undefined
210
189
return produce ( baseState , ( draft ) => {
211
190
draft . moves . push ( move )
212
191
draft . state = 0
213
192
draft . baseBoard = updated
214
193
draft . board = rehydrate ( updated , historyBoard )
194
+ draft . queueLength = finalQueueLength
195
+ draft . viewPos = finalQueueLength
196
+ draft . lastMove = lastMove
215
197
} )
216
198
}
217
199
let [ storedMove , updated , forbidden ] = updateBoardState ( baseBoard , previousMove , move , counting )
@@ -234,6 +216,15 @@ export function addMove(baseState, move) {
234
216
} )
235
217
}
236
218
219
+ function getFinalQueueLength ( { moves, queueLength} ) {
220
+ let copy = [ ...moves ]
221
+ copy = copy . slice ( 0 , queueLength )
222
+ while ( copy . length && copy [ copy . length - 1 ] . action === "pass" ) {
223
+ copy . pop ( )
224
+ }
225
+ return copy . length
226
+ }
227
+
237
228
export function createGameState ( game , auth ) {
238
229
let baseBoard = Array ( game . dim )
239
230
let historyBoard = Array ( game . dim )
0 commit comments