All files / player-engine/src/ruleset chart-facts.ts

96.66% Statements 29/30
100% Branches 27/27
100% Functions 4/4
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                                                      6271x     6271x 4368x   6271x 303x   6271x                                         3302x 3302x 3302x 3302x 3302x 3302x   128081x 128081x 27985x   100096x       3302x 3302x 319x         3302x 60x             3302x             3302x 3302x 1329x   3302x    
import { resolveChart, resolveChartLongNoteMode, type LongNoteMode, type ResolvedChart } from '@be-music/chart-model';
import { type BeMusicJSON } from '@be-music/chart-source';
import { resolveJudgeRankPercent } from '../core/judge-window.ts';
import type { TimedPlayableNote } from '../playable-notes.ts';
import { createEmptyRulesetNoteCounts, type RulesetChartFacts } from './facts.ts';
 
/**
 * Chart-side inputs the engine already has when it builds a play: the resolved JSON plus the prepared note arrays.
 * Structurally a subset of `PreparedPlaybackChartData`, so the engine hands its bundle over verbatim.
 */
export interface EngineRulesetChartInput {
  /** Scorable notes only — free-zone, mine, and invisible notes are excluded upstream. */
  scorableNotes: ReadonlyArray<TimedPlayableNote>;
  /** `resolveLaneDisplayMode` output — picks beatoraja's per-mode window tables. */
  laneDisplayMode: string;
  /** The resolved chart the notes came from (`PreparedPlaybackChartData.resolvedChart`); resolved from `json` if absent. */
  resolvedChart?: ResolvedChart;
}
 
/**
 * The judge-rank inputs a ruleset reads, as `RulesetChartFacts` / `ReplayChart` record them: the LR2-axis percent and
 * the chart's own `#RANK` / `#DEFEXRANK` (bmson `judge_rank`) from the resolved chart.
 */
export function resolveChartJudgeRankFacts(
  json: BeMusicJSON,
  resolved: ResolvedChart,
): { percent: number; sourceRank?: number; sourceExRank?: number } {
  const judgeRank: { percent: number; sourceRank?: number; sourceExRank?: number } = {
    percent: resolveJudgeRankPercent(json),
  };
  if (resolved.judgeRank.rank !== undefined) {
    judgeRank.sourceRank = resolved.judgeRank.rank;
  }
  if (resolved.judgeRank.exRank !== undefined) {
    judgeRank.sourceExRank = resolved.judgeRank.exRank;
  }
  return judgeRank;
}
 
/** Chart-level long-note mode after `#LNMODE` / bmson `info.ln_type` resolution (`@be-music/chart-model`). */
export { resolveChartLongNoteMode };
 
/**
 * Derives the ruleset's chart facts from the engine's own representation. The replay side has a matching adapter
 * (`rulesetChartFactsFromReplay`); both must agree, otherwise a live play and its replay would resolve different
 * rulesets from the same chart.
 */
export function rulesetChartFactsFromChart(
  json: BeMusicJSON,
  chart: EngineRulesetChartInput,
  dynamicJudgeRankChanges: ReadonlyArray<{ seconds: number; exRankValue: number }> = [],
  stageRanks: ReadonlyArray<{
    seconds: number;
    rank: number;
    table: 'chart' | 'lr2-nonstop' | 'bmson' | 'bms-exrank' | 'bms-exrank-change';
  }> = [],
): RulesetChartFacts {
  const resolved = chart.resolvedChart ?? resolveChart(json);
  const chartLNMode = resolved.lnMode;
  const notes = createEmptyRulesetNoteCounts();
  const long: Record<LongNoteMode, number> = { 1: 0, 2: 0, 3: 0 };
  let normal = 0;
  for (const note of chart.scorableNotes) {
    const hasTail =
      typeof note.endSeconds === 'number' && Number.isFinite(note.endSeconds) && note.endSeconds > note.seconds;
    if (hasTail) {
      long[note.longNoteMode ?? chartLNMode] += 1;
    } else {
      normal += 1;
    }
  }
 
  const judgeRank: RulesetChartFacts['judgeRank'] = resolveChartJudgeRankFacts(json, resolved);
  if (dynamicJudgeRankChanges.length > 0) {
    judgeRank.timeline = dynamicJudgeRankChanges.map((change) => ({
      timeUs: Math.round(change.seconds * 1_000_000),
      exRankValue: change.exRankValue,
    }));
  }
  if (stageRanks.length > 0) {
    judgeRank.stageRanks = stageRanks.map((change) => ({
      timeUs: Math.round(change.seconds * 1_000_000),
      rank: change.rank,
      table: change.table,
    }));
  }
 
  const facts: RulesetChartFacts = {
    sourceFormat: json.sourceFormat === 'bmson' || json.sourceFormat === 'lrx' ? json.sourceFormat : 'bms',
    laneMode: chart.laneDisplayMode,
    lnMode: chartLNMode,
    judgeRank,
    notes: { ...notes, normal, long },
  };
  if (json.bmson.info.totalUnit !== undefined) facts.totalUnit = json.bmson.info.totalUnit;
  if (resolved.total.value !== undefined) {
    facts.total = resolved.total.value;
  }
  return facts;
}