All files / player-engine/src/core play-auto.ts

98.5% Statements 66/67
93.93% Branches 31/33
100% Functions 20/20
100% Lines 61/61

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                                              56x 56x 56x 56x 56x 12x   56x 56x 56x 56x 56x               93x 93x 691x 691x 691x   514x       326x 326x 326x         2333x             326x 326x 326x 67961x 8396x 8396x 9566x         1642x     8396x   326x 68654x     326x     2132x       2042x   1830x 1831x 176x                   1831x     68432x   326x     326x 93x 93x 93x 522x   93x 466x   93x 93x       16x                 326x   68339x 68339x 68339x     68338x 1594x 67826x 315x 324x         127x            
import type { PlayerHCNLaneState } from './ui-signal-bus.ts';
import { resolveAltModifierLabel } from '../utils.ts';
import {
  createAutoJudgeState,
  isAutoJudgeSettled,
  runAutoJudgeStep,
  type AutoJudgeConfig,
  type AutoJudgeInput,
} from './auto-judge-core.ts';
import { clampToReversal } from './judge-core.ts';
import { resolveLongNoteEndSeconds, resolveRulesetLongNoteMode, toUILongNoteMode } from './judge-rules.ts';
import { printLaneMap, writePlayableSampleTriggerEventLog } from './engine-trace.ts';
import { createRealtimeAudioCursor } from './play-audio-cursor.ts';
import type { PlaySession } from './play-session.ts';
import { createSeekRebuildSink } from './play-seek.ts';
import type { JudgeStrategy, PlayJudge, PlayJudgeSeek } from './play-strategy.ts';
 
/**
 * The AUTO judge strategy: the auto judge core (`auto-judge-core.ts`) resolves every note as chart time reaches it,
 * and the strategy queues each note's keysound against the audio write head, ahead of its judgment.
 */
 
function writeAutoIntro(session: PlaySession): void {
  const { writeOutput, playbackChart } = session;
  const highSpeedModifierLabel = resolveAltModifierLabel();
  writeOutput('Auto play start\n');
  writeOutput(`Lane mode: ${playbackChart.laneDisplayMode}\n`);
  if (session.randomPatternSummary) {
    writeOutput(`${session.randomPatternSummary}\n`);
  }
  printLaneMap(writeOutput, playbackChart.laneBindings);
  writeOutput('Press Space to pause/resume. Press Shift+R to restart.\n');
  writeOutput('Press Ctrl+C or Esc to quit.\n');
  writeOutput(`Press ${highSpeedModifierLabel}+odd lane key to decrease HIGH-SPEED.\n`);
  writeOutput(`Press ${highSpeedModifierLabel}+even lane key to increase HIGH-SPEED.\n`);
}
 
/**
 * The chart seconds a play from the start judges at up to `seconds`, in order: every note's time and every long note's
 * end. Stepping a fresh judge through them makes its judgments in the order a play from the start made them.
 */
function rebuildSteps(config: AutoJudgeConfig, seconds: number): number[] {
  const steps = new Set<number>();
  for (const note of config.scorableNotes) {
    if (note.seconds <= seconds) steps.add(note.seconds);
    const endSeconds = resolveLongNoteEndSeconds(note);
    if (endSeconds !== undefined && endSeconds <= seconds) steps.add(endSeconds);
  }
  return [...steps].sort((a, b) => a - b);
}
 
