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

96.87% Statements 62/64
96.82% Branches 61/63
100% Functions 5/5
100% Lines 50/50

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 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163                                                                                                                              79x 79x   74x 79x       62x     79x       51x 51x 51x 51x 51x 125x 51x 119x 102x 102x 2x   100x 100x 3x     97x 95x 95x 95x 95x 95x   44x 44x 44x                             79x 79x 182x 157x 156x   25x 24x 24x   74x       23x 23x 23x         2x     21x 21x 19x 38x 1x     18x 18x 18x                    
import type { BeMusicCourseReplay } from './course.ts';
import { computeCourseReplayFingerprint } from './course-fingerprint.ts';
import { checkReplayEligibility, type ReplayAssistFlag } from './eligibility.ts';
import { resolveAssistClass, type AssistClass } from '../records/assist-policy.ts';
 
/**
 * Why a course run cannot be submitted. `auto-play`, `aborted` and `missing-fingerprint` mean what they mean for a
 * single play, for any stage (`missing-fingerprint` also covers an unreached stage without its chart fingerprint, and a
 * NONSTOP run's stitched play without its fingerprints). `not-a-course-run` is a log whose stages do not make one run:
 * no stage played, a played stage after an unreached one, a stage log that was not played as a stage of this course (no
 * `play.course`, or another course kind), or a NONSTOP run whose stitched play does not cover the course's stages.
 */
export type CourseReplayIneligibleReason =
  | 'auto-play'
  | 'two-player'
  | 'aborted'
  | 'missing-fingerprint'
  | 'not-a-course-run';
 
/**
 * Why a submitted course run is stored but not ranked. `tempo` is a run with a stage (or a NONSTOP run's stitched play)
 * played at another tempo: practice, as for a single play. `unreleased` is a course with `release: false` (beatoraja's
 * flag, also set on a drawn random course). `bmson-total` is a run with any played stage using
 * zero-total no-recovery, or a stitched NONSTOP play using that policy. `assist` is a run with an assisted stage (any
 * assist class but `none`): LR2 saves no course stage with an assist on and beatoraja no assisted course (#602).
 */
export type CourseReplayUnrankedReason = 'tempo' | 'unreleased' | 'bmson-total' | 'assist';
 
export type CourseReplayEligibility =
  | {
      eligible: true;
      /** The course's ranking key (`computeCourseReplayFingerprint`). */
      courseFingerprint: string;
      /**
       * The played stages' fingerprints, in order. Empty for a NONSTOP run: its stages were not played on their own, and
       * a verifier rebuilds them from their charts instead.
       */
      stages: Array<{ chartFingerprint: string; playFingerprint: string }>;
      /** A NONSTOP run: the fingerprints of its stitched play (`courseLog.nonstop.replay`), which was judged. */
      nonstop?: { chartFingerprint: string; playFingerprint: string };
      /** Every assist any stage used, once each. */
      assistFlags: ReplayAssistFlag[];
      /** The run's assist class: the highest of its stages'. */
      assistClass: AssistClass;
      /** Set when the run is stored but left out of the course rankings. */
      unranked?: CourseReplayUnrankedReason;
    }
  | { eligible: false; reason: CourseReplayIneligibleReason; /** The stage at fault, zero-based. */ stage?: number };
 
type Ineligible = Extract<CourseReplayEligibility, { eligible: false }>;
 
/**
 * Decides whether a course run can be submitted and whether it ranks (docs/leaderboard.md, course rankings), for the
 * client that submits it and the server that stores it. Each played stage must pass the single-play check, except that
 * being a course stage is what it must be here; assists of any stage flag the run and mark it `unranked` (`assist`), as a
 * stage at another tempo or an unreleased course does. The last stage played may have been given up (ESC): a course
 * given up is ranked as a failed run, as LR2 records it, so only an earlier stage's abort refuses the run.
 *
 * A NONSTOP run (`courseLog.nonstop`) was one play of the stitched chart: that play passes the single-play check
 * instead (an ESC abort is a failed run there too), it must cover every stage of the course, and each stage must still
 * name its chart, since the stage charts key the course.
 */
export function checkCourseReplayEligibility(courseLog: BeMusicCourseReplay): CourseReplayEligibility {
  const order = checkStageOrder(courseLog);
  if (order !== undefined) return order;
  const result =
    courseLog.nonstop !== undefined ? checkNonstopEligibility(courseLog) : checkStagesEligibility(courseLog);
  if (!result.eligible) return result;
  // A course that is not released (beatoraja's `release: false`, a drawn random course) is stored, never ranked.
  // A run with an assisted stage is stored unranked: neither LR2 nor beatoraja saves an assisted course.
  const unranked =
    courseLog.course.release === false
      ? 'unreleased'
      : (result.unranked ?? (result.assistClass === 'none' ? undefined : ('assist' as const)));
  return unranked !== undefined ? { ...result, unranked } : result;
}
 
