All files / player-engine/src utils.ts

89.55% Statements 60/67
88.57% Branches 62/70
100% Functions 10/10
89.23% Lines 58/65

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 149 150 151 152 153 154 155 156 157 158                317x 317x 317x                     298x 298x 298x 5x   293x 4x   289x 298x 2x   287x         149x 149x 2x   147x       447x       149x 149x 3x   146x 1x   145x 144x   1x         899x   899x 899x 47x   852x                                     5x 5x 4x           4x 2x 5x       147x 147x 147x     147x     147x 143x   4x     4x       4x 4x       509416x 509416x 509416x 509416x 509416x 509416x   509416x 509416x                 2101x                   2098x 2098x    
import { resolveChart, type ResolvedChart } from '@be-music/chart-model';
import type { BeMusicJSON, BeMusicPlayLevel } from '@be-music/chart-source';
import {
  estimateDifficultyFromNames,
  estimateDifficultyFromNoteCount,
  type DifficultyBasis,
} from './difficulty-estimate.ts';
 
const BMS_DEFAULT_DISPLAY_RANK = 2;
const BMS_DEFAULT_DISPLAY_PLAY_LEVEL = 3;
const BMSON_DEFAULT_DISPLAY_JUDGE_RANK = 100;
 
/**
 * The judge rank a chart screen shows: bmson `judge_rank` (100 when the chart has none), or BMS `#DEFEXRANK` when it is
 * above 0, else `#RANK 0`–`4` (truncated), else NORMAL (`2`).
 *
 * Reads the resolved chart's judge-rank facts (`ResolvedChart.judgeRank`); a source chart is resolved first. The facts
 * are the raw chart values, so the format defaults are filled here for display, as each ruleset fills them for play:
 * a replay records only what the chart sets and must replay to the same windows.
 */
export function resolveDisplayedJudgeRankValue(chart: BeMusicJSON | ResolvedChart): number {
  const resolved = toResolvedChart(chart);
  const { rank, exRank } = resolved.judgeRank;
  if (resolved.sourceFormat === 'bmson') {
    return exRank !== undefined && exRank > 0 ? exRank : BMSON_DEFAULT_DISPLAY_JUDGE_RANK;
  }
  if (exRank !== undefined && exRank > 0) {
    return exRank;
  }
  const rankValue = rank !== undefined ? Math.trunc(rank) : Number.NaN;
  if (rankValue >= 0 && rankValue <= 4) {
    return rankValue;
  }
  return BMS_DEFAULT_DISPLAY_RANK;
}
 
/** {@link resolveDisplayedJudgeRankValue} as a label (`NORMAL`, `120` …); `RANDOM` when `#EXRANKxx` changes it mid-play. */
export function resolveDisplayedJudgeRankLabel(chart: BeMusicJSON | ResolvedChart): string {
  const resolved = toResolvedChart(chart);
  if (resolved.judgeRank.changes.length > 0) {
    return 'RANDOM';
  }
  return formatDisplayedJudgeRankValue(resolveDisplayedJudgeRankValue(resolved));
}
 
function toResolvedChart(chart: BeMusicJSON | ResolvedChart): ResolvedChart {
  return 'judgeRank' in chart ? chart : resolveChart(chart);
}
 
export function resolveDisplayedPlayLevelValue(chart: BeMusicJSON): BeMusicPlayLevel | undefined {
  const playLevel = chart.metadata.playLevel;
  if (typeof playLevel === 'number' && Number.isFinite(playLevel) && playLevel >= 0) {
    return playLevel;
  }
  if (typeof playLevel === 'string' && playLevel.trim().length > 0) {
    return playLevel.trim();
  }
  if (chart.sourceFormat === 'bms') {
    return BMS_DEFAULT_DISPLAY_PLAY_LEVEL;
  }
  return undefined;
}
 
