All files / player-engine/src/records planned-assists.ts

100% Statements 17/17
95% Branches 19/20
100% Functions 6/6
100% Lines 15/15

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                                                                                                                              204x           204x             10x   3x 3x       114x 114x 114x                                       57x 57x 57x   45x                 32x 32x 32x              
/**
 * The record consequence of a play before it is played: the options picked, classified by the classifier a finished
 * play goes through ({@link classifyReplayAssists}). Some findings depend on the chart (a BPM GUIDE on a chart whose BPM
 * changes, a REMOVE modifier that removed something, a 2P option on a chart with a 2P side), and the chart is not
 * prepared yet, so the plan is classified twice: once as if no chart-dependent case arises and once as if every one
 * does. A finding only in the second is conditional.
 */
import type { BeMusicReplay } from '../replay/format.ts';
import type { RulesetId } from '../ruleset/definitions.ts';
import {
  classifyReplayAssists,
  resolveRecordPolicy,
  type AssistFinding,
  type AssistReplay,
  type RecordPolicy,
} from './assist-policy.ts';
 
/**
 * The options a play is about to start with, in the fields a replay records them in (`BeMusicReplay.play`). The
 * counts a prepared chart gives (`mineModifierChangeCount`, `longNoteModifierChangeCount`, `scrollModifierChanged`,
 * `extraNoteChangeCount`, `sevenToNineApplied`) are not read: the plan says whether the option is on instead.
 * `playerChartPlayOptions` holds the player's picks; a NOTES COPY counts only on a chart it copies.
 */
export interface PlannedPlay extends Partial<
  Pick<
    BeMusicReplay['play'],
    | 'judgeRuleset'
    | 'autoScratch'
    | 'autoScratch1P'
    | 'autoScratch2P'
    | 'anyKey'
    | 'customJudge'
    | 'options'
    | 'view'
    | 'randomLane'
    | 'playerChartPlayOptions'
    | 'battleMode'
    | 'lr2Mania'
    | 'lr2ExtraMode'
    | 'mineModifier'
    | 'scrollModifier'
    | 'longNoteModifier'
  >
> {
  /** An option that adds notes to the chart is on (beatoraja's extra note depth). */
  readonly extraNotes?: boolean;
  /** A 7-to-9 pattern is on. */
  readonly sevenToNine?: boolean;
}
 
/** What a planned play does to its record: for certain, and at most. */
export interface PlannedRecordPolicy {
  /** The findings that hold on any chart. */
  readonly findings: readonly AssistFinding[];
  /** The findings that hold only on a chart the option changes something on. */
  readonly conditionalFindings: readonly AssistFinding[];
  /** The policy of the certain findings: what the play does to its record on a chart no conditional finding holds on. */
  readonly policy: RecordPolicy;
  /** The policy with every conditional finding holding; equal to `policy` when there is none. */
  readonly conditionalPolicy: RecordPolicy;
}
 
/** A chart on which every chart-dependent finding holds: a 2P side, a generated charge note and a changing BPM. */
const EVERY_CASE_CHART = {
  notes: [{ channel: '21', generatedLongNote: true, lnMode: 2 }],
  bpmRange: { min: 1, max: 2 },
} as unknown as AssistReplay['chart'];
 
/** A chart on which none does: no 2P side, no generated note, one BPM. */
const NO_CASE_CHART = { notes: [], bpmRange: { min: 1, max: 1 } } as unknown as AssistReplay['chart'];
 
/** The player's picks as a play would run them: a NOTES COPY takes effect only on a 5 / 7 KEY chart outside a course. */
function plannedPlayerOptions(
  player: NonNullable<PlannedPlay['playerChartPlayOptions']>,
  everyCase: boolean,
): NonNullable<PlannedPlay['playerChartPlayOptions']> {
  if (everyCase || (player.notes !== 'COPY' && player.notes !== 'DBLCOPY')) return player;
  // Elsewhere COPY does nothing and DBLCOPY keeps its DOUBLE.
  const { notes, ...others } = player;
  return notes === 'DBLCOPY' ? { ...others, notes: 'DOUBLE' } : others;
}
 
function planAsReplay(plan: PlannedPlay, everyCase: boolean): AssistReplay {
  const { extraNotes, sevenToNine, playerChartPlayOptions, ...recorded } = plan;
  const count = everyCase ? 1 : 0;
  return {
    play: {
      ...recorded,
      ...(playerChartPlayOptions !== undefined
        ? { playerChartPlayOptions: plannedPlayerOptions(playerChartPlayOptions, everyCase) }
        : {}),
      mineModifierChangeCount: count,
      longNoteModifierChangeCount: count,
      scrollModifierChanged: everyCase,
      extraNoteChangeCount: extraNotes === true ? count : 0,
      sevenToNineApplied: sevenToNine === true && everyCase,
    } as BeMusicReplay['play'],
    chart: everyCase ? EVERY_CASE_CHART : NO_CASE_CHART,
  };
}
 
/** The assist findings of a planned play: the certain ones, and the ones that depend on the chart. */
export function classifyPlannedAssists(
  plan: PlannedPlay,
): Pick<PlannedRecordPolicy, 'findings' | 'conditionalFindings'> {
  const findings = classifyReplayAssists(planAsReplay(plan, false));
  const every = classifyReplayAssists(planAsReplay(plan, true));
  return {
    findings,
    conditionalFindings: every.filter((finding) => !findings.some((known) => known.option === finding.option)),
  };
}
 
/**
 * What a planned play does to its record under `ruleset` (the plan's judge ruleset by default), from the resolver a
 * finished play's record goes through ({@link resolveRecordPolicy}).
 */
export function resolvePlannedRecordPolicy(plan: PlannedPlay, ruleset?: RulesetId): PlannedRecordPolicy {
  const judged = ruleset ?? plan.judgeRuleset ?? 'lr2';
  const { findings, conditionalFindings } = classifyPlannedAssists(plan);
  return {
    findings,
    conditionalFindings,
    policy: resolveRecordPolicy(findings, judged),
    conditionalPolicy: resolveRecordPolicy([...findings, ...conditionalFindings], judged),
  };
}