All files / player-scene-model/src/play-result course.ts

100% Statements 28/28
95.55% Branches 43/45
100% Functions 6/6
100% Lines 26/26

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                                  189x                       9x 4x 9x 4x 4x 4x 4x 4x 9x 9x 9x 4x 4x   5x 4x   5x 4x   5x   9x   4x                                                     28x 28x   28x                     28x                   28x                                                                                      
import type { CourseRunStageExtras } from '@be-music/player-engine/course';
import { resolveLR2MoneyScore, resolvePlugoutScore } from '@be-music/player-engine/ruleset';
import type { BeMusicCourseReplay, ReplayCourseResult } from '@be-music/player-engine/replay';
import { resolveCourseView } from '../course-view/course-view.ts';
import type { LibraryCourse } from '../library/entries.ts';
import type { GaugeHistorySample, ScoreHistorySample } from '../play-state/types.ts';
import type { PlayResult } from './result.ts';
 
/** A stage's gauge and EX-score graphs (`progress` 0..1 over the stage), as its result shows them. */
export interface CourseStageHistory extends CourseRunStageExtras {
  gaugeHistory: readonly GaugeHistorySample[];
  scoreHistory: readonly ScoreHistorySample[];
  /** The stage's play time in seconds (its result's `playSeconds`), when known. */
  playSeconds?: number;
}
 
/** A stage without graphs (given up, or played without them). */
export const EMPTY_COURSE_STAGE_HISTORY: CourseStageHistory = { gaugeHistory: [], scoreHistory: [] };
 
/**
 * Every stage's graphs joined into one over the whole course, as beatoraja's course result draws its graphs
 * (`CourseResult.create`): each stage takes a share of the width by its `weights` (its length), the EX score runs on
 * from the stages before it, and a stage the run never reached reads a gauge of 0 (beatoraja fills it with zeros) with
 * the EX score where the run left it.
 */
export function joinCourseStageHistories(
  stages: readonly CourseStageHistory[],
  weights: readonly number[],
): CourseStageHistory {
  const total = weights.reduce((sum, weight) => sum + Math.max(0, weight), 0);
  const share = (index: number): number =>
    total > 0 ? Math.max(0, weights[index] ?? 0) / total : 1 / Math.max(1, weights.length);
  const gaugeHistory: GaugeHistorySample[] = [];
  const scoreHistory: ScoreHistorySample[] = [];
  let start = 0;
  let exScore = 0;
  weights.forEach((_weight, index) => {
    const width = share(index);
    const stage = stages[index];
    if (stage === undefined) {
      gaugeHistory.push({ progress: start, value: 0 }, { progress: start + width, value: 0 });
      scoreHistory.push({ progress: start, exScore }, { progress: start + width, exScore });
    } else {
      for (const sample of stage.gaugeHistory) {
        gaugeHistory.push({ progress: start + sample.progress * width, value: sample.value });
      }
      for (const sample of stage.scoreHistory) {
        scoreHistory.push({ progress: start + sample.progress * width, exScore: exScore + sample.exScore });
      }
      exScore += stage.scoreHistory.at(-1)?.exScore ?? 0;
    }
    start += width;
  });
  return { gaugeHistory, scoreHistory };
}
 
/**
 * The result of a course run as one play: the course's summed judgments, BP, course max combo and final gauge, under
 * the course's name (the first stage's chart supplies the song images), with each stage's graphs. `history` is the
 * graph the result itself draws: LR2 shows the last stage's (its result clears the graph at each stage start, OpenLR2
 * L24151); `stageHistories` are every stage's, for beatoraja's joined course graph.
 *
 * SCORE reads on the stage results' scale: LR2's money score over the course's notes (out of 200000, as LR2 keeps a
 * course's best), else the EX score. PLAY TIME is `playSeconds` (the stages' play time, `CourseRun.playSeconds`), else
 * `history.playSeconds` (a NONSTOP run passes its stitched play's result). An EXPERT / GRADE run is a `course` run (its
 * verdict reads COURSE CLEAR / COURSE FAILED); a NONSTOP run is a `nonstop` one, shown as one play on the result skin.
 */
export function playResultFromCourseRun(
  course: LibraryCourse,
  result: ReplayCourseResult,
  courseLog: BeMusicCourseReplay | undefined,
  history: CourseStageHistory = EMPTY_COURSE_STAGE_HISTORY,
  options: {
    playSeconds?: number;
    stageHistories?: readonly CourseStageHistory[];
    practice?: boolean;
    /** The run is a course replay played back ({@link PlayResult.fromReplay}). */
    fromReplay?: boolean;
  } = {},
): PlayResult {
  const first = course.stages[0]!.chart!;
  const lastStage = [...(courseLog?.stages ?? [])].reverse().find((entry) => entry.played);
  const judgeRuleset =
    lastStage?.played === true
      ? (lastStage.replay.play.judgeRuleset ?? 'lr2')
      : result.ruleset.startsWith('beatoraja')
        ? 'beatoraja'
        : result.ruleset.startsWith('____')
          ? '____'
          : result.ruleset.startsWith('plugout')
            ? 'plugout'
            : 'lr2';
  // The score a stage's result shows under the ruleset: LR2's money score, plugout's 100000-point score, else EX score.
  const score =
    judgeRuleset === 'lr2'
      ? resolveLR2MoneyScore({
          total: result.noteCount,
          perfect: result.judge.pgreat,
          great: result.judge.great,
          good: result.judge.good,
        })
      : judgeRuleset === 'plugout'
        ? resolvePlugoutScore(result.judge, result.noteCount)
        : result.exScore;
  return {
    ...(options.practice === true ? { practice: true } : {}),
    ...(options.fromReplay === true ? { fromReplay: true } : {}),
    // A course runs every stage AUTO or none: the last stage played tells.
    ...(lastStage?.played === true && lastStage.replay.play.mode === 'auto' ? { autoPlay: true } : {}),
    score: {
      total: result.noteCount,
      perfect: result.judge.pgreat,
      great: result.judge.great,
      good: result.judge.good,
      bad: result.judge.bad,
      poor: result.judge.poor,
      emptyPoor: result.judge.emptyPoor,
      exScore: result.exScore,
      score,
    },
    maxCombo: result.maxCombo,
    gauge: result.gauge.final,
    cleared: result.gauge.cleared,
    playSeconds: options.playSeconds ?? history.playSeconds ?? 0,
    chart: {
      ...first,
      id: `course:${course.id}`,
      title: course.definition.name,
      subtitle: `${result.stagesPlayed} / ${course.stages.length} STAGES${result.trophy !== undefined ? ` ยท ${result.trophy}` : ''}`,
      totalNotes: result.noteCount,
    },
    gaugeHistory: [...history.gaugeHistory],
    scoreHistory: [...history.scoreHistory],
    lamp: result.lamp,
    gaugeType: result.gauge.type,
    run: course.definition.kind === 'nonstop' ? 'nonstop' : 'course',
    course: resolveCourseView(course),
    courseRun: {
      stagesPlayed: result.stagesPlayed,
      ...(result.trophy !== undefined ? { trophy: result.trophy } : {}),
      ...(options.stageHistories !== undefined ? { stageHistories: options.stageHistories } : {}),
    },
    judgeRuleset,
    fast: result.fast,
    slow: result.slow,
  };
}