All files / app-play/src random-mix-save.ts

100% Statements 5/5
100% Branches 4/4
100% Functions 4/4
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                                                                                        16x       4x               8x                       5x    
// Keeping a RANDOM MIX run once it is over: what the run was started with, the course it played, and how the save
// dialog's answer turns it into one of the player's own courses (`CourseMaker.saveRandomMix`). The dialog itself is
// `ui/random-mix-save-dialog.ts`; these decisions are kept apart from it so they can be tested.
 
import type {
  CourseDefinition,
  LR2RandomMixSettings,
  NonstopConnection,
  RandomMixConditions,
} from '@be-music/player-engine/course';
import { resolveCourseView, type CourseView, type LibraryCourse } from '@be-music/player-scene-model';
 
/** What a RANDOM MIX run was started with: the RANDOM MIX dialog's conditions and draw settings, as confirmed. */
export interface RandomMixSetup {
  kind: 'expert' | 'nonstop';
  /** The conditions, with the stage count the draw took (`resolveRandomMixCount`). */
  conditions: RandomMixConditions;
  settings: LR2RandomMixSettings;
  /** The connection after every stage of a NONSTOP mix. */
  defaultConnection: NonstopConnection;
}
 
/** A RANDOM MIX run being played: its setup, and the draw kept as its folder's newest course (absent when endless). */
export interface RandomMixRun {
  setup: RandomMixSetup;
  kept?: CourseDefinition;
}
 
/** What the save dialog is opened with. */
export interface RandomMixSaveRequest {
  setup: RandomMixSetup;
  /** The run's course as the skins show it: the stages drawn, in order. */
  course: CourseView;
  /** When the run ended: the name the dialog offers. */
  endedAt: Date;
}
 
/** A RANDOM MIX run that is over, waiting for the save dialog: what it shows, and the course a save keeps. */
export interface FinishedRandomMix {
  request: RandomMixSaveRequest;
  /** The course to save: the kept draw (renamed), or the run's own course when it was not kept. */
  course: CourseDefinition;
}
 
const pad = (value: number): string => String(value).padStart(2, '0');
 
/** The name the save dialog offers: the local date and time the run ended, `YYYY-MM-DD HH:mm`. */
export function formatRandomMixSaveName(date: Date): string {
  return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}`;
}
 
/**
 * A RANDOM MIX run that is over, as the save dialog takes it: `course` is the run's course bar as it was played (an
 * endless run's settled to the stages it played), whose stages the dialog lists, all of them however far the run got.
 */
export function finishRandomMix(run: RandomMixRun, course: LibraryCourse, endedAt: Date): FinishedRandomMix {
  return {
    request: { setup: run.setup, course: resolveCourseView(course), endedAt },
    course: run.kept ?? course.definition,
  };
}
 
/** Saves a finished RANDOM MIX under `name` (the offered name when blank) as a course of the player's own. */
export function saveFinishedRandomMix(
  maker: { saveRandomMix(course: CourseDefinition, name: string): CourseDefinition },
  finished: FinishedRandomMix,
  name: string,
): CourseDefinition {
  return maker.saveRandomMix(finished.course, name.trim() || formatRandomMixSaveName(finished.request.endedAt));
}