All files / app-play/src/services gauge-graphs.ts

100% Statements 16/16
94.11% Branches 16/17
100% Functions 2/2
100% Lines 13/13

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                                      7x 7x 5x 4x 3x 7x 7x 7x 18x 18x 150x   18x 18x      
// The result screen's gauge graphs (beatoraja's CHANGE_GRAPH): a play's gauge history under each gauge of its ruleset.
// beatoraja logs every gauge during the play; here the others are re-simulated from the play's replay
// (`resolveReplayGaugeHistories`), which is deterministic, so each graph is what that gauge would have drawn.
import type { ResultGaugeGraph } from '@be-music/player-browser/host';
import { resolveReplayGaugeHistories } from '@be-music/player-engine/replay';
import type { PlayResult } from '@be-music/player-scene-model';
 
/** What the gauge graphs are worked out from: a single play's result. */
export type GaugeGraphResult = Pick<
  PlayResult,
  'replay' | 'gaugeHistory' | 'chartSeconds' | 'run' | 'course' | 'practice' | 'fromReplay'
>;
 
/**
 * The gauge graphs of a finished play, in its ruleset's gauge order, or none when they cannot be worked out: a play
 * without a replay or a chart length, and a course's (a stage's other gauges start from values the stage did not
 * record). A graph ends where the play ended, so a play that stopped as its gauge emptied shows no judgment after it.
 */
export function resolveResultGaugeGraphs(result: GaugeGraphResult): ResultGaugeGraph[] {
  const { replay, chartSeconds } = result;
  if (replay === undefined || chartSeconds === undefined || chartSeconds <= 0) return [];
  if (result.course !== undefined || replay.play.course !== undefined) return [];
  if (result.run !== undefined && result.run !== 'single') return [];
  const endProgress = result.gaugeHistory.at(-1)?.progress ?? 1;
  const chartUs = chartSeconds * 1_000_000;
  const histories = resolveReplayGaugeHistories(replay, endProgress < 1 ? { untilUs: endProgress * chartUs } : {});
  return histories.map(({ gauge, initial, samples }) => {
    const history = [{ progress: 0, value: initial }];
    for (const sample of samples)
      history.push({ progress: Math.max(0, Math.min(endProgress, sample.atUs / chartUs)), value: sample.gauge });
    // The graph reaches the end of the play, as the play's own does.
    history.push({ progress: endProgress, value: history.at(-1)!.value });
    return { type: gauge, history };
  });
}