All files / player-engine/src/course table.ts

91.83% Statements 45/49
91.66% Branches 55/60
100% Functions 9/9
100% Lines 38/38

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                    242x     242x                         38x         53x       41x 41x 41x 41x 3x 5x 5x 5x   5x 4x   38x     38x 38x 49x 48x 48x 48x     40x 40x     41x                 41x 40x                   37x 36x 36x 36x 41x 36x 36x 31x 35x   5x 2x   41x    
import {
  type CourseDefinition,
  type CourseStageRef,
  createCourseStageRef,
  isClassCourseConstraints,
  normalizeCourseConstraints,
  normalizeCourseTrophies,
} from './definition.ts';
 
/** jbmstable-parser's default course name (`Course.name`). */
const DEFAULT_TABLE_COURSE_NAME = '新規段位';
 
/** beatoraja's `Mode` hints, as difficulty tables write them, by key count. */
const TABLE_MODE_KEYS: Readonly<Record<string, number>> = {
  'beat-5k': 5,
  'beat-7k': 7,
  'beat-10k': 10,
  'beat-14k': 14,
  'popn-5k': 5,
  'popn-9k': 9,
  'keyboard-24k': 24,
  'keyboard-24k-double': 48,
};
 
/** The key count a table's `mode` hint names (`beat-7k` → 7), or `undefined` for none or an unknown one. */
export function resolveTableModeKeys(mode: unknown): number | undefined {
  return typeof mode === 'string' && Object.hasOwn(TABLE_MODE_KEYS, mode) ? TABLE_MODE_KEYS[mode] : undefined;
}
 
/** A stage's title, or beatoraja's `course N` for one without (`CourseData.validate`). */
function stageTitle(title: unknown, index: number): string {
  return typeof title === 'string' && title.length > 0 ? title : `course ${index + 1}`;
}
 
function parseTableCourse(value: unknown, legacy: boolean, keyMode: number | undefined): CourseDefinition | undefined {
  Iif (value === null || typeof value !== 'object' || Array.isArray(value)) return undefined;
  const data = value as Record<string, unknown>;
  const stages: CourseStageRef[] = [];
  if (!legacy && Array.isArray(data.charts)) {
    for (const chart of data.charts) {
      Iif (chart === null || typeof chart !== 'object') continue;
      const fields = chart as Record<string, unknown>;
      const stage = createCourseStageRef({ ...fields, title: stageTitle(fields.title, stages.length) });
      // beatoraja drops the whole course when one of its charts has no hash (`SongData.validate`).
      if (stage === undefined) return undefined;
      stages.push(stage);
    }
  } else Eif (Array.isArray(data.md5)) {
    // `song_title`, beside `md5` in the same order, is no part of beatoraja's table format (some tables, such as
    // Stardust, add it); Be-Music reads it for a stage's title only, which shows while the stage is not in the library.
    const titles: readonly unknown[] = Array.isArray(data.song_title) ? data.song_title : [];
    for (const [index, md5] of data.md5.entries()) {
      if (typeof md5 !== 'string') continue;
      const stage = createCourseStageRef({ md5, title: stageTitle(titles[index], stages.length) });
      Iif (stage === undefined) return undefined;
      stages.push(stage);
    }
  }
  Iif (stages.length === 0) return undefined;
  const constraints = legacy
    ? normalizeCourseConstraints(['grade_mirror', 'gauge_lr2'])
    : normalizeCourseConstraints(Array.isArray(data.constraint) ? data.constraint : []);
  const course: CourseDefinition = {
    name: typeof data.name === 'string' ? data.name : DEFAULT_TABLE_COURSE_NAME,
    kind: isClassCourseConstraints(constraints) ? 'grade' : 'expert',
    stages,
    constraints,
    trophies: legacy ? [] : normalizeCourseTrophies(Array.isArray(data.trophy) ? data.trophy : []),
    release: true,
    source: { format: 'table', legacyGrade: legacy },
  };
  if (keyMode !== undefined) course.keyMode = keyMode;
  return course;
}
 
/**
 * The courses of a difficulty-table header (`header.json`), grouped as jbmstable-parser groups them: `course` as an
 * array of groups (`Course[][]`), or as one group (`Course[]`), or else the legacy `grade` list, whose courses all get
 * `grade_mirror` and `gauge_lr2`. A course reads its stages from `charts` (table elements, which may carry `sha256`)
 * or from `md5`; one whose charts lack a hash is skipped, as beatoraja does. Empty groups are dropped.
 */
export function parseTableHeaderCourses(header: unknown): CourseDefinition[][] {
  if (header === null || typeof header !== 'object' || Array.isArray(header)) return [];
  const data = header as Record<string, unknown>;
  const keyMode = resolveTableModeKeys(data.mode);
  const parseGroup = (group: readonly unknown[], legacy: boolean): CourseDefinition[] =>
    group.map((course) => parseTableCourse(course, legacy, keyMode)).filter((course) => course !== undefined);
  let groups: CourseDefinition[][] = [];
  if (Array.isArray(data.course) && data.course.length > 0) {
    groups = Array.isArray(data.course[0])
      ? data.course.filter(Array.isArray).map((group) => parseGroup(group, false))
      : [parseGroup(data.course, false)];
  } else if (Array.isArray(data.grade)) {
    groups = [parseGroup(data.grade, true)];
  }
  return groups.filter((group) => group.length > 0);
}