All files / player-engine/src/course resolve.ts

100% Statements 4/4
100% Branches 6/6
100% Functions 3/3
100% Lines 2/2

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                                                    66x           61x    
import type { CourseDefinition, CourseStageRef } from './definition.ts';
 
/** Finds a local chart by file hash. Implementations return `undefined` when they hold no such chart. */
export interface CourseChartLookup<T> {
  bySHA256(sha256: string): T | undefined;
  byMD5(md5: string): T | undefined;
}
 
export interface ResolvedCourseStage<T> {
  ref: CourseStageRef;
  /** The local chart, or `undefined` when it is missing. */
  chart: T | undefined;
}
 
export interface ResolvedCourse<T> {
  course: CourseDefinition;
  stages: ResolvedCourseStage<T>[];
  /** Every stage was found. A course with a missing stage is listed but cannot be started (LR2 and beatoraja alike). */
  playable: boolean;
}
 
/**
 * Matches each stage to a local chart: by SHA-256 when the stage has one, else by MD5, as beatoraja's `TableBar` does.
 * A stage carrying both hashes falls back to its MD5 when no chart has its SHA-256.
 */
export function resolveCourseStages<T>(course: CourseDefinition, lookup: CourseChartLookup<T>): ResolvedCourse<T> {
  const stages = course.stages.map((ref) => ({
    ref,
    chart:
      (ref.sha256 !== undefined ? lookup.bySHA256(ref.sha256) : undefined) ??
      (ref.md5 !== undefined ? lookup.byMD5(ref.md5) : undefined),
  }));
  return { course, stages, playable: stages.every((stage) => stage.chart !== undefined) };
}