All files / player-engine/src/records assist-policy.ts

99.21% Statements 127/128
98.13% Branches 158/161
94.73% Functions 18/19
100% Lines 103/103

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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351                                    240x                                                                       240x                           240x                 2620x 2609x         1311x 1310x 1309x         158x         3x 3x           25x         2616x 146x 146x 111x 103x   7x 7x   96x                       1311x 1311x 1311x 1311x 1465x         1311x 1311x 1311x 47x     1311x 1311x 6x 1311x 2x   1311x         4x 1311x   1311x           1311x 1311x 7x               1311x 1308x 1308x   1308x 1308x 8x     1311x 1311x       18x         18x   1x 1x   5x 5x   2x 2x   1285x       1311x 1311x 36x 36x 36x 36x   1311x 6x 1311x 1311x 1311x 2x 1311x 2x   1311x     1311x 3x 1308x 4x 1304x         2x 1311x         1254x 1254x 1254x                                                                                               804x 804x 804x 720x               84x 25x               59x             59x 29x 28x 7x 9x   7x           31x 17x         14x         718x         709x 23x 9x    
import type { BeMusicReplay } from '../replay/format.ts';
import type { ReplayOptionChange, ReplayOptionState } from '../replay/options.ts';
import { isCustomJudgeAssist } from '../ruleset/custom-judge.ts';
import { compareClearLamps, type ClearLamp } from '../ruleset/clear-lamp.ts';
import type { RulesetId } from '../ruleset/definitions.ts';
import { splitOptionSource } from '../ruleset/option-source.ts';
 
/**
 * How assisted a play is, mildest first (#602). A play takes the highest class of its options.
 *
 * - `none`: no assist.
 * - `light`: beatoraja's LIGHT_ASSIST options, LR2's EASY-capped options and separate-lamp modes, and
 *   That-Which-Must-Not-Be-Named's assist options.
 * - `assist`: beatoraja's ASSIST options (and the debug judge-window override, which widens the windows as CUSTOM JUDGE
 *   does).
 * - `no-save`: an option whose source saves no data from the play (That-Which-Must-Not-Be-Named's BATTLE and the PC
 *   edition's ANY KEY, beatoraja PRACTICE).
 */
export const ASSIST_CLASSES = ['none', 'light', 'assist', 'no-save'] as const;
 
export type AssistClass = (typeof ASSIST_CLASSES)[number];
 
/** LR2's separate lamps, kept apart from a chart's ordinary record: D-BATTLE, SP TO DP and EXTRA MODE. */
export type LR2RecordSlot = 'd-battle' | 'sp-to-dp' | 'extra';
 
/** The record a play keeps under a ruleset: a chart's ordinary record, or one of LR2's separate lamps. */
export type RecordSlot = 'main' | LR2RecordSlot;
 
/**
 * Where an option comes from: a judge ruleset's source game, or Be-Music itself (its debug judge-window override, and its
 * record rule for a player's nanasigroove NOTES COPY).
 */
export type AssistSource = RulesetId | 'be-music';
 
/** One assisted option a play used, with the source that classifies it. */
export interface AssistFinding {
  /**
   * What the option is: `auto-scratch`, `any-key`, `judge-window-override`, `custom-judge`, `bpm-guide`, `hs-fix-constant`,
   * `practice`, `extra-notes`, `lr2-extra-mode`, `seven-to-nine`, `mine-remove`, `scroll-remove`, `long-note:<modifier>`,
   * `nanasi-notes:<COPY|DBLCOPY>` or a lane or battle option's value (`H-RANDOM`, `SCATTER`, `BATTLE`, `D-BATTLE`, …).
   */
  option: string;
  source: AssistSource;
  assistClass: Exclude<AssistClass, 'none'>;
  /** Set for LR2's separate-lamp modes. */
  lr2Slot?: LR2RecordSlot;
}
 
/** The facts of a play the classification reads: its recorded options and chart. */
export type AssistReplay = Pick<BeMusicReplay, 'play'> & {
  chart: Pick<BeMusicReplay['chart'], 'notes' | 'bpmRange'> & Partial<Pick<BeMusicReplay['chart'], 'chartPlayOptions'>>;
};
 
/** beatoraja's lane options that declare LIGHT_ASSIST (0.8.9 `Random`, `LaneShuffleModifier`, `NoteShuffleModifier`). */
const BEATORAJA_LIGHT_LANE_OPTIONS: ReadonlySet<string> = new Set([
  'SPIRAL',
  'H-RANDOM',
  'ALL-SCR',
  'RANDOM-EX',
  'S-RANDOM-EX',
  'RANDOM_PLAYABLE',
  'S_RANDOM_PLAYABLE',
]);
 