function checkStagesEligibility(courseLog: BeMusicCourseReplay): CourseReplayEligibility {
  const stages: Array<{ chartFingerprint: string; playFingerprint: string }> = [];
  const assistFlags = new Set<ReplayAssistFlag>();
  const assistClasses: Array<{ assistClass: AssistClass }> = [];
  let tempo = false;
  let zeroTotal = false;
  const lastPlayed = courseLog.stages.reduce((last, entry, index) => (entry.played ? index : last), -1);
  for (const [index, entry] of courseLog.stages.entries()) {
    if (!entry.played) continue;
    const { course, ...play } = entry.replay.play;
    if (course === undefined || course.kind !== courseLog.course.kind) {
      return { eligible: false, reason: 'not-a-course-run', stage: index };
    }
    const single = checkReplayEligibility({ ...entry.replay, play });
    if (!single.eligible) {
      return { eligible: false, reason: single.reason, stage: index };
    }
    // A course given up with ESC is a failed run (as LR2 records it): its last played stage may be aborted, no other.
    if (play.aborted === true && index !== lastPlayed) return { eligible: false, reason: 'aborted', stage: index };
    stages.push({ chartFingerprint: single.chartFingerprint, playFingerprint: single.playFingerprint });
    for (const flag of single.assistFlags) assistFlags.add(flag);
    assistClasses.push({ assistClass: single.assistClass });
    if (single.unranked === 'tempo') tempo = true;
    if (single.unranked === 'bmson-total') zeroTotal = true;
  }
  const courseFingerprint = computeCourseReplayFingerprint(courseLog);
  Iif (courseFingerprint === undefined) return { eligible: false, reason: 'missing-fingerprint' };
  return {
    eligible: true,
    courseFingerprint,
    stages,
    assistFlags: [...assistFlags],
    assistClass: resolveAssistClass(assistClasses),
    ...(tempo ? { unranked: 'tempo' as const } : zeroTotal ? { unranked: 'bmson-total' as const } : {}),
  };
}
 
/**
 * The checks every course log shares: the first stage was played, no stage was played after an unreached one, and
 * every unreached stage names its chart.
 */
function checkStageOrder(courseLog: BeMusicCourseReplay): Ineligible | undefined {
  let reachedEnd = false;
  for (const [index, entry] of courseLog.stages.entries()) {
    if (entry.played) {
      if (reachedEnd) return { eligible: false, reason: 'not-a-course-run', stage: index };
      continue;
    }
    if (index === 0) return { eligible: false, reason: 'not-a-course-run', stage: index };
    reachedEnd = true;
    if (entry.chartFingerprint === undefined) return { eligible: false, reason: 'missing-fingerprint', stage: index };
  }
  return undefined;
}
 
function checkNonstopEligibility(courseLog: BeMusicCourseReplay): CourseReplayEligibility {
  const run = courseLog.nonstop!;
  const { course, ...play } = run.replay.play;
  if (
    courseLog.course.kind !== 'nonstop' ||
    course?.kind !== 'nonstop' ||
    run.stages.length !== courseLog.stages.length
  ) {
    return { eligible: false, reason: 'not-a-course-run' };
  }
  // A NONSTOP play given up with ESC is a failed run.
  const single = checkReplayEligibility({ ...run.replay, play });
  if (!single.eligible) return { eligible: false, reason: single.reason };
  for (const [index, entry] of courseLog.stages.entries()) {
    if (entry.played && entry.replay.chart.chartFingerprint === undefined) {
      return { eligible: false, reason: 'missing-fingerprint', stage: index };
    }
  }
  const courseFingerprint = computeCourseReplayFingerprint(courseLog);
  Iif (courseFingerprint === undefined) return { eligible: false, reason: 'missing-fingerprint' };
  return {
    eligible: true,
    courseFingerprint,
    stages: [],
    nonstop: { chartFingerprint: single.chartFingerprint, playFingerprint: single.playFingerprint },
    assistFlags: single.assistFlags,
    assistClass: single.assistClass,
    ...(single.unranked === 'tempo' || single.unranked === 'bmson-total' ? { unranked: single.unranked } : {}),
  };
}