Added time control mechanics to the rules

This commit is contained in:
2026-11-06 22:29:07 +03:30
parent 48c0ad1265
commit 9921e64598
11 changed files with 560 additions and 39 deletions
+83 -1
View File
@@ -328,7 +328,9 @@ test("insufficient material: capturing last pawn triggers draw immediately", ()
players: [mk(0, "u0", "w"), mk(1, "u1", "b")],
board: Array(64).fill(null) as State["board"],
turn: 0, phase: "play", winner: null, result_reason: "",
turn_ms: 60000, timer_n: 0, timer_id: null, deadline: null, bot_timer: null, bot_n: 0,
time_control: "3m", initial_time_ms: 180000, increment_ms: 0,
clocks: [180000, 180000], turn_start_ms: 1700000000000,
timer_n: 0, timer_id: null, deadline: null, bot_timer: null, bot_n: 0,
moves_played: 50, captures: 29,
rights: { wk: false, wq: false, bk: false, bq: false }, ep: null,
};
@@ -364,3 +366,83 @@ test("insufficient material: capturing last pawn triggers draw immediately", ()
assert.equal((w.game as State).result_reason, "insufficient_material");
assert.equal(w.status, "finished");
});
test("time controls: bullet 1m, bullet 1m+1s, blitz 3m, blitz 3m+2s, rapid 10m, rapid 15m+10s", () => {
const base = create(false);
// Bullet mode default
const { w: wBullet } = initTable(chess, { ...base, modes: ["bullet"], options: {} }, 1);
const sBullet = wBullet.game as State;
assert.equal(sBullet.initial_time_ms, 60_000);
assert.equal(sBullet.increment_ms, 0);
// Bullet mode with 1m_1s option
const { w: wBulletInc } = initTable(chess, { ...base, modes: ["bullet"], options: { time_control: "1m_1s" } }, 1);
const sBulletInc = wBulletInc.game as State;
assert.equal(sBulletInc.initial_time_ms, 60_000);
assert.equal(sBulletInc.increment_ms, 1_000);
// Blitz mode default 3m
const { w: wBlitz } = initTable(chess, { ...base, modes: ["blitz"], options: {} }, 1);
const sBlitz = wBlitz.game as State;
assert.equal(sBlitz.initial_time_ms, 180_000);
assert.equal(sBlitz.increment_ms, 0);
// Blitz mode with 3m_2s option
const { w: wBlitzInc } = initTable(chess, { ...base, modes: ["blitz"], options: { time_control: "3m_2s" } }, 1);
const sBlitzInc = wBlitzInc.game as State;
assert.equal(sBlitzInc.initial_time_ms, 180_000);
assert.equal(sBlitzInc.increment_ms, 2_000);
// Rapid mode default 10m
const { w: wRapid } = initTable(chess, { ...base, modes: ["rapid"], options: {} }, 1);
const sRapid = wRapid.game as State;
assert.equal(sRapid.initial_time_ms, 600_000);
assert.equal(sRapid.increment_ms, 0);
// Rapid mode with 15m_10s option
const { w: wRapidInc } = initTable(chess, { ...base, modes: ["rapid"], options: { time_control: "15m_10s" } }, 1);
const sRapidInc = wRapidInc.game as State;
assert.equal(sRapidInc.initial_time_ms, 900_000);
assert.equal(sRapidInc.increment_ms, 10_000);
});
test("time controls: clocks deduct elapsed time and add increment on move", () => {
const base = create(false);
const { w } = initTable(chess, { ...base, modes: ["blitz"], options: { time_control: "3m_2s" } }, 1);
const s0 = w.game as State;
const seat = whiteSeatOf(s0);
assert.deepEqual(s0.clocks, [180_000, 180_000]);
// Player thinks for 5 seconds (5000 ms), then moves e2 -> e4
// New clock should be: 180,000 - 5,000 + 2,000 = 177,000
const tMove = base.created_at_ms + 5_000;
const result = applyInput(chess, w, {
v: 1, type: "move", payload: { from: "e2", to: "e4" },
uid: userOf(s0, seat), ci: "c0", cid: 1, role: "player", seat, ts_ms: tMove,
} as Input, 1);
assert.equal(result.status, "accepted");
const s1 = w.game as State;
assert.equal(s1.clocks[seat], 177_000);
assert.equal(s1.clocks[1 - seat], 180_000); // opponent clock hasn't started yet
});
test("time controls: timeout leads to game over", () => {
const base = create(false);
const { w } = initTable(chess, { ...base, modes: ["bullet"], options: { time_control: "1m" } }, 1);
const s0 = w.game as State;
const seat = whiteSeatOf(s0);
const timerId = s0.timer_id!;
assert.ok(timerId);
// Time runs out and $timer fires
applyInput(chess, w, {
v: 1, type: "$timer", payload: { timer_id: timerId, due_ms: base.created_at_ms + 60_000 },
ts_ms: base.created_at_ms + 60_000,
} as Input, 1);
const sEnd = w.game as State;
assert.equal(sEnd.phase, "over");
assert.equal(sEnd.winner, 1 - seat); // opponent wins on time
assert.equal(sEnd.result_reason, "timeout");
});