/**
 * LR2's lane options that cap the lamp at EASY and save no score (OpenLR2 `LR2f.cpp` L9334, `random > 3`): SCATTER and
 * the corrected nine-key CONVERGE (LR2's own CONVERGE is classified by its source below).
 */
const LR2_LIGHT_LANE_OPTIONS: ReadonlySet<string> = new Set(['SCATTER', 'SCATTER-CORRECTED', 'CONVERGE-CORRECTED']);
 
/** Whether a selected option took `value` at any point of the play: at the start or in a later change. */
function optionEverTook(
  options: Readonly<ReplayOptionState> | undefined,
  changes: readonly ReplayOptionChange[] | undefined,
  key: string,
  value: string | boolean,
): boolean {
  if (options?.[key] === value) return true;
  return (changes ?? []).some((change) => change.values[key] === value);
}
 
/** Whether the play kept its scroll with HS-FIX CONSTANT at any point (the view, or the selected option). */
function usedHSFixConstant(play: BeMusicReplay['play']): boolean {
  if (play.view?.hsFix === 'CONSTANT') return true;
  if ((play.viewChanges ?? []).some((change) => change.hsFix === 'CONSTANT')) return true;
  return optionEverTook(play.options, play.optionChanges, 'hsFix', 'CONSTANT');
}
 
/** Whether the chart has a 2P side: a note on a 2P channel (2x, 6x), where the 2P AUTO SCRATCH plays a part. */
function hasSecondPlayerSide(notes: AssistReplay['chart']['notes']): boolean {
  return notes.some((note) => note.channel.startsWith('2') || note.channel.startsWith('6'));
}
 
/** Whether a long-note modifier generated a CN or HCN (beatoraja `LongNoteModifier` marks only those ASSIST). */
function generatedChargeNote(replay: AssistReplay): boolean {
  return replay.chart.notes.some(
    (note) => note.generatedLongNote === true && note.lnMode !== undefined && note.lnMode !== 1,
  );
}
 
/** Which source a bare value or a ruleset-relative switch belongs to under `ruleset` (ADR 0019). */
function sourceOf(qualified: RulesetId | undefined, playedRuleset: RulesetId): RulesetId {
  return qualified ?? playedRuleset;
}
 
/** The lane-option findings of one side's recorded value. */
function laneFinding(value: string | undefined, playedRuleset: RulesetId): AssistFinding | undefined {
  if (value === undefined) return undefined;
  const { source: qualified, name } = splitOptionSource(value);
  if (BEATORAJA_LIGHT_LANE_OPTIONS.has(name)) return { option: name, source: 'beatoraja', assistClass: 'light' };
  if (LR2_LIGHT_LANE_OPTIONS.has(name)) return { option: name, source: 'lr2', assistClass: 'light' };
  if (name === 'CONVERGE') {
    // LR2's CONVERGE (`random` 5) and beatoraja's PMS CONVERGE (`ConvergeRandomizer`, LIGHT_ASSIST) are both light.
    const source = sourceOf(qualified, playedRuleset);
    return { option: name, source: source === 'beatoraja' ? 'beatoraja' : 'lr2', assistClass: 'light' };
  }
  return undefined;
}
 
/**
 * The assisted options a play used, each classified by the rules of the source it comes from (#602): LR2 beta3 100201
 * (OpenLR2 `LR2f.cpp` L9196-L9364, L27075-L27095), beatoraja 0.8.9 (`BMSPlayer` L201-L364 and the pattern modifiers),
 * and That-Which-Must-Not-Be-Named AC 34 (its option page: assist options, and BATTLE saves no score). A bare value that two sources
 * define (BATTLE, CONVERGE, AUTO SCRATCH) is the played ruleset's own, as ADR 0019 reads it. Only recorded facts are read,
 * so the server's re-simulation classifies a play the same way: beatoraja's chart-dependent cases use the recorded counts
 * (mines removed, notes converted, scroll changed) and the chart's BPM range.
 */
