@@ -135,9 +135,6 @@ export function isAtEnd(gameState) {
135135
136136export function teleport ( baseState , targetPos ) {
137137 let viewPos = baseState . viewPos
138- if ( targetPos >= baseState . queueLength ) {
139- return goToEnd ( baseState )
140- }
141138 let diff = targetPos - viewPos
142139 let direction = Math . sign ( diff )
143140 let moves = baseState . moves
@@ -172,26 +169,6 @@ export function moveForward(baseState) {
172169 return teleport ( baseState , viewPos + 1 )
173170}
174171
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-
195172export function addMove ( baseState , move ) {
196173 let { action, n} = move
197174 let { moves, baseBoard, historyBoard, state, queueLength} = baseState
@@ -207,11 +184,16 @@ export function addMove(baseState, move) {
207184 }
208185 if ( action === "end" ) {
209186 let updated = resetCounting ( baseBoard )
187+ let finalQueueLength = getFinalQueueLength ( baseState )
188+ let lastMove = finalQueueLength ? moves [ finalQueueLength - 1 ] : undefined
210189 return produce ( baseState , ( draft ) => {
211190 draft . moves . push ( move )
212191 draft . state = 0
213192 draft . baseBoard = updated
214193 draft . board = rehydrate ( updated , historyBoard )
194+ draft . queueLength = finalQueueLength
195+ draft . viewPos = finalQueueLength
196+ draft . lastMove = lastMove
215197 } )
216198 }
217199 let [ storedMove , updated , forbidden ] = updateBoardState ( baseBoard , previousMove , move , counting )
@@ -234,6 +216,15 @@ export function addMove(baseState, move) {
234216 } )
235217}
236218
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+
237228export function createGameState ( game , auth ) {
238229 let baseBoard = Array ( game . dim )
239230 let historyBoard = Array ( game . dim )
0 commit comments