function createAutoJudge(session: PlaySession): PlayJudge {
  const { playbackChart, uiEnabled, writeOutput, wavResources, idBase, audioSession, totalSeconds } = session;
  const { scorableNotes } = playbackChart;
  const config: AutoJudgeConfig = {
    ruleset: session.ruleset,
    scorableNotes,
    landmineNotes: playbackChart.landmineNotes,
    invisibleNotes: playbackChart.invisibleNotes,
    keyLabels: new Map(playbackChart.laneBindings.map((binding) => [binding.channel, binding.keyLabel])),
    // Only the terminal log writes traces; with a UI they would be built and discarded on every judgment.
    omitTraceEffects: uiEnabled,
    // LR2 negative-BPM reversal (#134): AUTO play freezes there like manual play — no judging and no audio past it
    // (the realtime streams are cut in `collectRealtimeAudioTriggers`), and a mirrored display clock.
    reversalSeconds: session.resolvedChart.reversal?.seconds,
  };
  let autoJudge = createAutoJudgeState(config, session.score);
  const empty: readonly PlayerHCNLaneState[] = [];
  session.readHCNLanes = (seconds) => {
    if (autoJudge.pendingLongNotes.length === 0) return empty;
    const lanes: PlayerHCNLaneState[] = [];
    for (const { note, endSeconds } of autoJudge.pendingLongNotes) {
      if (
        seconds >= note.seconds &&
        seconds < endSeconds &&
        resolveRulesetLongNoteMode(config.ruleset.longNoteStyle, note) === 3
      ) {
        lanes.push({ channel: note.channel, gaining: true });
      }
    }
    return lanes;
  };
  const step = (input: AutoJudgeInput): void => {
    runAutoJudgeStep(autoJudge, input, session.applyJudgeEffect);
  };
  // Note keysounds freeze with the judge pump at an LR2 negative-BPM reversal (#134).
  const noteKeysounds = createRealtimeAudioCursor(
    scorableNotes
      .flatMap((note) =>
        note.endSoundEvent && note.endSeconds !== undefined
          ? [note, { ...note, seconds: note.endSeconds, event: note.endSoundEvent, soundEvents: undefined }]
          : [note],
      )
      .sort((left, right) => left.seconds - right.seconds),
    (note) => {
      for (const event of note.soundEvents ?? [note.event]) {
        if (!uiEnabled) {
          writePlayableSampleTriggerEventLog(
            writeOutput,
            event,
            note.seconds,
            wavResources,
            'auto-note',
            note.channel,
            idBase,
          );
        }
        session.queueAudioEvent(event, note.seconds);
      }
    },
    (seconds) => clampToReversal(autoJudge.config, seconds),
  );
  let judgedThroughSeconds = 0;
 
  /** A fresh judge re-simulated from the start to `seconds`, the keysounds still ringing there started partway. */
  const seek = (seconds: number): PlayJudgeSeek => {
    autoJudge = createAutoJudgeState(config, session.score);
    const rebuild = createSeekRebuildSink(session.summary);
    for (const at of [...rebuildSteps(config, seconds), seconds]) {
      runAutoJudgeStep(autoJudge, { kind: 'advance', nowSec: at }, rebuild.emit);
    }
    noteKeysounds.seek(seconds, (note, offsetSeconds) => {
      for (const event of note.soundEvents ?? [note.event]) audioSession?.triggerEventAt?.(event, offsetSeconds);
    });
    judgedThroughSeconds = seconds;
    return {
      judgments: rebuild.judgments,
      bmsonBGAInputs: rebuild.bmsonBGAInputs,
      chartTextInputs: rebuild.chartTextInputs,
      heldLongNotes: autoJudge.pendingLongNotes.map(({ note }) => ({
        channel: note.channel,
        beat: note.beat,
        endBeat: typeof note.endBeat === 'number' && note.endBeat > note.beat ? note.endBeat : undefined,
        mode: toUILongNoteMode(resolveRulesetLongNoteMode(config.ruleset.longNoteStyle, note) ?? 2),
      })),
    };
  };
 
  return {
    judge: (tick) => {
      noteKeysounds.triggerUntil(tick.scheduledSec);
      step({ kind: 'advance', nowSec: tick.nowSec });
      judgedThroughSeconds = tick.nowSec;
    },
    seek,
    judgedThroughSeconds: (tick) => tick.nowSec,
    isSettled: () => noteKeysounds.done && isAutoJudgeSettled(autoJudge),
    isClosed: () => false,
    finish: () => step({ kind: 'finish', totalSeconds }),
    settledThroughSeconds: () => judgedThroughSeconds,
  };
}
 
/** AUTO play: every note played and judged PGREAT on time. */
export const autoStrategy: JudgeStrategy = {
  mode: 'auto',
  estimatesActiveAudio: true,
  writeIntro: writeAutoIntro,
  createJudge: createAutoJudge,
};