All files / player-engine/src/core high-speed-control.ts

94.11% Statements 48/51
92.3% Branches 48/52
100% Functions 4/4
94.11% Lines 48/51

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 88    145x 145x 145x 145x         15x 15x   15x 20x 20x 3x   17x 9x   8x   17x 2x       13x 6x   7x 5x   2x       3082x 3027x   55x 55x       15x 15x 15x 15x       20x 18x 18x 16x 16x 14x   2x 2x 2x           4x 4x 1x   3x 3x 2x     1x 1x 1x     1x 1x        
export type HighSpeedControlAction = 'increase' | 'decrease';
 
export const DEFAULT_HIGH_SPEED = 1;
export const MIN_HIGH_SPEED = 0.5;
export const MAX_HIGH_SPEED = 10;
export const HIGH_SPEED_STEP = 0.5;
 
export function resolveHighSpeedControlActionFromLaneChannels(
  channels: ReadonlyArray<string>,
): HighSpeedControlAction | undefined {
  let hasOddLane = false;
  let hasEvenLane = false;
 
  for (const channel of channels) {
    const laneIndex = resolvePlayableLaneIndex(channel);
    if (laneIndex === undefined) {
      continue;
    }
    if ((laneIndex & 1) === 0) {
      hasEvenLane = true;
    } else {
      hasOddLane = true;
    }
    if (hasOddLane && hasEvenLane) {
      return undefined;
    }
  }
 
  if (hasEvenLane) {
    return 'increase';
  }
  if (hasOddLane) {
    return 'decrease';
  }
  return undefined;
}
 
export function resolveHighSpeedMultiplier(value: number | undefined): number {
  if (!Number.isFinite(value) || (value ?? 0) <= 0) {
    return DEFAULT_HIGH_SPEED;
  }
  const clamped = Math.min(MAX_HIGH_SPEED, Math.max(MIN_HIGH_SPEED, Number(value)));
  return Math.round(clamped / HIGH_SPEED_STEP) * HIGH_SPEED_STEP;
}
 
export function applyHighSpeedControlAction(current: number, action: HighSpeedControlAction): number {
  const safeCurrent = Number.isFinite(current) && current > 0 ? current : DEFAULT_HIGH_SPEED;
  const delta = action === 'increase' ? HIGH_SPEED_STEP : -HIGH_SPEED_STEP;
  const next = Math.min(MAX_HIGH_SPEED, Math.max(MIN_HIGH_SPEED, safeCurrent + delta));
  return resolveHighSpeedMultiplier(next);
}
 
function resolvePlayableLaneIndex(channel: string): number | undefined {
  if (channel.length === 2) {
    const sideCode = channel.charCodeAt(0);
    if (sideCode === 0x31 || sideCode === 0x32) {
      const laneCode = channel.charCodeAt(1);
      if (laneCode >= 0x31 && laneCode <= 0x39) {
        return laneCode - 0x30;
      }
      const uppercaseLaneCode = laneCode & 0xdf;
      Eif (uppercaseLaneCode >= 0x41 && uppercaseLaneCode <= 0x5a) {
        return uppercaseLaneCode - 0x41 + 10;
      }
      return undefined;
    }
  }
 
  const normalized = channel.trim().toUpperCase();
  if (normalized.length !== 2) {
    return undefined;
  }
  const side = normalized[0];
  if (side !== '1' && side !== '2') {
    return undefined;
  }
 
  const lane = normalized[1];
  const code = lane.charCodeAt(0);
  Iif (code >= 0x31 && code <= 0x39) {
    return code - 0x30;
  }
  Eif (code >= 0x41 && code <= 0x5a) {
    return code - 0x41 + 10;
  }
  return undefined;
}