diff --git a/frontend-sba/composables/useWebSocket.ts b/frontend-sba/composables/useWebSocket.ts index f42c426..3aeaa12 100644 --- a/frontend-sba/composables/useWebSocket.ts +++ b/frontend-sba/composables/useWebSocket.ts @@ -306,10 +306,8 @@ export function useWebSocket() { socketInstance.on('game_state_sync', (data) => { console.log('[WebSocket] Full game state sync received') gameStore.setGameState(data.state) - // Add recent plays to history - data.recent_plays.forEach((play) => { - gameStore.addPlayToHistory(play) - }) + // Replace play history with synced plays (O(1) - no iteration/dedup needed) + gameStore.setPlayHistory(data.recent_plays) }) socketInstance.on('inning_change', (data) => { diff --git a/frontend-sba/store/game.ts b/frontend-sba/store/game.ts index 61be00c..055c825 100644 --- a/frontend-sba/store/game.ts +++ b/frontend-sba/store/game.ts @@ -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) { - // 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) // Update game state from play result if provided @@ -409,6 +412,7 @@ export const useGameStore = defineStore('game', () => { updateGameState, setLineups, updateLineup, + setPlayHistory, addPlayToHistory, setDecisionPrompt, clearDecisionPrompt,