Skip to content

Commit 58262f2

Browse files
Final
1 parent 6c31c26 commit 58262f2

1 file changed

Lines changed: 37 additions & 13 deletions

File tree

frontend/app/tictactoe/page.tsx

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export default function TicTacToe() {
2727

2828
const updateGameState = (data: GameState) => {
2929
console.log('Updating game state:', { turn: data.turn, winner: data.winner, won: data.won, board: data.totalBoard })
30+
// Always update gameData first to trigger re-renders
3031
setGameData(data)
3132
if (data.usernameToTacNumber) {
3233
const entries = Object.entries(data.usernameToTacNumber)
@@ -172,19 +173,17 @@ export default function TicTacToe() {
172173
console.log('Polling check:', { myTacNumber, currentTurn, isMyTurn, gameHasEnded, gameEndedState: gameEnded, gameData })
173174

174175
// Continue polling even after game ends to ensure both players see the result
175-
// Only stop polling if it's the player's turn and game hasn't ended
176-
if (isMyTurn && !gameHasEnded) {
177-
console.log('Stopping polling - my turn and game not ended')
178-
return
179-
}
176+
// IMPORTANT: If game has ended, we MUST keep polling so both clients see the result
177+
// This is especially important for the winner who just made the winning move
180178

181-
// If game ended and we've already set gameEnded state, poll a few more times then stop
179+
// If game ended and we've already set gameEnded state, continue polling briefly to ensure both clients see the result
182180
if (gameHasEnded && gameEnded) {
183-
// Already showing winner, just poll a couple more times to ensure sync, then stop
181+
// Already showing winner, continue polling a few more times to ensure both clients sync, then stop
184182
let pollCount = 0
185183
const interval = setInterval(() => {
186184
pollCount++
187-
if (pollCount >= 2) {
185+
// Poll for 3 more seconds to ensure both clients have time to see the countdown
186+
if (pollCount >= 3) {
188187
clearInterval(interval)
189188
return
190189
}
@@ -193,6 +192,28 @@ export default function TicTacToe() {
193192
return () => clearInterval(interval)
194193
}
195194

195+
// If game has ended but we haven't set gameEnded state yet, keep polling to get the update
196+
// This handles the case where the winner just made a move and the game ended
197+
// We MUST continue polling here to ensure the winner sees their win screen
198+
if (gameHasEnded && !gameEnded) {
199+
console.log('Game ended detected during polling, continuing to poll to get final state')
200+
// Continue polling to get the final state - don't return early
201+
fetchGameState()
202+
const interval = setInterval(() => {
203+
console.log('Polling for final game state with winner...')
204+
fetchGameState()
205+
// Stop after a few polls once we've gotten the state
206+
setTimeout(() => clearInterval(interval), 3000)
207+
}, 500) // Poll more frequently to catch the update
208+
return () => clearInterval(interval)
209+
}
210+
211+
// Only stop polling if it's the player's turn and game hasn't ended
212+
if (isMyTurn && !gameHasEnded) {
213+
console.log('Stopping polling - my turn and game not ended')
214+
return
215+
}
216+
196217
console.log('Starting polling - waiting for opponent or checking game end')
197218
// Fetch immediately, then poll every second
198219
fetchGameState()
@@ -258,13 +279,16 @@ export default function TicTacToe() {
258279
} else {
259280
console.log('Move successful, fetching game state')
260281
// Small delay to ensure backend has processed the move
261-
await new Promise(resolve => setTimeout(resolve, 100))
262-
// Fetch updated game state after making move
282+
await new Promise(resolve => setTimeout(resolve, 150))
283+
// Fetch updated game state after making move - this will update board and winner
263284
await fetchGameState()
264285
// Fetch again after a short delay to ensure we get the final state with winner
265-
setTimeout(async () => {
266-
await fetchGameState()
267-
}, 200)
286+
// This is important because the backend might need a moment to process win detection
287+
await new Promise(resolve => setTimeout(resolve, 200))
288+
await fetchGameState()
289+
// One more fetch to be absolutely sure we have the final state
290+
await new Promise(resolve => setTimeout(resolve, 200))
291+
await fetchGameState()
268292
}
269293
} catch (err) {
270294
console.error('Error making move:', err)

0 commit comments

Comments
 (0)