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 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | 2x 64x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 23x 25x 4x 4x 4x 4x 4x 4x 4x 3x 3x 3x 3x 3x 3x 24x 24x 24x 2x 1x 24x 24x 24x 24x 24x 24x 4x 3x 4x 20x 24x 24x 20x 20x 14x 11x 11x 20x 11x 9x 1x 1x 8x 3x 2x 2x 20x 24x 1x 19x 14x 14x 14x 14x 20x 20x 19x 19x 20x 17x 17x 17x 16x 16x 16x 8x 8x 7x 2x 2x 2x 5x 5x 5x 8x 4x 4x 8x 8x 8x 8x | import type { BeMusicJSON } from '@be-music/chart-source';
// The decide route: the decide screen (`createDecideScene`), whichever family draws it, between a start on the select
// screen and its play. A chart's play is made and loads off stage behind the screen (the scene router's `prepare`), and
// takes the stage once the screen is done: that play and no other, or one made again with the screen's draws when its
// load failed (`decide-handoff.ts`). The screen, the play and its load share one navigation. A course's screen leads to
// its first stage instead, and carries its run on. Without a decide screen the chart plays at once.
import { logger } from '@be-music/player-browser';
import type { ResolvedPlayChart } from '@be-music/player-engine';
import type { AudioService } from '@be-music/player-browser/audio';
import { resolveChartSource, type ChartFileHashes } from '@be-music/player-browser/collection';
import {
createDecideScene,
type ControlSettings,
type PlayScene,
type SelectScene,
} from '@be-music/player-browser/host';
import { resolvePlayChart, type BeMusicReplay } from '@be-music/player-engine/replay';
import { systemSoundSetFromBeatoraja, type LibraryChart } from '@be-music/player-scene-model';
import { sanitizeFilenameStem } from '../../player-utils.ts';
import { pickActiveFamilyForScene } from '../../family-dispatch.ts';
import { allowsLiveTempo, describePlayingStatus } from '../../play-tempo-settings.ts';
import { startArmedRecording, type RecordingDeps } from '../../recording-controller.ts';
import type { LibraryService } from '../../services/library.ts';
import type { PlayOptionsService } from '../../services/play-options.ts';
import type { RecordsService } from '../../services/records.ts';
import type { BeatorajaThemeService } from '../../services/themes/beatoraja.ts';
import type { PlayerGUIState, PlayerScene, PlayerWebElements } from '../../types.ts';
import type { BeatorajaSkinOptionsPanel } from '../../ui/beatoraja-skin-options.ts';
import { settleDecidePreload, type DecidePreload } from '../decide-handoff.ts';
import type { FamilyKits, ResolvedPlayKit } from '../family-kits.ts';
import type { SceneRouter, SceneRouteToken } from '../scene-router.ts';
import type { SelectRoute } from './select.ts';
const gamereplay = logger('gameplay');
/** What a play loading behind a decide screen is made with: the start's AUTO, and the chart as the screen drew it. */
interface DecidePlayOverrides {
readonly autoPlay?: boolean;
readonly practice?: boolean;
readonly preResolved: ResolvedPlayChart;
}
/**
* A play started loading as its chart was picked on the select screen, while the screen's launch outro plays
* (`preloadOnLaunch`): the decide screen that follows takes its navigation, its draws and the play over.
*/
interface LaunchPreload {
readonly chartEntry: LibraryChart;
readonly autoPlay: boolean | undefined;
readonly token: SceneRouteToken;
readonly resolved: ResolvedPlayChart;
readonly preload: DecidePreload<PlayScene, DecidePlayOverrides>;
}
/** What a decide screen leads to instead of playing its song: a course's first stage, and what backing out undoes. */
export interface DecideContinuation {
start: () => Promise<void>;
cancel?: () => void;
/** The decide screen mounted, or a direct play loaded successfully. */
shown?: () => void;
}
/** The family a play's status line names: the beatoraja play's; the built-in and LR2 plays name none. */
export function playingFamily(view: PlayScene): { family?: 'beatoraja' } {
return view.identity.family === 'beatoraja' ? { family: 'beatoraja' } : {};
}
export interface DecideRouteDeps {
/** The player's keys the screen reads its control buttons by (`ControlSettings`), as it opens. */
readonly controls: () => ControlSettings;
/** The scene router: the select scene it keeps, and the navigation the screen, the play and its load share. */
readonly router: Pick<
SceneRouter<PlayerScene, PlayScene, SelectScene>,
'select' | 'begin' | 'isCurrent' | 'leave' | 'show' | 'prepare'
>;
/** The select route: where its screen was, for its next showing to come back to. */
readonly selectRoute: Pick<SelectRoute, 'lastSelectNavigation'>;
/** The page: the shell's `playing` class. */
readonly elements: Pick<PlayerWebElements, 'shell'>;
/** The library: the chart's asset source. */
readonly library: Pick<LibraryService, 'collection'>;
/** The chart hashes the play's log records. */
readonly records: Pick<RecordsService, 'resolveChartHashes'>;
/** The family that draws the screen and the play, and the kits they draw with. */
readonly familyKits: Pick<FamilyKits, 'familyDispatchState' | 'playsOnBeatoraja' | 'decideKitFor' | 'resolvePlayKit'>;
/** The beatoraja theme's sounds: its decide sound. */
readonly beatoraja: Pick<BeatorajaThemeService, 'beatorajaThemeBGM'>;
/** The play options in force, which the screen shows. */
readonly playOptions: Pick<PlayOptionsService, 'current'>;
/** The Options dialog's auto play, and the tempo the status line shows. */
readonly guiState: Pick<PlayerGUIState, 'autoPlay' | 'tempoEnabled' | 'tempoPercent' | 'tempoPitchMode'>;
readonly audio: AudioService;
/** The skin-options panel of the beatoraja play showing. */
readonly beatorajaSkinOptions: Pick<BeatorajaSkinOptionsPanel, 'refreshBeatorajaPlaySkinOptions'>;
/** The play last made: whether it is a replay. */
readonly lastPlay: () => { readonly overrides: { readonly replay?: BeMusicReplay } } | undefined;
/** The video recording's deps: a recording armed on the select screen starts with the play. */
readonly recordingDeps: () => RecordingDeps;
/** Names the next saved recording after the chart. */
readonly setRecordingFilenameBase: (base: string) => void;
readonly setStatus: (text: string) => void;
readonly showChartDiagnostics: (chart?: BeMusicJSON, chartPath?: string) => void;
/** Shows a status about the play, which the select screen replaces when it comes back. */
readonly setPlayStatus: (text: string) => void;
/** Latches the options a play takes for it, and disables their controls. */
readonly lockReplayOptionsForPlay: (liveTempo?: boolean, chartEntry?: LibraryChart) => void;
readonly showSelect: () => Promise<void>;
/** Plays a chart with no decide screen: one with none, or a play made again after its load failed. */
readonly playChart: (
chartEntry: LibraryChart,
overrides: { autoPlay?: boolean; practice?: boolean; preResolved?: ResolvedPlayChart },
) => Promise<void>;
/** Makes the play scene for a chart with the play options in force. */
readonly buildPlayScene: (
chartEntry: LibraryChart,
kit: ResolvedPlayKit,
overrides: {
autoPlay?: boolean;
practice?: boolean;
preResolved?: ResolvedPlayChart;
chartHashes?: ChartFileHashes;
},
) => PlayScene;
/** Puts in force what the play was made with, as it takes the stage (`PlayRoute.latchPlay`). */
readonly latchPlay: (view: PlayScene) => void;
}
/** A start's decide screen, and the play it hands the stage to. */
export class DecideRoute {
private readonly router: DecideRouteDeps['router'];
private readonly controls: DecideRouteDeps['controls'];
private readonly selectRoute: DecideRouteDeps['selectRoute'];
private readonly elements: DecideRouteDeps['elements'];
private readonly library: DecideRouteDeps['library'];
private readonly records: DecideRouteDeps['records'];
private readonly familyKits: DecideRouteDeps['familyKits'];
private readonly beatoraja: DecideRouteDeps['beatoraja'];
private readonly playOptions: DecideRouteDeps['playOptions'];
private readonly guiState: DecideRouteDeps['guiState'];
private readonly audio: DecideRouteDeps['audio'];
private readonly beatorajaSkinOptions: DecideRouteDeps['beatorajaSkinOptions'];
private readonly lastPlay: DecideRouteDeps['lastPlay'];
private readonly recordingDeps: DecideRouteDeps['recordingDeps'];
private readonly setRecordingFilenameBase: DecideRouteDeps['setRecordingFilenameBase'];
private readonly setStatus: DecideRouteDeps['setStatus'];
private readonly showChartDiagnostics: DecideRouteDeps['showChartDiagnostics'];
private readonly setPlayStatus: DecideRouteDeps['setPlayStatus'];
private readonly lockReplayOptionsForPlay: DecideRouteDeps['lockReplayOptionsForPlay'];
private readonly showSelect: DecideRouteDeps['showSelect'];
private readonly playChart: DecideRouteDeps['playChart'];
private readonly buildPlayScene: DecideRouteDeps['buildPlayScene'];
private readonly latchPlay: DecideRouteDeps['latchPlay'];
/** The play loading since its chart was picked, for the decide screen about to show it. */
private launchPreload: LaunchPreload | undefined;
/** Bumped on each launch, so a launch's preload that is still deciding whether to start knows it is not the latest. */
private launchGeneration = 0;
public constructor(deps: DecideRouteDeps) {
this.router = deps.router;
this.controls = deps.controls;
this.selectRoute = deps.selectRoute;
this.elements = deps.elements;
this.library = deps.library;
this.records = deps.records;
this.familyKits = deps.familyKits;
this.beatoraja = deps.beatoraja;
this.playOptions = deps.playOptions;
this.guiState = deps.guiState;
this.audio = deps.audio;
this.beatorajaSkinOptions = deps.beatorajaSkinOptions;
this.lastPlay = deps.lastPlay;
this.recordingDeps = deps.recordingDeps;
this.setRecordingFilenameBase = deps.setRecordingFilenameBase;
this.setStatus = deps.setStatus;
this.showChartDiagnostics = deps.showChartDiagnostics;
this.setPlayStatus = deps.setPlayStatus;
this.lockReplayOptionsForPlay = deps.lockReplayOptionsForPlay;
this.showSelect = deps.showSelect;
this.playChart = deps.playChart;
this.buildPlayScene = deps.buildPlayScene;
this.latchPlay = deps.latchPlay;
}
/** The select screen (`createSelectScene`), whichever family draws it, kept by the router across the other screens. */
private get selectView(): SelectScene | undefined {
return this.router.select;
}
/**
* Starts loading the play of a chart picked on the select screen as the pick is made, so the load runs through the
* select screen's launch outro as well as the decide screen. Only when a decide screen will follow that is known at
* once (the Be-Music Skin's or an LR2 theme's; a beatoraja theme's decide entry loads first): `showDecide` takes the
* navigation, the chart's draws and the play over. A start that goes elsewhere (no decide screen, or another chart)
* begins its own navigation, and the play loaded here is disposed as stale.
*/
public preloadOnLaunch(chartEntry: LibraryChart, overrides: { autoPlay?: boolean } = {}): void {
const generation = ++this.launchGeneration;
this.launchPreload = undefined;
const decideFamily = pickActiveFamilyForScene(this.familyKits.familyDispatchState(), 'decide', chartEntry);
Iif (decideFamily === 'beatoraja') return;
void (async () => {
const kit = await this.familyKits.decideKitFor(
decideFamily,
chartEntry,
this.familyKits.playsOnBeatoraja(chartEntry),
);
if (kit === undefined || generation !== this.launchGeneration) return;
const token = this.router.begin();
// The decide screen shows the branches drawn here, and the play plays them.
const resolved = resolvePlayChart(chartEntry.chart);
const options: DecidePlayOverrides = { ...overrides, preResolved: resolved };
const play = this.preloadGameplay(chartEntry, options, token);
// A load nobody takes over fails quietly: the decide screen that takes it reports its failure.
play.catch(() => undefined);
this.launchPreload = { chartEntry, autoPlay: overrides.autoPlay, token, resolved, preload: { options, play } };
})();
}
/** The launch preload for this start of `chartEntry`, if it is still the newest navigation; it is taken once. */
private takeLaunchPreload(
chartEntry: LibraryChart,
overrides: { autoPlay?: boolean },
then: DecideContinuation | undefined,
): LaunchPreload | undefined {
const launch = this.launchPreload;
this.launchPreload = undefined;
if (launch === undefined || then !== undefined) return undefined;
if (launch.chartEntry.id !== chartEntry.id || launch.autoPlay !== overrides.autoPlay) return undefined;
return this.router.isCurrent(launch.token) ? launch : undefined;
}
/**
* Mounts the decide-screen splash and routes the user into gameplay when it dismisses (auto-advance OR Enter / Space
* / Escape input). Without a decide skin, falls straight through to `playChart` so themes that don't ship a Decide
* directory still play the chart immediately. Every family's splash is `createDecideScene`'s: on the beatoraja family
* the theme's decide entry, on the LR2 family the theme's decide skin, on the default family the Be-Music Skin's own
* decide screen, shown only when the skin declares one.
*
* The select screen's decide jingle, started as the chart was picked, plays on through the splash, which visually
* masks the chart-load + gameplay-mount window that comes next. The beatoraja splash plays its theme's decide sound
* itself.
*/
public async showDecide(
chartEntry: LibraryChart,
overrides: { autoPlay?: boolean; practice?: boolean } = {},
then?: DecideContinuation,
): Promise<void> {
const start = then?.start ?? (() => this.playChart(chartEntry, overrides));
// Family dispatch for the decide splash. Note this is decoupled from the gameplay family pick: a user can be
// playing in LR2 mode and have a beatoraja-only decide skin, or vice versa. The decide splash is a one-shot
// overlay so each scene transition consults the family helper afresh.
const decideFamily = pickActiveFamilyForScene(this.familyKits.familyDispatchState(), 'decide', chartEntry);
const playsOnBeatoraja = this.familyKits.playsOnBeatoraja(chartEntry);
const kit = await this.familyKits.decideKitFor(decideFamily, chartEntry, playsOnBeatoraja);
const launch = overrides.practice ? undefined : this.takeLaunchPreload(chartEntry, overrides, then);
if (kit === undefined) {
// No decide splash: a Be-Music Skin without one, an LR2-only setup that didn't ship a decide skin, or a beatoraja
// theme without a decide skin (or one that failed to load). The select screen's decide jingle still plays.
await start();
then?.shown?.();
return;
}
// One navigation from here into the play: the play loading behind the splash, the splash and the play share it. A
// course's splash (`then`) carries its run on; a chart's starts a run of its own.
const token = launch?.token ?? this.router.begin({ continuesRun: then !== undefined });
this.selectRoute.lastSelectNavigation = this.selectView?.snapshot();
// The select screen goes off the stage (kept), and any decide splash or play left over goes.
// A play loading since the launch stays for the screen to show.
Iif (!(await this.router.leave({ token, ...(launch !== undefined ? { keepHeldPlay: true } : {}) }))) return;
// Build the gameplay view eagerly and kick off its heavy load (chart parse, audio decode, BGA preload) IN PARALLEL
// with the Decide animation, off stage (`prepare` attaches nothing until the play is shown). The Decide splash
// typically runs ~3 s; chart asset decoding is mostly done by the time the splash auto-advances, so the hand-off to
// gameplay becomes instant instead of dropping a frozen frame.
// A course starts its own play once the splash is done, so nothing is preloaded for it. The play is preloaded once the
// decide screen is made, whichever family draws either, so it plays the `#RANDOM` branches the screen drew
// (`DecideScene.resolved`).
let preload: DecidePreload<PlayScene, DecidePlayOverrides> | undefined;
let advanced = false;
const advance = (then: () => void): void => {
// Idempotent — the auto-advance timer, key input, and pointer click can all race; whichever lands first wins and
// re-entries no-op.
if (advanced) return;
advanced = true;
then();
};
const callbacks = {
onContinue: () =>
advance(() => {
// A course's play takes the stage from the splash as it starts.
if (preload === undefined) {
void start();
return;
}
void this.startGameplayAfterDecide(chartEntry, preload, token);
}),
onCancel: () =>
advance(() => {
// User backed out of the splash: the select screen comes back, and the play prepared behind the splash goes.
then?.cancel?.();
void this.showSelect();
}),
};
const decide = createDecideScene(kit, {
controls: this.controls(),
chart: chartEntry,
playOptions: {
...this.playOptions.current,
autoPlay: overrides.autoPlay ?? this.guiState.autoPlay,
practice: overrides.practice ?? this.playOptions.current.practice,
},
source: resolveChartSource(this.library.collection, chartEntry),
// The LR2 / built-in select screen plays the theme's decide jingle as the chart is picked, and lets it ring on
// into the play, so those screens play none of their own. The beatoraja screen plays its theme's decide sound.
sounds: kit.family === 'beatoraja' ? systemSoundSetFromBeatoraja({ bgm: this.beatoraja.beatorajaThemeBGM }) : {},
audio: this.audio,
on: { continue: callbacks.onContinue, cancel: callbacks.onCancel },
...(launch !== undefined ? { resolved: launch.resolved } : {}),
});
if (launch !== undefined) {
preload = launch.preload;
} else if (then === undefined) {
const options = { ...overrides, preResolved: decide.resolved };
const play = this.preloadGameplay(chartEntry, options, token);
// The handoff handles the error once Decide advances. Catch it now in case the play fails during the splash.
play.catch(() => undefined);
preload = { options, play };
}
const shown = await this.router.show(decide, { role: 'decide', token });
if (shown) {
this.showChartDiagnostics(decide.resolved.chart, chartEntry.chartPath);
then?.shown?.();
}
if (shown && kit.family === 'beatoraja') this.setStatus(`Decide (beatoraja): ${chartEntry.title}`);
}
/**
* Makes the play scene (`createPlayScene`) with the current play-options snapshot and has the router load it off
* stage (`prepare`) for the navigation `token`. The returned promise resolves to that play once chart audio is decoded
* (`undefined` when a newer navigation replaced it) — the host awaits it inside the Decide `onContinue` handler and
* shows that play.
*
* Wired up here (rather than inline in `showDecide`) because the same option-marshalling + callback wiring is needed
* whether we're going through Decide or the no-decide fast-path. `playChart` shares this construction shape.
*/
private async preloadGameplay(
chartEntry: LibraryChart,
overrides: { autoPlay?: boolean; practice?: boolean; preResolved?: ResolvedPlayChart },
token: SceneRouteToken,
): Promise<PlayScene | undefined> {
this.setRecordingFilenameBase(sanitizeFilenameStem(chartEntry.title) || `gameplay-${Date.now()}`);
// The same kit as `playChart` picks, so the decide → gameplay preload path stays consistent with the no-decide
// fast-path.
const kit = await this.familyKits.resolvePlayKit(
chartEntry,
overrides.preResolved ? { preResolved: overrides.preResolved } : {},
);
if (kit === undefined) throw new Error(`no play skin for ${chartEntry.title}`);
const chartHashes = await this.records.resolveChartHashes(chartEntry);
const view = this.buildPlayScene(chartEntry, kit, { ...overrides, chartHashes });
return (await this.router.prepare(view, { token })) ? view : undefined;
}
/**
* Tears the Decide splash down and hands the stage over to the play prepared behind it (`preload`), that play and no
* other. Awaits the preload: when the user dismisses Decide before chart audio has finished decoding, the splash's
* last frame stays on screen until prepare resolves — visually a brief hold rather than the previous frozen-frame
* freeze. A preload that failed makes the play again without one, with the splash's draws (`preResolved`).
*/
private async startGameplayAfterDecide(
chartEntry: LibraryChart,
preload: DecidePreload<PlayScene, DecidePlayOverrides>,
token: SceneRouteToken,
): Promise<void> {
const handOff = await settleDecidePreload(preload, () => this.router.isCurrent(token));
if (handOff.kind === 'stale') return;
if (handOff.kind === 'reload') {
// The play that failed to load is gone already; the splash goes as the play made again takes the stage.
gamereplay.warn('preload failed; falling back to no-decide path', handOff.error);
await this.playChart(chartEntry, handOff.options);
return;
}
const view = handOff.play;
this.elements.shell.classList.add('playing');
this.selectRoute.lastSelectNavigation = this.selectView?.snapshot();
// The splash hands the stage over: it exits as the prepared play enters, then goes.
if (!(await this.router.show(view, { role: 'play', token }))) return;
// Only now does the play put what it was made with in force: a splash backed out of leaves it all as it was.
this.latchPlay(view);
this.setPlayStatus(
describePlayingStatus(chartEntry.title, {
...playingFamily(view),
tempoPercent: this.guiState.tempoEnabled ? this.guiState.tempoPercent : 0,
replay: this.lastPlay()?.overrides.replay !== undefined,
}),
);
this.lockReplayOptionsForPlay(
allowsLiveTempo(this.guiState, {
replay: this.lastPlay()?.overrides.replay !== undefined,
tempo: view.getTempo(),
}),
chartEntry,
);
view.start();
startArmedRecording(this.recordingDeps());
this.beatorajaSkinOptions.refreshBeatorajaPlaySkinOptions();
}
}
|