All files / player-engine/src/core bmson-bga.ts

98.85% Statements 86/87
96.42% Branches 81/84
100% Functions 14/14
98.71% Lines 77/78

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                                          6146x 52x   6094x 703x   5391x                     53x 53x 53x 53x     53x 53x     53x 53x 82x 82x   74x   175x     29x       95x 94x 94x 94x 50x 50x     94x 94x         10x 7x 7x 2x 10x       2230x 2229x 2229x 2229x 2382x 2382x 2382x 75x 75x 75x 75x 75x 2307x 78x 78x 78x       2x 76x 76x       64x 64x   2229x   2229x 2229x 2226x 2230x 2230x 158x 158x 158x 158x 158x   2226x 1262x 2230x 2230x 2230x 2230x 474x                     30x 30x 30x 30x 30x 30x 30x      
import { pickBmsonBGASequenceFrame, type BmsonBGAEventCue } from '@be-music/chart-model';
import type { JudgeCoreEffect } from './judge-core-types.ts';
 
export interface BmsonBGAEventInput {
  seconds: number;
  condition: 'play' | 'miss';
  /** Optional zero-based alternative choice supplied by a host; otherwise accepted triggers cycle in source order. */
  choice?: number;
}
 
export interface BmsonBGAFrame {
  plane: 'layer' | 'poor';
  key?: string;
  startedSeconds: number;
  frameStartSeconds: number;
  stage?: number;
  playbackRate?: number;
}
 
/** Shared by live judging and silent replay/AUTO seek reconstruction. */
export function resolveBmsonBGAEventInput(effect: JudgeCoreEffect): BmsonBGAEventInput | undefined {
  if (effect.kind === 'poor-bga' && effect.action === 'trigger') {
    return { seconds: effect.seconds, condition: 'miss' };
  }
  if (effect.kind === 'combo' && ['PERFECT', 'GREAT', 'GOOD'].includes(effect.judge ?? '')) {
    return { seconds: effect.seconds, condition: 'play' };
  }
  return undefined;
}
 
/**
 * Be-Music event-layer policy: PLAY/MISS trigger on judgments, interval throttles starts in milliseconds,
 * ALWAYS starts at selection and repeats at a positive interval, and alternatives cycle deterministically.
 * A new selection ends the previous animation; successful judgment clears an active MISS animation.
 */
export class BmsonBGAController {
  private readonly cues: BmsonBGAEventCue[];
  private readonly freezeSeconds: number;
  private inputs: BmsonBGAEventInput[] = [];
  private cueCursor = 0;
  private inputCursor = 0;
  private through = -Infinity;
  private selection: BmsonBGAEventCue | undefined;
  private active: { cue: BmsonBGAEventCue; seconds: number; choice: number } | undefined;
  private lastTrigger = -Infinity;
  private ordinal = 0;
 
  constructor(cues: readonly BmsonBGAEventCue[], freezeSeconds: number = Infinity) {
    this.freezeSeconds = freezeSeconds;
    this.cues = cues
      .filter((cue) => Number.isFinite(cue.seconds) && cue.seconds <= freezeSeconds)
      .map((cue) => ({
        ...cue,
        sequences: cue.sequences.map((sequence) => ({
          ...sequence,
          frames: sequence.frames.map((frame) => ({ ...frame })),
        })),
      }))
      .sort((left, right) => left.seconds - right.seconds);
  }
 
  addInput(input: BmsonBGAEventInput): void {
    if (!Number.isFinite(input.seconds) || input.seconds > this.freezeSeconds) return;
    let low = 0,
      high = this.inputs.length;
    while (low < high) {
      const middle = Math.floor((low + high) / 2);
      if (this.inputs[middle]!.seconds <= input.seconds) low = middle + 1;
      else Ehigh = middle;
    }
    this.inputs.splice(low, 0, { ...input });
    if (input.seconds <= this.through) this.rewind();
  }
 
  /** A seek replaces live history with the exact judgments rebuilt by the selected judge strategy. */
  reset(inputs: readonly BmsonBGAEventInput[] = []): void {
    this.inputs = inputs
      .filter((input) => Number.isFinite(input.seconds) && input.seconds <= this.freezeSeconds)
      .map((input) => ({ ...input }))
      .sort((left, right) => left.seconds - right.seconds);
    this.rewind();
  }
 
  read(seconds: number): BmsonBGAFrame | undefined {
    if (!Number.isFinite(seconds)) return undefined;
    seconds = Math.min(seconds, this.freezeSeconds);
    if (seconds < this.through) this.rewind();
    while (true) {
      const cue = this.cues[this.cueCursor];
      const input = this.inputs[this.inputCursor];
      if (cue && cue.seconds <= seconds && (!input || cue.seconds <= input.seconds)) {
        this.cueCursor++;
        this.selection = cue;
        this.active = undefined;
        this.lastTrigger = -Infinity;
        this.ordinal = 0;
      } else if (input && input.seconds <= seconds) {
        this.inputCursor++;
        const selected = this.selection;
        if (
          input.seconds >= (selected?.endSeconds ?? Infinity) ||
          input.seconds > (selected?.freezeSeconds ?? Infinity)
        )
          continue;
        if (input.condition === 'play' && this.active?.cue.condition === 'miss') this.active = undefined;
        if (
          selected?.condition === input.condition &&
          input.seconds - this.lastTrigger + 1e-9 >= Math.max(0, selected.interval) / 1000
        ) {
          this.active = { cue: selected, seconds: input.seconds, choice: input.choice ?? this.ordinal++ };
          this.lastTrigger = input.seconds;
        }
      } else break;
    }
    this.through = seconds;
    if (this.selection && seconds >= (this.selection.endSeconds ?? Infinity)) return undefined;
    const animationSeconds = Math.min(seconds, this.selection?.freezeSeconds ?? Infinity);
    let active = this.active;
    if (this.selection?.condition === 'always') {
      const cue = this.selection;
      const interval = Math.max(0, cue.interval) / 1000;
      const anchor = cue.animationStartSeconds ?? cue.seconds;
      const cycle = interval > 0 ? Math.floor((animationSeconds - anchor + 1e-9) / interval) : 0;
      active = { cue, seconds: anchor + cycle * interval, choice: cycle };
    }
    if (!active || active.cue.sequences.length === 0) return undefined;
    const choice = Number.isFinite(active.choice) ? Math.trunc(active.choice) : 0;
    const count = active.cue.sequences.length;
    const sequence = active.cue.sequences[((choice % count) + count) % count]!;
    const frame = pickBmsonBGASequenceFrame(sequence, Math.max(0, animationSeconds - active.seconds) + 1e-9);
    if (!frame) return undefined;
    return {
      plane: active.cue.condition === 'miss' ? 'poor' : 'layer',
      key: frame.key,
      startedSeconds: active.seconds,
      frameStartSeconds: active.seconds + frame.elapsedSeconds,
      ...(active.cue.stage !== undefined ? { stage: active.cue.stage } : {}),
      ...(active.cue.playbackRate !== undefined ? { playbackRate: active.cue.playbackRate } : {}),
    };
  }
 
  private rewind(): void {
    this.cueCursor = 0;
    this.inputCursor = 0;
    this.through = -Infinity;
    this.selection = undefined;
    this.active = undefined;
    this.lastTrigger = -Infinity;
    this.ordinal = 0;
  }
}