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

100% Statements 63/63
100% Branches 81/81
100% Functions 23/23
100% Lines 51/51

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        17x                                                                             629x     17x                       45x         17x       214x 213x       202x       129x 129x 129x 129x 128x 119x         268x         137x               148x 148x               130x 18x 9x       27x 130x 130x         2x 1x         26x 25x 25x     17x                 15x 14x 14x 13x 2x 2x   11x               4x                     13x     17x       51x               137x 137x 133x 133x         128x 256x 128x         9x         47x         3x            
import { clearLampRank, computeScoreRate, resolve____RankLabel } from '@be-music/player-engine/ruleset';
import { en, type Messages } from './i18n/en.ts';
 
/** Display name and colour of each clear lamp, LR2 / beatoraja style, by the rank the Leaderboard stores it as. */
export const LAMP_STYLES: Readonly<Record<number, { label: string; color: string; meaning: string }>> = {
  [clearLampRank('FAILED')]: { label: 'FAILED', color: '#7a1f2b', meaning: 'Did not clear.' },
  [clearLampRank('ASSIST_EASY')]: {
    label: 'ASSIST EASY',
    color: '#8a5cf6',
    meaning:
      'Cleared with an ASSIST option (beatoraja) or on the ASSISTED EASY gauge (That-Which-Must-Not-Be-Named), or before #603 on the ASSIST EASY gauge.',
  },
  [clearLampRank('LIGHT_ASSIST_EASY')]: {
    label: 'LIGHT ASSIST EASY',
    color: '#d946ef',
    meaning: 'Cleared on the ASSIST EASY gauge, or with a LIGHT ASSIST option (beatoraja).',
  },
  [clearLampRank('EASY')]: { label: 'EASY CLEAR', color: '#3fb950', meaning: 'Cleared on the EASY gauge.' },
  [clearLampRank('NORMAL')]: {
    label: 'CLEAR',
    color: '#3b82f6',
    meaning: en.lampMeanings[4],
  },
  [clearLampRank('HARD')]: { label: 'HARD CLEAR', color: '#e5e7eb', meaning: 'Cleared on the HARD gauge.' },
  [clearLampRank('EX_HARD')]: {
    label: 'EX HARD CLEAR',
    color: '#f5c518',
    meaning: en.lampMeanings[6],
  },
  [clearLampRank('FULL_COMBO')]: {
    label: 'FULL COMBO',
    color: '#22d3ee',
    meaning: en.lampMeanings[7],
  },
  [clearLampRank('PERFECT')]: {
    label: 'PERFECT',
    color: '#f472b6',
    meaning: 'A full combo without a GOOD (beatoraja).',
  },
  [clearLampRank('MAX')]: { label: 'MAX', color: '#ffffff', meaning: 'Every note a PGREAT (beatoraja).' },
};
 
export function lampStyle(lamp: number): { label: string; color: string; meaning: string } {
  return LAMP_STYLES[lamp] ?? { label: 'NO PLAY', color: '#6b7280', meaning: 'Not played.' };
}
 
export const RULESET_LABELS: Readonly<Record<string, string>> = {
  lr2: 'LR2',
  beatoraja: 'beatoraja',
  ____: '????',
  plugout: 'plugout',
};
 
/**
 * A ruleset as the pages name it: "LR2 rules". The bare name reads like the software a play came from; a ruleset is
 * only how the Leaderboard scores the play.
 */
export function rulesetName(ruleset: string): string {
  return `${RULESET_LABELS[ruleset] ?? ruleset} rules`;
}
 
/** What a ruleset is, for tooltips next to ruleset names. */
export const RULESET_MEANING =
  'Every play is scored under each ruleset: the judgment windows, gauges and clear lamps of LR2, beatoraja or That-Which-Must-Not-Be-Named, applied by the Be-Music engine to the replay. It is how the score is counted, not the software the play came from.';
 
