All files / app-leaderboard/src/pages stats.ts

98.14% Statements 53/54
73.8% Branches 31/42
95.45% Functions 21/22
97.67% Lines 42/43

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                                            12x   144x           28x 28x 280x 280x         12x 12x 12x 10x 9x 9x 10x   96x         14x 14x 7x 8x           12x 52x                         10x           10x 10x                                   10x                     5x           5x 5x                                   5x                                                                           12x           11x 66x       11x 11x               44x       66x                     11x   44x 44x   88x 11x           6x         66x      
import { and, count, countDistinct, eq, isNull, max, min, sql, type SQL } from 'drizzle-orm';
import {
  compareClearLampRanks,
  computeScoreRate,
  _____RANK_LABELS,
  type ____RankLabel,
  type RulesetId,
} from '@be-music/player-engine/ruleset';
import type { Database } from '../db/client.ts';
import { courseFilterConditions, type CourseRankingFilter } from '../courses/ranking.ts';
import { bestScore, chart, courseBestScore, courseScore, score, scoreResult, user } from '../db/schema.ts';
import { filterConditions, type RankingFilter } from '../ranking.ts';
import { RULESET_IDS } from '../rulesets.ts';
import { djLevel, LAMP_STYLES } from './format.ts';
 
/** How many players hold each clear lamp (or DJ Rank), highest first. */
export interface Distribution<Key> {
  total: number;
  buckets: Array<{ key: Key; count: number }>;
}
 
/** Every lamp, highest first by the lamp order (stored ranks are not one); a distribution lists them all so bars line up. */
const LAMPS_HIGH_TO_LOW = Object.keys(LAMP_STYLES)
  .map(Number)
  .sort((a, b) => compareClearLampRanks(b, a));
 
export type DjRank = ____RankLabel;
 
/** Lamp counts (`{lamp, count}` rows from a GROUP BY) as a distribution over every lamp, highest first. */
export function lampDistribution(rows: ReadonlyArray<{ lamp: number; count: number }>): Distribution<number> {
  const counts = new Map<number, number>();
  for (const row of rows) counts.set(row.lamp, (counts.get(row.lamp) ?? 0) + row.count);
  const buckets = LAMPS_HIGH_TO_LOW.map((lamp) => ({ key: lamp, count: counts.get(lamp) ?? 0 }));
  return { total: buckets.reduce((sum, bucket) => sum + bucket.count, 0), buckets };
}
 
/** DJ Ranks of best EX-Scores, AAA first. Charts without notes are left out. */
export function djRankDistribution(bests: ReadonlyArray<{ exScore: number; noteCount: number }>): Distribution<DjRank> {
  const counts = new Map<string, number>();
  let total = 0;
  for (const best of bests) {
    if (best.noteCount <= 0) continue;
    const rank = djLevel(best.exScore, best.noteCount);
    counts.set(rank, (counts.get(rank) ?? 0) + 1);
    total += 1;
  }
  return { total, buckets: _____RANK_LABELS.map((rank) => ({ key: rank, count: counts.get(rank) ?? 0 })) };
}
 
/** Mean score rate (0–1) of best EX-Scores, or null without any. */
export function averageRate(bests: ReadonlyArray<{ exScore: number; noteCount: number }>): number | null {
  const rated = bests.filter((best) => best.noteCount > 0);
  if (rated.length === 0) return null;
  return (
    rated.reduce((sum, best) => sum + computeScoreRate({ exScore: best.exScore, total: best.noteCount }), 0) /
    rated.length
  );
}
 
/** An aggregated timestamp: D1 hands MIN / MAX back as a number even where the column maps to a Date. */
const epochMs = (value: Date | number | null | undefined): number | null =>
  value === null || value === undefined ? null : typeof value === 'number' ? value : value.getTime();
 
export interface ChartStats {
  plays: number;
  players: number;
  firstPlayedAt: number | null;
  lastPlayedAt: number | null;
  /** Best lamps of the players in the ranking (same ruleset, assist class and anonymous filter). */
  lamps: Distribution<number>;
}
 
/** A chart's play counts and lamp spread, for the players the ranking shows. */
export async function loadChartStats(db: Database, filter: RankingFilter): Promise<ChartStats> {
  const playConditions: SQL[] = [
    eq(score.chartFingerprint, filter.chartFingerprint),
    eq(score.assisted, filter.assisted),
    eq(score.hidden, false),
    isNull(score.unranked),
  ];
  if (!filter.includeAnonymous) playConditions.push(eq(user.isAnonymous, false));
  const [[plays], lampRows] = await Promise.all([
    db
      .select({
        plays: count(),
        players: countDistinct(score.userId),
        first: min(score.createdAt),
        last: max(score.createdAt),
      })
      .from(score)
      .innerJoin(user, eq(user.id, score.userId))
      .where(and(...playConditions)),
    db
      .select({ lamp: bestScore.bestLamp, count: count() })
      .from(bestScore)
      .innerJoin(user, eq(user.id, bestScore.userId))
      .where(and(...filterConditions(filter)))
      .groupBy(bestScore.bestLamp),
  ]);
  return {
    plays: plays?.plays ?? 0,
    players: plays?.players ?? 0,
    firstPlayedAt: epochMs(plays?.first),
    lastPlayedAt: epochMs(plays?.last),
    lamps: lampDistribution(lampRows),
  };
}
 
