all repos — emchess @ 6b60c743e0c7b7cfb599852d110f27b229626bcb

src/game/clock.ts (view raw)

 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
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
import type { Color } from "./state";

export interface TimeControl {
  id: string;
  label: string;
  base: number; // seconds; 0 means no clock
  inc: number; // seconds added after each move
}

export const TIME_CONTROLS: TimeControl[] = [
  { id: "none", label: "no clock", base: 0, inc: 0 },
  { id: "1+0", label: "1 + 0   bullet", base: 60, inc: 0 },
  { id: "3+2", label: "3 + 2   blitz", base: 180, inc: 2 },
  { id: "5+0", label: "5 + 0   blitz", base: 300, inc: 0 },
  { id: "10+0", label: "10 + 0   rapid", base: 600, inc: 0 },
  { id: "15+10", label: "15 + 10   rapid", base: 900, inc: 10 },
  { id: "30+0", label: "30 + 0   classical", base: 1800, inc: 0 },
];

export function timeControl(id: string, custom?: { base: number; inc: number }): TimeControl {
  if (id === "custom" && custom) {
    return { id: "custom", label: "custom", base: custom.base, inc: custom.inc };
  }
  return TIME_CONTROLS.find((t) => t.id === id) ?? TIME_CONTROLS[0];
}

export class Clock {
  private remaining: Record<Color, number>;
  private incMs: number;
  private turn: Color | null = null;
  private last = 0;
  private timer: ReturnType<typeof setInterval> | undefined;
  flagged: Color | null = null;

  constructor(
    private tc: TimeControl,
    private onTick: (w: number, b: number) => void,
    private onFlag: (side: Color) => void
  ) {
    this.remaining = { w: tc.base * 1000, b: tc.base * 1000 };
    this.incMs = tc.inc * 1000;
  }

  get enabled() {
    return this.tc.base > 0;
  }

  // The initial time in ms, so the UI can render how much of the clock is left.
  get baseMs() {
    return this.tc.base * 1000;
  }

  start(side: Color) {
    if (!this.enabled) return;
    this.turn = side;
    this.last = performance.now();
    this.run();
  }

  // The side that just moved gets the increment; the other side's clock starts running.
  press(mover: Color) {
    if (!this.enabled || this.flagged) return;
    this.remaining[mover] += this.incMs;
    this.turn = mover === "w" ? "b" : "w";
    this.last = performance.now();
    this.onTick(this.remaining.w, this.remaining.b);
    this.run();
  }

  pause() {
    this.stop();
    this.turn = null;
  }

  stop() {
    if (this.timer) {
      clearInterval(this.timer);
      this.timer = undefined;
    }
  }

  private run() {
    if (!this.timer) this.timer = setInterval(this.tick, 100);
  }

  private tick = () => {
    if (!this.turn || this.flagged) return;
    const now = performance.now();
    this.remaining[this.turn] -= now - this.last;
    this.last = now;
    if (this.remaining[this.turn] <= 0) {
      this.remaining[this.turn] = 0;
      this.flagged = this.turn;
      this.turn = null;
      this.stop();
      this.onTick(this.remaining.w, this.remaining.b);
      this.onFlag(this.flagged);
      return;
    }
    this.onTick(this.remaining.w, this.remaining.b);
  };
}

export function formatTime(ms: number): string {
  if (ms < 0) ms = 0;
  if (ms < 10000) return (Math.floor(ms / 100) / 10).toFixed(1);
  const s = Math.ceil(ms / 1000);
  const m = Math.floor(s / 60);
  return m + ":" + String(s % 60).padStart(2, "0");
}