All files / player-skin-sdk/src select-list.ts

100% Statements 63/63
89.39% Branches 59/66
100% Functions 12/12
100% Lines 45/45

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                                                          7x 7x 7x 35x 35x 31x 31x 31x 22x   7x         3x 2x 1x                 12x 11x 8x 7x 6x 6x 8x 4x     5x 12x 1x 1x               6x 5x 4x 3x 2x   1x       122x                       6x 6x 5x 4x 6x 6x     8x 6x     8x               3x 1x 1x 2x      
import type { BeMusicCourseBestRecord, BeMusicDanRank, LibraryCourse, LibraryEntry } from './chart.ts';
 
/** One row of the select list to draw this frame. */
export interface SelectListRow {
  /** Index into `frame.entries`. */
  entryIndex: number;
  /**
   * Where the row sits, in rows from the top of the list window: 0 for the first row, fractional while the list is
   * pulled (`frame.listScroll`), -1 < slot < 0 for a row sliding out at the top. Its top is `listTop + slot * rowHeight`.
   */
  slot: number;
  /** How much of the row is inside the window, 0..1: fade the row by it so rows slide in and out at the edges. */
  alpha: number;
}
 
/**
 * The rows of the select list to draw, top to bottom: the window's rows (`firstVisibleIndex`, `visibleRows`) moved up
 * by `listScroll` (as a fraction of `rowHeight`), plus the row sliding in at the edge it opens. At rest it is exactly
 * the window, each row at a whole slot with alpha 1. Rows past either end of `entries` are left out.
 */
export function selectListRows(
  frame: {
    readonly entries: readonly unknown[];
    readonly firstVisibleIndex: number;
    readonly visibleRows: number;
    readonly listScroll?: number;
  },
  rowHeight: number,
): SelectListRow[] {
  const shift = rowHeight > 0 ? Math.max(-1, Math.min(1, (frame.listScroll ?? 0) / rowHeight)) : 0;
  const rows: SelectListRow[] = [];
  for (let offset = -1; offset <= frame.visibleRows; offset += 1) {
    const entryIndex = frame.firstVisibleIndex + offset;
    if (entryIndex < 0 || entryIndex >= frame.entries.length) continue;
    const slot = offset - shift;
    const alpha = Math.max(0, Math.min(1, Math.min(slot + 1, frame.visibleRows - slot)));
    if (alpha <= 0) continue;
    rows.push({ entryIndex, slot, alpha });
  }
  return rows;
}
 
/** The headline of a select-list row: a chart's title, a folder's name, or a course's name. */
export function browseEntryTitle(entry: LibraryEntry): string {
  if (entry.kind === 'chart') return entry.chart.title;
  if (entry.kind === 'course') return entry.course.definition.name;
  return entry.folder.label;
}
 
/**
 * The count a row shows in place of a chart's facts: a folder's charts (or courses, for a course folder), or a course's
 * stages with how many are missing from the collection (`random` for a random course, whose stages are drawn on start).
 * `undefined` for a chart row.
 */
export function browseEntryCount(entry: LibraryEntry): string | undefined {
  if (entry.kind === 'chart') return undefined;
  if (entry.kind === 'course') {
    if (entry.course.newCourse === true) return 'make a course';
    if (entry.course.randomMix === true) return 'draw a course';
    const stages = entry.course.stages.length;
    if (entry.course.random !== undefined) return `${stages} stage${stages === 1 ? '' : 's'} · random`;
    const missing = entry.course.stages.filter((stage) => stage.chart === undefined).length;
    return `${stages} stage${stages === 1 ? '' : 's'}${missing > 0 ? ` · ${missing} missing` : ''}`;
  }
  // Neither a NEW COURSE nor a RANDOM MIX bar is a course.
  const courses = entry.folder.courses?.filter((course) => course.newCourse !== true && course.randomMix !== true);
  if (courses !== undefined) return `${courses.length} course${courses.length === 1 ? '' : 's'}`;
  const charts = entry.folder.charts.length;
  return `${charts} chart${charts === 1 ? '' : 's'}`;
}
 
/**
 * The short badge a row shows where a chart shows its level: `DIR` for a folder, `DAN` for a dan course, `CRS` for
 * another course, and the level (or `-`) for a chart.
 */
export function browseEntryBadge(entry: LibraryEntry): string {
  if (entry.kind === 'folder') return 'DIR';
  if (entry.kind === 'course') {
    if (entry.course.newCourse === true) return 'NEW';
    if (entry.course.randomMix === true) return 'RND';
    return entry.course.definition.kind === 'grade' ? 'DAN' : 'CRS';
  }
  return entry.chart.playLevel !== undefined ? String(entry.chart.playLevel) : '-';
}
 
/** How a select card and a course result name a course's kind. */
export const COURSE_KIND_LABELS = { grade: 'DAN', expert: 'COURSE', nonstop: 'NONSTOP' } as const;
 
/**
 * What a select card shows for a focused course bar in place of a chart: the course's name, a line naming its kind and
 * its stage count (`make a course` on a NEW COURSE bar, `draw a course` on a RANDOM MIX bar), and its stage titles in order. With the player's `record`
 * on the course (`frame.courseBestRecord`), the line also names its best lamp and trophy (`DAN · 4 stages · HARD ·
 * goldmedal`); a lamp of `NO_PLAY` is left out.
 */
export function describeCourseCard(
  course: LibraryCourse,
  record?: BeMusicCourseBestRecord,
): { title: string; detail: string; stages: string[] } {
  const title = course.definition.name;
  if (course.newCourse === true) return { title, detail: 'make a course', stages: [] };
  if (course.randomMix === true) return { title, detail: 'draw a course', stages: [] };
  const kind = course.random !== undefined ? 'RANDOM COURSE' : COURSE_KIND_LABELS[course.definition.kind];
  const count = browseEntryCount({ kind: 'course', course }) ?? '';
  const best = [
    record !== undefined && record.lamp !== 'NO_PLAY' ? record.lamp.replaceAll('_', ' ') : undefined,
    record?.trophy,
  ].filter((part): part is string => part !== undefined);
  return {
    title,
    detail: [`${kind} · ${count}`, ...best].join(' · '),
    stages: course.stages.map((stage) => stage.title),
  };
}
 
/**
 * The player's dan ranks (`frame.danRanks`) on one line, fewest keys first: `7K 七段 · 14K 初段`. Empty without any.
 */
export function describeDanRanks(ranks: readonly BeMusicDanRank[] | undefined): string {
  if (ranks === undefined || ranks.length === 0) return '';
  return [...ranks]
    .sort((a, b) => a.keyMode - b.keyMode)
    .map((rank) => `${rank.keyMode}K ${rank.label}`)
    .join(' · ');
}