All files / player-browser/src/session/result result-keys.ts

100% Statements 19/19
100% Branches 28/28
100% Functions 4/4
100% Lines 13/13

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                                                                  72x                     29x 26x           87x 26x                         74x 67x 2x 65x 57x 42x 22x 22x    
import {
  DEFAULT_CONTROL_ACTIONS,
  type ControlActionKeyEvent,
  type ControlActionMap,
} from '../controls/control-actions.ts';
 
/** The subset of a `KeyboardEvent` the result-screen key matchers read. */
export interface ResultKeyEvent extends ControlActionKeyEvent {
  readonly code: string;
  readonly repeat: boolean;
  readonly ctrlKey: boolean;
  readonly metaKey: boolean;
  readonly altKey: boolean;
  readonly shiftKey: boolean;
}
 
/** What a key press asks of the result screen. */
export type ResultKeyAction =
  | { readonly type: 'retry'; readonly sameArrangement?: true }
  | { readonly type: 'change-graph' }
  | { readonly type: 'leave' }
  | { readonly type: 'continue' }
  | { readonly type: 'save-replay'; readonly slot: number };
 
/**
 * Whether a key press asks the result screen to retry the chart: a bare R by default. Presses with a modifier are left
 * alone so browser shortcuts such as Ctrl / Cmd+R (reload) keep working, and auto-repeats are ignored so a held key
 * can't fire a second retry.
 */
export function isResultRetryKey(
  event: ResultKeyEvent,
  actions: ControlActionMap = DEFAULT_CONTROL_ACTIONS.lr2,
): boolean {
  return !event.repeat && actions.is('result.retry', event);
}
 
/**
 * The replay slot (0-3) a key press saves into: a bare 1-4 by default, as beatoraja's result takes its number keys
 * (`ControlKeys.NUM1`-`NUM4`). Modified presses and auto-repeats save nothing.
 */
export function resultReplaySlotKey(
  event: ResultKeyEvent,
  actions: ControlActionMap = DEFAULT_CONTROL_ACTIONS.lr2,
): number | undefined {
  if (event.repeat) return undefined;
  const slots = [
    'result.save-replay-1',
    'result.save-replay-2',
    'result.save-replay-3',
    'result.save-replay-4',
  ] as const;
  const slot = slots.findIndex((id) => actions.is(id, event));
  return slot >= 0 ? slot : undefined;
}
 
/**
 * What a key press asks of the result screen by the player's keys (`actions`), in the order the screen takes them: a
 * retry, on a new arrangement or the same one (only when one is offered), the gauge graph's next gauge, leaving
 * (Escape), the next step (Enter / Space) and a replay slot.
 */
export function resolveResultKeyAction(
  event: ResultKeyEvent,
  retryOffered: boolean,
  actions: ControlActionMap = DEFAULT_CONTROL_ACTIONS.lr2,
): ResultKeyAction | undefined {
  if (retryOffered && isResultRetryKey(event, actions)) return { type: 'retry' };
  if (retryOffered && !event.repeat && actions.is('result.retry-same', event))
    return { type: 'retry', sameArrangement: true };
  if (!event.repeat && actions.is('result.change-graph', event)) return { type: 'change-graph' };
  if (actions.is('result.leave', event)) return { type: 'leave' };
  if (actions.is('result.continue', event)) return { type: 'continue' };
  const slot = resultReplaySlotKey(event, actions);
  return slot === undefined ? undefined : { type: 'save-replay', slot };
}