From 646878c572946355a8cbb66957fadfce8a6fe8d6 Mon Sep 17 00:00:00 2001 From: Cal Corum Date: Fri, 28 Nov 2025 12:41:50 -0600 Subject: [PATCH] CLAUDE: Optimize play history sync to O(1) with setPlayHistory MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- frontend-sba/composables/useWebSocket.ts | 6 ++---- frontend-sba/store/game.ts | 18 +++++++++++------- 2 files changed, 13 insertions(+), 11 deletions(-) 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,