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) => {
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) => {

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 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
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) {
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,