fix(card-sorting): prevent duplicate START_GAME moves on Play Again

Fix issue where clicking "Play Again" simultaneously on multiple windows
would generate different card sets on each window:

- Add guard in startGame() to prevent sending START_GAME if already in playing phase
- Add 500ms debounce check using gameStartTime to prevent rapid duplicate sends
- Add state.gamePhase and state.gameStartTime to startGame dependencies

Now only the first client to click "Play Again" will generate cards, and
all other clients will be blocked from sending duplicate START_GAME moves
within the debounce window, ensuring all windows see the same cards.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Thomas Hallock
2025-10-23 14:39:35 -05:00
parent 561599b672
commit a0b14f87e9

View File

@@ -395,6 +395,16 @@ export function CardSortingProvider({ children }: { children: ReactNode }) {
return
}
// Prevent multiple simultaneous START_GAME moves when multiple clients
// click "Play Again" at the same time. Only allow if we're NOT already starting/in a game.
// Also check if we just started a game (within 500ms) to prevent rapid double-sends.
const now = Date.now()
const justStarted = state.gameStartTime && now - state.gameStartTime < 500
if (state.gamePhase === 'playing' || justStarted) {
return
}
const playerMetadata = buildPlayerMetadata()
const selectedCards = shuffleCards(generateRandomCards(state.cardCount))
@@ -407,7 +417,15 @@ export function CardSortingProvider({ children }: { children: ReactNode }) {
selectedCards,
},
})
}, [localPlayerId, state.cardCount, buildPlayerMetadata, sendMove, viewerId])
}, [
localPlayerId,
state.cardCount,
state.gamePhase,
state.gameStartTime,
buildPlayerMetadata,
sendMove,
viewerId,
])
const placeCard = useCallback(
(cardId: string, position: number) => {