All files / player-engine/src/core lane-hold.ts

100% Statements 43/43
95.83% Branches 23/24
100% Functions 5/5
100% Lines 32/32

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                                132x     132x     13351x       86907x 86907x 86907x 119924x 119924x 52361x   86907x   32149x 63604x 32149x           86907x 86907x 86907x 60x   86847x   86907x         86459x 86443x 26x   86417x   86417x   86443x 86428x 86428x               3133x 3137x 3137x 3127x   6x    
/**
 * When a lane's key went down and came up, kept for a short while so the judge can ask whether the lane was down at
 * an instant that has already passed — the moment a mine crossed the judge line, say — however late the question
 * comes and in whatever order the driver delivered the inputs around it.
 *
 * A lane counts as down while any physical input holding it is: a second key on the same lane (two scratch keys) going
 * down or up while the first stays down changes nothing.
 */
export interface LaneHoldHistory {
  /** The key tokens holding the lane now (`''` stands for an input that came without tokens). */
  readonly activeTokens: Set<string>;
  /** The lane's down spans, oldest first; the last one's `upMs` is `undefined` while the lane is down. */
  readonly spans: Array<{ downMs: number; upMs: number | undefined }>;
}
 
/** How long (ms) spans are kept after they end. */
export const LANE_HOLD_HISTORY_MS = 2000;
 
/** The token an input without tokens is tracked under. */
const UNNAMED_TOKEN = '';
 
export function createLaneHoldHistory(): LaneHoldHistory {
  return { activeTokens: new Set(), spans: [] };
}
 
function prune(history: LaneHoldHistory, atMs: number): void {
  const horizonMs = atMs - LANE_HOLD_HISTORY_MS;
  let spanCount = 0;
  while (spanCount < history.spans.length) {
    const upMs = history.spans[spanCount]!.upMs;
    if (upMs === undefined || upMs >= horizonMs) break;
    spanCount += 1;
  }
  if (spanCount > 0) {
    // Shift in place: `splice` would allocate an array of the removed spans on every press.
    const spans = history.spans;
    for (let index = spanCount; index < spans.length; index += 1) spans[index - spanCount] = spans[index]!;
    spans.length -= spanCount;
  }
}
 
/** A press at `atMs` by `tokens` (none: an unnamed input). */
export function recordLaneDown(history: LaneHoldHistory, tokens: readonly string[], atMs: number): void {
  prune(history, atMs);
  const wasDown = history.activeTokens.size > 0;
  if (tokens.length === 0) {
    history.activeTokens.add(UNNAMED_TOKEN);
  } else {
    for (const token of tokens) history.activeTokens.add(token);
  }
  if (!wasDown) history.spans.push({ downMs: atMs, upMs: undefined });
}
 
/** A release at `atMs` of `tokens` (none: every input holding the lane). */
export function recordLaneUp(history: LaneHoldHistory, tokens: readonly string[], atMs: number): void {
  if (history.activeTokens.size === 0) return;
  if (tokens.length === 0) {
    history.activeTokens.clear();
  } else {
    for (const token of tokens) history.activeTokens.delete(token);
    // An unnamed press is released by any release of the lane.
    history.activeTokens.delete(UNNAMED_TOKEN);
  }
  if (history.activeTokens.size > 0) return;
  const open = history.spans[history.spans.length - 1];
  Eif (open !== undefined && open.upMs === undefined) open.upMs = Math.max(open.downMs, atMs);
}
 
/**
 * Whether the lane was down at `atMs`. At an instant shared by an input and the question, the input comes first: a key
 * that goes down at `atMs` is down, one that comes up at `atMs` is not.
 */
export function wasLaneDownAt(history: LaneHoldHistory, atMs: number): boolean {
  for (let index = history.spans.length - 1; index >= 0; index -= 1) {
    const span = history.spans[index]!;
    if (span.downMs > atMs) continue;
    return span.upMs === undefined || span.upMs > atMs;
  }
  return false;
}