All files / app-play/src/ui/options play-option-items.ts

100% Statements 68/68
95% Branches 57/60
100% Functions 35/35
100% Lines 54/54

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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294                                                                    4x               4x                                   4x                       120x             2365x         2806x 2806x 2347x 2347x                   11x 11x 11x 221x 221x 221x 221x 221x   11x 2585x 11x 221x 884x                     3x                                     3x     12x         4x                     210x 146x 130x 122x 106x                       158x       8x 52x 31x 31x     8x 52x 2x     156x                   1x 27x         4x                   233x                                 175x       8x 8x 58x       174x                 4x 2x       2x                           4x                             2x              
import {
  optionValueSources,
  qualifyGaugePick,
  qualifyPracticeGaugePick,
  resolveGaugeTypes,
  resolvePracticeGaugeTypes,
  RULESET_IDS,
  splitGaugePick,
  splitPracticeGaugePick,
  splitOptionSource,
  type GaugePick,
  type GaugeType,
  type RulesetId,
} from '@be-music/player-engine/ruleset';
import {
  formatGaugeLabel,
  laneCoversFromPlayOptions,
  resolveRandomModes,
  type LaneCoverState,
  type PlayOptions,
  type PracticeGaugePick,
  type PracticeGaugeType,
  type RandomMode,
} from '@be-music/player-scene-model';
 
/** One choice of an Options dialog list: its label and the value it sets. */
export interface PlayOptionItem<T> {
  text: string;
  value: T;
  /** Shown dimmed and not selectable (`OptionChoice.disabled`). */
  disabled?: boolean;
}
 
/** How the Options dialog names each source. */
const SOURCE_LABELS: Readonly<Record<RulesetId, string>> = {
  lr2: 'LR2',
  beatoraja: 'beatoraja',
  ____: '????',
  plugout: 'plugout',
};
 
/** The source of each lane option only one source defines (ADR 0019's registry, as the Options dialog groups them). */
const LANE_OPTION_SOURCES: Readonly<Record<string, RulesetId>> = {
  'R-RANDOM': 'beatoraja',
  SPIRAL: 'beatoraja',
  'H-RANDOM': 'beatoraja',
  'ALL-SCR': 'beatoraja',
  'RANDOM-EX': 'beatoraja',
  'S-RANDOM-EX': 'beatoraja',
  RANDOM_PLAYABLE: 'beatoraja',
  S_RANDOM_PLAYABLE: 'beatoraja',
  SCATTER: 'lr2',
  'SCATTER-CORRECTED': 'lr2',
  'S-RANDOM-CORRECTED': 'lr2',
  'CONVERGE-CORRECTED': 'lr2',
  'SYNCHRONIZE-RANDOM': '____',
  'SYMMETRY-RANDOM': '____',
};
 
/** How the Options dialog spells a lane option's name. */
const LANE_OPTION_NAMES: Readonly<Record<string, string>> = {
  'SCATTER-CORRECTED': 'SCATTER 9KEY corrected',
  'S-RANDOM-CORRECTED': 'S-RANDOM 9KEY corrected',
  'CONVERGE-CORRECTED': 'CONVERGE corrected',
  RANDOM_PLAYABLE: 'RANDOM PLAYABLE',
  S_RANDOM_PLAYABLE: 'S-RANDOM PLAYABLE',
  'SYNCHRONIZE-RANDOM': 'SYNCHRONIZE RANDOM',
  'SYMMETRY-RANDOM': 'SYMMETRY RANDOM',
};
 
/** The sources in the order the Options dialog groups them: the judge ruleset's own first. */
function sourceOrder(ruleset: RulesetId): RulesetId[] {
  return [ruleset, ...RULESET_IDS.filter((source) => source !== ruleset)];
}
 
/**
 * The source a lane option value comes from; `undefined` for OFF, MIRROR and RANDOM, which every source defines alike.
 */
export function laneOptionSource(mode: RandomMode): RulesetId | undefined {
  return splitOptionSource(mode).source ?? LANE_OPTION_SOURCES[mode];
}
 
