All files / player-engine/src/ruleset clear-lamp.ts

100% Statements 54/54
98.07% Branches 102/104
100% Functions 17/17
100% Lines 42/42

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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312                            387x                                           387x                           387x 4257x         13x               1093x         1194x         2942x         1442x         588x         16x                 1138x 923x                 214x 214x 41x 38x 34x 33x               387x                                               876x 693x 876x 688x 681x             387x                                 296x   265x 260x 142x         118x                               1172x 824x             1172x                           916x                                                   910x   910x 876x                 910x                                                                 296x 296x                                 1239x    
import type { GaugeType } from './gauge-type.ts';
 
/**
 * Clear lamps, worst first: not played, failed, the clears below EASY (ASSIST EASY, LIGHT ASSIST EASY), the gauge a play
 * cleared on (EASY … EX HARD), FULL COMBO (every note judged GOOD or better), PERFECT (GREAT or better) and MAX (every
 * note a PGREAT). This is the lamps' order: a later lamp is better ({@link compareClearLamps}). Each judge ruleset awards
 * its own subset: LR2 and That-Which-Must-Not-Be-Named stop at FULL COMBO, LR2 has no ASSIST EASY, LIGHT ASSIST EASY or
 * EX HARD, beatoraja goes up to MAX.
 *
 * - ASSIST EASY: beatoraja's `ClearType.AssistEasy` (a clear with an ASSIST-level option) and That-Which-Must-Not-Be-Named's
 *   ASSIST CLEAR (its ASSISTED EASY gauge, and an assist option under its rules).
 * - LIGHT ASSIST EASY: beatoraja's `ClearType.LightAssistEasy`, the ASSIST EASY gauge's clear and a clear with a
 *   LIGHT_ASSIST-level option (beatoraja 0.8.9 `ClearType`, `BMSPlayer.createScoreData`).
 */
export const CLEAR_LAMPS = [
  'NO_PLAY',
  'FAILED',
  'ASSIST_EASY',
  'LIGHT_ASSIST_EASY',
  'EASY',
  'NORMAL',
  'HARD',
  'EX_HARD',
  'FULL_COMBO',
  'PERFECT',
  'MAX',
] as const;
 
/** The class of clear a play earned, or a best record's best one (see {@link CLEAR_LAMPS}). */
export type ClearLamp = (typeof CLEAR_LAMPS)[number];
 
/**
 * Each lamp's stored rank, the number the Leaderboard stores it as. The table is append-only: a new lamp takes the next
 * free rank, so a stored rank never changes meaning. Ranks 0-9 are the lamps that predate LIGHT ASSIST EASY, which took
 * 10 (#603). A stored rank names a lamp; it is not an order.
 */
const CLEAR_LAMP_RANKS: Readonly<Record<ClearLamp, number>> = {
  NO_PLAY: 0,
  FAILED: 1,
  ASSIST_EASY: 2,
  EASY: 3,
  NORMAL: 4,
  HARD: 5,
  EX_HARD: 6,
  FULL_COMBO: 7,
  PERFECT: 8,
  MAX: 9,
  LIGHT_ASSIST_EASY: 10,
};
 
const LAMPS_BY_RANK: ReadonlyMap<number, ClearLamp> = new Map(
  Object.entries(CLEAR_LAMP_RANKS).map(([lamp, rank]) => [rank, lamp as ClearLamp]),
);
 
/** Whether `value` names a clear lamp. */
export function isClearLamp(value: unknown): value is ClearLamp {
  return typeof value === 'string' && (CLEAR_LAMPS as readonly string[]).includes(value);
}
 
/**
 * The stored rank of `lamp`: `NO_PLAY` 0 … `MAX` 9 and `LIGHT_ASSIST_EASY` 10. The ranks are append-only, so a stored rank
 * keeps its lamp; they are not an order ({@link compareClearLampRanks} orders them). An unknown lamp ranks 0.
 */
export function clearLampRank(lamp: ClearLamp): number {
  return CLEAR_LAMP_RANKS[lamp] ?? 0;
}
 
/** The lamp of a stored {@link clearLampRank}, or `undefined` for a rank no lamp has. */
export function clearLampAtRank(rank: number): ClearLamp | undefined {
  return Number.isInteger(rank) ? LAMPS_BY_RANK.get(rank) : undefined;
}
 
/** The position of `lamp` in the lamp order ({@link CLEAR_LAMPS}): higher is better. An unknown lamp is 0. */
export function clearLampOrder(lamp: ClearLamp): number {
  return Math.max(0, CLEAR_LAMPS.indexOf(lamp));
}
 
