All files / app-play/src family-dispatch.ts

75% Statements 27/36
80% Branches 40/50
66.66% Functions 2/3
84% Lines 21/25

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                                                                    145x     145x 145x 16x 9x 9x 6x         6x             145x 135x 135x 129x         19x   145x                                       145x 145x 145x 5x   140x 127x 94x                                        
import type { SkinFamilyId } from '@be-music/player-browser/skin';
import type { BeatorajaThemeBundle } from '@be-music/player-browser/skin';
import type { LibraryChart } from '@be-music/player-scene-model';
import { pickLR2PlaySkin, type LR2PlaySkinMap, type LR2Skin } from '@be-music/skin-lr2';
import type { SkinFamilyOverride, SkinFamilySceneKind } from './types.ts';
import { canPlayChartBeatoraja } from './chart-shape.ts';
 
/**
 * Read-only state snapshot the family-dispatch helpers operate on. The player's `FamilyKits` (`app/family-kits.ts`) builds
 * one of these from the theme services (see its `familyDispatchState()`) — these functions stay pure and easy to
 * unit-test in isolation.
 */
export interface FamilyDispatchState {
  readonly beatorajaTheme: BeatorajaThemeBundle | undefined;
  readonly selectSkin: LR2Skin | undefined;
  readonly decideSkin: LR2Skin | undefined;
  readonly resultSkin: LR2Skin | undefined;
  readonly playSkins: LR2PlaySkinMap;
  readonly skinFamilyOverride: SkinFamilyOverride;
}
 
/**
 * Returns the set of skin families that can render the given scene with the currently-loaded assets. `'default'`
 * is always present (it's the catch-all chrome with no asset dependency); `'lr2'` and `'beatoraja'` only appear
 * when the matching theme actually ships a skin for that scene type — passing `song` matters for `'gameplay'`
 * (the beatoraja path needs a play-variant skin compatible with the chart's key mode) and `'decide'` (skip
 * entirely when the beatoraja theme has no decide skin) where the per-chart availability differs from the
 * per-theme availability.
 */
export function availableFamiliesForScene(
  state: FamilyDispatchState,
  scene: SkinFamilySceneKind,
  chartEntry?: LibraryChart,
): Set<SkinFamilyId> {
  const available = new Set<SkinFamilyId>(['default']);
  // Beatoraja availability per scene. The play scene additionally needs a chart so we can check whether the
  // theme has a play-variant skin compatible with this chart's key mode.
  const beatorajaTheme = state.beatorajaTheme?.theme;
  if (beatorajaTheme !== undefined) {
    if (scene === 'select' && beatorajaTheme.selectSkin !== undefined) available.add('beatoraja');
    else Iif (scene === 'decide' && beatorajaTheme.decideSkin !== undefined) available.add('beatoraja');
    else if (scene === 'result' && beatorajaTheme.resultSkin !== undefined) available.add('beatoraja');
    else Eif (
      scene === 'gameplay' &&
      chartEntry !== undefined &&
      canPlayChartBeatoraja(state.beatorajaTheme, chartEntry)
    ) {
      available.add('beatoraja');
    }
  }
  // LR2 availability per scene. The play scene needs an LR2 skin for the chart's variant — if `pickLR2PlaySkin`
  // returns `undefined` the play path would fall through to the default-family scene anyway, so it's correct to
  // mark LR2 unavailable here. The other scenes consult the slot directly because every LR2 theme can ship a
  // partial set (e.g. a play-only theme has no `selectSkin`).
  if (scene === 'select' && state.selectSkin !== undefined) available.add('lr2');
  else Iif (scene === 'decide' && state.decideSkin !== undefined) available.add('lr2');
  else if (scene === 'result' && state.resultSkin !== undefined) available.add('lr2');
  else if (
    scene === 'gameplay' &&
    chartEntry !== undefined &&
    pickLR2PlaySkin(state.playSkins, chartEntry) !== undefined
  ) {
    available.add('lr2');
  }
  return available;
}
 
/**
 * Pick the family that should actually render the given scene right now. Honours the user's Options dialog pick
 * (`state.skinFamilyOverride`) when it's available for this scene; otherwise falls through to the auto
 * priority. Auto priority is:
 *
 *   beatoraja  →  lr2  →  default
 *
 * The order matches the legacy implicit behaviour (beatoraja was the first branch in every scene dispatcher,
 * LR2 second with internal fallback, default emerged as the bottom layer). Explicit override values that aren't
 * available for the scene fall through to `'default'` rather than the auto chain — picking `'beatoraja'` when
 * no beatoraja decide skin is loaded shouldn't silently land on the LR2 decide scene.
 */
export function pickActiveFamilyForScene(
  state: FamilyDispatchState,
  scene: SkinFamilySceneKind,
  chartEntry?: LibraryChart,
): SkinFamilyId {
  const available = availableFamiliesForScene(state, scene, chartEntry);
  const override = state.skinFamilyOverride;
  if (override !== 'auto') {
    return available.has(override) ? override : 'default';
  }
  if (available.has('beatoraja')) return 'beatoraja';
  if (available.has('lr2')) return 'lr2';
  return 'default';
}
 
/**
 * Subset of {@link FamilyDispatchState} {@link hasAnyLR2Skin} actually reads. Lets callers pass a snapshot that
 * doesn't carry the beatoraja / override fields (e.g. when probing inside `rebuildSkinFamilyPicker`).
 */
export type LR2SkinPresence = Pick<FamilyDispatchState, 'selectSkin' | 'decideSkin' | 'resultSkin' | 'playSkins'>;
 
/**
 * Returns `true` when any LR2 skin asset is loaded — used by `rebuildSkinFamilyPicker` to decide whether the
 * `'LR2'` dropdown entry should be enabled. A play-only theme without `selectSkin` still counts: the gameplay
 * scene will paint LR2 chrome even though the select stays on the default family.
 */
export function hasAnyLR2Skin(state: LR2SkinPresence): boolean {
  if (state.selectSkin !== undefined) return true;
  if (state.decideSkin !== undefined) return true;
  if (state.resultSkin !== undefined) return true;
  return Object.keys(state.playSkins).length > 0;
}