All files / player-engine/src/core scoring.ts

100% Statements 32/32
100% Branches 34/34
100% Functions 4/4
100% Lines 32/32

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                                                                                                    3184x           3184x 168x   3184x         51855x 51855x 120x         99641x 31693x 67948x 7422x 60526x 9239x 51287x 6667x   44620x     99641x 99641x   99641x 48354x 48354x 18318x   48354x 777x 777x     51287x     99641x               99641x 31693x   67948x 7422x   60526x    
import { resolvePlugoutScore } from '../ruleset/plugout.ts';
import {
  _____EX_SCORE_PER_GREAT,
  _____EX_SCORE_PER_PGREAT,
  resolveLR2MoneyScore,
  type JudgmentKind,
  type ScoreSummary,
} from '../ruleset/score.ts';
 
// The score vocabulary is the ruleset's (`../ruleset/score.ts`, exported from `@be-music/player-engine/ruleset`); the engine
// modules read it through here, next to the trackers that fill a summary as a play is judged.
export {
  computeScoreRate,
  createEmptyScore,
  _____EX_SCORE_PER_GREAT,
  _____EX_SCORE_PER_PGREAT,
  _____RANK_LABELS,
  LR2_MONEY_SCORE_MAX,
  resolveDisplayedPoor,
  resolve____RankIndexFromRate,
  resolve____RankIndexFromScore,
  resolve____RankLabel,
  resolveLR2MoneyScore,
  type ____RankLabel,
  type JudgmentKind,
  type ScoreSummary,
} from '../ruleset/score.ts';
 
export interface ScoreTracker {
  combo: number;
  /** Highest combo reached so far — latched by {@link applyJudgeToSummary}, never reset by BAD / POOR. */
  maxCombo: number;
  /**
   * True when the active ruleset defines LR2's 200000-point "money score". Rulesets without one report EX-SCORE in
   * `ScoreSummary.score` instead — that is what beatoraja shows, and what That-Which-Must-Not-Be-Named has shown since BISTROVER retired
   * its own money score.
   */
  moneyScore: boolean;
  /** True when the active ruleset is plugout's, whose 100000-point score (`resolvePlugoutScore`) `ScoreSummary.score` reports. */
  plugoutScore?: boolean;
  /**
   * The course-wide combo and max combo, for a course stage: carried in from the previous stage and kept alongside the
   * stage's own combo, breaking where it breaks (beatoraja `JudgeManager.coursecombo`, LR2 `now_combo_course`).
   */
  course?: { combo: number; maxCombo: number };
}
 
export function createScoreTracker(
  options: { moneyScore?: boolean; plugoutScore?: boolean; course?: { combo: number; maxCombo: number } } = {},
): ScoreTracker {
  const tracker: ScoreTracker = {
    combo: 0,
    maxCombo: 0,
    moneyScore: options.moneyScore ?? true,
    ...(options.plugoutScore === true ? { plugoutScore: true } : {}),
  };
  if (options.course !== undefined) {
    tracker.course = { combo: options.course.combo, maxCombo: Math.max(options.course.maxCombo, options.course.combo) };
  }
  return tracker;
}
 
/** Breaks the combo the max combo is measured on, and the course-wide combo with it. */
export function breakTrackedCombo(tracker: ScoreTracker): void {
  tracker.combo = 0;
  if (tracker.course !== undefined) {
    tracker.course.combo = 0;
  }
}
 
export function applyJudgeToSummary(summary: ScoreSummary, judge: JudgmentKind, tracker: ScoreTracker): void {
  if (judge === 'PERFECT') {
    summary.perfect += 1;
  } else if (judge === 'GREAT') {
    summary.great += 1;
  } else if (judge === 'GOOD') {
    summary.good += 1;
  } else if (judge === 'BAD') {
    summary.bad += 1;
  } else {
    summary.poor += 1;
  }
 
  const exScoreDelta = resolveExScoreDelta(judge);
  summary.exScore += exScoreDelta;
 
  if (judge === 'PERFECT' || judge === 'GREAT' || judge === 'GOOD') {
    tracker.combo += 1;
    if (tracker.combo > tracker.maxCombo) {
      tracker.maxCombo = tracker.combo;
    }
    if (tracker.course !== undefined) {
      tracker.course.combo += 1;
      tracker.course.maxCombo = Math.max(tracker.course.maxCombo, tracker.course.combo);
    }
  } else {
    breakTrackedCombo(tracker);
  }
 
  summary.score = tracker.plugoutScore
    ? resolvePlugoutScore({ pgreat: summary.perfect, great: summary.great, good: summary.good }, summary.total)
    : tracker.moneyScore
      ? resolveLR2MoneyScore(summary)
      : summary.exScore;
}
 
function resolveExScoreDelta(judge: JudgmentKind): number {
  if (judge === 'PERFECT') {
    return _____EX_SCORE_PER_PGREAT;
  }
  if (judge === 'GREAT') {
    return _____EX_SCORE_PER_GREAT;
  }
  return 0;
}