All files / app-leaderboard/src/courses figures.ts

100% Statements 13/13
100% Branches 12/12
100% Functions 3/3
100% Lines 9/9

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                                              234x                                   110x 40x 68x   1x                   15x                   137x 137x 46x    
import {
  resolveRulesetResultMissCount,
  type ReplayCourseInfo,
  type ReplayRulesetResult,
} from '@be-music/player-engine/replay';
 
/** One reached stage of a course run under one ruleset, as the run page breaks the run down. */
export interface CourseStageFigures {
  exScore: number;
  noteCount: number;
  /** BP: BADs, POORs and empty POORs, plus the notes the stage never judged (it failed before them). */
  missCount: number;
  maxCombo: number;
  /** Judgment counts, with `fast` and `slow`. */
  judges: Record<string, number>;
  gaugeType: string;
  /** The gauge the stage ended on, in percent: what the next stage carries in. */
  gaugeFinal: number;
  cleared: boolean;
}
 
/** A stage result of `simulateCourseReplay` (`ReplayCourseResult.stages`) as it is stored. */
export function toCourseStageFigures(result: ReplayRulesetResult): CourseStageFigures {
  return {
    exScore: result.exScore,
    noteCount: result.noteCount,
    missCount: resolveRulesetResultMissCount(result),
    maxCombo: result.maxCombo,
    judges: { ...result.judge, fast: result.fast, slow: result.slow },
    gaugeType: result.gauge.type,
    gaugeFinal: result.gauge.final,
    cleared: result.gauge.cleared,
  };
}
 
/**
 * Where an earned trophy stands in its course's trophy list, which lists them easiest first (beatoraja
 * `GradeBar.getTrophy` keeps the last one that qualifies): 1 for the first, 0 for none or a name the list lacks. A name
 * listed twice takes its last place, as the last qualifying trophy is the one earned.
 */
export function courseTrophyRank(trophies: ReplayCourseInfo['trophies'], trophy: string | undefined | null): number {
  if (trophy === undefined || trophy === null) return 0;
  for (let index = trophies.length - 1; index >= 0; index -= 1) {
    if (trophies[index]!.name === trophy) return index + 1;
  }
  return 0;
}
 
/** A player's best trophy on one course, ruleset and assist class, and the run that earned it. */
export interface CourseTrophyBest {
  trophy: string | null;
  rank: number;
  scoreId: string | null;
}
 
export const NO_TROPHY: CourseTrophyBest = { trophy: null, rank: 0, scoreId: null };
 
/**
 * Folds a run's trophy into the best, as `mergeBestScores` folds the other bests: only a strictly higher trophy
 * replaces it, so a tie keeps the run that earned it first.
 */
export function mergeCourseTrophy(
  current: CourseTrophyBest | undefined,
  run: { scoreId: string; trophy: string | null; rank: number },
): { next: CourseTrophyBest; improved: boolean } {
  const best = current ?? NO_TROPHY;
  if (run.trophy === null || run.rank <= best.rank) return { next: best, improved: false };
  return { next: { trophy: run.trophy, rank: run.rank, scoreId: run.scoreId }, improved: true };
}