All files / web/src/arcade-games/matching/utils gameScoring.ts

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

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 332                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       
import type { GameStatistics, MemoryPairsState, Player } from '../types'

// Calculate final game score based on multiple factors
export function calculateFinalScore(
  matchedPairs: number,
  totalPairs: number,
  moves: number,
  gameTime: number,
  difficulty: number,
  gameMode: 'single' | 'two-player'
): number {
  // Base score for completing pairs
  const baseScore = matchedPairs * 100

  // Efficiency bonus (fewer moves = higher bonus)
  const idealMoves = totalPairs * 2 // Perfect game would be 2 moves per pair
  const efficiency = idealMoves / Math.max(moves, idealMoves)
  const efficiencyBonus = Math.round(baseScore * efficiency * 0.5)

  // Time bonus (faster completion = higher bonus)
  const timeInMinutes = gameTime / (1000 * 60)
  const timeBonus = Math.max(0, Math.round((1000 * difficulty) / timeInMinutes))

  // Difficulty multiplier
  const difficultyMultiplier = 1 + (difficulty - 6) * 0.1

  // Two-player mode bonus
  const modeMultiplier = gameMode === 'two-player' ? 1.2 : 1.0

  const finalScore = Math.round(
    (baseScore + efficiencyBonus + timeBonus) * difficultyMultiplier * modeMultiplier
  )

  return Math.max(0, finalScore)
}

// Calculate star rating (1-5 stars) based on performance
export function calculateStarRating(
  accuracy: number,
  efficiency: number,
  gameTime: number,
  difficulty: number
): number {
  // Normalize time score (assuming reasonable time ranges)
  const expectedTime = difficulty * 30000 // 30 seconds per pair as baseline
  const timeScore = Math.max(0, Math.min(100, (expectedTime / gameTime) * 100))

  // Weighted average of different factors
  const overallScore = accuracy * 0.4 + efficiency * 0.4 + timeScore * 0.2

  // Convert to stars
  if (overallScore >= 90) return 5
  if (overallScore >= 80) return 4
  if (overallScore >= 70) return 3
  if (overallScore >= 60) return 2
  return 1
}

// Get achievement badges based on performance
export interface Achievement {
  id: string
  name: string
  description: string
  icon: string
  earned: boolean
}

export function getAchievements(
  state: MemoryPairsState,
  gameMode: 'single' | 'multiplayer'
): Achievement[] {
  const { matchedPairs, totalPairs, moves, scores, gameStartTime, gameEndTime } = state
  const accuracy = moves > 0 ? (matchedPairs / moves) * 100 : 0
  const gameTime = gameStartTime && gameEndTime ? gameEndTime - gameStartTime : 0
  const gameTimeInSeconds = gameTime / 1000

  const achievements: Achievement[] = [
    {
      id: 'perfect_game',
      name: 'Perfect Memory',
      description: 'Complete a game with 100% accuracy',
      icon: '🧠',
      earned: matchedPairs === totalPairs && moves === totalPairs * 2,
    },
    {
      id: 'speed_demon',
      name: 'Speed Demon',
      description: 'Complete a game in under 2 minutes',
      icon: '⚡',
      earned: gameTimeInSeconds > 0 && gameTimeInSeconds < 120 && matchedPairs === totalPairs,
    },
    {
      id: 'accuracy_ace',
      name: 'Accuracy Ace',
      description: 'Achieve 90% accuracy or higher',
      icon: '🎯',
      earned: accuracy >= 90 && matchedPairs === totalPairs,
    },
    {
      id: 'marathon_master',
      name: 'Marathon Master',
      description: 'Complete the hardest difficulty (15 pairs)',
      icon: '🏃',
      earned: totalPairs === 15 && matchedPairs === totalPairs,
    },
    {
      id: 'complement_champion',
      name: 'Complement Champion',
      description: 'Master complement pairs mode',
      icon: '🤝',
      earned:
        state.gameType === 'complement-pairs' && matchedPairs === totalPairs && accuracy >= 85,
    },
    {
      id: 'two_player_triumph',
      name: 'Two-Player Triumph',
      description: 'Win a two-player game',
      icon: '👥',
      earned:
        gameMode === 'multiplayer' &&
        matchedPairs === totalPairs &&
        Object.keys(scores).length > 1 &&
        Math.max(...Object.values(scores)) > 0,
    },
    {
      id: 'shutout_victory',
      name: 'Shutout Victory',
      description: 'Win a two-player game without opponent scoring',
      icon: '🛡️',
      earned:
        gameMode === 'multiplayer' &&
        matchedPairs === totalPairs &&
        Object.values(scores).some((score) => score === totalPairs) &&
        Object.values(scores).some((score) => score === 0),
    },
    {
      id: 'comeback_kid',
      name: 'Comeback Kid',
      description: 'Win after being behind by 3+ points',
      icon: '🔄',
      earned: false, // This would need more complex tracking during the game
    },
    {
      id: 'first_timer',
      name: 'First Timer',
      description: 'Complete your first game',
      icon: '🌟',
      earned: matchedPairs === totalPairs,
    },
    {
      id: 'consistency_king',
      name: 'Consistency King',
      description: 'Achieve 80%+ accuracy in 5 consecutive games',
      icon: '👑',
      earned: false, // This would need persistent game history
    },
  ]

  return achievements
}

