All files / player-browser/src/host result.ts

95.45% Statements 21/22
70% Branches 21/30
88.88% Functions 8/9
95.45% Lines 21/22

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                                        28x                                                                                                                                       19x   8x   10x   1x                         19x 19x   8x 8x   3x       10x         10x   2x       1x 1x                 2x             19x 19x       5x                 19x                       19x            
/**
 * The host API for the result screen: `createResultScene(kit, options)` pairs the result session with the view of the
 * kit's family and returns the scene to show with `PixiSceneHost.setScene`.
 */
 
import { DOMResultView } from '../view/built-in/dom-view.ts';
import type { BeMusicJSON } from '@be-music/chart-source';
import type { PlayResult, PlayRunKind, ReplaySlotState, SystemSoundSet } from '@be-music/player-scene-model';
import type { BrowserAssetSource } from '../collection/types.ts';
import type { AudioService } from '../runtime/audio-service.ts';
import { ResultSession, type ResultGaugeGraph, type ResultSessionEvents } from '../session/result/result-session.ts';
import { resolveFamilyControlActions } from '../session/controls/control-actions.ts';
import { SessionScene, type SessionSceneOptions } from '../session/session-scene.ts';
import { logger } from '../logger.ts';
import { BeatorajaResultView, resolveBeatorajaResultTiming } from '../view/beatoraja/result-view.ts';
import { BUILT_IN_RESULT_TIMING, BuiltInResultView } from '../view/built-in/result-view.ts';
import { LR2ResultView, resolveLR2ResultTiming } from '../view/lr2/result-view.ts';
import type { ResultFrame, ResultIntent, ResultView, ResultViewTiming } from '../view/types.ts';
import type { ResultKit } from './family-kit.ts';
 
const log = logger('result');
 
/**
 * The kits the result scene takes: a Be-Music Skin, an LR2 result skin (the course-result skin for a course run), or a
 * beatoraja theme's result entry (its course-result entry for a course run), loaded with `loadBeatorajaSceneKit`.
 */
export type ResultSceneKit = ResultKit;
 
export interface ResultSceneOptions extends SessionSceneOptions {
  /**
   * The play (or the course run) the screen shows. An LR2 or beatoraja skin shows the course of `result.course` (its
   * ops, numbers and texts): the course a stage belongs to, with the stage, or the course run.
   */
  readonly result: PlayResult;
  /**
   * Which play the result is of, when `result.run` does not say (a course stage's result): a course run plays the course
   * jingles, and only a single play or a NONSTOP run offers a quick retry.
   */
  readonly run?: PlayRunKind;
  /** Where the result's chart files are, for the images a skin shows. */
  readonly source?: BrowserAssetSource;
  /**
   * The chart as the play played it (its `#RANDOM` branches drawn), when the host has it: a beatoraja skin's BPM graph,
   * note kinds and TOTAL readout read it, and show nothing of the chart without it.
   */
  readonly playChart?: BeMusicJSON;
  /** The theme's sounds: the screen plays its clear, fail or result jingle and stops it as it leaves. */
  readonly sounds: SystemSoundSet;
  /**
   * The host's audio service: the jingle plays through it, and so do a beatoraja skin's Lua sounds (`skinSounds`,
   * resolved against the theme's files set with `setTheme`), which stop as the screen leaves.
   */
  readonly audio: AudioService;
  /**
   * The replay slots of the chart (or the course), read every frame: a beatoraja skin shows them (ops 196-198 and
   * 1196-1204). Pair it with `on.saveReplay`.
   */
  readonly replaySlots?: () => readonly ReplaySlotState[];
  /**
   * The gauge graphs the screen steps through with the gauge graph key (beatoraja's CHANGE_GRAPH): the play's gauge
   * history under each gauge of its ruleset. Asked once, at the first step; without it the graph stays the play's own.
   */
  readonly gaugeGraphs?: () => readonly ResultGaugeGraph[];
  /**
   * Where the screen leads: `continue` when the player is done, `retry` to play the chart again (R). Without `retry`
   * the screen offers no quick retry. Each fires once per entry. `saveReplay` saves the play into a replay slot (keys
   * 1-4, a beatoraja skin's replay buttons).
   */
  readonly on: ResultSessionEvents;
}
 
