All files / player-engine/src/core judge-rules.ts

97.67% Statements 42/43
97.67% Branches 42/43
100% Functions 15/15
97.61% Lines 41/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                                                      186069x                   138x                               82341x 82341x               82337x             3647x   1667x   795x   695x   490x               4x 4x 4x 1x   3x 1x   2x     138x                       3639x         3639x 3639x         49730x         1333146x 1007610x   325536x         1270489x 966173x   304316x         1269569x 1269569x         466x 1x   465x 465x         918x 918x         16760x                             465x 465x 10x   465x    
import type { LongNoteMode } from '@be-music/chart-model';
import type { LongNoteStyle } from '../ruleset/definitions.ts';
import { classifyRulesetJudge, RULESET_JUDGE_NONE } from '../ruleset/judge.ts';
import type { JudgeWindowSetUs } from '../ruleset/definitions.ts';
import {
  isChargeLongNoteMode,
  resolveEffectiveLongNoteMode,
  resolveLNEarlyRelease,
  type LNJudgeIndex,
  type LNTimedJudge,
} from '../ruleset/long-note.ts';
import type { RulesetConfig } from '../ruleset/definitions.ts';
import type { TimedPlayableNote } from '../playable-notes.ts';
import type { JudgmentKind } from './scoring.ts';
import type { PlayerUILongNoteMode } from './ui-signal-bus.ts';
 
/**
 * Pure judgment rules of the live engine: timing classification, long-note judgment merging and the long-note mode a
 * ruleset plays. Shared by the judge core (`judge-core.ts`) and the auto-play driver; nothing here reads a clock.
 */
 
/**
 * `noteTime - inputTime` in whole microseconds, the unit of the replay's clock and of the ruleset window tables.
 * The difference of two float second values can land a hair outside a window edge that an exact delta sits on (a
 * press 150 ms late reading as 150.0000001 ms), so every window comparison of the judge core goes through this.
 */
export function judgeDeltaUs(noteSeconds: number, inputSeconds: number): number {
  return Math.round((noteSeconds - inputSeconds) * 1e6);
}
 
/** A judgment with the signed timing it was made at (positive = the press was LATE). */
export interface TimedManualJudge {
  kind: JudgmentKind;
  signedDeltaMs: number;
}
 
/** Judgment indices, best to worst, in the order the ruleset window sets use. */
export const RULESET_JUDGE_KINDS: readonly [JudgmentKind, JudgmentKind, JudgmentKind, JudgmentKind] = [
  'PERFECT',
  'GREAT',
  'GOOD',
  'BAD',
];
 
/**
 * Classify a signed timing delta against one ruleset window set, or `undefined` when the press cannot reach the
 * note at all. Both legs of every window matter: beatoraja's seven-key BAD window reaches 280 ms late but only
 * 220 ms early, so "within the BAD width" is not a question that can be asked of an absolute delta.
 *
 * `signedDeltaMs` is the engine's convention (positive = the press was LATE); the window tables use beatoraja's
 * (`noteTime - inputTime`, positive = EARLY), hence the negation.
 */
export function resolveManualJudgmentKind(signedDeltaMs: number, windows: JudgeWindowSetUs): JudgmentKind | undefined {
  const judge = classifyRulesetJudge(Math.round(-signedDeltaMs * 1000), windows);
  return judge === RULESET_JUDGE_NONE ? undefined : RULESET_JUDGE_KINDS[judge];
}
 
/**
 * Same classification, for contexts where "out of reach" is itself a miss — a long-note end that the player never
 * released inside its window is a POOR, not a non-event.
 */
export function resolveManualTimedJudge(signedDeltaMs: number, windows: JudgeWindowSetUs): TimedManualJudge {
  return {
    kind: resolveManualJudgmentKind(signedDeltaMs, windows) ?? 'POOR',
    signedDeltaMs,
  };
}
 
function resolveJudgeSeverity(judge: JudgmentKind): number {
  switch (judge) {
    case 'PERFECT':
      return 0;
    case 'GREAT':
      return 1;
    case 'GOOD':
      return 2;
    case 'BAD':
      return 3;
    case 'POOR':
      return 4;
  }
}
 
/** An LN's single judgment: the worse of head and tail, and on a tie the one further off its mark. */
export function combineLongNoteJudges(head: TimedManualJudge, tail: TimedManualJudge): TimedManualJudge {
  const headSeverity = resolveJudgeSeverity(head.kind);
  const tailSeverity = resolveJudgeSeverity(tail.kind);
  if (headSeverity > tailSeverity) {
    return head;
  }
  if (tailSeverity > headSeverity) {
    return tail;
  }
  return Math.abs(head.signedDeltaMs) >= Math.abs(tail.signedDeltaMs) ? head : tail;
}
 