/** A course's run counts and lamp spread, for the players its ranking shows, as {@link loadChartStats} for a chart. */
export async function loadCourseStats(db: Database, filter: CourseRankingFilter): Promise<ChartStats> {
  const runConditions: SQL[] = [
    eq(courseScore.courseFingerprint, filter.courseFingerprint),
    eq(courseScore.assisted, filter.assisted),
    eq(courseScore.hidden, false),
    isNull(courseScore.unranked),
  ];
  if (!filter.includeAnonymous) runConditions.push(eq(user.isAnonymous, false));
  const [[runs], lampRows] = await Promise.all([
    db
      .select({
        plays: count(),
        players: countDistinct(courseScore.userId),
        first: min(courseScore.createdAt),
        last: max(courseScore.createdAt),
      })
      .from(courseScore)
      .innerJoin(user, eq(user.id, courseScore.userId))
      .where(and(...runConditions)),
    db
      .select({ lamp: courseBestScore.bestLamp, count: count() })
      .from(courseBestScore)
      .innerJoin(user, eq(user.id, courseBestScore.userId))
      .where(and(...courseFilterConditions(filter)))
      .groupBy(courseBestScore.bestLamp),
  ]);
  return {
    plays: runs?.plays ?? 0,
    players: runs?.players ?? 0,
    firstPlayedAt: epochMs(runs?.first),
    lastPlayedAt: epochMs(runs?.last),
    lamps: lampDistribution(lampRows),
  };
}
 
export interface PlayerStats {
  plays: number;
  charts: number;
  firstPlayedAt: number | null;
  lastPlayedAt: number | null;
  /** Plays per ruleset the player played under. */
  playsByRuleset: Array<{ ruleset: RulesetId; count: number }>;
  /** Best lamps per chart under the display ruleset, unassisted. */
  lamps: Distribution<number>;
  /** DJ Ranks of the best EX-Scores per chart under the display ruleset, unassisted. */
  djRanks: Distribution<DjRank>;
  averageRate: number | null;
  /** Time spent playing, summed over every play (plays older than the recorded length count once backfilled). */
  playTimeMs: number;
  /** Notes played under the display ruleset, and how each was judged. */
  notes: number;
  judges: JudgeTotals;
}
 
/** Judgment counts summed over plays; `emptyPoor` counts presses that hit no note (空POOR). */
export interface JudgeTotals {
  pgreat: number;
  great: number;
  good: number;
  bad: number;
  poor: number;
  emptyPoor: number;
}
 
export const JUDGE_KEYS = ['pgreat', 'great', 'good', 'bad', 'poor', 'emptyPoor'] as const satisfies ReadonlyArray<
  keyof JudgeTotals
>;
 
/** A player's play counts and the spread of their bests under one ruleset. */
export async function loadPlayerStats(db: Database, userId: string, ruleset: RulesetId): Promise<PlayerStats> {
  const judgeSum = (key: keyof JudgeTotals) =>
    sql<number | null>`sum(json_extract(${scoreResult.judges}, ${`$.${key}`}))`;
  // One walk over the player's plays: counts, dates, plays per ruleset played, and the totals under the display ruleset.
  // The result row is joined on the left so every play counts; the totals add up only plays that have one, as an inner
  // join over the results would.
  const hasResult = sql`${scoreResult.scoreId} is not null`;
  const [[plays], bests] = await Promise.all([
    db
      .select({
        plays: count(),
        charts: countDistinct(score.chartFingerprint),
        first: min(score.createdAt),
        last: max(score.createdAt),
        ...Object.fromEntries(
          RULESET_IDS.map((id) => [`played:${id}`, sql<number>`sum(${score.playedRuleset} = ${id})`]),
        ),
        playTimeMs: sql<number | null>`sum(case when ${hasResult} then ${score.durationMs} end)`,
        notes: sql<number | null>`sum(${scoreResult.noteCount})`,
        ...Object.fromEntries(JUDGE_KEYS.map((key) => [key, judgeSum(key)])),
      })
      .from(score)
      .leftJoin(scoreResult, and(eq(scoreResult.scoreId, score.id), eq(scoreResult.ruleset, ruleset)))
      .where(and(eq(score.userId, userId), eq(score.hidden, false), isNull(score.unranked))),
    db
      .select({ lamp: bestScore.bestLamp, exScore: bestScore.bestEx, noteCount: chart.noteCount })
      .from(bestScore)
      .innerJoin(chart, eq(chart.chartFingerprint, bestScore.chartFingerprint))
      .where(and(eq(bestScore.userId, userId), eq(bestScore.ruleset, ruleset), eq(bestScore.assisted, false))),
  ]);
  const totals = plays as Record<string, unknown> | undefined;
  // Most played first; a tie in ruleset id order, as the grouped query listed them.
  const byRuleset = RULESET_IDS.map((id) => ({ ruleset: id, count: Number(totals?.[`played:${id}`] ?? 0) }))
    .filter((entry) => entry.count > 0)
    .sort((left, right) => right.count - left.count || (left.ruleset < right.ruleset ? -1 : 1));
  const totalOf = (value: unknown) => Number(value ?? 0);
  return {
    plays: plays?.plays ?? 0,
    charts: plays?.charts ?? 0,
    firstPlayedAt: epochMs(plays?.first),
    lastPlayedAt: epochMs(plays?.last),
    playsByRuleset: byRuleset,
    lamps: lampDistribution(bests.map((best) => ({ lamp: best.lamp, count: 1 }))),
    djRanks: djRankDistribution(bests),
    averageRate: averageRate(bests),
    playTimeMs: totalOf(totals?.playTimeMs),
    notes: totalOf(totals?.notes),
    judges: Object.fromEntries(JUDGE_KEYS.map((key) => [key, totalOf(totals?.[key])])) as unknown as JudgeTotals,
  };
}