All files / app-play/src/services courses.ts

100% Statements 26/26
100% Branches 7/7
100% Functions 11/11
100% Lines 24/24

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                                                        262x               43x         42x         50x                       56x 56x 56x         6x                 7x 7x 7x         20x 20x 20x         5x                     4x 4x 4x 4x 4x               81x 23x 23x 23x      
// The course run in progress: the EXPERT / GRADE course between its stages, the AUTO it was started with, and where the
// select screen was when it started. Every change to them is one of the transitions below, made by the course and
// NONSTOP routes (`app/routes/course.ts`, `app/routes/nonstop.ts`), the play and result screens, and the scene router's
// interrupt. What a course's flow decides is `course-flow.ts`'s, and the run itself is `CourseRun`
// (`@be-music/player-engine/course`).
 
import type { BeMusicCourseReplay, BeMusicReplay } from '@be-music/player-engine/replay';
import type { BrowseSnapshot, LibraryCourse, PlayResult } from '@be-music/player-scene-model';
import { startCourseRun, type ActiveCourse, type EndlessRandomMix } from '../course-flow.ts';
 
/**
 * The course run in progress, from the course bar started to its course result, or until a navigation the scenes did
 * not ask for ends it ({@link endRun}). A NONSTOP course is one play and keeps no run here, only its AUTO and where the
 * select screen was.
 */
export class CourseRunService {
  /** The EXPERT / GRADE course being played, between its stages. */
  private activeCourse: ActiveCourse | undefined;
  /** Set when a course was started with AUTO (`true`), so its every stage plays itself; `undefined` follows the menu. */
  private courseAutoPlay: boolean | undefined;
  /**
   * Where the select screen was when a course started. A later stage starts with no select screen up, so the course
   * keeps this to put the cursor back on the course bar once it is over.
   */
  private courseReturnNavigation: BrowseSnapshot | undefined;
 
  /** The EXPERT / GRADE course being played, between its stages. */
  public get active(): ActiveCourse | undefined {
    return this.activeCourse;
  }
 
  /**
   * The AUTO the course was started with, which its every play takes (each stage, and a NONSTOP run's every restitch):
   * `true`, or `undefined` to follow the Options dialog's auto play.
   */
  public get autoPlay(): boolean | undefined {
    return this.courseAutoPlay;
  }
 
  /** A course bar is started: with AUTO (`true`), or with PLAY (`undefined`). */
  public start(autoPlay: boolean | undefined): void {
    this.courseAutoPlay = autoPlay;
  }
 
  /** The course leaves the select screen at `navigation`, where the cursor comes back once the course is over. */
  public leaveSelect(navigation: BrowseSnapshot | undefined): void {
    this.courseReturnNavigation = navigation;
  }
 
  /**
   * An EXPERT / GRADE course's run begins (its stages all in the collection): played, or, with `replay`, that course
   * replay played back.
   */
  public beginRun(
    course: LibraryCourse,
    replay?: BeMusicCourseReplay,
    options: { endless?: EndlessRandomMix; unkept?: boolean } = {},
  ): ActiveCourse {
    const active = startCourseRun(course, replay, options);
    this.activeCourse = active;
    return active;
  }
 
  /** Backing out of the course's decide screen drops its run, unless another run has begun since. */
  public cancelRun(active: ActiveCourse): void {
    if (this.activeCourse === active) this.activeCourse = undefined;
  }
 
  /**
   * ESC gave `active`'s stage up: the stage is recorded, with its replay once the engine has handed it over, and the
   * run is the running one again. Returns whether it goes on to its next stage (LR2's course gauges, while the gauge
   * holds); otherwise its course result shows it as a failed run.
   */
  public giveUp(active: ActiveCourse, replay: BeMusicReplay | undefined, result?: PlayResult): 'next' | 'finished' {
    const outcome = active.run.giveUp(replay, result);
    this.activeCourse = active;
    return outcome;
  }
 
  /** The course result takes the run, which ends there; `undefined` when none is running. */
  public finishRun(): ActiveCourse | undefined {
    const active = this.activeCourse;
    this.activeCourse = undefined;
    return active;
  }
 
  /** F5 in a course stage drops the run with no result, as quick retry is off there. */
  public abandonRun(): void {
    this.activeCourse = undefined;
  }
 
  /**
   * Ends the course run (EXPERT, GRADE or NONSTOP) when a navigation the scenes did not ask for takes over (the router's
   * `onInterrupt`: a drop, a skin or theme change, files or a replay loaded): the run is dropped unrecorded, so the
   * next single play is not taken for its next stage. Returns where the select screen was when the course started, for
   * the select screen to come back to unless that navigation has put it somewhere already (a drop opens what it
   * brought). Outside a run it does nothing and returns `undefined`.
   */
  public endRun(): BrowseSnapshot | undefined {
    this.activeCourse = undefined;
    this.courseAutoPlay = undefined;
    const navigation = this.courseReturnNavigation;
    this.courseReturnNavigation = undefined;
    return navigation;
  }
 
  /**
   * Back on the select screen once no course runs: where the select screen was when the course started, so the cursor
   * returns to the course bar it was started from, taken once. `undefined` while a course runs or after none.
   */
  public takeReturnNavigation(): BrowseSnapshot | undefined {
    if (this.courseReturnNavigation === undefined || this.activeCourse !== undefined) return undefined;
    const navigation = this.courseReturnNavigation;
    this.courseReturnNavigation = undefined;
    return navigation;
  }
}