All files / app-play/src nonstop-course.ts

96.29% Statements 26/27
62.5% Branches 20/32
100% Functions 8/8
96% Lines 24/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 113                                                                                          6x 3x 6x   3x 6x   3x 2x 2x 4x           2x 2x   4x 4x 4x                                         2x 2x       3x     2x   2x 2x 3x             4x     3x 3x    
import type { RandomDraws } from '@be-music/chart-model';
import {
  prepareNonstopCourse,
  type NonstopPrepareFailure,
  type PreparedNonstopCourse,
} from '@be-music/player-engine/course';
import type { LibraryChart, LibraryCourse } from '@be-music/player-scene-model';
import { createNonstopAssets, NONSTOP_CHART_PATH, type BrowserAssetSource } from '@be-music/player-browser/collection';
 
/** A NONSTOP course stitched for the player to play as one chart. */
export interface PreparedNonstopPlay {
  /** The stitched chart as a song entry the gameplay scenes play (title: the course name). */
  chart: LibraryChart;
  /** Where the stitched chart's assets load from (each stage's files, named per stage). */
  source: BrowserAssetSource;
  /** The course as stitched (`prepareNonstopCourse`): its map, stage charts and draws, for the play and its course log. */
  stitched: PreparedNonstopCourse;
}
 
/** Why a NONSTOP course cannot be played, for the status line. */
export type NonstopPlayFailure = 'missing-stage' | NonstopPrepareFailure;
 
/**
 * Stitches a NONSTOP course from the collection with the shared pipeline (`prepareNonstopCourse` in
 * `@be-music/player-engine/course`, which the Leaderboard rebuilds runs with): each stage's `#RANDOM` drawn with `random`, the
 * course's long-note constraint applied, and the stages stitched with the course's connections. The player's part is the
 * asset plumbing (each stage's files found in its own source, `createNonstopAssets`) and the song entry the scenes play.
 * `hashesOf` gives each stage's file hashes, which its cut-out replay records. A recorded run is stitched again as it was
 * played: with each stage's recorded draws (`stageDraws`) and the stitching rules it was built by (`stitch`).
 *
 * The stages' WAV / BMP files are looked up first, a slice at a time between host tasks (`createNonstopAssets`'s
 * `prefetch`): on a library of many thousand files the lookups are most of the work, and the stitch that follows runs in
 * one go.
 */
export async function prepareNonstopPlay(
  course: LibraryCourse,
  options: {
    sourceOf: (chartEntry: LibraryChart) => BrowserAssetSource | undefined;
    hashesOf?: (chartEntry: LibraryChart) => { sha256?: string; md5?: string } | undefined;
    placement?: 'intended' | 'lr2';
    random?: () => number;
    stageDraws?: ReadonlyArray<RandomDraws | undefined>;
    stitch?: number;
  },
): Promise<{ ok: true; prepared: PreparedNonstopPlay } | { ok: false; reason: NonstopPlayFailure; stage?: number }> {
  const chartEntries = course.stages.map((stage) => stage.chart);
  const sources = chartEntries.map((chartEntry) =>
    chartEntry !== undefined ? options.sourceOf(chartEntry) : undefined,
  );
  const missing = chartEntries.findIndex(
    (chartEntry, index) => chartEntry === undefined || sources[index] === undefined,
  );
  if (missing >= 0) return { ok: false, reason: 'missing-stage', stage: missing };
  const stages = chartEntries as LibraryChart[];
  const assets = createNonstopAssets(
    stages.map((chartEntry, index) => ({
      chart: chartEntry.chart,
      source: sources[index]!,
      chartPath: chartEntry.chartPath,
    })),
  );
  await assets.prefetch();
  const stitched = prepareNonstopCourse(
    stages.map((chartEntry, index) => {
      const hashes = options.hashesOf?.(chartEntry);
      const randomDraws = options.stageDraws?.[index];
      return {
        chart: chartEntry.chart,
        chartPath: chartEntry.chartPath,
        ...(randomDraws !== undefined ? { randomDraws } : {}),
        facts: {
          title: chartEntry.title,
          ...(chartEntry.subtitle !== undefined ? { subtitle: chartEntry.subtitle } : {}),
          ...(chartEntry.artist !== undefined ? { artist: chartEntry.artist } : {}),
          ...(hashes?.sha256 !== undefined ? { sha256: hashes.sha256 } : {}),
          ...(hashes?.md5 !== undefined ? { md5: hashes.md5 } : {}),
        },
      };
    }),
    {
      constraints: course.definition.constraints,
      connections: course.definition.connections ?? [],
      placement: options.placement ?? 'intended',
      ...(options.stitch !== undefined ? { stitch: options.stitch } : {}),
      title: course.definition.name,
      ...(options.random !== undefined ? { random: options.random } : {}),
      assetPath: (stageCharts) => {
        assets.setStageCharts(stageCharts);
        return assets.assetPath;
      },
    },
  );
  Iif (!stitched.ok) {
    return { ok: false, reason: stitched.reason, ...(stitched.stage !== undefined ? { stage: stitched.stage } : {}) };
  }
  const { source } = assets;
  // The stitched chart's own format names its path: an LRX course is an `.lrs`.
  const format = stitched.course.chart.sourceFormat;
  const chartPath = format === 'bmson' ? 'nonstop.bmson' : format === 'lrx' ? 'nonstop.lrs' : NONSTOP_CHART_PATH;
  const chartEntry: LibraryChart = {
    ...stages[0]!,
    id: `nonstop:${course.id}`,
    sourceId: source.id,
    chartPath,
    fileLabel: chartPath,
    title: course.definition.name,
    totalNotes: stages.reduce((sum, stage) => sum + stage.totalNotes, 0),
    chart: stitched.course.chart,
  };
  delete chartEntry.subtitle;
  return { ok: true, prepared: { chart: chartEntry, source, stitched: stitched.course } };
}