All files / player-browser/src/chart/beatoraja dp-flip.ts

95% Statements 19/20
91.66% Branches 11/12
100% Functions 3/3
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 62 63 64 65 66                                        29x                               58x 55x 55x 45x                           6x 6x 6x 6x 36x 36x 32x 32x   6x 5x    
// DP-flip chart transform.
//
// BMS channels encode `(lane group, lane number)` as a 2-char string where the first char
// identifies the side (and visible / invisible / LN / landmine slot):
//
//   `1X` 1P visible      `2X` 2P visible
//   `3X` 1P invisible    `4X` 2P invisible
//   `5X` 1P LN           `6X` 2P LN
//   `DX` 1P landmine     `EX` 2P landmine
//
// where X ∈ [1..9] (key 1..7 + scratch 6 + extra 8/9). DP flip swaps the side bit on every
// matching event channel — `1X ↔ 2X`, `3X ↔ 4X`, `5X ↔ 6X`, `DX ↔ EX` — leaving SP-only
// channels (BPM, BGA, key sounds at `01`, etc.) untouched. The result is a chart whose
// 1P / 2P sides are swapped, useful for players who prefer the opposite hand layout.
//
// Mirrors beatoraja's `Config.flipMode` + `BMSPlayer.applyFlip` semantics. The transform is
// pure — produces a new chart object without mutating the input.
 
import type { BeMusicJSON, BeMusicEvent } from '@be-music/chart-source';
 
const FLIP_MAP: Readonly<Record<string, string>> = Object.freeze({
  '1': '2',
  '2': '1',
  '3': '4',
  '4': '3',
  '5': '6',
  '6': '5',
  D: 'E',
  E: 'D',
});
 
/**
 * Swap the side digit on a single channel string. Returns the original string when the
 * channel doesn't match any flippable prefix (BPM `03`, BGA `04`, key sound `01`, etc.).
 */
export function flipDPChannel(channel: string): string {
  if (channel.length !== 2) return channel;
  const replacement = FLIP_MAP[channel[0]!];
  if (replacement === undefined) return channel;
  return replacement + channel[1]!;
}
 
/**
 * Walk every event in a chart and swap side digits on channels that map. Other events pass
 * through unchanged. Returns a new chart object — input is untouched, so callers can keep
 * the unflipped chart for fallback / re-flipping.
 *
 * Both BMS-mode and bmson-mode charts run their note events through the same
 * `BeMusicJSON.events[]` channel-string field — the parser unifies channel encoding so we
 * only need to swap one place. The `measures[]` block carries no per-channel data; same
 * for `bms.events` (which is empty in our parser today — events are on the top-level).
 */
export function flipDPChart(chart: BeMusicJSON): BeMusicJSON {
  const events = chart.events;
  Iif (!Array.isArray(events) || events.length === 0) return chart;
  let mutated = false;
  const flippedEvents: BeMusicEvent[] = events.map((event) => {
    const next = flipDPChannel(event.channel);
    if (next === event.channel) return event;
    mutated = true;
    return { ...event, channel: next };
  });
  if (!mutated) return chart;
  return { ...chart, events: flippedEvents };
}