All files / player-engine/src/ruleset custom-judge.ts

100% Statements 24/24
100% Branches 12/12
100% Functions 6/6
100% Lines 17/17

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                                  418x                       32x 32x 31x         17x                     44x 11x 11x 33x 66x 66x 66x 66x     11x                     2x 2x 2x              
import type { JudgeWindowSetUs, RulesetWindowTables } from './definitions.ts';
 
/**
 * beatoraja 0.8.9 CUSTOM JUDGE (`PlayerConfig.customJudge`): per-judge window rates in percent for keys and scratches,
 * and the key long-note release margin rate. Present only while the option is on.
 */
export interface BeatorajaCustomJudge {
  keyPerfectGreat: number;
  keyGreat: number;
  keyGood: number;
  scratchPerfectGreat: number;
  scratchGreat: number;
  scratchGood: number;
  longNoteMargin: number;
}
 
/** `PlayerConfig` field defaults. */
export const DEFAULT_BEATORAJA_CUSTOM_JUDGE: Readonly<BeatorajaCustomJudge> = {
  keyPerfectGreat: 400,
  keyGreat: 400,
  keyGood: 100,
  scratchPerfectGreat: 400,
  scratchGreat: 400,
  scratchGood: 100,
  longNoteMargin: 100,
};
 
/** `PlayerConfig.validate`: PGREAT rates 25..400, every other rate 0..400, as integers. */
export function clampCustomJudgeRate(field: keyof BeatorajaCustomJudge, value: number): number {
  const min = field === 'keyPerfectGreat' || field === 'scratchPerfectGreat' ? 25 : 0;
  if (!Number.isFinite(value)) return DEFAULT_BEATORAJA_CUSTOM_JUDGE[field];
  return Math.max(min, Math.min(400, Math.trunc(value)));
}
 
/** `BMSPlayer` marks the play ASSIST when any rate (or the LN margin rate) exceeds 100 %. */
export function isCustomJudgeAssist(judge: Readonly<BeatorajaCustomJudge>): boolean {
  return Object.values(judge).some((rate) => rate > 100);
}
 
/**
 * The rate step of `JudgeWindowRule.create`, after the judgerank stage: scale PGREAT, GREAT and GOOD (Java long
 * arithmetic, truncating), clamp each leg to the BAD leg, then widen a leg narrower than the one inside it.
 */
export function applyJudgeWindowRates(
  set: JudgeWindowSetUs,
  rates: readonly [number, number, number],
): JudgeWindowSetUs {
  const judges = set.judges.map((pair) => [...pair]) as unknown as Array<[number, number]>;
  const bad = judges[3]!;
  for (let judge = 0; judge < 3; judge++) {
    for (let side = 0; side < 2; side++) {
      let value = Math.trunc((judges[judge]![side]! * rates[judge]!) / 100);
      if (Math.abs(value) > Math.abs(bad[side]!)) value = bad[side]!;
      if (judge > 0 && Math.abs(value) < Math.abs(judges[judge - 1]![side]!)) value = judges[judge - 1]![side]!;
      judges[judge]![side] = value;
    }
  }
  return { ...set, judges: judges as unknown as JudgeWindowSetUs['judges'] };
}
 
/**
 * `JudgeManager.init`: note heads use the key rates and scratch heads the scratch rates, while `JudgeWindow` builds
 * the key and scratch long-note end tables with the key rates.
 */
export function applyCustomJudgeTables(
  tables: RulesetWindowTables,
  judge: Readonly<BeatorajaCustomJudge>,
): RulesetWindowTables {
  const key = [judge.keyPerfectGreat, judge.keyGreat, judge.keyGood] as const;
  const scratch = [judge.scratchPerfectGreat, judge.scratchGreat, judge.scratchGood] as const;
  return {
    note: applyJudgeWindowRates(tables.note, key),
    scratch: applyJudgeWindowRates(tables.scratch, scratch),
    longNoteEnd: applyJudgeWindowRates(tables.longNoteEnd, key),
    longScratchEnd: applyJudgeWindowRates(tables.longScratchEnd, key),
  };
}