/** Negative when `a` is the worse lamp, positive when it is the better one, 0 when they are the same. */
export function compareClearLamps(a: ClearLamp, b: ClearLamp): number {
  return clearLampOrder(a) - clearLampOrder(b);
}
 
/** {@link compareClearLamps} for two stored ranks; a rank no lamp has orders as `NO_PLAY`. */
export function compareClearLampRanks(a: number, b: number): number {
  return compareClearLamps(clearLampAtRank(a) ?? 'NO_PLAY', clearLampAtRank(b) ?? 'NO_PLAY');
}
 
/** The better of two lamps; `current` on a tie. */
export function betterClearLamp(current: ClearLamp, candidate: ClearLamp): ClearLamp {
  return compareClearLamps(candidate, current) > 0 ? candidate : current;
}
 
/**
 * The best lamp a judge ruleset awards: LR2 and That-Which-Must-Not-Be-Named stop at FULL COMBO (they have no PERFECT or MAX lamp); beatoraja,
 * and a ruleset this does not know, go up to MAX.
 */
export function topClearLamp(ruleset: string | undefined): ClearLamp {
  // plugout's lamps end at PERFECT (0x43a7d0).
  if (ruleset === 'plugout') return 'PERFECT';
  return ruleset === 'lr2' || ruleset === '____' ? 'FULL_COMBO' : 'MAX';
}
 
/**
 * plugout's gauge lamps (0x43a7d0): NORMAL CLEAR, EASY CLEAR, HARD CLEAR, and FULLCOMBO for a HAZARD clear (PERFECT for
 * EXHAZARD); an EX gauge clears on its type's lamp. It has no EX HARD lamp. Its course gauges are named for the type
 * they come from (`COURSE-HARD`, `GRADE`).
 */
function plugoutGaugeLamp(gauge: string | undefined): ClearLamp {
  const id = (gauge ?? 'NORMAL').toUpperCase().replace(/^(COURSE|GRADE)(-|$)/, '');
  if (id === 'EASY' || id === 'EX-EASY') return 'EASY';
  if (id === 'HARD' || id === 'EX-HARD') return 'HARD';
  if (id === 'HAZARD') return 'FULL_COMBO';
  if (id === 'EX-HAZARD') return 'PERFECT';
  return 'NORMAL';
}
 
/**
 * The lamp a cleared single play earns from its gauge, by the gauge ids the rulesets use (`GROOVE` / `EASY` / `HARD` /
 * `DEATH` under LR2, `ASSIST-EASY` / `EASY` / `NORMAL` / `HARD` / `EX-HARD` / `HAZARD` under beatoraja,
 * `ASSISTED-EASY` … under That-Which-Must-Not-Be-Named). Ruleset-specific exceptions are below.
 */
const GAUGE_LAMPS: Readonly<Record<string, ClearLamp>> = {
  // beatoraja 0.8.9 `ClearType`: the ASSIST EASY gauge (gauge type 0) clears on LightAssistEasy.
  'ASSIST-EASY': 'LIGHT_ASSIST_EASY',
  // That-Which-Must-Not-Be-Named's ASSISTED EASY gauge clears on its own lamp below EASY.
  'ASSISTED-EASY': 'ASSIST_EASY',
  EASY: 'EASY',
  GROOVE: 'NORMAL',
  NORMAL: 'NORMAL',
  HARD: 'HARD',
  'P-ATTACK': 'HARD',
  'G-ATTACK': 'HARD',
  'EX-HARD': 'EX_HARD',
  DEATH: 'EX_HARD',
  'DEATH-CORRECTED': 'EX_HARD',
  HAZARD: 'EX_HARD',
};
 
/**
 * The lamp a cleared single play earns from its gauge (a ruleset gauge id, any case), before the full-combo lamps:
 * `NORMAL` for a gauge the table does not know. LR2 awards HARD only for its HARD-family gauges and NORMAL for every
 * other one but EASY, so a DEATH clear is NORMAL there (OpenLR2 `LR2f.cpp` L2593-L2600, LR2body.exe CheckClear
 * @0x405da0). beatoraja maps HAZARD (gauge type 5) to FULL COMBO in `ClearType`.
 */