/** The sources that define a lane option value as it runs: its source, and those sharing it (AC's S-RANDOM is LR2's). */
function laneOptionSources(mode: RandomMode): readonly RulesetId[] {
  const shared = optionValueSources(mode);
  if (shared.length > 0) return shared;
  const source = laneOptionSource(mode);
  return source === undefined ? [] : [source];
}
 
/**
 * The Options dialog's lane options under `ruleset`, grouped by source (ADR 0019): OFF, MIRROR and RANDOM, then the judge
 * ruleset's own source's options, then the other sources'. Each source-specific option names its sources, so
 * `lr2:S-RANDOM` reads `S-RANDOM (LR2 / ????)` and `beatoraja:S-RANDOM` reads `S-RANDOM (beatoraja)`; a shared one sits
 * in the group of the first source in that order.
 */
export function laneOptionItems(ruleset: RulesetId, duplicatedBattle1P = false): PlayOptionItem<RandomMode>[] {
  const modes = resolveRandomModes(ruleset, duplicatedBattle1P);
  const order = sourceOrder(ruleset);
  const item = (mode: RandomMode): PlayOptionItem<RandomMode> => {
    const sources = laneOptionSources(mode);
    const name = splitOptionSource(mode).name;
    const text = LANE_OPTION_NAMES[name] ?? name;
    const label = sources.map((source) => SOURCE_LABELS[source]).join(' / ');
    return { text: sources.length === 0 ? text : `${text} (${label})`, value: mode };
  };
  const group = (mode: RandomMode): RulesetId | undefined =>
    order.find((source) => laneOptionSources(mode).includes(source));
  return [
    ...modes.filter((mode) => group(mode) === undefined),
    ...order.flatMap((source) => modes.filter((mode) => group(mode) === source)),
  ].map(item);
}
 
/**
 * The Options dialog's battle and conversion modes under `ruleset`, grouped by source like the lane options. BATTLE names
 * LR2's two-player play (`TWO-PLAYER`, offered under every ruleset; That-Which-Must-Not-Be-Named calls it 2P PLAY) and
 * the beatoraja and That-Which-Must-Not-Be-Named one-player copies (`beatoraja:BATTLE`, `____:BATTLE`), so each
 * carries its source. COUPLE stays with LR2 rules.
 */
export function battleModeItems(ruleset: RulesetId): PlayOptionItem<PlayOptions['battleMode']>[] {
  const groups: Record<RulesetId, PlayOptionItem<PlayOptions['battleMode']>[]> = {
    lr2: [
      ...(ruleset === '____' ? [] : [{ text: 'BATTLE (LR2)', value: 'TWO-PLAYER' as const }]),
      ...(ruleset === 'lr2' ? [{ text: 'COUPLE (LR2)', value: 'COUPLE' as const }] : []),
      { text: 'D-BATTLE (LR2)', value: 'D-BATTLE' },
      { text: 'SP TO DP (LR2)', value: 'SP-TO-DP' },
      { text: 'SP TO DP corrected (LR2)', value: 'SP-TO-DP-CORRECTED' },
    ],
    beatoraja: [
      { text: 'BATTLE (beatoraja)', value: 'beatoraja:BATTLE' },
      { text: 'BATTLE AS (beatoraja)', value: 'BATTLE-AS' },
    ],
    ____: [
      { text: 'BATTLE (????)', value: '____:BATTLE' },
      ...(ruleset === '____' ? [{ text: '2P PLAY (????)', value: 'TWO-PLAYER' as const }] : []),
    ],
    // plugout's two-player play is LR2's BATTLE, listed under LR2.
    plugout: [],
  };
  return [
    { text: 'OFF', value: 'OFF' },
    { text: 'CHART HEADER', value: 'AUTO' },
    ...sourceOrder(ruleset).flatMap((source) => groups[source]),
  ];
}
 
