All files / app-leaderboard/src/scores chart-play-options.ts

91.66% Statements 11/12
95.23% Branches 20/21
100% Functions 2/2
100% Lines 9/9

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                                  275x 275x                 275x 262x 262x 262x 261x 261x         275x    
import { canonicalizeJSONValue, type ChartPlayOptions, type ChartPlayVariant } from '@be-music/chart-model';
import { resolveChartPlayProgram, type BeMusicReplay } from '@be-music/player-engine/replay';
 
export type ChartPlayOptionsCheck = { ok: true } | { ok: false; reason: 'chart-play-options-mismatch' };
 
/**
 * Checks a replay's chart gameplay program (`chart.chartPlayOptions`, #844 / #932) against the chart it played, so a
 * replay can neither strip nor add one (docs/leaderboard.md section 5.3). The play document, already checked against
 * the play fingerprint (L3), carries the options the chart declares (`playOptions`) and its lane mode; the server narrows
 * them using the same rules as the player. The replay must record both the effective options and which ones the
 * player supplied (`play.playerChartPlayOptions`, #1243).
 * A NONSTOP run's stitched play applies no stage's options, so its program must be empty.
 */
export function checkChartPlayOptions(
  replay: Pick<BeMusicReplay, 'chart' | 'play'>,
  playDocument: unknown,
): ChartPlayOptionsCheck {
  const expected = expectedChartPlayOptions(replay, playDocument);
  return expected !== undefined &&
    canonicalizeJSONValue(replay.chart.chartPlayOptions?.options ?? {}) === canonicalizeJSONValue(expected.options) &&
    canonicalizeJSONValue(replay.play.playerChartPlayOptions ?? {}) === canonicalizeJSONValue(expected.playerOptions)
    ? { ok: true }
    : { ok: false, reason: 'chart-play-options-mismatch' };
}
 
/** The options a play of the document's chart runs, or `undefined` for a document without a lane mode. */
function expectedChartPlayOptions(replay: Pick<BeMusicReplay, 'chart' | 'play'>, playDocument: unknown) {
  if (replay.play.course?.kind === 'nonstop') return { options: {}, playerOptions: {} };
  Iif (typeof playDocument !== 'object' || playDocument === null) return undefined;
  const { laneMode, playOptions } = playDocument as { laneMode?: unknown; playOptions?: ChartPlayOptions };
  if (typeof laneMode !== 'string') return undefined;
  const recorded = replay.chart.chartPlayOptions;
  const { program, playerOptions } = resolveChartPlayProgram(
    { version: recorded?.version ?? 1, options: playOptions ?? {} },
    recorded?.options,
    { playVariant: laneMode as ChartPlayVariant, course: replay.play.course !== undefined },
  );
  return { options: program.options, playerOptions };
}