function gaugeClearLamp(gauge: string | undefined, ruleset: string | undefined): ClearLamp {
  if (ruleset === 'plugout') return plugoutGaugeLamp(gauge);
  const id = gauge?.toUpperCase();
  if (ruleset === 'lr2' && (id === 'DEATH' || id === 'DEATH-CORRECTED')) return 'NORMAL';
  if (ruleset === 'beatoraja' && id === 'HAZARD') return 'FULL_COMBO';
  return (id !== undefined ? GAUGE_LAMPS[id] : undefined) ?? 'NORMAL';
}
 
/**
 * Course gauges → the lamp a course run's clear earns, before the full-combo lamps: beatoraja's class gauges (CLASS
 * NORMAL, EXCLASS HARD, EXHARDCLASS EX HARD), and LR2's course gauges for a ruleset the course rule does not know.
 */
const COURSE_GAUGE_LAMPS: Readonly<Record<string, ClearLamp>> = {
  CLASS: 'NORMAL',
  EXCLASS: 'HARD',
  EXHARDCLASS: 'EX_HARD',
  'GRADE-HARD': 'HARD',
  'GRADE-DEATH': 'HARD',
  'COURSE-HARD': 'HARD',
  'COURSE-DEATH': 'HARD',
  'COURSE-EASY': 'EASY',
};
 
/**
 * The lamp a cleared course run earns from its gauge. LR2's follows the player's gauge pick (option 3 EASY → EASY, HARD
 * → HARD, anything else NORMAL: LR2 awards HARD only for its HARD-family gauges, so a DEATH clear is NORMAL,
 * LR2body.exe CheckCourseClear @0x4086d0); beatoraja's follows the class gauge the run played on.
 */
function courseGaugeClearLamp(gauge: string, ruleset: string, gaugeType: GaugeType): ClearLamp {
  if (ruleset === 'plugout') return plugoutGaugeLamp(gauge);
  // LR2 resets EASY and G.ATTACK to GROOVE when a grade course starts.
  if (ruleset === 'lr2' && gauge === 'GRADE' && (gaugeType === 'EASY' || gaugeType === 'G-ATTACK')) return 'NORMAL';
  if (ruleset === 'lr2')
    return gaugeType === 'EASY'
      ? 'EASY'
      : gaugeType === 'HARD' || gaugeType === 'P-ATTACK' || gaugeType === 'G-ATTACK'
        ? 'HARD'
        : 'NORMAL';
  return COURSE_GAUGE_LAMPS[gauge] ?? 'NORMAL';
}
 
/**
 * The one clear-lamp rule. A run that did not clear is FAILED whatever its judgments. A cleared full combo is FULL
 * COMBO, PERFECT without a GOOD and MAX with nothing but PGREATs; any other clear takes the lamp of its gauge. The lamp
 * is then kept to the ruleset's own set: no better than `top`.
 */
function finishClearLamp(play: {
  top: ClearLamp;
  cleared: boolean;
  gaugeLamp: ClearLamp;
  fullCombo: boolean;
  great: number;
  good: number;
}): ClearLamp {
  if (!play.cleared) return 'FAILED';
  const lamp: ClearLamp = !play.fullCombo
    ? play.gaugeLamp
    : play.good > 0
      ? 'FULL_COMBO'
      : play.great > 0
        ? 'PERFECT'
        : 'MAX';
  return compareClearLamps(lamp, play.top) > 0 ? play.top : lamp;
}
 
/**
 * Whether a play is a full combo: it has notes and judged every one of them GOOD or better. A play that stopped before
 * its last note is none. A mode that breaks combo on empty POOR also supplies its final combo eligibility.
 */
export function isFullCombo(score: {
  total: number;
  perfect: number;
  great: number;
  good: number;
  fullCombo?: boolean;
}): boolean {
  return score.fullCombo !== false && score.total > 0 && score.perfect + score.great + score.good >= score.total;
}
 
/** What the clear lamp of a single play is read from. */
export interface PlayClearLampFacts {
  /** The judge ruleset the play ran under (`lr2`, `beatoraja`, `____`); an unknown or missing value uses the shared defaults. */
  ruleset?: string;
  /** The ruleset's verdict on the gauge the play ended on. */
  cleared: boolean;
  /** The gauge the play ran on, by its source's gauge id (`GROOVE`, `HARD`, `EX-HARD`, …); none counts as NORMAL. */
  gauge?: string;
  /**
   * The source whose gauge that is, when it is not the judge ruleset's own (`lr2:HARD` under beatoraja judging, #1171).
   * The gauge's lamp follows its source; the full-combo lamps and the top lamp stay the judge ruleset's.
   */
  gaugeSource?: string;
  /** The judgments over the ruleset's `total` judgment notes (PGREATs are `perfect`). */
  score: { total: number; perfect: number; great: number; good: number; fullCombo?: boolean; emptyPoor?: number };
}
 
