All files / app-leaderboard/src/scores bests.ts

100% Statements 31/31
85.71% Branches 6/7
100% Functions 10/10
100% Lines 27/27

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                    17x   17x   17x                 168x                   168x       160x                                   932x                                                   8x 6x 6x 7x                                                     6x                 6x 6x 508x 508x 508x             508x                 6x 260x   6x   7x   41x     6x         12x 48x 12x    
import { and, asc, eq, getTableColumns, inArray, isNull } from 'drizzle-orm';
import type { BatchItem } from 'drizzle-orm/batch';
import type { RulesetId } from '@be-music/player-engine/replay';
import type { Database } from '../db/client.ts';
import { bestScore, score, scoreResult } from '../db/schema.ts';
import { mergeBestScores, type BestScores } from '../scoring/best.ts';
 
export type BestScoreRow = typeof bestScore.$inferInsert;
 
/** D1 binds at most 100 parameters to one statement (https://developers.cloudflare.com/d1/platform/limits/). */
const D1_MAX_BOUND_PARAMETERS = 100;
/** Charts one statement lists: the rest of the budget goes to the user id and the hidden flag. */
const CHARTS_PER_STATEMENT = D1_MAX_BOUND_PARAMETERS - 2;
/** Best rows one insert carries: each row binds one parameter per column. */
const BEST_ROWS_PER_INSERT = Math.floor(D1_MAX_BOUND_PARAMETERS / Object.keys(getTableColumns(bestScore)).length);
 
/** The stored personal bests of one player on one chart and assist class, keyed by ruleset. */
export async function loadBestScores(
  db: Database,
  userId: string,
  chartFingerprint: string,
  assisted: boolean,
): Promise<Map<RulesetId, BestScores & { playedHere: boolean }>> {
  const rows = await db
    .select()
    .from(bestScore)
    .where(
      and(
        eq(bestScore.userId, userId),
        eq(bestScore.chartFingerprint, chartFingerprint),
        eq(bestScore.assisted, assisted),
      ),
    );
  return new Map(rows.map((row) => [row.ruleset, { ...toBestScores(row), playedHere: row.playedHere }]));
}
 
export function toBestScores(row: typeof bestScore.$inferSelect): BestScores {
  return {
    bestEx: row.bestEx,
    bestExScoreId: row.bestExScoreId,
    bestExAt: row.bestExAt.getTime(),
    bestLamp: row.bestLamp,
    bestLampScoreId: row.bestLampScoreId,
    minMiss: row.minMiss,
    minMissScoreId: row.minMissScoreId,
    playCount: row.playCount,
  };
}
 
export function toBestScoreRow(
  key: { userId: string; chartFingerprint: string; ruleset: RulesetId; assisted: boolean },
  bests: BestScores,
  playedHere: boolean,
  now: number,
): BestScoreRow {
  return {
    ...key,
    bestEx: bests.bestEx,
    bestExScoreId: bests.bestExScoreId,
    bestExAt: new Date(bests.bestExAt),
    bestLamp: bests.bestLamp,
    bestLampScoreId: bests.bestLampScoreId,
    minMiss: bests.minMiss,
    minMissScoreId: bests.minMissScoreId,
    playCount: bests.playCount,
    playedHere,
    updatedAt: new Date(now),
  };
}
 
/**
 * Recomputes a player's personal bests on the given charts from their stored plays, folding the plays in the order they
 * were accepted so ties keep the earlier play. Hidden plays are left out. Used when plays change owner (an anonymous
 * account linked to a real one) and when a player hides or shows a play.
 */
export async function rebuildBestScores(
  db: Database,
  userId: string,
  chartFingerprints: readonly string[],
  now: number,
): Promise<void> {
  if (chartFingerprints.length === 0) return;
  const chartChunks = chunk(chartFingerprints, CHARTS_PER_STATEMENT);
  const selects = chartChunks.map((charts) =>
    db
      .select({
        scoreId: score.id,
        chartFingerprint: score.chartFingerprint,
        assisted: score.assisted,
        playedRuleset: score.playedRuleset,
        createdAt: score.createdAt,
        ruleset: scoreResult.ruleset,
        exScore: scoreResult.exScore,
        clearLamp: scoreResult.clearLamp,
        missCount: scoreResult.missCount,
      })
      .from(score)
      .innerJoin(scoreResult, eq(scoreResult.scoreId, score.id))
      .where(
        and(
          eq(score.userId, userId),
          inArray(score.chartFingerprint, charts),
          eq(score.hidden, false),
          isNull(score.unranked),
          // A truncated result moves no personal best (ADR 0019).
          isNull(scoreResult.outcome),
        ),
      )
      .orderBy(asc(score.createdAt), asc(score.id)),
  );
  // Each chart's plays come from one statement, in order, so folding the chunks one after another keeps that order.
  const rows = (await db.batch(selects as [(typeof selects)[number], ...(typeof selects)[number][]])).flat();
 
  type Folded = {
    chartFingerprint: string;
    ruleset: RulesetId;
    assisted: boolean;
    bests: BestScores;
    playedHere: boolean;
  };
  const folded = new Map<string, Folded>();
  for (const row of rows) {
    const key = `${row.chartFingerprint}\u0000${row.ruleset}\u0000${row.assisted ? 1 : 0}`;
    const current = folded.get(key);
    const { next } = mergeBestScores(current?.bests, {
      scoreId: row.scoreId,
      playedAt: row.createdAt.getTime(),
      exScore: row.exScore,
      lamp: row.clearLamp,
      missCount: row.missCount,
    });
    folded.set(key, {
      chartFingerprint: row.chartFingerprint,
      ruleset: row.ruleset,
      assisted: row.assisted,
      bests: next,
      playedHere: (current?.playedHere ?? false) || row.playedRuleset === row.ruleset,
    });
  }
 
  const values = [...folded.values()].map(({ chartFingerprint, ruleset, assisted, bests, playedHere }) =>
    toBestScoreRow({ userId, chartFingerprint, ruleset, assisted }, bests, playedHere, now),
  );
  const statements: BatchItem<'sqlite'>[] = [
    ...chartChunks.map((charts) =>
      db.delete(bestScore).where(and(eq(bestScore.userId, userId), inArray(bestScore.chartFingerprint, charts))),
    ),
    ...chunk(values, BEST_ROWS_PER_INSERT).map((rows) => db.insert(bestScore).values(rows)),
  ];
  // A batch runs as one transaction: the old bests are never gone without the new ones in their place.
  await db.batch(statements as [BatchItem<'sqlite'>, ...BatchItem<'sqlite'>[]]);
}
 
/** `items` in runs of at most `size`, in order. */
function chunk<T>(items: readonly T[], size: number): T[][] {
  const runs: T[][] = [];
  for (let start = 0; start < items.length; start += size) runs.push(items.slice(start, start + size));
  return runs;
}