All files / player-engine/src/core chart-text.ts

97.43% Statements 38/39
100% Branches 30/30
93.75% Functions 15/16
97.05% Lines 33/34

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                                              47326x               68x         68x 68x 68x           3506x 8x 8x 8x   3498x 3477x 21x 21x     18x 4x 10x 10x   7x 3x     4x   14x 5x 5x       785x     1321x     4725x 4725x 778x 778x   3947x 3947x         237x   3710x      
import { pickChartText, type ChartTextTimeline } from '@be-music/chart-model';
import { findLastIndexAtOrBefore } from '@be-music/shared-utils/core';
import { DEFAULT_POOR_BGA_DISPLAY_SECONDS } from './bga-timeline.ts';
import type { JudgeCoreEffect } from './judge-core-types.ts';
 
/** A source text program placed in one exclusive NONSTOP ownership window. */
export interface ChartTextStage {
  startSeconds: number;
  endSeconds: number;
  offsetSeconds: number;
  rate: number;
  timeline: ChartTextTimeline;
  /** Source-chart reversal time, before placement and FIT scaling. */
  freezeSeconds?: number;
}
 
export interface ChartTextInput {
  seconds: number;
  action: 'trigger' | 'clear';
}
 
/** The same effects drive live presentation and silent AUTO/replay seek reconstruction. */
export function resolveChartTextInput(effect: JudgeCoreEffect): ChartTextInput | undefined {
  return effect.kind === 'poor-bga' ? { seconds: effect.seconds, action: effect.action } : undefined;
}
 
/**
 * Be-Music's TEXT00 policy: miss text temporarily replaces channel-99 text for the shared miss-display interval.
 * A clear judgment ends it early. No host timer or wall clock participates; absent TEXT00 leaves normal text intact.
 */
export class ChartTextController {
  private inputs: ChartTextInput[] = [];
  private readonly stages?: { definition: ChartTextStage; controller: ChartTextController }[];
  private readonly timeline: ChartTextTimeline;
  private readonly freezeSeconds: number;
  constructor(timeline: ChartTextTimeline, freezeSeconds: number = Infinity, stages?: readonly ChartTextStage[]) {
    this.timeline = timeline;
    this.freezeSeconds = freezeSeconds;
    this.stages = stages?.map((definition) => ({
      definition,
      controller: new ChartTextController(definition.timeline, definition.freezeSeconds),
    }));
  }
  addInput(input: ChartTextInput): void {
    if (this.stages) {
      const stage = this.stageAt(input.seconds);
      if (stage) stage.controller.addInput({ ...input, seconds: this.sourceSeconds(stage.definition, input.seconds) });
      return;
    }
    if (this.timeline.missText === undefined || !Number.isFinite(input.seconds) || input.seconds > this.freezeSeconds)
      return;
    const index = findLastIndexAtOrBefore(this.inputs, input.seconds, (entry) => entry.seconds);
    this.inputs.splice(index + 1, 0, { ...input });
  }
  reset(inputs: readonly ChartTextInput[] = []): void {
    if (this.stages) {
      for (const stage of this.stages) {
        const { definition } = stage;
        stage.controller.reset(
          inputs
            .filter((input) => input.seconds >= definition.startSeconds && input.seconds < definition.endSeconds)
            .map((input) => ({ ...input, seconds: this.sourceSeconds(definition, input.seconds) })),
        );
      }
      return;
    }
    this.inputs = inputs
      .filter((input) => Number.isFinite(input.seconds) && input.seconds <= this.freezeSeconds)
      .map((input) => ({ ...input }))
      .sort((left, right) => left.seconds - right.seconds);
  }
  private sourceSeconds(stage: ChartTextStage, seconds: number): number {
    return (seconds - stage.offsetSeconds) * stage.rate;
  }
  private stageAt(seconds: number): { definition: ChartTextStage; controller: ChartTextController } | undefined {
    return this.stages?.find(({ definition }) => seconds >= definition.startSeconds && seconds < definition.endSeconds);
  }
  read(seconds: number): string {
    const at = Math.min(seconds, this.freezeSeconds);
    if (this.stages) {
      const stage = this.stageAt(at);
      return stage ? stage.controller.read(this.sourceSeconds(stage.definition, at)) : '';
    }
    const input = this.inputs[findLastIndexAtOrBefore(this.inputs, at, (entry) => entry.seconds)];
    if (
      this.timeline.missText !== undefined &&
      input?.action === 'trigger' &&
      at < input.seconds + DEFAULT_POOR_BGA_DISPLAY_SECONDS
    ) {
      return this.timeline.missText;
    }
    return pickChartText(this.timeline, at);
  }
}