export function classifyReplayAssists(replay: AssistReplay): AssistFinding[] {
  const { play } = replay;
  const playedRuleset: RulesetId = play.judgeRuleset ?? 'lr2';
  const findings: AssistFinding[] = [];
  const add = (finding: AssistFinding | undefined): void => {
    if (finding !== undefined && !findings.some((known) => known.option === finding.option)) findings.push(finding);
  };
 
  // AUTO SCRATCH: LR2 caps the lamp at EASY (`p1_assist` / `p2_assist`), That-Which-Must-Not-Be-Named lists it as an assist option;
  // beatoraja has no standalone switch, so LR2's rule stands in.
  const autoScratch1P = play.autoScratch1P ?? play.autoScratch;
  const autoScratch2P = (play.autoScratch2P ?? play.autoScratch) && hasSecondPlayerSide(replay.chart.notes);
  if (autoScratch1P || autoScratch2P)
    add({ option: 'auto-scratch', source: playedRuleset === '____' ? '____' : 'lr2', assistClass: 'light' });
 
  // ANY KEY is the PC edition's assist, and the PC edition saves no play data from it (NO PLAY).
  if (play.anyKey === true) add({ option: 'any-key', source: '____', assistClass: 'no-save' });
  if (play.judgeWindowOverrideMs !== undefined)
    add({ option: 'judge-window-override', source: 'be-music', assistClass: 'assist' });
  if (play.customJudge !== undefined && isCustomJudgeAssist(play.customJudge))
    add({ option: 'custom-judge', source: 'beatoraja', assistClass: 'assist' });
  // beatoraja assists BPM GUIDE only on a chart whose BPM changes.
  if (
    optionEverTook(play.options, play.optionChanges, 'bpmGuide', true) &&
    replay.chart.bpmRange !== undefined &&
    replay.chart.bpmRange.min < replay.chart.bpmRange.max
  )
    add({ option: 'bpm-guide', source: 'beatoraja', assistClass: 'light' });
  if (usedHSFixConstant(play)) add({ option: 'hs-fix-constant', source: 'lr2', assistClass: 'light' });
  // PRACTICE is beatoraja's mode, offered under every ruleset (#933); it saves nothing whatever the ruleset.
  if (play.options?.practice === true) add({ option: 'practice', source: 'beatoraja', assistClass: 'no-save' });
 
  // A player's nanasigroove NOTES COPY / DBLCOPY plays a single-play chart as a double-play one. A chart's record is of
  // its own key mode, so the play keeps none: Be-Music's rule (#1243), since nanasigroove records every option's play.
  // `playerChartPlayOptions` holds only what took effect, so a course stage, which leaves COPY out, is not caught. A
  // chart that forces COPY is not the player's choice; its record is of the double-play chart.
  const playerNotes = play.playerChartPlayOptions?.notes;
  if (playerNotes === 'COPY' || playerNotes === 'DBLCOPY')
    add({ option: `nanasi-notes:${playerNotes}`, source: 'be-music', assistClass: 'no-save' });
 
  // A lane arrangement the chart forced (#844) is nanasigroove's, which has no assist class, and every play of the chart
  // gets it: it is not the player's assist, whatever lane value the host recorded for it. A chart RANDOM+ is recorded as
  // nanasigroove's own `RANDOM+` since #1244, which no rule here classifies; older replays recorded it as RANDOM-EX.
  // The 2P lane option arranges nothing on a chart without a 2P side, and beatoraja builds its 2P modifier only for a
  // two-player mode (`BMSPlayer` L292), so it counts there only. LR2 caps the lamp for a 2P SCATTER or CONVERGE on any
  // chart (`random > 3`, OpenLR2 `LR2f.cpp` L9334), so LR2's own 2P values always count (#1080).
  if (replay.chart?.chartPlayOptions?.options.random === undefined) {
    add(laneFinding(play.randomLane?.p1, playedRuleset));
    const secondSide = laneFinding(play.randomLane?.p2, playedRuleset);
    // Without the chart's notes the side is unknown, so the option counts.
    const notes = replay.chart?.notes;
    if (secondSide !== undefined && (secondSide.source === 'lr2' || notes === undefined || hasSecondPlayerSide(notes)))
      add(secondSide);
  }
 
  const battle = play.battleMode === undefined ? undefined : splitOptionSource(play.battleMode);
  switch (battle?.name) {
    case 'BATTLE':
      // beatoraja's BATTLE is LIGHT_ASSIST (`BMSPlayer` L252); That-Which-Must-Not-Be-Named's saves no score data. A
      // qualified value keeps its source; a bare one is the played ruleset's (LR2 defines no one-player BATTLE).
      add(
        sourceOf(battle.source, playedRuleset) === '____'
          ? { option: 'BATTLE', source: '____', assistClass: 'no-save' }
          : { option: 'BATTLE', source: 'beatoraja', assistClass: 'light' },
      );
      break;
    case 'BATTLE-AS':
      add({ option: 'BATTLE-AS', source: 'beatoraja', assistClass: 'light' });
      break;
    case 'D-BATTLE':
      add({ option: 'D-BATTLE', source: 'lr2', assistClass: 'light', lr2Slot: 'd-battle' });
      break;
    case 'SP-TO-DP':
      add({ option: 'SP-TO-DP', source: 'lr2', assistClass: 'light', lr2Slot: 'sp-to-dp' });
      break;
    default:
      break;
  }
  // LR2's mania chart generators. ADD NOTES and LOUDNESS set LR2's `isNosave`; ADD LONGNOTES and ADD MINES only fail its
  // score-save condition (OpenLR2 `LR2f.cpp` L67-L79, L27073-L27080), like SCATTER.
  const mania = play.lr2Mania;
  if (mania !== undefined) {
    if (mania.addNotes > 0) add({ option: 'lr2-add-notes', source: 'lr2', assistClass: 'no-save' });
    if (mania.loudness > 0) add({ option: 'lr2-loudness', source: 'lr2', assistClass: 'no-save' });
    if (mania.addLongNotes > 0) add({ option: 'lr2-add-longnotes', source: 'lr2', assistClass: 'light' });
    if (mania.addMines > 0) add({ option: 'lr2-add-mines', source: 'lr2', assistClass: 'light' });
  }
  if (play.lr2ExtraMode === true)
    add({ option: 'lr2-extra-mode', source: 'lr2', assistClass: 'light', lr2Slot: 'extra' });
  if ((play.extraNoteChangeCount ?? 0) > 0) add({ option: 'extra-notes', source: 'beatoraja', assistClass: 'assist' });
  if (play.sevenToNineApplied === true) add({ option: 'seven-to-nine', source: 'beatoraja', assistClass: 'light' });
  if (play.mineModifier === 'REMOVE' && (play.mineModifierChangeCount ?? 0) > 0)
    add({ option: 'mine-remove', source: 'beatoraja', assistClass: 'light' });
  if (play.scrollModifier === 'REMOVE' && play.scrollModifierChanged === true)
    add({ option: 'scroll-remove', source: 'beatoraja', assistClass: 'light' });
 
  const longNotes = play.longNoteModifier;
  // LEGACY NOTE assists only a chart it converted: a chart without charge notes keeps its standard lamp (RemyWiki's
  // IIDX Glossary; the official page names no lamp). A replay recorded before the count was stored reads as converted.
  if (longNotes === 'LEGACY' && (play.longNoteModifierChangeCount ?? 1) > 0)
    add({ option: 'long-note:LEGACY', source: '____', assistClass: 'light' });
  else if (longNotes === 'REMOVE' && (play.longNoteModifierChangeCount ?? 0) > 0)
    add({ option: 'long-note:REMOVE', source: 'beatoraja', assistClass: 'assist' });
  else if (
    (longNotes === 'ADD_CN' || longNotes === 'ADD_HCN' || longNotes === 'ADD_ALL') &&
    (play.longNoteModifierChangeCount ?? 0) > 0 &&
    generatedChargeNote(replay)
  )
    add({ option: `long-note:${longNotes}`, source: 'beatoraja', assistClass: 'assist' });
  return findings;
}
 
