All files / skin-lr2/src/state decide.ts

100% Statements 20/20
100% Branches 14/14
100% Functions 5/5
100% Lines 15/15

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                                  42x               17x 17x 17x 17x 17x 17x 17x   2x 6x   1x                                     16x 16x 16x 16x    
import { resolveLibraryChartDifficultyValue, type CourseView, type LibraryChart } from '@be-music/player-scene-model';
import { LR2_OP, LR2_TIMER } from '../ids/index.ts';
import { resolveLR2ChartText, resolveLR2DifficultyOp, resolveLR2KeyModeOp } from './chart.ts';
import { resolveLR2CourseNumber, resolveLR2CourseOps, resolveLR2CourseText } from './course.ts';
import type { LR2SceneState } from './scene-state.ts';
 
/** What the LR2 decide screen shows: the chart about to be played, and the course it is a stage of. */
export interface LR2DecideInput {
  /** The chart about to be played. */
  readonly chart: LibraryChart;
  /** The course the chart is a stage of (with the stage being played): the course ops, numbers and texts. */
  readonly course?: CourseView;
  /** The skin's `#CUSTOMOPTION` choices: their ops (900-999). */
  readonly customOps?: Iterable<number>;
}
 
/** Ops the decide screen always lights: no internet ranking, and the chart is ready. */
const DECIDE_BASE_OPS: readonly number[] = [LR2_OP.OFFLINE, LR2_OP.LOADED];
 
/**
 * The LR2 decide screen's state: the base ops, the chart's difficulty (150-155; the generic plate for none) and key
 * mode (160-164) ops, the chart's texts (10-28) and, for a course stage, the course ops, numbers and texts. It draws
 * no slider.
 */
export function resolveLR2DecideState(input: LR2DecideInput): LR2SceneState {
  const { chart, course } = input;
  const ops = new Set<number>(DECIDE_BASE_OPS);
  for (const op of input.customOps ?? []) ops.add(op);
  ops.add(resolveLR2DifficultyOp(resolveLibraryChartDifficultyValue(chart)));
  ops.add(resolveLR2KeyModeOp(chart));
  if (course !== undefined) for (const op of resolveLR2CourseOps(course)) ops.add(op);
  return {
    ops,
    number: (num) => (course !== undefined ? resolveLR2CourseNumber(num, course) : undefined),
    text: (st) =>
      (course !== undefined ? resolveLR2CourseText(st, course) : undefined) ?? resolveLR2ChartText(st, chart),
    slider: () => undefined,
  };
}
 
/** A moment of the decide screen, as its LR2 timers read it; every time is on the clock the screen stamps timers with. */
export interface LR2DecideTimerInput {
  /** The time now, and when the screen was entered. */
  readonly clock: { readonly nowMs: number; readonly enteredAtMs: number };
  /** When input opens: the skin's `#STARTINPUT` after the entry. */
  readonly inputOpenAtMs: number;
  /** When the screen started leaving; absent until it does. */
  readonly leaving?: { readonly atMs: number };
}
 
/**
 * The LR2 timers running on a moment of the decide screen, each with when it started: timer 0 (the scene) from the
 * screen's entry, timer 1 (input) once input has opened, and timer 2 (the fade-out) once the screen is leaving.
 */
export function resolveLR2DecideTimers(input: LR2DecideTimerInput): ReadonlyMap<number, number> {
  const timers = new Map<number, number>([[LR2_TIMER.SCENE_START, input.clock.enteredAtMs]]);
  if (input.clock.nowMs >= input.inputOpenAtMs) timers.set(LR2_TIMER.START_INPUT, input.inputOpenAtMs);
  if (input.leaving !== undefined) timers.set(LR2_TIMER.FADE_OUT, input.leaving.atMs);
  return timers;
}