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

100% Statements 22/22
100% Branches 37/37
100% Functions 6/6
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 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                                                                                                            12x 12x 12x 12x                 29x                               3x                         6x   1x   2x   3x                                                 13x 2x 2x               11x   11x         13x   13x                 13x 8x 8x                       16x      
import type { ResolvedPlayChart } from '@be-music/player-engine';
// Which family draws each screen, and the kit it draws with: the Options dialog's "Skin system" pick, or the auto chain
// (beatoraja → LR2 → built-in), over what the theme services have loaded (`family-dispatch.ts`).
 
import type { DecideSceneKit, PlaySceneKit, SelectSceneKit } from '@be-music/player-browser/host';
import type { SkinFamilyId } from '@be-music/player-browser/skin';
import { pickLR2PlaySkin, pickLR2PlaySkinVariant } from '@be-music/skin-lr2';
import {
  resolveCourseView,
  type LibraryChart,
  type LibraryCourse,
  type SystemSoundSet,
} from '@be-music/player-scene-model';
import type { BeMusicReplay } from '@be-music/player-engine/replay';
import type { CourseStagePlay } from '../course-flow.ts';
import { pickActiveFamilyForScene, type FamilyDispatchState } from '../family-dispatch.ts';
import type { BeatorajaPlayEntry, BeatorajaThemeService } from '../services/themes/beatoraja.ts';
import type { BuiltInThemeService } from '../services/themes/built-in.ts';
import type { LR2ThemeService } from '../services/themes/lr2.ts';
import type { PlayerGUIState } from '../types.ts';
 
export interface FamilyKitsOptions {
  /** The LR2 theme: its skins and its select sounds. */
  readonly lr2: Pick<
    LR2ThemeService,
    'playSkins' | 'selectSkin' | 'decideSkin' | 'resultSkin' | 'courseLR2Skin' | 'selectSoundSet'
  >;
  /** The beatoraja theme: its entries loaded as kits, and its select sounds. */
  readonly beatoraja: Pick<
    BeatorajaThemeService,
    'beatorajaTheme' | 'loadBeatorajaPlayKit' | 'loadBeatorajaDecideKit' | 'selectSoundSet'
  >;
  /** The Be-Music Skin the built-in family draws with. */
  readonly builtIn: Pick<BuiltInThemeService, 'beMusicSkin' | 'builtInKit'>;
  /** The Options dialog's "Skin system" pick (`skinFamilyOverride`) and the battle mode, which picks the LR2 play skin. */
  readonly guiState: Pick<PlayerGUIState, 'skinFamilyOverride' | 'battleMode'> &
    Partial<Pick<PlayerGUIState, 'judgeRuleset'>>;
}
 
/** The kit a play is drawn with, and on a beatoraja theme the play entry it was loaded from. */
export interface ResolvedPlayKit {
  readonly kit: PlaySceneKit;
  /** The beatoraja theme's play entry, which the play puts in force as it takes the stage (`activeBeatorajaPlay`). */
  readonly beatorajaPlay?: BeatorajaPlayEntry;
}
 
/** The family each screen is drawn by, and its kit, from the theme services and the "Skin system" pick. */
export class FamilyKits {
  private readonly lr2: FamilyKitsOptions['lr2'];
  private readonly beatoraja: FamilyKitsOptions['beatoraja'];
  private readonly builtIn: FamilyKitsOptions['builtIn'];
  private readonly guiState: FamilyKitsOptions['guiState'];
 
  public constructor(options: FamilyKitsOptions) {
    this.lr2 = options.lr2;
    this.beatoraja = options.beatoraja;
    this.builtIn = options.builtIn;
    this.guiState = options.guiState;
  }
 
  /**
   * Bundle the theme services' skins and the "Skin system" pick into the {@link FamilyDispatchState} shape consumed by
   * `family-dispatch.ts`. Built per call (cheap — a half-dozen direct references) so the pure helpers can stay
   * unaware of where the state lives.
   */
  public familyDispatchState(): FamilyDispatchState {
    return {
      beatorajaTheme: this.beatoraja.beatorajaTheme,
      selectSkin: this.lr2.selectSkin,
      decideSkin: this.lr2.decideSkin,
      resultSkin: this.lr2.resultSkin,
      playSkins: this.lr2.playSkins,
      skinFamilyOverride: this.guiState.skinFamilyOverride,
    };
  }
 
  /**
   * The select screen's sounds as a `SystemSoundSet` for the family drawing it: a beatoraja theme's cues and select BGM
   * (by path, read from the theme's files; its decide sound plays on its decide screen), or the LR2 theme's select BGM,
   * decide jingle and cues (by bytes); none without a theme.
   */
  public selectSoundSet(family: SelectSceneKit['family']): SystemSoundSet {
    return family === 'beatoraja' ? this.beatoraja.selectSoundSet() : this.lr2.selectSoundSet();
  }
 
