All files / player-browser/src/session/play play-option-mismatch.ts

100% Statements 43/43
100% Branches 48/48
100% Functions 6/6
100% Lines 34/34

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 98 99 100 101 102 103 104 105 106                                              8x 8x 8x 8x 3x       6x                 26x 26x 8x 4x                                   2x 2x 2x                 13x 13x 4x 13x 2x 13x 2x 13x 13x 1x 13x 13x 13x 13x 13x 13x                       6x 5x 4x 2x 2x    
/**
 * A play whose options do not fit its chart. Some lane options and conversions apply to certain charts only (beatoraja
 * CONVERGE and playable RANDOM to PMS, LR2 SCATTER to 9KEY, SP TO DP and EXTRA MODE to BMS, ...), and the chart
 * preparation rejects the rest with a `RangeError`. {@link toPlayOptionMismatch} names the options that convert the
 * chart, so the host can tell the player which ones to change instead of leaving the play screen black.
 */
 
import {
  canonicalOptionValue,
  optionValueSources,
  resolveGaugeTypes,
  splitGaugePick,
  splitOptionSource,
  type GaugePick,
  type RulesetId,
} from '@be-music/player-engine/ruleset';
import type { PlaySessionOptions } from './play-options.ts';
 
/**
 * How a source-qualified value reads: `beatoraja:CONVERGE` as `CONVERGE (beatoraja)`, `____:BATTLE` as `BATTLE (????)`,
 * a value two sources share with both, `lr2:S-RANDOM` as `S-RANDOM (LR2 / ????)`, and a merged one as its current option.
 */
function optionLabel(value: string): string {
  const current = canonicalOptionValue(value);
  const { name } = splitOptionSource(current);
  const sources = optionValueSources(current);
  if (sources.length === 0) return name;
  return `${name} (${sources.map(sourceLabel).join(' / ')})`;
}
 
function sourceLabel(source: RulesetId): string {
  return source === 'lr2' ? 'LR2' : source === '____' ? '????' : source;
}
 
/**
 * A gauge pick the judge ruleset does not define itself, as the Options dialog labels it: another source's gauge
 * (`lr2:HARD` under beatoraja reads `HARD (LR2)`), or a bare gauge the ruleset's own menu lacks (LR2's P-ATTACK under
 * beatoraja, which the engine refuses). `undefined` for the ruleset's own gauge, which is never the mismatch.
 */
function foreignGaugeLabel(pick: GaugePick | undefined, ruleset: RulesetId): string | undefined {
  const parts = splitGaugePick(pick);
  if (parts === undefined) return undefined;
  if (parts.source === undefined) return resolveGaugeTypes(ruleset).includes(parts.type) ? undefined : parts.type;
  return parts.source === ruleset ? undefined : `${parts.type} (${sourceLabel(parts.source)})`;
}
 
/**
 * The play options that convert the chart, with the gauges and the judge ruleset that reads them, as the Options dialog
 * labels them.
 */
export type PlayOptionMismatchSource = Pick<
  PlaySessionOptions,
  'random1P' | 'random2P' | 'battleMode' | 'lr2ExtraMode' | 'longNoteModifier' | 'gauge' | 'gauge2P' | 'judgeRuleset'
>;
 
/** The chart could not be prepared with the selected play options. */
export class PlayOptionMismatchError extends RangeError {
  /** The selected options that convert the chart (`Random 1P: CONVERGE`, ...). */
  readonly options: readonly string[];
 
  constructor(options: readonly string[], cause: unknown) {
    super(`The selected play options cannot be used with this chart: ${options.join(', ')}.`, { cause });
    this.name = 'PlayOptionMismatchError';
    this.options = options;
  }
}
 
/**
 * The selected options that convert the chart, labelled as the Options dialog shows them, and each gauge that is not the
 * judge ruleset's own (`Gauge: HARD (LR2)`).
 */
export function describeChartConvertingOptions(options: PlayOptionMismatchSource): string[] {
  const labels: string[] = [];
  if (options.random1P !== undefined && options.random1P !== 'OFF')
    labels.push(`Random 1P: ${optionLabel(options.random1P)}`);
  if (options.random2P !== undefined && options.random2P !== 'OFF')
    labels.push(`Random 2P: ${optionLabel(options.random2P)}`);
  if (options.battleMode !== undefined && options.battleMode !== 'OFF')
    labels.push(`Battle: ${optionLabel(options.battleMode)}`);
  if (options.lr2ExtraMode === true) labels.push('EXTRA MODE (LR2)');
  if (options.longNoteModifier !== undefined && options.longNoteModifier !== 'OFF')
    labels.push(`Long notes: ${options.longNoteModifier}`);
  const ruleset = options.judgeRuleset ?? 'lr2';
  const gauge = foreignGaugeLabel(options.gauge, ruleset);
  if (gauge !== undefined) labels.push(`Gauge: ${gauge}`);
  const gauge2P = foreignGaugeLabel(options.gauge2P, ruleset);
  if (gauge2P !== undefined) labels.push(`Gauge 2P: ${gauge2P}`);
  return labels;
}
 
/**
 * The chart preparation's `error` as a {@link PlayOptionMismatchError}, or `undefined` when it is something else: not a
 * `RangeError`, a replay or a kept arrangement (they play recorded options, not the selected ones), or a play with no
 * chart-converting option selected.
 */
export function toPlayOptionMismatch(
  error: unknown,
  options: PlayOptionMismatchSource & Pick<PlaySessionOptions, 'replay' | 'fixedArrangement'>,
): PlayOptionMismatchError | undefined {
  if (error instanceof PlayOptionMismatchError) return error;
  if (!(error instanceof RangeError)) return undefined;
  if (options.replay !== undefined || options.fixedArrangement !== undefined) return undefined;
  const labels = describeChartConvertingOptions(options);
  return labels.length > 0 ? new PlayOptionMismatchError(labels, error) : undefined;
}