All files / app-leaderboard/src/scores played-file.ts

100% Statements 19/19
100% Branches 18/18
100% Functions 4/4
100% Lines 15/15

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                                    178x 178x 1708x 2281x 178x                         184x 184x 184x 184x     14x             405x 405x 810x 810x   405x    
import type { BeMusicReplay } from '@be-music/player-engine/replay';
 
/**
 * What a replay says about the play beyond its judgments: which chart file it came from, how long it ran, and the
 * lane arrangement it used.
 */
export interface PlayedFile {
  fileSha256: string | null;
  fileMd5: string | null;
  durationMs: number;
  laneOption: string;
}
 
/**
 * The chart file of a play (its well-formed `chart.sha256` / `chart.md5`, lowercased) and its length: from the chart's
 * zero to the end of the last note (a long note's tail) or the last input, whichever is later, in milliseconds.
 */
export function playedFileOf(replay: Pick<BeMusicReplay, 'chart' | 'inputs' | 'play'>): PlayedFile {
  const hashes = new Map(fileHashAliases(replay.chart).map((alias) => [alias.kind, alias.hash]));
  let endUs = 0;
  for (const note of replay.chart.notes) endUs = Math.max(endUs, note.endTimeUs ?? note.timeUs);
  for (const input of replay.inputs) endUs = Math.max(endUs, input.timeUs);
  return {
    fileSha256: hashes.get('sha256') ?? null,
    fileMd5: hashes.get('md5') ?? null,
    durationMs: Math.round(endUs / 1000),
    laneOption: laneOptionOf(replay.play),
  };
}
 
/**
 * The lane arrangement of a play as players name it: `OFF`, `RANDOM`, `MIRROR / RANDOM` for the two sides of a double
 * play when they differ, with ` FLIP` appended when the sides were swapped.
 */
export function laneOptionOf(play: Pick<BeMusicReplay['play'], 'randomLane' | 'dpFlip'>): string {
  const p1 = (play.randomLane?.p1 ?? 'OFF').toUpperCase();
  const p2 = play.randomLane?.p2?.toUpperCase();
  const sides = p2 === undefined || p2 === p1 ? p1 : `${p1} / ${p2}`;
  return play.dpFlip === true ? `${sides} FLIP` : sides;
}
 
const FILE_HASH_PATTERNS = { sha256: /^[0-9a-f]{64}$/, md5: /^[0-9a-f]{32}$/ } as const;
 
/** The well-formed file hashes of a replay's chart, lowercased, as chart aliases. */
export function fileHashAliases(chart: {
  sha256?: string;
  md5?: string;
}): Array<{ kind: 'sha256' | 'md5'; hash: string }> {
  const aliases: Array<{ kind: 'sha256' | 'md5'; hash: string }> = [];
  for (const kind of ['sha256', 'md5'] as const) {
    const hash = chart[kind]?.toLowerCase();
    if (hash !== undefined && FILE_HASH_PATTERNS[kind].test(hash)) aliases.push({ kind, hash });
  }
  return aliases;
}