All files / player-engine/src judging.ts

97.72% Statements 43/44
93.93% Branches 31/33
100% Functions 5/5
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                                          101456x 101456x 101456x 702416x 702416x 311885x   390531x     101456x               3x 3x 3x   2x   3x 9x 9x 9x 2x   7x 1x   6x 1x   5x 2x   3x 3x     3x 3x 3x     3x                 1x                   2x                 12521x 12x         12509x 12509x 129665x 129665x 129665x 118704x   10961x     1548x    
export interface JudgeCandidate {
  channel: string;
  seconds: number;
}
 
interface JudgeableNote extends JudgeCandidate {
  judged: boolean;
}
 
export interface FindClosestCandidateOptions<T extends JudgeCandidate> {
  nowSec: number;
  judgeWindowSec: number;
  candidateChannels?: ReadonlySet<string>;
  channel?: string;
  startIndex?: number;
  sortedBySeconds?: boolean;
  isConsumed?: (candidate: T) => boolean;
}
 
/** First index in a time-sorted note array whose `seconds` is at least `target`. */
export function lowerBoundBySeconds<T extends JudgeCandidate>(notes: ReadonlyArray<T>, target: number): number {
  let low = 0;
  let high = notes.length;
  while (low < high) {
    const mid = (low + high) >>> 1;
    if (notes[mid]!.seconds < target) {
      low = mid + 1;
    } else {
      high = mid;
    }
  }
  return low;
}
 
export function findClosestCandidateInWindow<T extends JudgeCandidate>(
  notes: ReadonlyArray<T>,
  options: FindClosestCandidateOptions<T>,
): T | undefined {
  let best: T | undefined;
  let bestDelta = Number.POSITIVE_INFINITY;
  let startIndex = Math.max(0, Math.trunc(options.startIndex ?? 0));
  if (options.sortedBySeconds === true && options.startIndex === undefined) {
    // Skip the entire prefix before the window opens — O(log N) instead of O(N).
    startIndex = lowerBoundBySeconds(notes, options.nowSec - options.judgeWindowSec);
  }
  for (let index = startIndex; index < notes.length; index += 1) {
    const note = notes[index]!;
    const signedDelta = note.seconds - options.nowSec;
    if (options.sortedBySeconds === true && signedDelta > options.judgeWindowSec) {
      break;
    }
    if (options.channel !== undefined && note.channel !== options.channel) {
      continue;
    }
    if (options.candidateChannels !== undefined && !options.candidateChannels.has(note.channel)) {
      continue;
    }
    if (options.isConsumed?.(note) === true) {
      continue;
    }
    const delta = Math.abs(signedDelta);
    Iif (delta > options.judgeWindowSec) {
      continue;
    }
    Eif (delta < bestDelta) {
      bestDelta = delta;
      best = note;
    }
  }
  return best;
}
 
export function findBestCandidate<T extends JudgeableNote>(
  notes: ReadonlyArray<T>,
  candidateChannels: ReadonlySet<string>,
  nowSec: number,
  judgeWindowSec: number,
): T | undefined {
  return findClosestCandidateInWindow(notes, {
    candidateChannels,
    nowSec,
    judgeWindowSec,
    sortedBySeconds: true,
    isConsumed: noteIsJudged,
  });
}
 
function noteIsJudged(note: JudgeableNote): boolean {
  return note.judged;
}
 
export function findLaneSoundCandidate<T extends JudgeCandidate>(
  notes: ReadonlyArray<T>,
  candidateChannels: ReadonlySet<string>,
  nowSec: number,
  activationWindowSec = 0,
): T | undefined {
  if (notes.length === 0 || candidateChannels.size === 0) {
    return undefined;
  }
 
  // LR2-style lane sound hold: do not look ahead to the closest future note. A lane's fallback sound only advances
  // when that note's early judgment window has opened, then remains current until the next same-lane window opens.
  const latestEligibleSeconds = nowSec + Math.max(0, activationWindowSec) + 1e-9;
  for (let index = lowerBoundBySeconds(notes, latestEligibleSeconds); index > 0;) {
    index -= 1;
    const note = notes[index]!;
    if (!candidateChannels.has(note.channel)) {
      continue;
    }
    return note;
  }
 
  return undefined;
}