All files / player-scene-model/src/library difficulty.ts

100% Statements 16/16
100% Branches 16/16
100% Functions 3/3
100% Lines 14/14

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                                194x     702x 702x 294x 294x             294x   702x                         746x 746x 702x 702x 158x 746x         700x    
import {
  estimateDifficultyFromNames,
  estimateDifficultyFromNoteCount,
  resolveDisplayedDifficultyValue,
  type DisplayedDifficulty,
} from '@be-music/player-engine/display';
import type { LibraryChart } from './entries.ts';
 
/**
 * What a library chart's difficulty is read from: the parsed chart, and its file name and note count when the entry has
 * them.
 */
export type LibraryChartDifficultySource = Pick<LibraryChart, 'chart'> &
  Partial<Pick<LibraryChart, 'fileLabel' | 'chartPath' | 'totalNotes'>>;
 
/** The estimate of each chart, from its names (which do not change); `null` for a chart its names give none. */
const estimates = new WeakMap<LibraryChartDifficultySource, number | null>();
 
function estimate(chart: LibraryChartDifficultySource): number | undefined {
  let value = estimates.get(chart);
  if (value === undefined) {
    const { metadata, bmson } = chart.chart;
    value =
      estimateDifficultyFromNames({
        title: metadata.title,
        subtitle: metadata.subtitle,
        chartName: bmson.info.chartName,
        fileName: chart.fileLabel ?? chart.chartPath,
      }) ?? null;
    estimates.set(chart, value);
  }
  return value ?? undefined;
}
 
/**
 * The difficulty (1 BEGINNER … 5 INSANE) a library chart is listed, filtered and drawn under, with its `basis` so a
 * screen can tell an estimate apart: its own `#DIFFICULTY`; else the one its names suggest
 * (`estimateDifficultyFromNames`); else the one its note count gives, as beatoraja falls back to
 * (`estimateDifficultyFromNoteCount`). `undefined` when none gives one. The chart's own value is read on every call, so
 * an edited difficulty (LR2's level edit) takes over from an estimate.
 *
 * Only what the player sees reads this: a replay, a score and a written chart keep the chart's own `#DIFFICULTY`.
 */
export function resolveLibraryChartDifficulty(chart: LibraryChartDifficultySource): DisplayedDifficulty | undefined {
  const declared = resolveDisplayedDifficultyValue(chart.chart);
  if (declared !== undefined) return { value: declared, basis: 'chart' };
  const named = estimate(chart);
  if (named !== undefined) return { value: named, basis: 'names' };
  const counted = chart.totalNotes !== undefined ? estimateDifficultyFromNoteCount(chart.totalNotes) : undefined;
  return counted !== undefined ? { value: counted, basis: 'notes' } : undefined;
}
 
/** {@link resolveLibraryChartDifficulty}'s value alone, for a consumer that draws no mark (an LR2 or beatoraja op). */
export function resolveLibraryChartDifficultyValue(chart: LibraryChartDifficultySource): number | undefined {
  return resolveLibraryChartDifficulty(chart)?.value;
}