All files / player-browser/src/session/play long-note-tracker.ts

100% Statements 45/45
100% Branches 39/39
100% Functions 12/12
100% Lines 40/40

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                                  10x 10x   3x   3x   4x                                             362x       8x 8x 8x 8x         6x 6x 5x 5x 5x               13x 5x 5x   8x       254x       16x 16x 8x               8x   16x         30x         27x 27x         4x         3x 3x                     23x 23x 17x               18x 18x 12x 9x 9x   8x      
import type { LongNoteStyle } from '@be-music/player-engine/ruleset';
import type {
  PlayerUIHellChargeState,
  PlayerUILongNoteMode,
  PlayerUILongNoteOutcome,
} from '@be-music/player-engine/signals';
import type { BeMusicLongNoteMode, BeMusicLongNoteState } from '@be-music/player-skin-sdk';
 
/**
 * How a ruleset plays a chart long note, mirroring the engine: LR2 plays every one as an LN, That-Which-Must-Not-Be-Named as a charge note
 * (hell charge where the chart says 3), beatoraja follows the chart. A chart note without a mode is a CN, as in the
 * engine.
 */
export function resolveBeMusicLongNoteMode(
  style: LongNoteStyle,
  chartMode: 1 | 2 | 3 | undefined,
): BeMusicLongNoteMode {
  const mode = chartMode ?? 2;
  switch (style) {
    case 'ln':
      return 'ln';
    case 'charge':
      return mode === 3 ? 'hcn' : 'cn';
    default:
      return mode === 3 ? 'hcn' : mode === 2 ? 'cn' : 'ln';
  }
}
 
interface LaneLongNotes {
  /** Head beat of the long note held on the lane, or `undefined` when none is. */
  activeBeat: number | undefined;
  activeMode: PlayerUILongNoteMode;
  activeStartedAtMs: number;
  /** How each long note that finished on the lane ended, by head beat. */
  readonly outcomes: Map<number, PlayerUILongNoteOutcome>;
  /** Play-clock ms each long note that did not complete was let go, by head beat. */
  readonly releasedAtMs: Map<number, number>;
  /** Whether each hell charge note passing the judgement line gains or drains the gauge, by head beat. */
  readonly hellCharge: Map<number, 'gaining' | 'draining'>;
}
 
/**
 * Per-lane long-note book-keeping fed by the engine's `long-note-start` / `long-note-end` / `hell-charge` commands, read
 * back each frame to label drawn long notes and lanes. Allocates only when a lane sees its first long note and when a
 * long note finishes; the per-frame reads allocate nothing.
 */
export class LongNoteTracker {
  private readonly lanes = new Map<string, LaneLongNotes>();
 
  /** A long note with its head at `beat` started being held on `channel` at play-clock `atMs`. */
  start(channel: string, beat: number, mode: PlayerUILongNoteMode, atMs: number): void {
    const lane = this.laneOf(channel, mode);
    lane.activeBeat = beat;
    lane.activeMode = mode;
    lane.activeStartedAtMs = atMs;
  }
 
  /** The long note held on `channel` finished at play-clock `atMs`. Ignored when none is held there. */
  end(channel: string, outcome: PlayerUILongNoteOutcome, atMs: number): void {
    const lane = this.lanes.get(channel);
    if (!lane || lane.activeBeat === undefined) return;
    lane.outcomes.set(lane.activeBeat, outcome);
    if (outcome !== 'complete') lane.releasedAtMs.set(lane.activeBeat, atMs);
    lane.activeBeat = undefined;
  }
 
  /**
   * The hell charge note with its head at `beat` on `channel` started gaining or draining the gauge, or passed the
   * judgement line (`'passed'`), as the engine's `hell-charge` command says.
   */
  hellCharge(channel: string, beat: number, state: PlayerUIHellChargeState): void {
    if (state === 'passed') {
      this.lanes.get(channel)?.hellCharge.delete(beat);
      return;
    }
    this.laneOf(channel, 'hcn').hellCharge.set(beat, state);
  }
 
  clear(): void {
    this.lanes.clear();
  }
 
  private laneOf(channel: string, mode: PlayerUILongNoteMode): LaneLongNotes {
    let lane = this.lanes.get(channel);
    if (!lane) {
      lane = {
        activeBeat: undefined,
        activeMode: mode,
        activeStartedAtMs: 0,
        outcomes: new Map(),
        releasedAtMs: new Map(),
        hellCharge: new Map(),
      };
      this.lanes.set(channel, lane);
    }
    return lane;
  }
 
  /** True while a long note is held on `channel`. */
  isHolding(channel: string): boolean {
    return this.lanes.get(channel)?.activeBeat !== undefined;
  }
 
  /** Play-clock ms the hold on `channel` started, while one is held. */
  holdStartedAtMs(channel: string): number | undefined {
    const lane = this.lanes.get(channel);
    return lane?.activeBeat === undefined ? undefined : lane.activeStartedAtMs;
  }
 
  /** Play-clock ms the long note with its head at `beat` on `channel` was let go early, once it ended dropped. */
  releasedAtMs(channel: string, beat: number): number | undefined {
    return this.lanes.get(channel)?.releasedAtMs.get(beat);
  }
 
  /** The mode the engine announced for the long note at `beat` on `channel`, while it is held. */
  heldMode(channel: string, beat: number): PlayerUILongNoteMode | undefined {
    const lane = this.lanes.get(channel);
    return lane?.activeBeat === beat ? lane.activeMode : undefined;
  }
 
  /**
   * Where the long note with its head at `beat` on `channel` is in its life. `judged` is the note's own flag: a judged
   * long note that never started was missed at the head, so it reads as dropped. A hell charge note passing the
   * judgement line reads as the engine last said: `'held'` while it gains (held, or its tail released well), and
   * `'draining'` while it drains (let go or missed at the head, and a press makes it gain again); past its tail it
   * reads as {@link holdStateOf} does.
   */
  stateOf(channel: string, beat: number, judged: boolean): BeMusicLongNoteState {
    const hellCharge = this.lanes.get(channel)?.hellCharge.get(beat);
    if (hellCharge !== undefined) return hellCharge === 'gaining' ? 'held' : 'draining';
    return this.holdStateOf(channel, beat, judged);
  }
 
  /**
   * {@link stateOf} without the hell charge: a long note let go early or missed at the head reads as dropped for good,
   * whatever its gauge does after. LR2's half alpha follows this.
   */
  holdStateOf(channel: string, beat: number, judged: boolean): Exclude<BeMusicLongNoteState, 'draining'> {
    const lane = this.lanes.get(channel);
    if (lane) {
      if (lane.activeBeat === beat) return 'held';
      const outcome = lane.outcomes.get(beat);
      if (outcome !== undefined) return outcome === 'complete' ? 'done' : 'dropped';
    }
    return judged ? 'dropped' : 'pending';
  }
}