/** The highest class among `findings` ({@link ASSIST_CLASSES}); `none` for none. */
export function resolveAssistClass(findings: readonly { assistClass: AssistClass }[]): AssistClass {
  let index = 0;
  for (const finding of findings) index = Math.max(index, ASSIST_CLASSES.indexOf(finding.assistClass));
  return ASSIST_CLASSES[index]!;
}
 
/**
 * How a cleared play's lamp is limited: `cap` keeps any lamp up to `lamp` (LR2's EASY), `replace` awards `lamp` for
 * any clear (beatoraja's LIGHT ASSIST EASY / ASSIST EASY, That-Which-Must-Not-Be-Named's ASSIST CLEAR). A failed play stays FAILED.
 */
export interface RecordLampRule {
  rule: 'cap' | 'replace';
  lamp: ClearLamp;
}
 
/** What a play's assist class means for its records under one judge ruleset: one output per decision, not one boolean. */
export interface RecordPolicy {
  ruleset: RulesetId;
  assistClass: AssistClass;
  /** Whether the play keeps anything in its local record (`no-save` keeps nothing). */
  keepsRecord: boolean;
  /** Whether its EX score, BP and max combo may improve the record; otherwise only its lamp and play count count. */
  updatesScore: boolean;
  /** How its lamp is limited; absent when it is not. */
  lamp?: RecordLampRule;
  /** Which record the play keeps: the chart's ordinary one, or an LR2 separate lamp. */
  recordSlot: RecordSlot;
  /** Whether a course run with a stage of this class keeps a course record, and the stage its chart's (LR2 and beatoraja: no). */
  keepsCourseRecord: boolean;
  /** Where the Leaderboard puts the play: the main board, the assisted board, or stored without ranking. */
  leaderboard: 'main' | 'assisted' | 'unranked';
  /** The replay is always kept: it is what the records are derived from. */
  keepsReplay: true;
}
 