// Get performance metrics and analysis
export function getPerformanceAnalysis(state: MemoryPairsState): {
  statistics: GameStatistics
  grade: 'A+' | 'A' | 'B+' | 'B' | 'C+' | 'C' | 'D' | 'F'
  strengths: string[]
  improvements: string[]
  starRating: number
} {
  const { matchedPairs, totalPairs, moves, difficulty, gameStartTime, gameEndTime } = state
  const gameTime = gameStartTime && gameEndTime ? gameEndTime - gameStartTime : 0

  // Calculate statistics
  const accuracy = moves > 0 ? (matchedPairs / moves) * 100 : 0
  const averageTimePerMove = moves > 0 ? gameTime / moves : 0
  const statistics: GameStatistics = {
    totalMoves: moves,
    matchedPairs,
    totalPairs,
    gameTime,
    accuracy,
    averageTimePerMove,
  }

  // Calculate efficiency (ideal vs actual moves)
  const idealMoves = totalPairs * 2
  const efficiency = (idealMoves / Math.max(moves, idealMoves)) * 100

  // Determine grade
  let grade: 'A+' | 'A' | 'B+' | 'B' | 'C+' | 'C' | 'D' | 'F' = 'F'
  if (accuracy >= 95 && efficiency >= 90) grade = 'A+'
  else if (accuracy >= 90 && efficiency >= 85) grade = 'A'
  else if (accuracy >= 85 && efficiency >= 80) grade = 'B+'
  else if (accuracy >= 80 && efficiency >= 75) grade = 'B'
  else if (accuracy >= 75 && efficiency >= 70) grade = 'C+'
  else if (accuracy >= 70 && efficiency >= 65) grade = 'C'
  else if (accuracy >= 60 && efficiency >= 50) grade = 'D'

  // Calculate star rating
  const starRating = calculateStarRating(accuracy, efficiency, gameTime, difficulty)

  // Analyze strengths and areas for improvement
  const strengths: string[] = []
  const improvements: string[] = []

  if (accuracy >= 90) {
    strengths.push('Excellent memory and pattern recognition')
  } else if (accuracy < 70) {
    improvements.push('Focus on remembering card positions more carefully')
  }

  if (efficiency >= 85) {
    strengths.push('Very efficient with minimal unnecessary moves')
  } else if (efficiency < 60) {
    improvements.push('Try to reduce random guessing and use memory strategies')
  }

  const avgTimePerMoveSeconds = averageTimePerMove / 1000
  if (avgTimePerMoveSeconds < 3) {
    strengths.push('Quick decision making')
  } else if (avgTimePerMoveSeconds > 8) {
    improvements.push('Practice to improve decision speed')
  }

  if (difficulty >= 12) {
    strengths.push('Tackled challenging difficulty levels')
  }

  if (state.gameType === 'complement-pairs' && accuracy >= 80) {
    strengths.push('Strong mathematical complement skills')
  }

  // Fallback messages
  if (strengths.length === 0) {
    strengths.push('Keep practicing to improve your skills!')
  }
  if (improvements.length === 0) {
    improvements.push('Great job! Continue challenging yourself with harder difficulties.')
  }

  return {
    statistics,
    grade,
    strengths,
    improvements,
    starRating,
  }
}

// Format time duration for display
export function formatGameTime(milliseconds: number): string {
  const seconds = Math.floor(milliseconds / 1000)
  const minutes = Math.floor(seconds / 60)
  const remainingSeconds = seconds % 60

  if (minutes > 0) {
    return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`
  }
  return `${remainingSeconds}s`
}

// Get two-player game winner
// @deprecated Use getMultiplayerWinner instead which supports N players
export function getTwoPlayerWinner(
  state: MemoryPairsState,
  activePlayers: Player[]
): {
  winner: Player | 'tie'
  winnerScore: number
  loserScore: number
  margin: number
} {
  const { scores } = state
  const [player1, player2] = activePlayers

  if (!player1 || !player2) {
    throw new Error('getTwoPlayerWinner requires at least 2 active players')
  }

  const score1 = scores[player1] || 0
  const score2 = scores[player2] || 0

  if (score1 > score2) {
    return {
      winner: player1,
      winnerScore: score1,
      loserScore: score2,
      margin: score1 - score2,
    }
  } else if (score2 > score1) {
    return {
      winner: player2,
      winnerScore: score2,
      loserScore: score1,
      margin: score2 - score1,
    }
  } else {
    return {
      winner: 'tie',
      winnerScore: score1,
      loserScore: score2,
      margin: 0,
    }
  }
}

// Get multiplayer game winner (supports N players)
export function getMultiplayerWinner(
  state: MemoryPairsState,
  activePlayers: Player[]
): {
  winners: Player[]
  winnerScore: number
  scores: { [playerId: string]: number }
  isTie: boolean
} {
  const { scores } = state

  // Find the highest score
  const maxScore = Math.max(...activePlayers.map((playerId) => scores[playerId] || 0))

  // Find all players with the highest score
  const winners = activePlayers.filter((playerId) => (scores[playerId] || 0) === maxScore)

  return {
    winners,
    winnerScore: maxScore,
    scores,
    isTie: winners.length > 1,
  }
}