/** The chart's own `#DIFFICULTY` when it is 1–5 (truncated), else `undefined`. */
export function resolveDisplayedDifficultyValue(chart: BeMusicJSON): number | undefined {
  const difficulty = chart.metadata.difficulty;
  const normalized =
    typeof difficulty === 'number' && Number.isFinite(difficulty) ? Math.trunc(difficulty) : Number.NaN;
  if (Number.isFinite(normalized) && normalized >= 1 && normalized <= 5) {
    return normalized;
  }
  return undefined;
}
 
/** A difficulty a chart screen shows, and where it comes from. */
export interface DisplayedDifficulty {
  /** 1 BEGINNER … 5 INSANE. */
  readonly value: number;
  readonly basis: DifficultyBasis;
}
 
/**
 * The difficulty a chart screen shows: the chart's own `#DIFFICULTY` (1–5); else the one its title, subtitle, bmson
 * chart name and file name suggest (`estimateDifficultyFromNames`); else, given the chart's `noteCount`, the one
 * beatoraja gives by it (`estimateDifficultyFromNoteCount`). `undefined` when none gives one.
 */
export function resolveDisplayedDifficulty(
  chart: BeMusicJSON,
  options: { readonly fileName?: string; readonly noteCount?: number } = {},
): DisplayedDifficulty | undefined {
  const declared = resolveDisplayedDifficultyValue(chart);
  if (declared !== undefined) return { value: declared, basis: 'chart' };
  const named = estimateDifficultyFromNames({
    title: chart.metadata.title,
    subtitle: chart.metadata.subtitle,
    chartName: chart.bmson.info.chartName,
    fileName: options.fileName,
  });
  if (named !== undefined) return { value: named, basis: 'names' };
  const counted = options.noteCount !== undefined ? estimateDifficultyFromNoteCount(options.noteCount) : undefined;
  return counted !== undefined ? { value: counted, basis: 'notes' } : undefined;
}
 
function formatDisplayedJudgeRankValue(value: number): string {
  const normalized = Math.trunc(value);
  Eif (normalized === value) {
    Iif (normalized === 0) {
      return 'VERY HARD';
    }
    Iif (normalized === 1) {
      return 'HARD';
    }
    if (normalized === 2) {
      return 'NORMAL';
    }
    Iif (normalized === 3) {
      return 'EASY';
    }
    Iif (normalized === 4) {
      return 'VERY EASY';
    }
  }
  const rounded = Math.round(value * 100) / 100;
  return rounded.toFixed(2).replace(/(?:\.0+|(\.\d+?)0+)$/, '$1');
}
 
export function formatSeconds(seconds: number): string {
  const safe = Number.isFinite(seconds) ? Math.max(0, seconds) : 0;
  const totalCentiseconds = Math.floor(safe * 100);
  const centiseconds = totalCentiseconds % 100;
  const totalSeconds = Math.floor(totalCentiseconds / 100);
  const secondsPart = totalSeconds % 60;
  const totalMinutes = Math.floor(totalSeconds / 60);
 
  Eif (totalMinutes < 60) {
    return `${totalMinutes}:${secondsPart.toString().padStart(2, '0')}.${centiseconds.toString().padStart(2, '0')}`;
  }
 
  const minutesPart = totalMinutes % 60;
  const hours = Math.floor(totalMinutes / 60);
  return `${hours}:${minutesPart.toString().padStart(2, '0')}:${secondsPart.toString().padStart(2, '0')}.${centiseconds.toString().padStart(2, '0')}`;
}
 
export function resolveAltModifierLabel(platform: NodeJS.Platform = resolveDefaultPlatform()): 'Alt' | 'Option' {
  return platform === 'darwin' ? 'Option' : 'Alt';
}
 
/**
 * Browser-safe `process.platform` lookup. Falls back to `'linux'` when running outside Node so a static
 * `process.platform` reference doesn't `ReferenceError` the moment a web bundle calls a default-param consumer.
 * The value's only consumer is the human-readable Alt/Option modifier label, and `'Alt'` is the right fallback
 * for any non-Mac surface.
 */
function resolveDefaultPlatform(): NodeJS.Platform {
  const proc = (globalThis as { process?: { platform?: NodeJS.Platform } }).process;
  return proc?.platform ?? ('linux' as NodeJS.Platform);
}