/**
 * The clear lamp of a single play: FAILED when its gauge did not clear, FULL COMBO / PERFECT / MAX for a cleared full
 * combo ({@link isFullCombo}), else the lamp of the gauge it cleared on, kept to the judge ruleset's own lamps: under
 * LR2 and That-Which-Must-Not-Be-Named a full combo is the top lamp. LR2 awards it before the gauge check; a DEATH clear is NORMAL.
 */
export function resolveClearLamp(play: PlayClearLampFacts): ClearLamp {
  const fullCombo = isFullCombo(play.score);
  // LR2 CheckClear (0x405da4–0x405dc9) returns FULL COMBO before inspecting the gauge.
  if (play.ruleset === 'lr2' && fullCombo) return 'FULL_COMBO';
  const lamp = finishClearLamp({
    top: topClearLamp(play.ruleset),
    cleared: play.cleared,
    gaugeLamp: gaugeClearLamp(play.gauge, play.gaugeSource ?? play.ruleset),
    fullCombo,
    great: play.score.great,
    good: play.score.good,
  });
  // plugout's PERFECT also needs no empty POOR; with one it is a FULLCOMBO.
  return play.ruleset === 'plugout' && lamp === 'PERFECT' && (play.score.emptyPoor ?? 0) > 0 ? 'FULL_COMBO' : lamp;
}
 
/** What the clear lamp of a course run is read from. */
export interface CourseClearLampFacts {
  /** The judge ruleset the run was judged under; a ruleset this does not know stops at FULL COMBO. */
  ruleset: string;
  /** Whether the run cleared: every stage reached and the last one's gauge cleared. */
  cleared: boolean;
  /** The course gauge the run ended on (`GRADE`, `COURSE-HARD`, `CLASS`, …). */
  gauge: string;
  /** The source whose course gauge that is, when it is not the judge ruleset's own (#1171). */
  gaugeSource?: string;
  /** The gauge the player picked: LR2's course lamps follow it. */
  gaugeType: GaugeType;
  /** Judgment notes over every stage of the course, reached or not. */
  noteCount: number;
  /** The course-wide max combo. */
  maxCombo: number;
  /** The final combo eligibility, where the mode breaks combo on empty POOR. */
  fullCombo?: boolean;
  /** The judgments summed over the stages played. */
  judge: { great: number; good: number };
}
 
/**
 * The clear lamp of a course run (LR2 `CheckCourseClear`, beatoraja `MusicResult`): FAILED when it did not clear; a FULL
 * COMBO when the course max combo covers every note (Σ notes == course max combo, as both check), PERFECT without a GOOD
 * and MAX with only PGREATs; otherwise the lamp of its gauge — LR2's by the gauge picked, beatoraja's and That-Which-Must-Not-Be-Named's by the
 * class gauge. Kept to the ruleset's own lamps, with FULL COMBO the top lamp for a ruleset this does not know.
 */
export function resolveCourseClearLamp(run: CourseClearLampFacts): ClearLamp {
  const known =
    run.ruleset === 'lr2' || run.ruleset === 'beatoraja' || run.ruleset === '____' || run.ruleset === 'plugout';
  return finishClearLamp({
    top: known ? topClearLamp(run.ruleset) : 'FULL_COMBO',
    cleared: run.cleared,
    gaugeLamp: courseGaugeClearLamp(run.gauge, run.gaugeSource ?? run.ruleset, run.gaugeType),
    fullCombo: run.fullCombo !== false && run.noteCount > 0 && run.maxCombo >= run.noteCount,
    great: run.judge.great,
    good: run.judge.good,
  });
}
 
/**
 * BP (miss count): BADs, missed POORs and empty POORs, as LR2 and beatoraja count it (LR2 adds an empty POOR to its POOR
 * count, LR2body.exe ApplyJudgeNote @0x4061f1), plus `unjudgedNotes`: the notes a play or course stage that failed or
 * was given up mid-way, or a course run's unreached stages, never judged, which LR2 and beatoraja count in BP (OpenLR2
 * L9111-L9113 and L9436-L9439, beatoraja `BMSPlayer` L915).
 */
export function resolveMissCount(judge: { bad: number; poor: number; emptyPoor: number }, unjudgedNotes = 0): number {
  return judge.bad + judge.poor + judge.emptyPoor + Math.max(0, unjudgedNotes);
}