/** plugout's names for its gauges (the select skin's NORMAL / HARD / HAZARD / EASY and their EX variants). */
const PLUGOUT_GAUGE_NAMES: Partial<Readonly<Record<GaugeType, string>>> = {
  GROOVE: 'NORMAL',
  DEATH: 'HAZARD',
  'EX-GROOVE': 'EXNORMAL',
  'EX-HARD': 'EXHARD',
  'EX-DEATH': 'EXHAZARD',
  'EX-EASY': 'EX EASY',
};
 
/** A gauge's name as its source spells it: beatoraja's and AC's GROOVE is NORMAL, beatoraja's DEATH its HAZARD. */
function gaugeName(source: RulesetId, type: GaugeType): string {
  if (source === 'plugout') return PLUGOUT_GAUGE_NAMES[type] ?? formatGaugeLabel(type) ?? type;
  if (type === 'ASSIST-EASY') return source === '____' ? 'ASSISTED EASY' : 'ASSIST EASY';
  if (type === 'DEATH' && source === 'beatoraja') return 'HAZARD';
  if (type === 'GROOVE' && source !== 'lr2') return 'NORMAL';
  return formatGaugeLabel(type) ?? type;
}
 
/**
 * The Options dialog's gauges under `ruleset`, grouped by source like the lane options (ADR 0019, #1171): the judge
 * ruleset's own gauges as bare values, then each other source's gauges as qualified picks named with their source, so
 * `lr2:HARD` reads `HARD (LR2)` under beatoraja. A qualified pick keeps its source's gauge whichever ruleset judges.
 *
 * `selected` are the picks the dialog holds. One qualified with the judge ruleset itself (`lr2:HARD` under LR2, kept
 * across a ruleset change) plays as the bare gauge does, so it is listed after the own gauges only while it is selected.
 */
export function gaugeItems(ruleset: RulesetId, selected: readonly GaugePick[] = []): PlayOptionItem<GaugePick>[] {
  const qualified = (source: RulesetId, type: GaugeType): PlayOptionItem<GaugePick> => ({
    text: `${gaugeName(source, type)} (${SOURCE_LABELS[source]})`,
    value: qualifyGaugePick(source, type),
  });
  const kept = resolveGaugeTypes(ruleset).filter((type) =>
    selected.some((pick) => {
      const parts = splitGaugePick(pick);
      return parts?.source === ruleset && parts.type === type;
    }),
  );
  return [
    ...resolveGaugeTypes(ruleset).map((type) => ({ text: gaugeName(ruleset, type), value: type })),
    ...kept.map((type) => qualified(ruleset, type)),
    ...sourceOrder(ruleset)
      .slice(1)
      .flatMap((source) => resolveGaugeTypes(source).map((type) => qualified(source, type))),
  ];
}
 
/** {@link gaugeItems} for the dialog's state, as a select's label-to-value choices. */
export function gaugeChoices(state: {
  judgeRuleset: RulesetId;
  gauge: GaugePick;
  gauge2P: GaugePick;
}): Record<string, GaugePick> {
  return Object.fromEntries(
    gaugeItems(state.judgeRuleset, [state.gauge, state.gauge2P]).map((item) => [item.text, item.value]),
  );
}
 
/** How the Options dialog spells each PRACTICE gauge id. */
const PRACTICE_GAUGE_NAMES: Readonly<Partial<Record<PracticeGaugeType, string>>> = {
  'ASSIST-EASY': 'ASSIST EASY',
  'ASSISTED-EASY': 'ASSISTED EASY',
  CLASS: 'GRADE',
  EXCLASS: 'EX GRADE',
  EXHARDCLASS: 'EXHARD GRADE',
};
 
/** A PRACTICE gauge's name as its source spells it (LR2's DEATH is its HAZARD). */
function practiceGaugeName(source: RulesetId, type: PracticeGaugeType): string {
  return source === 'lr2' && type === 'DEATH'
    ? 'HAZARD'
    : (PRACTICE_GAUGE_NAMES[type] ?? formatGaugeLabel(type) ?? type);
}
 