  /**
   * The kit the decide splash of `family` is drawn with, or `undefined` for no splash: the beatoraja theme's decide
   * entry, the LR2 theme's decide skin, or the Be-Music Skin's decide screen. A Be-Music Skin's decide screen hands over
   * to the LR2 / default gameplay preloaded behind it, so it is skipped when the play itself runs on a beatoraja theme.
   */
  public async decideKitFor(
    family: SkinFamilyId,
    chartEntry: LibraryChart,
    playsOnBeatoraja: boolean,
  ): Promise<DecideSceneKit | undefined> {
    switch (family) {
      case 'beatoraja':
        return this.beatoraja.loadBeatorajaDecideKit(chartEntry);
      case 'lr2':
        return this.lr2.decideSkin !== undefined ? { family: 'lr2', skin: this.lr2.decideSkin } : undefined;
      default:
        return this.builtIn.beMusicSkin.decide !== undefined && !playsOnBeatoraja ? this.builtIn.builtInKit : undefined;
    }
  }
 
  /**
   * The kit `song` plays with, for the gameplay family the Options dialog's "Skin family" pick (or the auto chain) gives it:
   *
   * - **beatoraja** (`playsOnBeatoraja`, a replay and a stitched NONSTOP chart included) → the theme's play entry
   *   (`loadBeatorajaPlayKit`), with the entry; `undefined` when it cannot be loaded.
   * - **LR2** with a play skin for the chart's key mode → an LR2 kit with that skin (parsed again with the course's ops
   *   for a course stage or a NONSTOP run), and the theme's 9-keys skin, whose Pop'n green wide note at index 3 is drawn
   *   over each invisible note when {@link PlayerGUIState.showInvisibleNotes} is on.
   * - otherwise → the built-in kit with the active Be-Music Skin.
   *
   * Nothing is put in force here: the play may load behind a decide screen that is backed out of.
   */
  public async resolvePlayKit(
    chartEntry: LibraryChart,
    overrides: {
      courseStage?: CourseStagePlay;
      nonstop?: { readonly course: LibraryCourse };
      replay?: BeMusicReplay;
      preResolved?: ResolvedPlayChart;
    },
  ): Promise<ResolvedPlayKit | undefined> {
    if (this.playsOnBeatoraja(chartEntry)) {
      const loaded = await this.beatoraja.loadBeatorajaPlayKit(chartEntry);
      return loaded !== undefined
        ? {
            kit: loaded.kit,
            beatorajaPlay: { entryPath: loaded.entryPath, header: loaded.header, variant: loaded.variant },
          }
        : undefined;
    }
    // Picking `'default'` from the Options dialog strips the LR2 skin even when one is loaded for this chart.
    const gameplayFamily = pickActiveFamilyForScene(this.familyDispatchState(), 'gameplay', chartEntry);
    // A battle mode plays on the converted chart's skin (LR2's BATTLE play skin for two players); a replay on its own.
    const battle = {
      battleMode: overrides.replay ? (overrides.replay.play.battleMode ?? 'OFF') : this.guiState.battleMode,
      judgeRuleset: overrides.replay?.play.judgeRuleset ?? this.guiState.judgeRuleset,
      course: overrides.courseStage !== undefined || overrides.nonstop !== undefined,
    };
    const skinChart = overrides.preResolved ? { ...chartEntry, chart: overrides.preResolved.chart } : chartEntry;
    const playSkin =
      gameplayFamily !== 'lr2'
        ? undefined
        : overrides.courseStage !== undefined || overrides.nonstop !== undefined
          ? this.lr2.courseLR2Skin(
              pickLR2PlaySkin(this.lr2.playSkins, skinChart, battle),
              overrides.courseStage?.courseInfo ?? resolveCourseView(overrides.nonstop!.course, 0),
              { kind: 'play', playVariant: pickLR2PlaySkinVariant(this.lr2.playSkins, skinChart, battle) },
            )
          : pickLR2PlaySkin(this.lr2.playSkins, skinChart, battle);
    if (playSkin === undefined) return { kit: this.builtIn.builtInKit };
    const invisibleNoteSkin = this.lr2.playSkins['9'];
    return {
      kit: { family: 'lr2', skin: playSkin, ...(invisibleNoteSkin !== undefined ? { invisibleNoteSkin } : {}) },
    };
  }
 
  /**
   * Whether a play of `song` runs on the beatoraja play screen: the gameplay family says so, whatever the play is (a
   * replay and a stitched NONSTOP chart included: the play session carries the replay arrangement and the per-stage
   * audio for every family). Every path into gameplay — `playChart`, and the decide splash deciding what to preload — asks
   * this one question.
   */
  public playsOnBeatoraja(chartEntry: LibraryChart): boolean {
    return pickActiveFamilyForScene(this.familyDispatchState(), 'gameplay', chartEntry) === 'beatoraja';
  }
}