All files / app-play/src/services record-consequence.ts

97.56% Statements 40/41
96.49% Branches 55/57
100% Functions 7/7
96.55% Lines 28/29

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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205                                                                                                                            27x   27x 27x                                                                               5x                                                     14x       5x                       18x 10x 7x   18x         18x 18x                       16x 15x 15x 3x 3x         16x                       14x 13x 12x 9x 8x 8x 7x 5x 5x   5x      
// What a play does to its record, in one line: before the play from the options picked (`resolvePlannedRecordPolicy`),
// and after it from the replay (`resolveReplayRecordPolicy`). Both go through the engine's record policy, so the line
// before a play and the record after it cannot disagree.
import {
  resolvePlannedRecordPolicy,
  resolveReplayRecordPolicy,
  type AssistFinding,
  type PlannedPlay,
  type PlannedRecordPolicy,
  type RecordPolicy,
} from '@be-music/player-engine/records';
import { classifyReplayAssists } from '@be-music/player-engine/records';
import { isBmsonRecoveryDisabled } from '@be-music/player-engine/ruleset';
import type { PlayResult } from '@be-music/player-scene-model';
import { playerText, type PlayerCopy } from '../player-copy.ts';
import type { PlayerGUIState } from '../types.ts';
 
/** The settings a play's record consequence is read from. */
export type RecordConsequenceState = Pick<
  PlayerGUIState,
  | 'judgeRuleset'
  | 'autoPlay'
  | 'autoScratch1P'
  | 'autoScratch2P'
  | 'anyKey'
  | 'customJudge'
  | 'customJudgeKeyPerfectGreat'
  | 'customJudgeKeyGreat'
  | 'customJudgeKeyGood'
  | 'customJudgeScratchPerfectGreat'
  | 'customJudgeScratchGreat'
  | 'customJudgeScratchGood'
  | 'customJudgeLongNoteMargin'
  | 'bpmGuide'
  | 'hsFix'
  | 'floatingHiSpeed'
  | 'classicHiSpeed'
  | 'normalHiSpeedLevel'
  | 'practice'
  | 'random1P'
  | 'random2P'
  | 'battleMode'
  | 'lr2AddNotes'
  | 'lr2Loudness'
  | 'lr2AddLongNotes'
  | 'lr2AddMines'
  | 'lr2ExtraMode'
  | 'mineModifier'
  | 'crazyAddMine'
  | 'crazyNotes'
  | 'scrollModifier'
  | 'longNoteModifier'
  | 'extraNoteDepth'
  | 'sevenToNinePattern'
>;
 
/**
 * The play the settings would start, in the fields a replay records it in: each option as the play takes it
 * (`resolvePlaySceneOptions`, `createPlayScene`, `resolveReplayRecording`). A course's lane limits are not applied: the
 * line describes a single play of the chart.
 */
export function plannedPlayOf(state: RecordConsequenceState): PlannedPlay {
  const ruleset = state.judgeRuleset;
  // HS-FIX is off while a hi-speed mode of its own scrolls the notes.
  const ownHiSpeed = state.floatingHiSpeed || state.classicHiSpeed || state.normalHiSpeedLevel > 0;
  return {
    judgeRuleset: ruleset,
    autoScratch1P: state.autoScratch1P,
    autoScratch2P: state.autoScratch2P,
    // ANY KEY is offered with the `____` judge rules only, and CUSTOM JUDGE with beatoraja's.
    anyKey: state.anyKey && ruleset === '____',
    ...(state.customJudge && ruleset === 'beatoraja'
      ? {
          customJudge: {
            keyPerfectGreat: state.customJudgeKeyPerfectGreat,
            keyGreat: state.customJudgeKeyGreat,
            keyGood: state.customJudgeKeyGood,
            scratchPerfectGreat: state.customJudgeScratchPerfectGreat,
            scratchGreat: state.customJudgeScratchGreat,
            scratchGood: state.customJudgeScratchGood,
            longNoteMargin: state.customJudgeLongNoteMargin,
          },
        }
      : {}),
    options: { bpmGuide: state.bpmGuide, hsFix: ownHiSpeed ? 'OFF' : state.hsFix, practice: state.practice },
    randomLane: { p1: state.random1P, p2: state.random2P },
    battleMode: state.battleMode as NonNullable<PlannedPlay['battleMode']>,
    lr2Mania: {
      addNotes: state.lr2AddNotes,
      loudness: state.lr2Loudness,
      addLongNotes: state.lr2AddLongNotes,
      addMines: state.lr2AddMines,
    } as NonNullable<PlannedPlay['lr2Mania']>,
    lr2ExtraMode: state.lr2ExtraMode,
    // Only NOTES bears on the record: a COPY play keeps none on a chart it copies (#1243).
    ...(state.crazyNotes !== 'OFF' ? { playerChartPlayOptions: { notes: state.crazyNotes } } : {}),
    mineModifier: state.crazyAddMine ? 'NANASI_ADD' : state.mineModifier,
    scrollModifier: state.scrollModifier,
    longNoteModifier: state.longNoteModifier,
    extraNotes: state.extraNoteDepth > 0,
    sevenToNine: state.sevenToNinePattern !== 0,
  };
}
 
