All files StatsCalculator.ts

95.23% Statements 140/147
80% Branches 28/35
100% Functions 6/6
95.23% Lines 140/147

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 1481x 1x 1x 1x 1x 1x 1x 1x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 5x 498x 498x 498x 498x 498x 498x 498x       498x 498x 498x 455x 455x 498x 5x 5x 5x 5x 5x 5x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 5x 5x 5x 5x 5x 5x 920x 920x 920x 5x 5x 5x 4x 4x 4x 4x 4x 4x 4x 4x 4x 920x 920x 920x 460x 920x 460x 460x 460x       4x 4x 4x 4x 4x 4x 4x 4x 498x 498x 278x 498x 498x 498x 498x 498x 498x 184x 498x 498x 498x 498x 498x 498x 36x 498x   498x 498x 4x  
import { ApiGame } from "../types/apidata"
import { GameMode, SettingsJSON, TimeInterval } from "../types/settings"
import { Stats, Wld } from "../types/stats"
 
/**
 * A utility class for calculating chess statistics based on filtered games.
 */
export class StatsCalculator {
  private validTimeIntervals: { [key: string]: number }
 
  /**
   * Constructs a new instance of the StatsCalculator class.
   *
   * @param {SettingsJSON} settingsJSON - The settings JSON object.
   */
  constructor(settingsJSON: SettingsJSON) {
    this.validTimeIntervals = settingsJSON.timeIntervalsMS
  }
 
  /**
   * Calculates chess statistics for filtered games.
   *
   * @param {ApiGame[]} games - The array of API games.
   * @param {GameMode[]} gameModes - The array of game modes to include.
   * @param {TimeInterval} timeInterval - The time interval for filtering games.
   * @param {string} username - The username for identifying player's games.
   * @returns {Stats} The calculated statistics.
   */
  calculateStats(
    games: ApiGame[],
    gameModes: GameMode[],
    timeInterval: TimeInterval,
    username: string
  ): Stats {
    games = this.filterGames(games, gameModes, timeInterval)
 
    const stats: Stats = {
      wld: { wins: 0, loses: 0, draws: 0, games: games.length },
      accuracy: {
        avg: 0,
        wld: {
          wins: 0,
          loses: 0,
          draws: 0,
          games: games.length
        }
      }
    }
 
    games.forEach((game) => {
      const color =
        game.white.username.toLowerCase() === username.toLowerCase()
          ? "white"
          : "black"
 
      const result = this.transformResult(game[color].result)
      if (!result) {
        console.error("Unknown result", result)
        return
      }
      stats.wld[result]++
 
      if (game.accuracies) {
        stats.accuracy.avg += game.accuracies[color] || 0
        stats.accuracy.wld[result]++
      } else stats.accuracy.wld.games--
    })
 
    stats.accuracy.avg = parseFloat(
      (stats.accuracy.avg / stats.accuracy.wld.games).toFixed(0)
    )
    if (isNaN(stats.accuracy.avg)) stats.accuracy.avg = 0
 
    return stats
  }
 
  /**
   * Filters games based on game modes and time intervals.
   *
   * @param {ApiGame[]} games - The array of API games.
   * @param {GameMode[]} gameModes - The array of game modes to include.
   * @param {TimeInterval} timeInterval - The time interval for filtering games.
   * @returns {ApiGame[]} The filtered array of games.
   */
  filterGames(
    games: ApiGame[],
    gameModes: GameMode[],
    timeInterval: TimeInterval
  ): ApiGame[] {
    return games
      .filter((game) =>
        Array.isArray(gameModes)
          ? gameModes.includes(game.time_class as GameMode)
          : game.time_class === gameModes
      )
      .filter((game) => this.checkTimeInterval(game.end_time, timeInterval))
  }
 
  /**
   * Checks if a game end time falls within the specified time interval.
   *
   * @param {number} end_time - End time of the chess game in seconds since epoch.
   * @param {TimeInterval} time_interval - Time interval as a string (e.g., "last 6 hours", "last week").
   * @returns {boolean} True if the game end time falls within the specified time interval, otherwise false.
   */
  checkTimeInterval(end_time: number, time_interval: TimeInterval): boolean {
    const current_date = Math.floor(Date.now() / 1000)
 
    if (time_interval === "this month") return true
 
    if (end_time > current_date) return false
 
    if (time_interval in this.validTimeIntervals)
      return end_time > current_date - this.validTimeIntervals[time_interval]

    return false
  }
 
  /**
   * Transforms a game result string into a corresponding Wld key.
   *
   * @param {string} result - Game result string.
   * @returns {keyof Wld | undefined} A key of the Wld object (e.g., "wins", "loses", "draws") or undefined if the result is unknown.
   */
  transformResult(result: string): keyof Wld | undefined {
    switch (result) {
      case "win":
        return "wins"
      case "lose":
      case "checkmated":
      case "resigned":
      case "timeout":
      case "abandoned":
      case "bughousepartnerlose":
        return "loses"
      case "agreed":
      case "timevsinsufficient":
      case "repetition":
      case "stalemate":
      case "insufficient":
      case "50move":
        return "draws"
      default:
        throw `Cannot transform unknown result: ${result}`
    }
  }
}