# Code coverage report for composables/useGameActions.ts

# All files / composables useGameActions.ts

0% Statements 0/169
0% Branches 0/1
0% Functions 0/1
0% Lines 0/169

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

     /**
     * Game Actions Composable
     *
     * Provides type-safe methods for emitting game actions to the server.
     * Wraps Socket.io emit calls with proper error handling and validation.
     *
     * This composable is league-agnostic and will be shared between SBA and PD.
     */

    import { computed } from 'vue'
    import type {
      DefensiveDecision,
      OffensiveDecision,
      ManualOutcomeSubmission,
      PlayOutcome,
    } from '~/types'
    import { useWebSocket } from './useWebSocket'
    import { useGameStore } from '~/store/game'
    import { useUiStore } from '~/store/ui'

    export function useGameActions(gameId?: string) {
      const { socket, isConnected } = useWebSocket()
      const gameStore = useGameStore()
      const uiStore = useUiStore()

      // Use provided gameId or get from store
      const currentGameId = computed(() => gameId || gameStore.gameId)

      /**
       * Validate socket connection before emitting
       */
      function validateConnection(): boolean {
        if (!isConnected.value) {
          uiStore.showError('Not connected to game server')
          return false
        }

        if (!socket.value) {
          uiStore.showError('WebSocket not initialized')
          return false
        }

        if (!currentGameId.value) {
          uiStore.showError('No active game')
          return false
        }

        return true
      }

      // ============================================================================
      // Connection Actions
      // ============================================================================

      /**
       * Join a game room
       */
      function joinGame(role: 'player' | 'spectator' = 'player') {
        if (!validateConnection()) return

        console.log(`[GameActions] Joining game ${currentGameId.value} as ${role}`)

        socket.value!.emit('join_game', {
          game_id: currentGameId.value!,
          role,
        })
      }

      /**
       * Leave current game room
       */
      function leaveGame() {
        if (!socket.value || !currentGameId.value) return

        console.log(`[GameActions] Leaving game ${currentGameId.value}`)

        socket.value.emit('leave_game', {
          game_id: currentGameId.value,
        })

        // Clear game state
        gameStore.resetGame()
      }

      // ============================================================================
      // Strategic Decision Actions
      // ============================================================================

      /**
       * Submit defensive decision
       */
      function submitDefensiveDecision(decision: DefensiveDecision) {
        if (!validateConnection()) return

        console.log('[GameActions] Submitting defensive decision:', decision)

        socket.value!.emit('submit_defensive_decision', {
          game_id: currentGameId.value!,
          alignment: decision.alignment,
          infield_depth: decision.infield_depth,
          outfield_depth: decision.outfield_depth,
          hold_runners: decision.hold_runners,
        })
      }

      /**
       * Submit offensive decision
       */
      function submitOffensiveDecision(decision: OffensiveDecision) {
        if (!validateConnection()) return

        console.log('[GameActions] Submitting offensive decision:', decision)

        socket.value!.emit('submit_offensive_decision', {
          game_id: currentGameId.value!,
          approach: decision.approach,
          steal_attempts: decision.steal_attempts,
          hit_and_run: decision.hit_and_run,
          bunt_attempt: decision.bunt_attempt,
        })
      }

      // ============================================================================
      // Manual Outcome Workflow
      // ============================================================================

      /**
       * Roll dice for manual outcome selection
       */
      function rollDice() {
        if (!validateConnection()) return

        if (!gameStore.canRollDice) {
          uiStore.showWarning('Cannot roll dice at this time')
          return
        }

        console.log('[GameActions] Rolling dice')

        socket.value!.emit('roll_dice', {
          game_id: currentGameId.value!,
        })

        uiStore.showInfo('Rolling dice...', 2000)
      }

      /**
       * Submit manual outcome after reading card
       */
      function submitManualOutcome(outcome: PlayOutcome, hitLocation?: string) {
        if (!validateConnection()) return

        if (!gameStore.canSubmitOutcome) {
          uiStore.showWarning('Must roll dice first')
          return
        }

        console.log('[GameActions] Submitting outcome:', outcome, hitLocation)

        socket.value!.emit('submit_manual_outcome', {
          game_id: currentGameId.value!,
          outcome: outcome,
          hit_location: hitLocation,
        })
      }

      // ============================================================================
      // Substitution Actions
      // ============================================================================

      /**
       * Request pinch hitter substitution
       */
      function requestPinchHitter(
        playerOutLineupId: number,
        playerInCardId: number,
        teamId: number
      ) {
        if (!validateConnection()) return

        console.log('[GameActions] Requesting pinch hitter')

        socket.value!.emit('request_pinch_hitter', {
          game_id: currentGameId.value!,
          player_out_lineup_id: playerOutLineupId,
          player_in_card_id: playerInCardId,
          team_id: teamId,
        })

        uiStore.showInfo('Requesting pinch hitter...', 3000)
      }

      /**
       * Request defensive replacement
       */
      function requestDefensiveReplacement(
        playerOutLineupId: number,
        playerInCardId: number,
        newPosition: string,
        teamId: number
      ) {
        if (!validateConnection()) return

        console.log('[GameActions] Requesting defensive replacement')

        socket.value!.emit('request_defensive_replacement', {
          game_id: currentGameId.value!,
          player_out_lineup_id: playerOutLineupId,
          player_in_card_id: playerInCardId,
          new_position: newPosition,
          team_id: teamId,
        })

        uiStore.showInfo('Requesting defensive replacement...', 3000)
      }

      /**
       * Request pitching change
       */
      function requestPitchingChange(
        playerOutLineupId: number,
        playerInCardId: number,
        teamId: number
      ) {
        if (!validateConnection()) return

        console.log('[GameActions] Requesting pitching change')

        socket.value!.emit('request_pitching_change', {
          game_id: currentGameId.value!,
          player_out_lineup_id: playerOutLineupId,
          player_in_card_id: playerInCardId,
          team_id: teamId,
        })

        uiStore.showInfo('Requesting pitching change...', 3000)
      }

      // ============================================================================
      // Data Request Actions
      // ============================================================================

      /**
       * Get lineup for a team
       */
      function getLineup(teamId: number) {
        if (!validateConnection()) return

        console.log('[GameActions] Requesting lineup for team:', teamId)

        socket.value!.emit('get_lineup', {
          game_id: currentGameId.value!,
          team_id: teamId,
        })
      }

      /**
       * Get box score
       */
      function getBoxScore() {
        if (!validateConnection()) return

        console.log('[GameActions] Requesting box score')

        socket.value!.emit('get_box_score', {
          game_id: currentGameId.value!,
        })
      }

      /**
       * Request full game state (for reconnection)
       */
      function requestGameState() {
        if (!validateConnection()) return

        console.log('[GameActions] Requesting full game state')

        socket.value!.emit('request_game_state', {
          game_id: currentGameId.value!,
        })

        uiStore.showInfo('Syncing game state...', 3000)
      }

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

      return {
        // Connection
        joinGame,
        leaveGame,

        // Strategic decisions
        submitDefensiveDecision,
        submitOffensiveDecision,

        // Manual workflow
        rollDice,
        submitManualOutcome,

        // Substitutions
        requestPinchHitter,
        requestDefensiveReplacement,
        requestPitchingChange,

        // Data requests
        getLineup,
        getBoxScore,
        requestGameState,
      }
    }