/** EX score rate in percent with two decimals (`ex / (notes × 2)`), or `-` without notes. */
export function formatScoreRate(exScore: number, noteCount: number): string {
  if (noteCount <= 0) return '-';
  return `${(computeScoreRate({ exScore, total: noteCount }) * 100).toFixed(2)}%`;
}
 
/** DJ LEVEL on the That-Which-Must-Not-Be-Named ninths (AAA from 8/9 of the maximum EX score down to F), or `-` without notes. */
export const djLevel = (exScore: number, noteCount: number): string => resolve____RankLabel(exScore, noteCount);
 
/** A length of time for totals: `3h 25m`, `25m 12s`, `48s`. */
export function formatPlayTime(ms: number, duration: Messages['duration'] = en.duration): string {
  const seconds = Math.max(0, Math.floor(ms / 1000));
  const hours = Math.floor(seconds / 3600);
  const minutes = Math.floor((seconds % 3600) / 60);
  if (hours > 0) return duration.hoursMinutes(hours, minutes);
  if (minutes > 0) return duration.minutesSeconds(minutes, seconds % 60);
  return duration.seconds(seconds);
}
 
/** `2026-10-13 14:51` in UTC, so a cached page reads the same everywhere. */
export function formatTimestamp(epochMs: number): string {
  return `${new Date(epochMs).toISOString().slice(0, 16).replace('T', ' ')} UTC`;
}
 
/** A chart's display title, falling back to its fingerprint when the replay carried no title. */
export function chartTitle(title: string | null, chartFingerprint: string): string {
  return title !== null && title.trim().length > 0 ? title : `${chartFingerprint.slice(0, 18)}…`;
}
 
/**
 * The gauge at the end of a play: its type and percentage with one decimal (`GROOVE 82.4%`), or just the type for
 * plays stored before the final value was recorded.
 */
export function formatGauge(type: string, final: number | null): string {
  const label = type === 'DEATH-CORRECTED' ? 'DEATH corrected' : type;
  return final === null ? label : `${label} ${final.toFixed(1)}%`;
}
 
/**
 * The software that recorded a play, from its replay's `recorder`: `be-music-web 0.5.0 (build 6471cd16, engine 0.8.0,
 * web)`, or `unknown` for logs written before the field existed.
 */
export function formatRecorder(recorder: Record<string, string> | null): string {
  if (recorder === null) return 'unknown';
  const name = [recorder.name, recorder.version].filter((part) => part !== undefined && part !== '').join(' ');
  const extras = [
    recorder.build !== undefined ? `build ${recorder.build}` : undefined,
    recorder.engine !== undefined ? `engine ${recorder.engine}` : undefined,
    recorder.platform,
  ].filter((part): part is string => part !== undefined && part !== '');
  const head = name !== '' ? name : 'unknown app';
  return extras.length > 0 ? `${head} (${extras.join(', ')})` : head;
}
 
/** EX-Score with its maximum and rate: `81 / 104 (77.88%)` (the maximum is two per note). */
export function formatExScore(exScore: number, noteCount: number): string {
  if (noteCount <= 0) return String(exScore);
  return `${exScore} / ${noteCount * 2} (${formatScoreRate(exScore, noteCount)})`;
}
 
/** `7keys` → `7 KEYS`, `popn-9k` → `POPN-9K`: a replay's lane mode as players name it. */
export function formatLaneMode(laneMode: string | null): string {
  if (laneMode === null || laneMode.length === 0) return '-';
  const keys = /^(\d+)keys?$/i.exec(laneMode);
  return keys !== null ? `${keys[1]} KEYS` : laneMode.toUpperCase();
}
 
const RANK_NAMES = ['VERY HARD', 'HARD', 'NORMAL', 'EASY', 'VERY EASY'];
 
/**
 * The judge rank as the chart wrote it, then the window it gives: `#RANK 2 (NORMAL) · 75%`, `#DEFEXRANK 120 · 90%`,
 * or the percentage alone (bmson `judge_rank`, or no rank written).
 */
