All files / player-engine/src/core scratch-spin.ts

100% Statements 20/20
93.75% Branches 15/16
100% Functions 5/5
100% Lines 16/16

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                            218x                                   514x 514x 3464x 506x 1013x 1013x 2014x 2014x       514x               2616x 2616x 2616x   2373x                       2615x 2614x    
/**
 * Which way a scratch press spins the turntable (#940), for display: the turntable's animation, live and in a replay.
 * Judging does not read it: a back-spin scratch (`isBackSpinScratch` in `ruleset/long-note.ts`) ends on a press from
 * another input source than the one that hit its head (`input-source.ts`, #605).
 *
 * - A controller that reports rotation (a turntable or an analog axis) sends the direction it actually turned.
 * - A keyboard uses odd-even: a scratch lane's keys alternate as the two directions, so its first key spins
 *   clockwise, its second counterclockwise, its third clockwise again, and so on ({@link createScratchSpinByToken}).
 * - Input with no direction (a touch lane) sends none.
 */
export type ScratchSpin = 'cw' | 'ccw';
 
/** True for the two values a {@link ScratchSpin} takes (replay validation). */
export function isScratchSpin(value: unknown): value is ScratchSpin {
  return value === 'cw' || value === 'ccw';
}
 
/** The part of a lane binding the odd-even rule reads (`LaneBinding` in `lane-layout.ts`). */
export interface ScratchSpinBinding {
  readonly isScratch: boolean;
  /**
   * The lane's physical keys in order, each as the tokens it may arrive as (`['ctrl-left', 'control-left']`). Absent
   * on a lane with a single key.
   */
  readonly inputKeys?: readonly (readonly string[])[];
}
 
/**
 * The spin each scratch key gives under the odd-even rule: the lane's keys at even positions spin `'cw'`, those at odd
 * positions `'ccw'`. Tokens are lowercased, as the input maps key them. Keys of non-scratch lanes have no spin.
 */
export function createScratchSpinByToken(bindings: readonly ScratchSpinBinding[]): ReadonlyMap<string, ScratchSpin> {
  const spins = new Map<string, ScratchSpin>();
  for (const binding of bindings) {
    if (!binding.isScratch || binding.inputKeys === undefined) continue;
    binding.inputKeys.forEach((tokens, index) => {
      const spin: ScratchSpin = index % 2 === 0 ? 'cw' : 'ccw';
      for (const token of tokens) {
        const normalized = token.toLowerCase();
        Eif (!spins.has(normalized)) spins.set(normalized, spin);
      }
    });
  }
  return spins;
}
 
/** The spin of a keyboard press: that of its first token bound to a scratch key, or `undefined` for any other key. */
export function resolveScratchSpin(
  tokens: readonly string[],
  spinByToken: ReadonlyMap<string, ScratchSpin>,
): ScratchSpin | undefined {
  for (const token of tokens) {
    const spin = spinByToken.get(token.toLowerCase());
    if (spin !== undefined) return spin;
  }
  return undefined;
}
 
/**
 * The spin of a `lane-input` press: the one the input reported, none when it reported `null` (input with no direction,
 * such as a touch lane, whatever key token it names), or else the keyboard's odd-even spin of its tokens.
 */
export function resolvePressSpin(
  reported: ScratchSpin | null | undefined,
  tokens: readonly string[],
  spinByToken: ReadonlyMap<string, ScratchSpin>,
): ScratchSpin | undefined {
  if (reported === null) return undefined;
  return reported ?? resolveScratchSpin(tokens, spinByToken);
}