All files / app-play/src chart-level-edits.ts

95% Statements 38/40
92% Branches 46/50
100% Functions 6/6
100% Lines 33/33

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            3x                             23x         18x 18x 18x 17x 17x 2x 2x 2x 2x 1x   2x 1x   2x   17x   1x           2x 2x               7x 2x 7x                       7x 7x 7x 3x 3x   4x               17x 20x 20x 2x 2x      
// LR2's level edit (F3 held with the arrows on the select screen): the player's own difficulty (`#DIFFICULTY`, 1
// BEGINNER … 5 INSANE) and level (`#PLAYLEVEL`) for a chart, kept in this browser and put over the chart's own each time
// the library is read. A chart is known by its path and title, so the same folder dropped again keeps its edits.
import type { LibraryChart } from '@be-music/player-scene-model';
 
/** Where the edits are kept. */
export const CHART_LEVEL_EDITS_STORAGE_KEY = 'be-music.chart-level-edits';
 
/** The player's own difficulty and level for a chart. */
export interface ChartLevelEdit {
  readonly difficulty?: number;
  readonly playLevel?: number;
}
 
/** Every chart's edit, by {@link chartLevelKey}. */
export type ChartLevelEdits = Readonly<Record<string, ChartLevelEdit>>;
 
type KeyValueStorage = Pick<Storage, 'getItem' | 'setItem'>;
 
/** The key a chart's edit is kept under: its path in its source and its title. */
export function chartLevelKey(chart: Pick<LibraryChart, 'chartPath' | 'title'>): string {
  return `${chart.chartPath}\u0000${chart.title}`;
}
 
/** The edits kept in `storage`, keeping only difficulties of 1-5 and whole levels of 0-99. */
export function readChartLevelEdits(storage: KeyValueStorage | undefined): ChartLevelEdits {
  try {
    const raw: unknown = JSON.parse(storage?.getItem(CHART_LEVEL_EDITS_STORAGE_KEY) ?? '{}');
    Iif (typeof raw !== 'object' || raw === null || Array.isArray(raw)) return {};
    const edits: Record<string, ChartLevelEdit> = {};
    for (const [key, value] of Object.entries(raw)) {
      Iif (typeof value !== 'object' || value === null) continue;
      const { difficulty, playLevel } = value as Record<string, unknown>;
      const edit: { difficulty?: number; playLevel?: number } = {};
      if (Number.isInteger(difficulty) && (difficulty as number) >= 1 && (difficulty as number) <= 5) {
        edit.difficulty = difficulty as number;
      }
      if (Number.isInteger(playLevel) && (playLevel as number) >= 0 && (playLevel as number) <= 99) {
        edit.playLevel = playLevel as number;
      }
      if (edit.difficulty !== undefined || edit.playLevel !== undefined) edits[key] = edit;
    }
    return edits;
  } catch {
    return {};
  }
}
 
/** Keeps `edits` in `storage`. */
export function writeChartLevelEdits(storage: KeyValueStorage | undefined, edits: ChartLevelEdits): void {
  try {
    storage?.setItem(CHART_LEVEL_EDITS_STORAGE_KEY, JSON.stringify(edits));
  } catch {
    // Storage full or blocked: the edit holds for this visit only.
  }
}
 
/** A chart's level as a number: its `#PLAYLEVEL`'s leading digits (`12`, `★5` → 5), 0 without one. */
function levelNumber(playLevel: LibraryChart['playLevel']): number {
  if (typeof playLevel === 'number') return playLevel;
  const digits = /\d+/u.exec(playLevel ?? '');
  return digits === null ? 0 : Number(digits[0]);
}
 
/**
 * The chart's edit after one step of LR2's level edit: the difficulty steps through 1-5, wrapping, and the level by 1
 * between 0 and 99.
 */
export function stepChartLevel(
  chart: Pick<LibraryChart, 'playLevel'> & { readonly chart: { readonly metadata: { readonly difficulty?: number } } },
  part: 'difficulty' | 'level',
  step: 1 | -1,
): ChartLevelEdit {
  const difficulty = chart.chart.metadata.difficulty;
  const playLevel = levelNumber(chart.playLevel);
  if (part === 'difficulty') {
    const current = difficulty !== undefined && difficulty >= 1 && difficulty <= 5 ? difficulty : step > 0 ? 0 : 6;
    return { ...(difficulty !== undefined ? { difficulty } : {}), difficulty: ((current - 1 + step + 5) % 5) + 1 };
  }
  return {
    ...(difficulty !== undefined ? { difficulty } : {}),
    playLevel: Math.min(99, Math.max(0, playLevel + step)),
  };
}
 
/** Puts `edits` over the charts' own difficulty and level, in place. */
export function applyChartLevelEdits(charts: readonly LibraryChart[], edits: ChartLevelEdits): void {
  for (const chart of charts) {
    const edit = edits[chartLevelKey(chart)];
    if (edit === undefined) continue;
    Eif (edit.playLevel !== undefined) chart.playLevel = edit.playLevel;
    if (edit.difficulty !== undefined) chart.chart.metadata.difficulty = edit.difficulty;
  }
}