All files / player-engine/src/replay scratch-channels.ts

100% Statements 14/14
100% Branches 10/10
100% Functions 4/4
100% Lines 10/10

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    331x                     331x   331x               11197x 11197x   17939x   2918x 324x 324x   11197x    
import { PLAY_MODE_LAYOUTS, type PlayModeLayoutId } from '@be-music/chart-model';
 
const LAYOUT_BY_KEY_COUNT: Readonly<Record<string, PlayModeLayoutId>> = {
  '5': '5',
  '7': '7',
  '9': '9-pms',
  '10': '10',
  '14': '14',
  '24': '24',
  '48': '48',
};
 
/** Resolved scratch lanes per lane-mode label; the play-mode table is constant, so a label resolves once. */
const SCRATCH_CHANNELS_BY_LANE_MODE = new Map<string, ReadonlySet<string>>();
/** Bounds the cache, since a replay file may carry any lane-mode spelling. */
const SCRATCH_CHANNELS_CACHE_LIMIT = 64;
 
/**
 * The scratch lanes of a recorded lane mode: the lanes the play-mode table marks `scratch` in the layout the engine
 * played (`ReplayChart.laneMode` is its label), the same set the engine binds as scratch. Other spellings resolve by
 * their leading key count, as the ruleset reads the lane mode, with 7 KEY for anything else.
 */
export function resolveScratchChannels(laneMode: string): ReadonlySet<string> {
  let channels = SCRATCH_CHANNELS_BY_LANE_MODE.get(laneMode);
  if (channels === undefined) {
    const layout =
      Object.values(PLAY_MODE_LAYOUTS).find((candidate) => candidate.label === laneMode) ??
      PLAY_MODE_LAYOUTS[LAYOUT_BY_KEY_COUNT[/^\d+/.exec(laneMode)?.[0] ?? ''] ?? '7'];
    channels = new Set(layout.lanes.filter((lane) => lane.kind === 'scratch').map((lane) => lane.channel));
    if (SCRATCH_CHANNELS_BY_LANE_MODE.size >= SCRATCH_CHANNELS_CACHE_LIMIT) SCRATCH_CHANNELS_BY_LANE_MODE.clear();
    SCRATCH_CHANNELS_BY_LANE_MODE.set(laneMode, channels);
  }
  return channels;
}