const LN_JUDGE_KINDS: readonly JudgmentKind[] = ['PERFECT', 'GREAT', 'GOOD', 'BAD', 'POOR'];
 
/**
 * The single judgment of an LN released before its tail, by the ruleset's rule ({@link resolveLNEarlyRelease}).
 * `releaseDeltaMs` is the release against the tail in the engine's convention (negative = before the tail).
 */
export function resolveLongNoteEarlyReleaseJudge(
  ruleset: Pick<RulesetConfig, 'lnEarlyRelease'>,
  head: TimedManualJudge,
  releaseDeltaMs: number,
  endWindows: JudgeWindowSetUs,
): TimedManualJudge {
  const toRuleset = (judge: TimedManualJudge): LNTimedJudge => ({
    judge: resolveJudgeSeverity(judge.kind) as LNJudgeIndex,
    dmUs: Math.round(-judge.signedDeltaMs * 1000),
  });
  // Whole microseconds, like every other window comparison (see `judgeDeltaUs`).
  const released = resolveLNEarlyRelease(ruleset, toRuleset(head), Math.round(-releaseDeltaMs * 1000), endWindows);
  return { kind: LN_JUDGE_KINDS[released.judge]!, signedDeltaMs: -released.dmUs / 1000 };
}
 
/** True when a playable note carries a tail — a long / charge note rather than a single hit. */
export function isLongPlayableNote(note: TimedPlayableNote): boolean {
  return typeof note.endSeconds === 'number' && Number.isFinite(note.endSeconds) && note.endSeconds > note.seconds;
}
 
/** The tail time of a long note, or `undefined` for a single hit. */
export function resolveLongNoteEndSeconds(note: TimedPlayableNote): number | undefined {
  if (typeof note.endSeconds !== 'number' || !Number.isFinite(note.endSeconds) || note.endSeconds <= note.seconds) {
    return undefined;
  }
  return note.endSeconds;
}
 
/** The chart's long-note mode of a note (2 when the chart names none), or `undefined` for a single hit. */
export function resolvePlayableLongNoteMode(note: TimedPlayableNote): LongNoteMode | undefined {
  if (resolveLongNoteEndSeconds(note) === undefined) {
    return undefined;
  }
  return note.longNoteMode ?? 2;
}
 
/** The long-note mode the ruleset plays this note as, or `undefined` for a note without one. */
export function resolveRulesetLongNoteMode(style: LongNoteStyle, note: TimedPlayableNote): LongNoteMode | undefined {
  const chartMode = resolvePlayableLongNoteMode(note);
  return chartMode === undefined ? undefined : resolveEffectiveLongNoteMode(style, chartMode);
}
 
/** True when the ruleset scores this long note's tail as a judgment of its own (charge modes). */
export function longNoteScoresTail(style: LongNoteStyle, note: TimedPlayableNote): boolean {
  if (!isLongPlayableNote(note)) {
    return false;
  }
  const mode = resolveRulesetLongNoteMode(style, note);
  return mode !== undefined && isChargeLongNoteMode(mode);
}
 
/** The long-note mode a log line reports for a note: the chart's mode, LN for anything else. */
export function resolveLoggedLongNoteMode(note: TimedPlayableNote): 1 | 2 | 3 {
  const mode = resolvePlayableLongNoteMode(note);
  return mode === 2 || mode === 3 ? mode : 1;
}
 
/** The UI-command spelling of an effective long-note mode. */
export function toUILongNoteMode(mode: LongNoteMode): PlayerUILongNoteMode {
  return mode === 3 ? 'hcn' : mode === 2 ? 'cn' : 'ln';
}
 
/** An auto-played long note waiting for its tail. */
export interface PendingAutoLongNoteState {
  endSeconds: number;
  note: TimedPlayableNote;
}
 
/** Inserts a pending auto long note, keeping the queue sorted by tail time (stable for equal tails). */
export function insertPendingAutoLongNote(
  pendingNotes: PendingAutoLongNoteState[],
  note: TimedPlayableNote,
  endSeconds: number,
): void {
  let insertIndex = pendingNotes.length;
  while (insertIndex > 0 && pendingNotes[insertIndex - 1]!.endSeconds > endSeconds) {
    insertIndex -= 1;
  }
  pendingNotes.splice(insertIndex, 0, { endSeconds, note });
}