All files / player-scene-model/src/play-options practice.ts

100% Statements 24/24
100% Branches 22/22
100% Functions 4/4
100% Lines 21/21

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 107                                                                                                                10x                 16x                           8x 8x 8x   8x 8x 8x   8x 2x   8x 8x 8x 8x 8x 8x 8x         6x 6x 6x 6x    
import {
  isPracticeGaugePick,
  splitPracticeGaugePick,
  practiceReadsJudgeRankAndTotal,
  resolveDefaultPracticeGauge,
  type BeatorajaGaugeCategory,
  type RulesetId,
} from '@be-music/player-engine/ruleset';
import type { PlayOptions, PracticeGaugePick } from './options.ts';
 
/** The PRACTICE play options a play reads. */
export type PracticePlayOptions = Pick<
  PlayOptions,
  | 'practiceGauge'
  | 'practiceGaugeCategory'
  | 'practiceStartGauge'
  | 'practiceJudgeRank'
  | 'practiceTotal'
  | 'practiceRestBefore'
  | 'practiceRestAfter'
  | 'practiceCarriedLongNotes'
>;
 
/**
 * The bars a PRACTICE play loops, as chart milliseconds: from the first bar's line up to the line closing the last
 * (`chartScoreRangeTimes`). Absent `endMs` plays to the chart's end.
 */
export interface PracticeRange {
  readonly startMs: number;
  readonly endMs?: number;
}
 
/** The range and rules of a PRACTICE play, as the engine takes them (`PlayerOptions.practice`). */
export interface PracticeSelection {
  startMs: number;
  /** Absent plays to the chart's end. */
  endMs?: number;
  /** The judge ruleset's own PRACTICE gauge, or another source's qualified with it. */
  gauge: PracticeGaugePick;
  /** beatoraja only; absent uses the play mode's. */
  gaugeCategory?: BeatorajaGaugeCategory;
  startGauge: number;
  /** Absent keeps the chart's. */
  judgeRank?: number;
  /** Absent keeps the chart's. */
  total?: number;
  /** Bars of rest before the range. */
  restBarsBefore: number;
  /** Bars of rest after the range. */
  restBarsAfter: number;
  /** Keeps a long note held into the range from before it. */
  carriedLongNotes: boolean;
}
 
/** The source whose gauge a PRACTICE pick plays on under the judge ruleset `ruleset`: its own, or the ruleset's. */
export function practiceGaugeSource(gauge: PracticeGaugePick, ruleset: RulesetId): RulesetId {
  return splitPracticeGaugePick(gauge)?.source ?? ruleset;
}
 
/**
 * `gauge` when it is one of `ruleset`'s PRACTICE gauges or another source's qualified with it, else the ruleset's default
 * (#933): a bare PRACTICE gauge is the judge ruleset's own, so changing the ruleset restores its default gauge, as the
 * play's gauge pick does; a qualified one keeps its source (ADR 0019).
 */
export function keepPracticeGauge(gauge: PracticeGaugePick, ruleset: RulesetId): PracticeGaugePick {
  return isPracticeGaugePick(gauge, ruleset) ? gauge : resolveDefaultPracticeGauge(ruleset);
}
 
/**
 * The PRACTICE a play under `ruleset` runs (#933): the bars picked for it (`range`; the whole chart without one), the
 * ruleset's own gauge (its default when the stored one is another ruleset's), GAUGE CATEGORY under beatoraja only, and
 * JUDGERANK and TOTAL where the ruleset reads them (not under That-Which-Must-Not-Be-Named). A 0 JUDGERANK or TOTAL
 * keeps the chart's.
 */
export function resolvePracticeSelection(
  options: PracticePlayOptions,
  ruleset: RulesetId,
  range?: PracticeRange,
): PracticeSelection {
  const readsRules = practiceReadsJudgeRankAndTotal(ruleset);
  const gauge = keepPracticeGauge(options.practiceGauge, ruleset);
  const startMs = Math.max(0, Math.trunc(range?.startMs ?? 0));
  // Built field by field in the original key order; conditional spreads would copy an object for every optional field.
  const selection: PracticeSelection = { startMs } as PracticeSelection;
  if (range?.endMs !== undefined && range.endMs > startMs) selection.endMs = Math.trunc(range.endMs);
  selection.gauge = gauge;
  // GAUGE CATEGORY is beatoraja's: it follows a beatoraja gauge, under every judge ruleset.
  if (practiceGaugeSource(gauge, ruleset) === 'beatoraja' && options.practiceGaugeCategory !== 'MODE') {
    selection.gaugeCategory = options.practiceGaugeCategory;
  }
  selection.startGauge = options.practiceStartGauge;
  selection.restBarsBefore = options.practiceRestBefore;
  selection.restBarsAfter = options.practiceRestAfter;
  selection.carriedLongNotes = options.practiceCarriedLongNotes;
  if (readsRules && options.practiceJudgeRank > 0) selection.judgeRank = options.practiceJudgeRank;
  if (readsRules && options.practiceTotal > 0) selection.total = options.practiceTotal;
  return selection;
}
 
/** `ms` as `m:ss.s`, tenths rounded down (`83_456` → `1:23.4`). */
export function formatPracticeTime(ms: number): string {
  const tenths = Math.max(0, Math.floor(ms / 100));
  const minutes = Math.floor(tenths / 600);
  const seconds = (tenths % 600) / 10;
  return `${minutes}:${seconds.toFixed(1).padStart(4, '0')}`;
}