All files / player-scene-model/src/library entries.ts

100% Statements 4/4
100% Branches 3/3
100% Functions 1/1
100% Lines 4/4

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                                                                                                                                                                                                                                    168x   79x   73x   16x      
import type { BeMusicJSON } from '@be-music/chart-source';
import type { CourseChartRecord, CourseDefinition, RandomCourseDefinition } from '@be-music/player-engine/course';
 
/*
 * The library a select screen browses: its charts, the folders they are grouped in and the courses. Types and fields
 * follow the glossary: a list entry is a chart, so a chart bar is `{ kind: 'chart', chart }`, a folder lists its
 * `charts` and a course stage names its `chart`.
 */
 
/** Where a chart of the library came from: a dropped directory, a zip, or loose files. */
export type ChartSourceKind = 'directory' | 'zip' | 'files';
 
/** One chart in the library: its metadata and the parsed chart. */
export interface LibraryChart {
  id: string;
  sourceId: string;
  sourceLabel: string;
  sourceKind: ChartSourceKind;
  bemuseServer?: true;
  chartPath: string;
  directoryLabel: string;
  fileLabel: string;
  title: string;
  subtitle?: string;
  artist?: string;
  genre?: string;
  playLevel?: number | string;
  bpm?: number;
  /** The notes a play scores: judged notes without FREE ZONE tails; mines and invisible objects are not notes. */
  totalNotes: number;
  chart: BeMusicJSON;
}
 
/**
 * One folder bar of the library. A chart folder (`groupChartsByFolder`) is labelled with the top-level directory of its
 * charts inside their source (falling back to the source label) and lists them in `charts`; a course folder (a course
 * file, or one group of a difficulty table's courses) lists courses instead.
 */
export interface LibraryFolder {
  label: string;
  kind?: 'bemuse-server' | 'difficulty-table';
  charts: readonly LibraryChart[];
  /** A course folder lists courses instead of charts. */
  courses?: readonly LibraryCourse[];
}
 
/** One stage of a course in the library: the local chart it resolved to, if any. */
export interface LibraryCourseStage {
  /** The chart's title, or the course file's title for a stage that is not in the library. */
  title: string;
  chart?: LibraryChart;
}
 
/**
 * A course in the library: its definition (from `@be-music/player-engine/course`) with each stage matched to a local chart.
 * A course with a missing stage is listed but cannot be started.
 *
 * A beatoraja random course carries `random`: its stages are drawn from the library when it is started, so
 * `definition` has the course's name, kind and constraints but no stages yet, and each of `stages` has no `chart` and
 * shows the stage's title (or its filter, or `RANDOM`).
 */
export interface LibraryCourse {
  /** Stable within a session: the folder label and the course's index in it. */
  id: string;
  definition: CourseDefinition;
  stages: readonly LibraryCourseStage[];
  /** Every stage was found locally (for a random course: every stage filter can be evaluated). */
  playable: boolean;
  /** Set on a random course: the definition its stages are drawn from. */
  random?: RandomCourseDefinition;
  /**
   * LR2's NEW COURSE bar, closing a list of the player's own courses: deciding it starts making a course of
   * `definition.kind` (no stages yet). A host routes it through its course-select callback.
   */
  newCourse?: true;
  /**
   * LR2's RANDOM MIX bar, after the NEW COURSE bar of the player's EXPERT and NONSTOP courses: deciding it draws a
   * course of `definition.kind` by the RANDOM MIX settings (`drawLR2RandomMix`) and plays it. A host routes it through
   * its course-select callback.
   */
  randomMix?: true;
  /** A course the player made: it can be renamed and deleted, and a NONSTOP one's connections edited. */
  editable?: true;
}
 
/**
 * One bar of the list a select screen shows: folder bars at the root, chart bars inside a chart folder (and at the root
 * while searching), course bars inside a course folder.
 */
export type LibraryEntry =
  | { kind: 'folder'; folder: LibraryFolder }
  | { kind: 'chart'; chart: LibraryChart }
  | { kind: 'course'; course: LibraryCourse };
 
/**
 * What a select screen browses: the library's charts, grouped into chart folders at the root, and its course folders,
 * listed after them. The browser player's chart collection is one.
 */
export interface Library {
  readonly charts: readonly LibraryChart[];
  readonly courseFolders?: readonly LibraryFolder[];
  /**
   * A chart as a chart filter search (`?…`) sees it: its song columns, best record and song information. Without it
   * the search reads what the library entry holds (`libraryChartFilterRecord`). Give a new function when what it reads
   * changes (a new best record): the list is derived again for it.
   */
  readonly chartFilterRecord?: (chart: LibraryChart) => CourseChartRecord;
}
 
/**
 * A key naming one bar of the list across rebuilds: a chart by its id, a folder by its label, a course by its id. A
 * restored browse state puts the cursor back on the bar with the key it saved (the glossary's focus key).
 */
export function libraryEntryKey(entry: LibraryEntry): string {
  switch (entry.kind) {
    case 'folder':
      return `folder:${entry.folder.label}`;
    case 'chart':
      return `chart:${entry.chart.id}`;
    case 'course':
      return `course:${entry.course.id}`;
  }
}