Added time control mechanics to the rules
This commit is contained in:
+67
-23
@@ -22,7 +22,11 @@ export interface State {
|
||||
phase: "play" | "over";
|
||||
winner: number | null; // winning seat, or null for draw
|
||||
result_reason: string;
|
||||
turn_ms: number;
|
||||
time_control: string;
|
||||
initial_time_ms: number;
|
||||
increment_ms: number;
|
||||
clocks: [number, number]; // remaining time in ms for seat 0 and seat 1
|
||||
turn_start_ms: number; // timestamp when current turn started
|
||||
timer_n: number;
|
||||
timer_id: string | null;
|
||||
deadline: number | null;
|
||||
@@ -353,7 +357,9 @@ function setDeadline(s: State, t: { now: number; setTimer(id: string, due: numbe
|
||||
if (s.timer_id) t.clearTimer?.(s.timer_id);
|
||||
s.timer_n += 1;
|
||||
s.timer_id = `turn-${s.timer_n}`;
|
||||
s.deadline = t.now + s.turn_ms;
|
||||
const remaining = s.clocks[s.turn];
|
||||
s.turn_start_ms = t.now;
|
||||
s.deadline = t.now + remaining;
|
||||
t.setTimer(s.timer_id, s.deadline);
|
||||
}
|
||||
|
||||
@@ -437,7 +443,7 @@ function finish(s: State, ctx: Ctx, winner: number | null, reason: string = "com
|
||||
if (s.bot_timer) ctx.clearTimer(s.bot_timer);
|
||||
s.bot_timer = null;
|
||||
ctx.finish({
|
||||
reason: reason === "abandoned" ? "abandoned" : "completed",
|
||||
reason: reason === "abandoned" ? "abandoned" : (reason === "timeout" ? "timeout" : "completed"),
|
||||
standings: s.players.map((p) => ({
|
||||
user_id: p.user_id,
|
||||
seat: p.seat,
|
||||
@@ -453,13 +459,44 @@ function finish(s: State, ctx: Ctx, winner: number | null, reason: string = "com
|
||||
});
|
||||
}
|
||||
|
||||
const TIME_CONTROLS: Record<string, { initial_ms: number; increment_ms: number }> = {
|
||||
"1m": { initial_ms: 60_000, increment_ms: 0 },
|
||||
"1m_1s": { initial_ms: 60_000, increment_ms: 1_000 },
|
||||
"3m": { initial_ms: 180_000, increment_ms: 0 },
|
||||
"3m_2s": { initial_ms: 180_000, increment_ms: 2_000 },
|
||||
"10m": { initial_ms: 600_000, increment_ms: 0 },
|
||||
"15m_10s": { initial_ms: 900_000, increment_ms: 10_000 },
|
||||
};
|
||||
|
||||
function resolveTimeControl(modes: string[], options: Record<string, unknown>): { tcId: string; initial_ms: number; increment_ms: number } {
|
||||
// If explicitly specified in options
|
||||
if (typeof options.time_control === "string" && TIME_CONTROLS[options.time_control]) {
|
||||
const tcId = options.time_control;
|
||||
return { tcId, ...TIME_CONTROLS[tcId] };
|
||||
}
|
||||
// Otherwise deduce from mode
|
||||
if (modes.includes("bullet")) {
|
||||
return { tcId: "1m", ...TIME_CONTROLS["1m"] };
|
||||
}
|
||||
if (modes.includes("rapid")) {
|
||||
return { tcId: "10m", ...TIME_CONTROLS["10m"] };
|
||||
}
|
||||
// Default is blitz 3m
|
||||
return { tcId: "3m", ...TIME_CONTROLS["3m"] };
|
||||
}
|
||||
|
||||
export const chess: GameDef<State> = {
|
||||
manifest,
|
||||
validateModes: (modes) =>
|
||||
modes.every((m) => m === "classic") ? [] : [{ code: "chess.unknown_mode", path: "/modes", message: "unknown mode" }],
|
||||
validateModes: (modes) => {
|
||||
const valid = ["blitz", "bullet", "rapid", "classic"];
|
||||
return modes.every((m) => valid.includes(m))
|
||||
? []
|
||||
: [{ code: "chess.unknown_mode", path: "/modes", message: "unknown mode" }];
|
||||
},
|
||||
init(create: CreateTable, ictx: InitCtx): State {
|
||||
const seats = [...create.participants].sort((a, b) => a.seat - b.seat);
|
||||
const first = firstSeat(create, ictx.rng, 2);
|
||||
const tc = resolveTimeControl(create.modes, create.options);
|
||||
const s: State = {
|
||||
players: seats.map((p, i) => ({
|
||||
seat: i,
|
||||
@@ -476,7 +513,11 @@ export const chess: GameDef<State> = {
|
||||
phase: "play",
|
||||
winner: null,
|
||||
result_reason: "",
|
||||
turn_ms: typeof create.options.turn_ms === "number" ? (create.options.turn_ms as number) : 60000,
|
||||
time_control: tc.tcId,
|
||||
initial_time_ms: tc.initial_ms,
|
||||
increment_ms: tc.increment_ms,
|
||||
clocks: [tc.initial_ms, tc.initial_ms],
|
||||
turn_start_ms: ictx.now,
|
||||
timer_n: 0,
|
||||
timer_id: null,
|
||||
deadline: null,
|
||||
@@ -545,6 +586,11 @@ export const chess: GameDef<State> = {
|
||||
promo = payload.promotion;
|
||||
}
|
||||
if (!isValidMove(s, fromIdx, toIdx, p.seat)) return ctx.reject("chess.invalid_move");
|
||||
|
||||
// Deduct elapsed time and add increment
|
||||
const elapsed = Math.max(0, ctx.now - s.turn_start_ms);
|
||||
s.clocks[p.seat] = Math.max(0, s.clocks[p.seat] - elapsed) + s.increment_ms;
|
||||
|
||||
applyMove(s, fromIdx, toIdx, p.seat, promo);
|
||||
return postMove(s, ctx, p.seat);
|
||||
}
|
||||
@@ -566,6 +612,10 @@ export const chess: GameDef<State> = {
|
||||
players: s.players.map((p) => ({ seat: p.seat, name: p.name, color: p.color, bot: p.bot, connected: p.conns.length > 0, left: p.left })),
|
||||
moves_played: s.moves_played,
|
||||
captures: s.captures,
|
||||
clocks: s.clocks,
|
||||
turn_start_ms: s.turn_start_ms,
|
||||
time_control: s.time_control,
|
||||
increment_ms: s.increment_ms,
|
||||
};
|
||||
},
|
||||
};
|
||||
@@ -584,6 +634,10 @@ function handleBotMove(s: State, ctx: Ctx) {
|
||||
const seatBefore = s.turn;
|
||||
const botMove = decideBot(s, s.turn, ctx.rng);
|
||||
if (botMove !== null && typeof botMove.fromIdx === "number" && typeof botMove.toIdx === "number") {
|
||||
// Deduct elapsed time and add increment for bot
|
||||
const elapsed = Math.max(0, ctx.now - s.turn_start_ms);
|
||||
s.clocks[seatBefore] = Math.max(0, s.clocks[seatBefore] - elapsed) + s.increment_ms;
|
||||
|
||||
applyMove(s, botMove.fromIdx, botMove.toIdx, seatBefore, botMove.promotion);
|
||||
return postMove(s, ctx, seatBefore);
|
||||
}
|
||||
@@ -596,22 +650,12 @@ function handleBotMove(s: State, ctx: Ctx) {
|
||||
|
||||
function handleTimeout(s: State, ctx: Ctx) {
|
||||
s.timer_id = null;
|
||||
// On timeout, make a simple default move: first legal move found
|
||||
const seat = s.turn;
|
||||
for (let from = 0; from < 64; from++) {
|
||||
const p = s.board[from];
|
||||
if (!p || p.color !== s.players[seat].color) continue;
|
||||
for (let to = 0; to < 64; to++) {
|
||||
if (isValidMove(s, from, to, seat)) {
|
||||
applyMove(s, from, to, seat);
|
||||
return postMove(s, ctx, seat);
|
||||
}
|
||||
}
|
||||
}
|
||||
// No legal moves — checkmate loss if in check, else stalemate draw.
|
||||
if (isKingInCheck(s, s.players[seat].color)) {
|
||||
finish(s, ctx, 1 - seat, "checkmate");
|
||||
} else {
|
||||
finish(s, ctx, null, "stalemate");
|
||||
const timedOutSeat = s.turn;
|
||||
s.clocks[timedOutSeat] = 0;
|
||||
// If the opponent does not have sufficient material to checkmate, it's a draw by timeout vs insufficient material
|
||||
const oppSeat = 1 - timedOutSeat;
|
||||
if (hasInsufficientMaterial(s)) {
|
||||
return finish(s, ctx, null, "timeout_draw");
|
||||
}
|
||||
return finish(s, ctx, oppSeat, "timeout");
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@3miz/chess-server",
|
||||
"version": "1.6.0",
|
||||
"version": "1.7.0",
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": { "start": "tsx main.ts", "test": "node --test --import tsx ../tests/*.test.ts" },
|
||||
|
||||
Reference in New Issue
Block a user