# All files / store game.ts

92.38% Statements 194/210
84% Branches 63/75
81.25% Functions 13/16
92.38% Lines 194/210

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    3321x

    1x

    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x

    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x

    28x
    28x

    28x
    28x
    28x

    28x
    4x
    4x
    4x
    4x
    4x
    28x

    28x
    28x
    4x
    28x

    28x
    2x
    2x
    1x
    1x
    28x

    28x
    2x
    2x
    1x
    1x
    28x

    28x
    2x
    2x
    1x
    1x
    28x

    28x
    2x
    2x
    1x
    1x
    28x

    28x
    3x
    28x

    28x
    2x
    28x

    28x
    1x
    28x

    28x
    3x
    28x

    28x
    2x
    28x

    28x
    1x
    28x

    28x
    17x
    17x
    17x

    28x
    20x
    3x
    3x
    20x

    28x
    1x
    1x
    1x

    28x
    1x
    1x
    1x

    1x

    28x
    18x

    18x
    18x
    18x
    18x

    28x
    5x
    5x

    28x
    1x
    1x

    28x
    4x
    4x

    28x
    1x
    1x

    28x
    3x
    3x

    28x
    3x
    3x

    28x
    3x
    3x

    28x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x
    1x

    28x

    28x

    28x

    28x

    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x

    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x

    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
    28x
     /**
     * Game Store
     *
     * Manages active game state, synchronized with backend via WebSocket.
     * This is the central state container for real-time gameplay.
     */

    import { defineStore } from 'pinia'
    import { ref, computed } from 'vue'
    import type {
      GameState,
      PlayResult,
      DecisionPrompt,
      DefensiveDecision,
      OffensiveDecision,
      RollData,
      Lineup,
    } from '~/types'

    export const useGameStore = defineStore('game', () => {
      // ============================================================================
      // State
      // ============================================================================

      const gameState = ref<GameState | null>(null)
      const homeLineup = ref<Lineup[]>([])
      const awayLineup = ref<Lineup[]>([])
      const playHistory = ref<PlayResult[]>([])
      const currentDecisionPrompt = ref<DecisionPrompt | null>(null)
      const pendingRoll = ref<RollData | null>(null)
      const isConnected = ref(false)
      const isLoading = ref(false)
      const error = ref<string | null>(null)

      // ============================================================================
      // Getters
      // ============================================================================

      const gameId = computed(() => gameState.value?.game_id ?? null)
      const leagueId = computed(() => gameState.value?.league_id ?? null)
      const currentInning = computed(() => gameState.value?.inning ?? 1)
      const currentHalf = computed(() => gameState.value?.half ?? 'top')
      const outs = computed(() => gameState.value?.outs ?? 0)
      const balls = computed(() => gameState.value?.balls ?? 0)
      const strikes = computed(() => gameState.value?.strikes ?? 0)
      const homeScore = computed(() => gameState.value?.home_score ?? 0)
      const awayScore = computed(() => gameState.value?.away_score ?? 0)
      const gameStatus = computed(() => gameState.value?.status ?? 'pending')

      const isGameActive = computed(() => gameStatus.value === 'active')
      const isGameComplete = computed(() => gameStatus.value === 'completed')

      const currentBatter = computed(() => gameState.value?.current_batter ?? null)
      const currentPitcher = computed(() => gameState.value?.current_pitcher ?? null)
      const currentCatcher = computed(() => gameState.value?.current_catcher ?? null)

      const runnersOnBase = computed(() => {
        const runners: number[] = []
        if (gameState.value?.on_first) runners.push(1)
        if (gameState.value?.on_second) runners.push(2)
        if (gameState.value?.on_third) runners.push(3)
        return runners
      })

      const basesLoaded = computed(() => runnersOnBase.value.length === 3)
      const runnerInScoringPosition = computed(() =>
        runnersOnBase.value.includes(2) || runnersOnBase.value.includes(3)
      )

      const battingTeamId = computed(() => {
        if (!gameState.value) return null
        return gameState.value.half === 'top'
          ? gameState.value.away_team_id
          : gameState.value.home_team_id
      })

      const fieldingTeamId = computed(() => {
        if (!gameState.value) return null
        return gameState.value.half === 'top'
          ? gameState.value.home_team_id
          : gameState.value.away_team_id
      })

      const isBattingTeamAI = computed(() => {
        if (!gameState.value) return false
        return gameState.value.half === 'top'
          ? gameState.value.away_team_is_ai
          : gameState.value.home_team_is_ai
      })

      const isFieldingTeamAI = computed(() => {
        if (!gameState.value) return false
        return gameState.value.half === 'top'
          ? gameState.value.home_team_is_ai
          : gameState.value.away_team_is_ai
      })

      const needsDefensiveDecision = computed(() => {
        return currentDecisionPrompt.value?.phase === 'defense'
      })

      const needsOffensiveDecision = computed(() => {
        return currentDecisionPrompt.value?.phase === 'offensive_approach'
      })

      const needsStolenBaseDecision = computed(() => {
        return currentDecisionPrompt.value?.phase === 'stolen_base'
      })

      const canRollDice = computed(() => {
        return gameState.value?.decision_phase === 'resolution' && !pendingRoll.value
      })

      const canSubmitOutcome = computed(() => {
        return pendingRoll.value !== null
      })

      const recentPlays = computed(() => {
        return playHistory.value.slice(-10).reverse()
      })

      // ============================================================================
      // Actions
      // ============================================================================

      /**
       * Set complete game state (from server)
       */
      function setGameState(state: GameState) {
        gameState.value = state
        error.value = null
      }

      /**
       * Update partial game state
       */
      function updateGameState(updates: Partial<GameState>) {
        if (gameState.value) {
          gameState.value = { ...gameState.value, ...updates }
        }
      }

      /**
       * Set lineups for both teams
       */
      function setLineups(home: Lineup[], away: Lineup[]) {
        homeLineup.value = home
        awayLineup.value = away
      }

      /**
       * Update lineup for a specific team
       */
      function updateLineup(teamId: number, lineup: Lineup[]) {
        if (teamId === gameState.value?.home_team_id) {
          homeLineup.value = lineup
        } else if (teamId === gameState.value?.away_team_id) {
          awayLineup.value = lineup
        }
      }

      /**
       * Add play to history
       */
      function addPlayToHistory(play: PlayResult) {
        playHistory.value.push(play)

        // Update game state from play result if provided
        if (play.new_state) {
          updateGameState(play.new_state)
        }
      }

      /**
       * Set current decision prompt
       */
      function setDecisionPrompt(prompt: DecisionPrompt | null) {
        currentDecisionPrompt.value = prompt
      }

      /**
       * Clear decision prompt after submission
       */
      function clearDecisionPrompt() {
        currentDecisionPrompt.value = null
      }

      /**
       * Set pending dice roll
       */
      function setPendingRoll(roll: RollData | null) {
        pendingRoll.value = roll
      }

      /**
       * Clear pending roll after outcome submission
       */
      function clearPendingRoll() {
        pendingRoll.value = null
      }

      /**
       * Set connection status
       */
      function setConnected(connected: boolean) {
        isConnected.value = connected
      }

      /**
       * Set loading state
       */
      function setLoading(loading: boolean) {
        isLoading.value = loading
      }

      /**
       * Set error
       */
      function setError(message: string | null) {
        error.value = message
      }

      /**
       * Reset game store (when leaving game)
       */
      function resetGame() {
        gameState.value = null
        homeLineup.value = []
        awayLineup.value = []
        playHistory.value = []
        currentDecisionPrompt.value = null
        pendingRoll.value = null
        isConnected.value = false
        isLoading.value = false
        error.value = null
      }

      /**
       * Get active lineup for a team
       */
      function getActiveLineup(teamId: number): Lineup[] {
        const lineup = teamId === gameState.value?.home_team_id
          ? homeLineup.value
          : awayLineup.value
        return lineup.filter(p => p.is_active)
      }

      /**
       * Get bench players for a team
       */
      function getBenchPlayers(teamId: number): Lineup[] {
        const lineup = teamId === gameState.value?.home_team_id
          ? homeLineup.value
          : awayLineup.value
        return lineup.filter(p => !p.is_active)
      }

      /**
       * Find player in lineup by lineup_id
       */
      function findPlayerInLineup(lineupId: number): Lineup | undefined {
        return [...homeLineup.value, ...awayLineup.value].find(
          p => p.id === lineupId
        )
      }

      // ============================================================================
      // Return Store API
      // ============================================================================

      return {
        // State
        gameState: readonly(gameState),
        homeLineup: readonly(homeLineup),
        awayLineup: readonly(awayLineup),
        playHistory: readonly(playHistory),
        currentDecisionPrompt: readonly(currentDecisionPrompt),
        pendingRoll: readonly(pendingRoll),
        isConnected: readonly(isConnected),
        isLoading: readonly(isLoading),
        error: readonly(error),

        // Getters
        gameId,
        leagueId,
        currentInning,
        currentHalf,
        outs,
        balls,
        strikes,
        homeScore,
        awayScore,
        gameStatus,
        isGameActive,
        isGameComplete,
        currentBatter,
        currentPitcher,
        currentCatcher,
        runnersOnBase,
        basesLoaded,
        runnerInScoringPosition,
        battingTeamId,
        fieldingTeamId,
        isBattingTeamAI,
        isFieldingTeamAI,
        needsDefensiveDecision,
        needsOffensiveDecision,
        needsStolenBaseDecision,
        canRollDice,
        canSubmitOutcome,
        recentPlays,

        // Actions
        setGameState,
        updateGameState,
        setLineups,
        updateLineup,
        addPlayToHistory,
        setDecisionPrompt,
        clearDecisionPrompt,
        setPendingRoll,
        clearPendingRoll,
        setConnected,
        setLoading,
        setError,
        resetGame,
        getActiveLineup,
        getBenchPlayers,
        findPlayerInLineup,
      }
    })