/**
 * The consequences of a play's assisted options under `ruleset`, each ruleset by its own rules (#602):
 *
 * - `lr2` (OpenLR2 L9196-L9364): an assisted play caps its lamp at EASY and keeps only its lamp and play count. D-BATTLE,
 *   SP TO DP and EXTRA MODE keep a separate lamp instead, capped at EASY only with another assist (AUTO SCRATCH on
 *   D-BATTLE excepted, as LR2's `isForceEasy` exempts it).
 * - `beatoraja` (`BMSPlayer.createScoreData`, `PlayDataAccessor.updateScore`): a cleared LIGHT_ASSIST play is LIGHT ASSIST
 *   EASY and an ASSIST one ASSIST EASY; `updateScore` is off, so only the lamp and play count update.
 * - `____`: an assisted clear is ASSIST CLEAR (the ASSIST EASY lamp) and saves no score, as the original pages leave the
 *   score open and both LR2 and beatoraja block it.
 *
 * `no-save` keeps nothing under any ruleset. A course run with any assisted stage keeps no course record (LR2's
 * `CheckScoreSaveConditon`, beatoraja's `updateCourseScore`). The Leaderboard ranks `none` on the main board, `light` and
 * `assist` on the assisted board, and stores `no-save` unranked.
 */
export function resolveRecordPolicy(findings: readonly AssistFinding[], ruleset: RulesetId): RecordPolicy {
  const assistClass = resolveAssistClass(findings);
  const base = { ruleset, assistClass, keepsReplay: true as const };
  if (assistClass === 'none')
    return {
      ...base,
      keepsRecord: true,
      updatesScore: true,
      recordSlot: 'main',
      keepsCourseRecord: true,
      leaderboard: 'main',
    };
  if (assistClass === 'no-save')
    return {
      ...base,
      keepsRecord: false,
      updatesScore: false,
      recordSlot: 'main',
      keepsCourseRecord: false,
      leaderboard: 'unranked',
    };
  const assisted = {
    ...base,
    keepsRecord: true,
    updatesScore: false,
    keepsCourseRecord: false,
    leaderboard: 'assisted' as const,
  };
  if (ruleset === 'lr2') {
    const slot = findings.find((finding) => finding.lr2Slot !== undefined)?.lr2Slot;
    if (slot === undefined) return { ...assisted, recordSlot: 'main', lamp: { rule: 'cap', lamp: 'EASY' } };
    const forceEasy = findings.some(
      (finding) => finding.lr2Slot === undefined && !(slot === 'd-battle' && finding.option === 'auto-scratch'),
    );
    return {
      ...assisted,
      recordSlot: slot,
      ...(forceEasy ? { lamp: { rule: 'cap' as const, lamp: 'EASY' as const } } : {}),
    };
  }
  if (ruleset === 'beatoraja')
    return {
      ...assisted,
      recordSlot: 'main',
      lamp: { rule: 'replace', lamp: assistClass === 'light' ? 'LIGHT_ASSIST_EASY' : 'ASSIST_EASY' },
    };
  return { ...assisted, recordSlot: 'main', lamp: { rule: 'replace', lamp: 'ASSIST_EASY' } };
}
 
/** {@link resolveRecordPolicy} of a recorded play, under `ruleset` (the ruleset it was played under by default). */
export function resolveReplayRecordPolicy(replay: AssistReplay, ruleset?: RulesetId): RecordPolicy {
  return resolveRecordPolicy(classifyReplayAssists(replay), ruleset ?? replay.play.judgeRuleset ?? 'lr2');
}
 
/** The lamp a play keeps under `policy`: unchanged without a lamp rule and for NO PLAY and FAILED. */
export function applyRecordPolicyLamp(lamp: ClearLamp, policy: Pick<RecordPolicy, 'lamp'>): ClearLamp {
  if (policy.lamp === undefined || lamp === 'NO_PLAY' || lamp === 'FAILED') return lamp;
  if (policy.lamp.rule === 'replace') return policy.lamp.lamp;
  return compareClearLamps(lamp, policy.lamp.lamp) > 0 ? policy.lamp.lamp : lamp;
}