All files / player-engine/src/replay course-playback.ts

100% Statements 27/27
100% Branches 40/40
100% Functions 4/4
100% Lines 21/21

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                                                                      18x 41x   41x 41x 41x                                 32x 11x 4x 1x                                         19x 19x 34x 31x 31x   34x   34x             34x 34x   9x 9x 9x    
import { isCourseStageFailed } from '../ruleset/course.ts';
import type { BeMusicCourseReplay } from './course.ts';
import type { BeMusicReplay } from './format.ts';
 
/*
 * Playing a course replay back as a course: which chart each stage needs, and which stage replays play in order and
 * where the run stops. A host plays each stage replay with its recorded carry-in (`play.course`), as LR2 and beatoraja
 * replay a course stage by stage; a NONSTOP run plays its one stitched replay (`BeMusicCourseReplay.nonstop`) instead.
 */
 
/** The chart a course replay's stage was played on (or, for a stage the run never reached, the course names). */
export interface CourseReplayStageChart {
  /** Zero-based stage index. */
  index: number;
  /** Whether the run reached the stage. */
  played: boolean;
  /** The chart's title and artist, as a played stage's replay records them. */
  title?: string;
  artist?: string;
  /** The chart file's hashes, lower-case hex. */
  sha256?: string;
  md5?: string;
  /** The Be-Music chart fingerprint (`bmfp0:…`). */
  chartFingerprint?: string;
}
 
/** What a loaded chart is known by, to match it against a stage ({@link matchesCourseReplayStageChart}). */
export interface CourseReplayChartKeys {
  sha256?: string;
  md5?: string;
  chartFingerprint?: string;
}
 
/** Every stage's chart, in order: a played stage's from its replay, an unreached one's from the course log. */
export function listCourseReplayStageCharts(courseLog: Pick<BeMusicCourseReplay, 'stages'>): CourseReplayStageChart[] {
  return courseLog.stages.map((entry, index): CourseReplayStageChart => {
    const chart = entry.played ? entry.replay.chart : entry;
    // An unreached stage is listed by its hashes and chart facts only: the course log does not name it.
    const title = entry.played ? entry.replay.chart.title : undefined;
    const artist = entry.played ? entry.replay.chart.artist : undefined;
    return {
      index,
      played: entry.played,
      ...(title !== undefined ? { title } : {}),
      ...(artist !== undefined ? { artist } : {}),
      ...(chart.sha256 !== undefined ? { sha256: chart.sha256.toLowerCase() } : {}),
      ...(chart.md5 !== undefined ? { md5: chart.md5.toLowerCase() } : {}),
      ...(chart.chartFingerprint !== undefined ? { chartFingerprint: chart.chartFingerprint } : {}),
    };
  });
}
 
/**
 * Whether a loaded chart is a stage's: by SHA-256 when the stage names one, else by MD5, else by chart fingerprint. A
 * stage that names none matches no chart, so a course replay that cannot name its charts never starts on guesses.
 */
export function matchesCourseReplayStageChart(stage: CourseReplayStageChart, chart: CourseReplayChartKeys): boolean {
  if (stage.sha256 !== undefined) return chart.sha256?.toLowerCase() === stage.sha256;
  if (stage.md5 !== undefined) return chart.md5?.toLowerCase() === stage.md5;
  if (stage.chartFingerprint !== undefined) return chart.chartFingerprint === stage.chartFingerprint;
  return false;
}
 
/** How a replayed stage ends the run, or that it goes on. */
export type CourseReplayStageEnd = 'next' | 'cleared' | 'failed' | 'given-up';
 
/** One stage a course replay plays back: its own replay, which carries the state carried in, and how it ends. */
export interface CourseReplayStagePlayback {
  index: number;
  replay: BeMusicReplay;
  end: CourseReplayStageEnd;
}
 
/**
 * The stage replays a course replay plays back, in order, up to where the recorded run stopped: the first stage given
 * up (`play.aborted`) that the run did not go on after (LR2's course gauges go on past one,
 * `courseGoesOnAfterGivingUp`), or failed (its recorded gauge, `isCourseStageFailed`), else the last stage played. The
 * last entry says how the run ended; every other one is `next`. A stage without a recorded result (`results` is a
 * regenerable cache) goes on unless it is the last stage played.
 */
export function planCourseReplayPlayback(courseLog: Pick<BeMusicCourseReplay, 'stages'>): CourseReplayStagePlayback[] {
  const plan: CourseReplayStagePlayback[] = [];
  for (const [index, entry] of courseLog.stages.entries()) {
    if (!entry.played) break;
    const { replay } = entry;
    const native = replay.results?.native;
    // A stage given up that the recorded run went on after plays back as one that goes on: the log says it did.
    const wentOn = courseLog.stages[index + 1]?.played === true;
    const end: CourseReplayStageEnd =
      replay.play.aborted === true
        ? wentOn
          ? 'next'
          : 'given-up'
        : native !== undefined && isCourseStageFailed(replay.play.judgeRuleset ?? 'lr2', native.gauge)
          ? 'failed'
          : 'next';
    plan.push({ index, replay, end });
    if (end !== 'next') return plan;
  }
  const last = plan.at(-1);
  if (last !== undefined) last.end = last.index + 1 < courseLog.stages.length ? 'failed' : 'cleared';
  return plan;
}