/** The result screen as a scene. */
export interface ResultScene extends SessionScene<ResultFrame, ResultIntent> {
  /**
   * Takes the player's new settings for the kit's skin; the next frame draws with them. The scene keeps its family and
   * its skin: a kit of another family or skin is ignored.
   */
  updateKit(kit: ResultSceneKit): void;
}
 
/** A family's result view, and how it takes a new kit. */
interface FamilyResultView {
  readonly view: ResultView;
  updateKit(kit: ResultSceneKit): void;
}
 
/** The skin timing of `kit`'s result view, which the session times the screen from. */
function resultTimingOf(kit: ResultSceneKit): ResultViewTiming {
  switch (kit.family) {
    case 'lr2':
      return resolveLR2ResultTiming(kit.skin);
    case 'beatoraja':
      return resolveBeatorajaResultTiming(kit.kit.skin);
    case 'built-in':
      return BUILT_IN_RESULT_TIMING;
  }
}
 
/**
 * The result view of `kit`'s family, reading the chart's files from `options.source`. A beatoraja skin's Lua sounds
 * play through the audio service as the session's, so they stop as the screen leaves.
 */
function createResultView(
  kit: ResultSceneKit,
  session: ResultSession,
  options: Pick<ResultSceneOptions, 'result' | 'source' | 'audio'>,
): FamilyResultView {
  const source = options.source !== undefined ? { source: options.source } : {};
  switch (kit.family) {
    case 'lr2': {
      const view = new LR2ResultView(kit, source);
      return {
        view,
        updateKit: (next) => (next.family === 'lr2' ? view.updateKit(next) : ignoreKit(next, 'lr2')),
      };
    }
    case 'beatoraja': {
      const view = new BeatorajaResultView(kit, {
        chart: options.result.chart,
        ...source,
        skinSounds: options.audio.skinSounds.owned(session),
      });
      return {
        view,
        updateKit: (next) => (next.family === 'beatoraja' ? view.updateKit(next) : ignoreKit(next, 'beatoraja')),
      };
    }
    case 'built-in': {
      const view = kit.skin.dom?.result ? new DOMResultView(kit) : new BuiltInResultView(kit, source);
      return {
        view,
        updateKit: (next) => (next.family === 'built-in' ? view.updateKit(next) : ignoreKit(next, 'built-in')),
      };
    }
  }
}
 
function ignoreKit(kit: ResultSceneKit, family: ResultSceneKit['family']): void {
  log.warn(`a ${family} result scene keeps its family; ignoring a ${kit.family} kit`);
}
 
class FamilyResultScene extends SessionScene<ResultFrame, ResultIntent> implements ResultScene {
  private readonly familyView: FamilyResultView;
 
  public constructor(session: ResultSession, familyView: FamilyResultView, options: SessionSceneOptions) {
    super(session, familyView.view, options);
    this.familyView = familyView;
  }
 
  public updateKit(kit: ResultSceneKit): void {
    this.familyView.updateKit(kit);
  }
}
 
/**
 * The result screen for `kit`: the session plays the result's jingle, steps the screen as the player presses and says
 * where it leads; the kit's family view draws it. Show it with `PixiSceneHost.setScene`, and dispose it once it is gone.
 */
export function createResultScene(kit: ResultSceneKit, options: ResultSceneOptions): ResultScene {
  const session = new ResultSession({
    result: options.result,
    actions: resolveFamilyControlActions(kit.family, options.controls),
    ...(options.run !== undefined ? { run: options.run } : {}),
    ...(options.playChart !== undefined ? { playChart: options.playChart } : {}),
    sounds: options.sounds,
    audio: options.audio,
    timing: resultTimingOf(kit),
    ...(options.replaySlots !== undefined ? { replaySlots: options.replaySlots } : {}),
    ...(options.gaugeGraphs !== undefined ? { gaugeGraphs: options.gaugeGraphs } : {}),
    on: options.on,
  });
  return new FamilyResultScene(session, createResultView(kit, session, options), {
    ...(options.keyTarget !== undefined ? { keyTarget: options.keyTarget } : {}),
    ...(options.controls !== undefined ? { controls: options.controls } : {}),
    ...(options.now !== undefined ? { now: options.now } : {}),
  });
}