All files / player-engine/src/course/nonstop chart-options.ts

100% Statements 26/26
100% Branches 17/17
100% Functions 8/8
100% Lines 23/23

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                                                412x 412x   412x     3x     412x 412x 412x 412x 412x 9x 8x 412x 412x 7x 1x   412x   8x   412x 9x 9x       1x 8x                   412x    
import {
  buildChartOptionTimeline,
  compileChartOptions,
  isChartPlayOptionDeclaration,
  type ChartOptionDiagnostic,
  type ChartOptionState,
  type ChartOptionStateCue,
  type ResolvedChart,
} from '@be-music/chart-model';
import type { NonstopStagePlacement } from './plan.ts';
 
export interface NonstopChartOptionDiagnostic extends ChartOptionDiagnostic {
  stage: number;
  /** Unplaced source-chart time, when the diagnostic came from A6. `seconds` uses the course clock. */
  sourceSeconds?: number;
}
 
/** Place a stage's effective operations in its exclusive course window, including a trimmed initial selection. */
export function placeNonstopChartOptions(
  chart: ResolvedChart,
  placement: Pick<NonstopStagePlacement, 'offsetSeconds' | 'rate'>,
  window: { start: number; end: number },
  stage: number,
): { cues: ChartOptionStateCue[]; diagnostics: NonstopChartOptionDiagnostic[]; hasOperations: boolean } {
  const timeline = buildChartOptionTimeline(chart.source, chart.timing);
  const { program, diagnostics: compiledDiagnostics } = compileChartOptions(timeline);
  // One stitched play runs one ruleset and one arrangement, so a stage's RANDOM and gameplay CRAZY (#844) do not apply.
  const sourceDiagnostics: ChartOptionDiagnostic[] = [
    ...timeline.initial
      .filter(isChartPlayOptionDeclaration)
      .map((declaration) => ({ code: 'unsupported-option' as const, declaration })),
    ...compiledDiagnostics,
  ];
  const place = (seconds: number) => placement.offsetSeconds + seconds / placement.rate;
  const startUs = Math.max(0, Math.round(window.start * 1_000_000));
  const endUs = Number.isFinite(window.end) ? Math.round(window.end * 1_000_000) : Infinity;
  const freezeUs = chart.reversal ? Math.round(chart.reversal.seconds * 1_000_000) : Infinity;
  const placed = program.cues
    .filter((cue) => cue.timeUs <= freezeUs)
    .map((cue) => ({ timeUs: Math.round(place(cue.timeUs / 1_000_000) * 1_000_000), state: { ...cue.state } }));
  let initial: ChartOptionState = { ...program.initial };
  for (const cue of placed) {
    if (cue.timeUs > startUs) break;
    initial = { ...cue.state };
  }
  const cues = [
    { timeUs: startUs, state: initial },
    ...placed.filter((cue) => cue.timeUs > startUs && cue.timeUs < endUs),
  ];
  const diagnostics = sourceDiagnostics.flatMap((diagnostic): NonstopChartOptionDiagnostic[] => {
    const sourceSeconds = diagnostic.seconds;
    if (
      sourceSeconds !== undefined &&
      (sourceSeconds * 1_000_000 > freezeUs || place(sourceSeconds) * 1_000_000 >= endUs)
    )
      return [];
    return [
      {
        ...diagnostic,
        stage,
        seconds:
          sourceSeconds === undefined ? startUs / 1_000_000 : Math.max(startUs / 1_000_000, place(sourceSeconds)),
        ...(sourceSeconds !== undefined ? { sourceSeconds } : {}),
      },
    ];
  });
  return { cues, diagnostics, hasOperations: cues.some((cue) => Object.keys(cue.state).length > 0) };
}