/**
 * PRACTICE's GAUGE TYPE under `ruleset`, grouped by source like the play's gauges (ADR 0019): the judge ruleset's own
 * gauges (#933) as bare values, named as that ruleset names them, then each other source's PRACTICE gauges as qualified
 * picks named with their source (`ASSIST EASY (beatoraja)` under LR2), which keep that source's gauge.
 *
 * `selected` is the pick the dialog holds: one qualified with the judge ruleset itself (kept across a ruleset change)
 * plays as the bare gauge does, so it is listed after the own gauges only while it is selected.
 */
export function practiceGaugeItems(
  ruleset: RulesetId,
  selected?: PracticeGaugePick,
): PlayOptionItem<PracticeGaugePick>[] {
  const qualified = (source: RulesetId, type: PracticeGaugeType): PlayOptionItem<PracticeGaugePick> => ({
    text: `${practiceGaugeName(source, type)} (${SOURCE_LABELS[source]})`,
    value: qualifyPracticeGaugePick(source, type),
  });
  const kept = splitPracticeGaugePick(selected);
  return [
    ...resolvePracticeGaugeTypes(ruleset).map((type) => ({ text: practiceGaugeName(ruleset, type), value: type })),
    ...(kept?.source === ruleset ? [qualified(ruleset, kept.type)] : []),
    ...sourceOrder(ruleset)
      .slice(1)
      .flatMap((source) => resolvePracticeGaugeTypes(source).map((type) => qualified(source, type))),
  ];
}
 
/**
 * The lowest auto-shift gauge under `ruleset`, named as the ruleset's own gauges (#933). LR2 and plugout have no ASSIST
 * EASY (the engine reads it as EASY), so their lowest choice is EASY.
 */
export function bottomShiftableGaugeItems(ruleset: RulesetId): PlayOptionItem<PlayOptions['bottomShiftableGauge']>[] {
  if (ruleset === 'lr2' || ruleset === 'plugout')
    return [
      { text: 'EASY', value: 'EASY' },
      { text: ruleset === 'lr2' ? 'GROOVE' : 'NORMAL', value: 'GROOVE' },
    ];
  return [
    { text: ruleset === '____' ? 'ASSISTED EASY' : 'ASSIST EASY', value: 'ASSIST-EASY' },
    { text: 'EASY', value: 'EASY' },
    { text: 'NORMAL', value: 'GROOVE' },
  ];
}
 
/**
 * The SUDDEN+ and HIDDEN+ covers the menu's Effect, Lane cover and Cover height set, for a running play to take as a
 * `cover-set`. The LIFT stays the play's own, so it is left out.
 */
export function menuLaneCovers(
  options: Pick<PlayOptions, 'hiddenSudden1P' | 'hiddenSudden2P' | 'shutter' | 'laneCover'>,
): Pick<LaneCoverState, 'side' | 'sudden' | 'suddenOn' | 'hidden' | 'hiddenOn'>[] {
  return laneCoversFromPlayOptions(options, ['1P', '2P']).map(({ side, sudden, suddenOn, hidden, hiddenOn }) => ({
    side,
    sudden,
    suddenOn,
    hidden,
    hiddenOn,
  }));
}
 
/**
 * nanasigroove's NOTES choices for the bar under the cursor. A course keeps one key mode, so a course stage leaves COPY
 * out and DBLCOPY keeps its DOUBLE (ADR 0035): on a course bar COPY and DBLCOPY are shown but cannot be picked, while
 * DOUBLE, which a stage still plays, stays offered. A pick made on a chart bar is kept, so it returns with the chart.
 */
export function crazyNotesItems(course: boolean): PlayOptionItem<PlayOptions['crazyNotes']>[] {
  return [
    { text: 'OFF', value: 'OFF' },
    { text: 'DOUBLE', value: 'DOUBLE' },
    { text: 'COPY', value: 'COPY', ...(course ? { disabled: true } : {}) },
    { text: 'DBLCOPY', value: 'DBLCOPY', ...(course ? { disabled: true } : {}) },
  ];
}