All files / player-engine/src/replay input-sources.ts

97.29% Statements 36/37
96.77% Branches 30/31
100% Functions 3/3
100% Lines 34/34

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                                                                      678x 678x 678x 1267x 1267x 1267x 1267x 1265x 1265x 1x   1264x 1x   1263x 3x   1260x 1260x 216x 3x   213x 1044x 1x   1256x 6x 4x       2x   1252x           209x 209x 1x   208x 208x 206x   207x 206x    
import {
  isInputSourceDevice,
  isInputSourceDeviceName,
  isInputSourceKind,
  isInputSourceLabel,
  isTurntableDirection,
  type IdentifiedInputSource,
} from '../core/input-source.ts';
import { expectNonNegativeInteger, expectRecord, ReplayParseError } from './parse.ts';
 
/**
 * One entry of a replay's input-source table (`play.inputSources`, #605): a physical input the play used, which every
 * input event names by `id` (`ReplayInputEvent.source`). See `core/input-source.ts`.
 */
export type ReplayInputSource = IdentifiedInputSource;
 
/**
 * How the host turned an analog turntable axis into the presses and releases the replay records: a named, versioned
 * algorithm and its parameters, for audit and display. The replay records the resulting events, never raw axis
 * samples, and simulators never re-run the algorithm.
 */
export interface ReplayAnalogScratch {
  /** The algorithm's versioned name (`'be-music/1'`). */
  algorithm: string;
  /** Axis ticks a turn must move before it counts as a press. */
  thresholdTicks?: number;
  /** How long (ms) the axis must stay still before the turn counts as released. */
  stopMs?: number;
}
 
/**
 * Validates `play.inputSources`: well-formed entries, unique ids, a `direction` exactly on turntable sources, and a
 * `deviceName` (present only when the player opted in to recording it) that is a kept device name.
 */
export function parseReplayInputSources(value: unknown, path = 'play.inputSources'): ReplayInputSource[] {
  Iif (!Array.isArray(value)) throw new ReplayParseError(`${path}: expected an array`);
  const ids = new Set<number>();
  return value.map((entry, index) => {
    const at = `${path}[${index}]`;
    const record = expectRecord(entry, at);
    const id = expectNonNegativeInteger(record.id, `${at}.id`);
    if (ids.has(id)) throw new ReplayParseError(`${at}.id: duplicate id ${id}`);
    ids.add(id);
    if (!isInputSourceKind(record.kind)) {
      throw new ReplayParseError(`${at}.kind: expected 'key' | 'button' | 'turntable'`);
    }
    if (!isInputSourceDevice(record.device)) {
      throw new ReplayParseError(`${at}.device: expected 'keyboard' | 'controller' | 'touch'`);
    }
    if (!isInputSourceLabel(record.label)) {
      throw new ReplayParseError(`${at}.label: expected a lowercase name without whitespace (1 to 32 characters)`);
    }
    const source: ReplayInputSource = { id, kind: record.kind, device: record.device, label: record.label };
    if (record.kind === 'turntable') {
      if (!isTurntableDirection(record.direction)) {
        throw new ReplayParseError(`${at}.direction: expected 'up' | 'down' on a turntable source`);
      }
      source.direction = record.direction;
    } else if (record.direction !== undefined) {
      throw new ReplayParseError(`${at}.direction: only a turntable source has a direction`);
    }
    if (record.deviceName !== undefined) {
      if (!isInputSourceDeviceName(record.deviceName)) {
        throw new ReplayParseError(
          `${at}.deviceName: expected 1 to 128 characters without control characters or surrounding spaces`,
        );
      }
      source.deviceName = record.deviceName;
    }
    return source;
  });
}
 
/** Validates `play.analogScratch`. */
export function parseReplayAnalogScratch(value: unknown, path = 'play.analogScratch'): ReplayAnalogScratch {
  const record = expectRecord(value, path);
  if (typeof record.algorithm !== 'string' || record.algorithm.length === 0 || record.algorithm.length > 64) {
    throw new ReplayParseError(`${path}.algorithm: expected a non-empty string of at most 64 characters`);
  }
  const parsed: ReplayAnalogScratch = { algorithm: record.algorithm };
  if (record.thresholdTicks !== undefined) {
    parsed.thresholdTicks = expectNonNegativeInteger(record.thresholdTicks, `${path}.thresholdTicks`);
  }
  if (record.stopMs !== undefined) parsed.stopMs = expectNonNegativeInteger(record.stopMs, `${path}.stopMs`);
  return parsed;
}