/** An assisted option's name on the line: the dialog's own name for it, or the lane option's as recorded. */
const FINDING_NAMES: Readonly<Record<string, string>> = {
  'auto-scratch': 'AUTO SCRATCH',
  'any-key': 'ANY KEY',
  'judge-window-override': 'JUDGE WINDOW',
  'custom-judge': 'CUSTOM JUDGE',
  'bpm-guide': 'Guide: BPM',
  'hs-fix-constant': 'HS-FIX CONSTANT',
  practice: 'PRACTICE',
  'lr2-add-notes': 'ADD NOTES',
  'lr2-loudness': 'LOUDNESS',
  'lr2-add-longnotes': 'ADD LONGNOTES',
  'lr2-add-mines': 'ADD MINES',
  'lr2-extra-mode': 'EXTRA MODE',
  'extra-notes': 'EXTRA NOTE',
  'seven-to-nine': '7 TO 9',
  'mine-remove': 'MINE REMOVE',
  'scroll-remove': 'SCROLL REMOVE',
  'long-note:LEGACY': 'LEGACY NOTE',
  'long-note:REMOVE': 'LN REMOVE',
  'long-note:ADD_CN': 'ADD CN',
  'long-note:ADD_HCN': 'ADD HCN',
  'long-note:ADD_ALL': 'ADD ALL',
  'nanasi-notes:COPY': 'NOTES COPY',
  'nanasi-notes:DBLCOPY': 'NOTES DBLCOPY',
};
 
function names(findings: readonly AssistFinding[]): string {
  return findings.map((finding) => FINDING_NAMES[finding.option] ?? finding.option).join(', ');
}
 
/** The lamps a lamp rule names, as the lamp lists show them. */
const LAMP_NAMES: Readonly<Record<string, string>> = {
  EASY: 'EASY',
  LIGHT_ASSIST_EASY: 'LIGHT ASSIST EASY',
  ASSIST_EASY: 'ASSIST EASY',
};
 
/** What `policy` keeps of a play, caused by `findings`: one line. */
export function describeRecordPolicy(
  policy: RecordPolicy,
  findings: readonly AssistFinding[],
  copy: PlayerCopy['record'] = playerText().record,
): string {
  if (policy.assistClass === 'none') return copy.full;
  if (policy.assistClass === 'no-save') return copy.notSaved(names(findings));
  const lampName = policy.lamp === undefined ? undefined : (LAMP_NAMES[policy.lamp.lamp] ?? policy.lamp.lamp);
  const lamp =
    policy.lamp === undefined || lampName === undefined
      ? copy.lampKept
      : policy.lamp.rule === 'cap'
        ? copy.lampCap(lampName)
        : copy.lampReplace(lampName);
  const slot = policy.recordSlot === 'main' ? '' : copy.separateLamp(copy.slots[policy.recordSlot]);
  return copy.assisted(copy.classes[policy.assistClass], lamp + slot, names(findings));
}
 
/**
 * The record consequence of the options picked, before a play: what holds on any chart, then what a chart-dependent
 * option adds on a chart it changes.
 */
export function describePlannedRecord(
  planned: PlannedRecordPolicy,
  autoPlay = false,
  copy: PlayerCopy['record'] = playerText().record,
): string {
  if (autoPlay) return copy.autoPlay;
  const certain = describeRecordPolicy(planned.policy, planned.findings, copy);
  if (planned.conditionalFindings.length === 0) return certain;
  const every = [...planned.findings, ...planned.conditionalFindings];
  return `${certain} ${copy.conditional(names(planned.conditionalFindings), describeRecordPolicy(planned.conditionalPolicy, every, copy))}`;
}
 
/** {@link describePlannedRecord} of the player's settings. */
export function describeRecordConsequence(state: RecordConsequenceState): string {
  return describePlannedRecord(resolvePlannedRecordPolicy(plannedPlayOf(state)), state.autoPlay);
}
 
/**
 * What a finished play did to its record: `kept` is whether the record took it. A replay's result and a play without
 * a replay say nothing.
 */
export function describeResultRecord(
  data: Pick<PlayResult, 'replay' | 'practice' | 'autoPlay' | 'fromReplay' | 'secondPlayer'>,
  kept: boolean,
  copy: PlayerCopy['record'] = playerText().record,
): string | undefined {
  if (data.fromReplay === true) return undefined;
  if (data.autoPlay === true) return copy.autoPlay;
  if (data.practice === true) return copy.notSaved('PRACTICE');
  if (data.secondPlayer !== undefined) return copy.twoPlayer;
  const replay = data.replay;
  if (replay === undefined) return undefined;
  if (isBmsonRecoveryDisabled(replay.chart, replay.play.bmsonTotalMode)) return copy.bmsonTotal;
  const findings = classifyReplayAssists(replay);
  const policy = resolveReplayRecordPolicy(replay);
  // A record that did not take the play though its policy keeps one (no record key for the chart, say).
  Eif (!kept && policy.keepsRecord) return undefined;
  return describeRecordPolicy(policy, findings, copy);
}