export function formatJudgeRank(
  judgeRank: { percent: number; sourceRank?: number; sourceExRank?: number } | null,
): string {
  if (judgeRank === null) return '-';
  const percent = `${Number(judgeRank.percent.toFixed(2))}%`;
  if (judgeRank.sourceExRank !== undefined) return `#DEFEXRANK ${judgeRank.sourceExRank} · ${percent}`;
  if (judgeRank.sourceRank !== undefined) {
    const name = RANK_NAMES[judgeRank.sourceRank];
    return `#RANK ${judgeRank.sourceRank}${name !== undefined ? ` (${name})` : ''} · ${percent}`;
  }
  return percent;
}
 
/**
 * How the chart's long notes are judged (`#LNMODE`, or the default LN when the chart names none): 1 LN, 2 CN (charge
 * note), 3 HCN (hell charge note), each with what it means. The mode applies only if the chart has long notes.
 */
export function formatLNMode(lnMode: number | null): string {
  return lnMode === 1
    ? 'LN — the start is judged; hold to the end'
    : lnMode === 2
      ? 'CN — the release is judged too'
      : lnMode === 3
        ? 'HCN — the release is judged, and holding gains or drains the gauge'
        : '-';
}
 
/** A 0–1 rate as `77.88%`, or `-`. */
export function formatPercent(rate: number | null): string {
  return rate === null ? '-' : `${(rate * 100).toFixed(2)}%`;
}
 
const DIFFICULTY_NAMES = ['BEGINNER', 'NORMAL', 'HYPER', 'ANOTHER', 'INSANE'];
 
/** `#DIFFICULTY` by name: 1 BEGINNER .. 5 INSANE, or undefined for anything else. */
export function difficultyName(difficulty: number | null): string | undefined {
  return difficulty === null ? undefined : DIFFICULTY_NAMES[difficulty - 1];
}
 
/**
 * A lane mode the short way players write it: `7 KEY SP` → `7K`, `14 KEY DP` → `14K`, `5 KEY DP` → `10K`,
 * `9 KEY (PMS-STD)` → `9K`, and the older `7keys` → `7K`. Undefined when there is none or it names no key count.
 */
export function shortLaneMode(laneMode: string | null): string | undefined {
  const keys = laneMode === null ? null : /^(\d+)\s*keys?(?:\s+(SP|DP))?/i.exec(laneMode);
  if (keys === null) return undefined;
  const count = Number(keys[1]);
  return `${keys[2]?.toUpperCase() === 'DP' && count === 5 ? 10 : count}K`;
}
 
/** A chart's mode and level in one short label: `7K ☆12`, `14K`, `☆★5`; undefined when neither is known. */
export function formatChartLevel(laneMode: string | null, playLevel: string | null): string | undefined {
  const parts = [shortLaneMode(laneMode), playLevel !== null && playLevel !== '' ? `☆${playLevel}` : undefined];
  const label = parts.filter((part) => part !== undefined).join(' ');
  return label === '' ? undefined : label;
}
 
/** A signed difference for a change against an earlier best: `+23`, `−5` (a minus sign, not a hyphen), `±0`. */
export function formatDelta(delta: number): string {
  return delta > 0 ? `+${delta}` : delta < 0 ? `−${-delta}` : '±0';
}
 
/** Whether a play's gauge is worth showing next to its lamp: only when it did not clear, where it tells how close. */
export function showsGauge(clearLamp: number): boolean {
  return clearLamp <= clearLampRank('FAILED');
}
 
/** What a verification layer means (docs/leaderboard.md section 5.3), for its tooltip and the info panel. */
export function verificationMeaning(verification: string): string {
  return verification === 'L4'
    ? 'L4 — the chart played is known to be this one'
    : verification === 'L3'
      ? 'L3 — the chart played matches its fingerprint'
      : verification;
}