CLAUDE: Optimize play history sync to O(1) with setPlayHistory

Replaced O(n²) deduplication check per play with O(1) array replacement:
- Added setPlayHistory() for game_state_sync (replaces entire array)
- Simplified addPlayToHistory() for live play_resolved events (just push)

This separates sync operations (replace) from live events (append),
eliminating the need for deduplication checks during gameplay.

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Cal Corum 2025-11-28 12:41:50 -06:00
parent 19b35f148b
commit 646878c572
2 changed files with 13 additions and 11 deletions

View File

@ -306,10 +306,8 @@ export function useWebSocket() {
socketInstance.on('game_state_sync', (data) => { socketInstance.on('game_state_sync', (data) => {
console.log('[WebSocket] Full game state sync received') console.log('[WebSocket] Full game state sync received')
gameStore.setGameState(data.state) gameStore.setGameState(data.state)
// Add recent plays to history // Replace play history with synced plays (O(1) - no iteration/dedup needed)
data.recent_plays.forEach((play) => { gameStore.setPlayHistory(data.recent_plays)
gameStore.addPlayToHistory(play)
})
}) })
socketInstance.on('inning_change', (data) => { socketInstance.on('inning_change', (data) => {

View File

@ -178,15 +178,18 @@ export const useGameStore = defineStore('game', () => {
} }
/** /**
* Add play to history (with deduplication by play_number) * Set play history (replaces entire array - used for initial sync)
* O(1) operation - no deduplication needed
*/
function setPlayHistory(plays: PlayResult[]) {
playHistory.value = plays
}
/**
* Add play to history (used for live play_resolved events)
* O(1) operation - assumes no duplicates from live events
*/ */
function addPlayToHistory(play: PlayResult) { function addPlayToHistory(play: PlayResult) {
// Check if play already exists (deduplicate by play_number)
const exists = playHistory.value.some(p => p.play_number === play.play_number)
if (exists) {
return
}
playHistory.value.push(play) playHistory.value.push(play)
// Update game state from play result if provided // Update game state from play result if provided
@ -409,6 +412,7 @@ export const useGameStore = defineStore('game', () => {
updateGameState, updateGameState,
setLineups, setLineups,
updateLineup, updateLineup,
setPlayHistory,
addPlayToHistory, addPlayToHistory,
setDecisionPrompt, setDecisionPrompt,
clearDecisionPrompt, clearDecisionPrompt,