All files / player-browser/src/chart replay-arrangement.ts

95.45% Statements 42/44
94.28% Branches 33/35
100% Functions 4/4
95.34% Lines 41/43

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                                                    55x 55x 55x 456x 456x 456x 456x 311x   145x               55x 55x 456x 456x 1x   455x 175x   456x     55x 207x   207x 207x 207x 207x 207x 2x   205x   53x 246x 246x     246x   53x 3x 3x     3x   53x 143x 1x       52x 451x   52x       503x    
import { resolveReplaySourceChannel, type ReplayNote } from '@be-music/player-engine/replay';
 
/**
 * Re-applies a recorded replay's note arrangement onto a freshly prepared chart for replay playback.
 *
 * A replay stores the RESOLVED chart the player saw — post lane-shuffle (RANDOM / MIRROR / S-RANDOM) and post
 * DP-flip. A fresh chart prepare (with those options off) yields the same notes at the same times but with the
 * ORIGINAL channels, so replaying against it would judge the inputs on the wrong lanes. Lane transforms never move
 * a note in time, only across channels, so the recorded arrangement can be re-applied by matching notes on
 * `(kind, timeUs, endTimeUs, original lane)` (legacy replays omit the last key) and assigning the recorded channels back — no shuffle seed needed.
 *
 * Mutates the `channel` field of the given note objects in place (they are the same instances the prepared-chart
 * bundle and the engine share). Returns an error string when the chart does not match the replay — a different
 * `#RANDOM` control-flow roll, a different chart file, or an edited chart — in which case the notes are left
 * untouched (channels are only written after every bucket matched).
 */
export function applyReplayArrangement(
  replayNotes: readonly ReplayNote[],
  target: {
    /** Playable notes (freezone included) — `PreparedPlaybackChartData.notes`. */
    notes: Array<{ channel: string; seconds: number; event?: { channel: string }; endSeconds?: number }>;
    landmineNotes: Array<{ channel: string; seconds: number; event?: { channel: string } }>;
    invisibleNotes: Array<{ channel: string; seconds: number; event?: { channel: string } }>;
    activeFreeZoneChannels: ReadonlySet<string>;
  },
): { ok: true } | { ok: false; reason: string } {
  const buckets = new Map<string, ReplayNote[]>();
  const sourceAware = new Set<string>();
  for (const note of replayNotes) {
    const key = `${note.type}:${note.timeUs}:${note.endTimeUs ?? ''}`;
    if (note.sourceChannel !== undefined) sourceAware.add(key);
    const bucket = buckets.get(key);
    if (bucket) {
      bucket.push(note);
    } else {
      buckets.set(key, [note]);
    }
  }
 
  interface Assignment {
    note: { channel: string };
    channel: string;
  }
  const assignments: Assignment[] = [];
  const takeChannel = (key: string, note: { channel: string; event?: { channel: string } }): string | undefined => {
    const bucket = buckets.get(key);
    if (!bucket || bucket.length === 0) {
      return undefined;
    }
    const index = sourceAware.has(key)
      ? bucket.findIndex((entry) => (entry.sourceChannel ?? entry.channel) === resolveReplaySourceChannel(note))
      : 0;
    return index < 0 ? undefined : bucket.splice(index, 1)[0]!.channel;
  };
 
  for (const note of target.notes) {
    const timeUs = secondsToMicroseconds(note.seconds);
    const hasTail =
      typeof note.endSeconds === 'number' && Number.isFinite(note.endSeconds) && note.endSeconds > note.seconds;
    const endTimeUs = hasTail ? secondsToMicroseconds(note.endSeconds!) : undefined;
    const kind = target.activeFreeZoneChannels.has(note.channel) ? 'freezone' : hasTail ? 'long' : 'normal';
    const channel = takeChannel(`${kind}:${timeUs}:${endTimeUs ?? ''}`, note);
    if (channel === undefined) {
      return { ok: false, reason: `no recorded ${kind} note at ${timeUs}µs` };
    }
    assignments.push({ note, channel });
  }
  for (const mine of target.landmineNotes) {
    const channel = takeChannel(`mine:${secondsToMicroseconds(mine.seconds)}:`, mine);
    Iif (channel === undefined) {
      return { ok: false, reason: `no recorded mine at ${secondsToMicroseconds(mine.seconds)}µs` };
    }
    assignments.push({ note: mine, channel });
  }
  for (const invisible of target.invisibleNotes) {
    const channel = takeChannel(`invisible:${secondsToMicroseconds(invisible.seconds)}:`, invisible);
    Iif (channel === undefined) {
      return { ok: false, reason: `no recorded invisible note at ${secondsToMicroseconds(invisible.seconds)}µs` };
    }
    assignments.push({ note: invisible, channel });
  }
  for (const [key, bucket] of buckets) {
    if (bucket.length > 0) {
      return { ok: false, reason: `recorded chart has ${bucket.length} unmatched note(s) at ${key}` };
    }
  }
 
  for (const { note, channel } of assignments) {
    note.channel = channel;
  }
  return { ok: true };
}
 
function secondsToMicroseconds(seconds: number): number {
  return Math.round(seconds * 1_000_000);
}