All files / player-engine/src/ruleset selection.ts

60% Statements 6/10
58.33% Branches 7/12
50% Functions 1/2
60% Lines 6/10

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                                                                  14340x 11519x   2821x 2x   2819x 14340x                                              
import type { BeatorajaJudgeAlgorithm, JudgeWindowSetUs } from './definitions.ts';
import type { RulesetJudgeIndex } from './judge.ts';
 
/**
 * One note a press could resolve against. `dmUs` follows the window convention
 * (`dmUs = noteTimeUs - inputTimeUs`, positive = the press is EARLY).
 */
export interface JudgeSelectionCandidate {
  noteTimeUs: number;
  dmUs: number;
  judge: RulesetJudgeIndex;
  /** The window set this note judges on — its own lane kind, not the pressing lane's. */
  windows: JudgeWindowSetUs;
}
 
/**
 * Should `candidate` displace `best` as the note this press resolves against?
 *
 * beatoraja `JudgeAlgorithm` (`JudgeManager.java`), with the caller supplying candidates in ascending note order:
 *
 * - `lowest` — never displace. The earliest note in reach wins, so a press always clears the oldest pending note
 *   even when a later one is closer in time. LR2 and That-Which-Must-Not-Be-Named both work this way.
 * - `duration` — the note closest in absolute time wins.
 * - `combo` (beatoraja's default) / `score` — displace only once `best` has fallen out of the late side of its own
 *   GOOD (`combo`) or GREAT (`score`) window while the candidate is still inside the early side of its. This keeps
 *   a combo alive by preferring the note that can still be judged well.
 */
export function preferJudgeCandidate(
  algorithm: BeatorajaJudgeAlgorithm,
  best: JudgeSelectionCandidate,
  candidate: JudgeSelectionCandidate,
  inputTimeUs: number,
): boolean {
  if (algorithm === 'lowest') {
    return false;
  }
  if (algorithm === 'duration') {
    return Math.abs(candidate.dmUs) < Math.abs(best.dmUs);
  }
  const judgeIndex = algorithm === 'combo' ? 2 : 1;
  return (
    best.noteTimeUs < inputTimeUs + best.windows.judges[judgeIndex]![0] &&
    candidate.noteTimeUs <= inputTimeUs + candidate.windows.judges[judgeIndex]![1]
  );
}
 
/**
 * Pick the note a press resolves against, from candidates given in ascending note order. Pure reduction over
 * {@link preferJudgeCandidate} — callers that also track unscoreable candidates (empty POOR) rank those first.
 */
export function selectJudgeCandidate<T extends JudgeSelectionCandidate>(
  candidates: readonly T[],
  algorithm: BeatorajaJudgeAlgorithm,
  inputTimeUs: number,
): T | undefined {
  let best: T | undefined;
  for (const candidate of candidates) {
    if (best === undefined || preferJudgeCandidate(algorithm, best, candidate, inputTimeUs)) {
      best = candidate;
    }
  }
  return best;
}