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
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "chess-client",
"version": "1.6.0",
"version": "1.7.0",
"private": true,
"type": "module",
"scripts": {
+72 -7
View File
@@ -29,12 +29,18 @@ interface GameSnapshot {
players: PlayerSnapshot[];
moves_played: number;
captures: number;
clocks?: [number, number];
turn_start_ms?: number;
time_control?: string;
increment_ms?: number;
}
let S: GameSnapshot | null = null;
let prevS: GameSnapshot | null = null;
let init: InitMsg | null = null;
let vfx: VfxEngine | null = null;
let clockInterval: number | null = null;
let snapReceivedAt = Date.now();
let selectedSquare = "";
let legalTargets: { target: string; isCapture: boolean }[] = [];
@@ -61,6 +67,11 @@ const host = connectHost({
}
prevS = S;
S = env.body as GameSnapshot;
snapReceivedAt = Date.now();
if (!clockInterval) {
clockInterval = window.setInterval(updateClocks, 200);
}
if (S.turn !== S.me) {
selectedSquare = "";
@@ -368,8 +379,43 @@ function updateUI() {
renderModalContainer();
}
function formatTime(ms: number): string {
const totalSeconds = Math.max(0, Math.floor(ms / 1000));
const m = Math.floor(totalSeconds / 60);
const s = totalSeconds % 60;
return `${m}:${s < 10 ? "0" : ""}${s}`;
}
function getRemainingTime(seat: number): number {
if (!S || !S.clocks) return 0;
const base = S.clocks[seat] ?? 0;
if (S.phase === "play" && S.turn === seat) {
const elapsed = Date.now() - snapReceivedAt;
return Math.max(0, base - elapsed);
}
return base;
}
function updateClocks() {
if (!S || !S.clocks || S.phase !== "play") return;
for (const barId of ["top-bar", "bottom-bar"]) {
const barEl = document.getElementById(barId);
if (!barEl) continue;
const clockEl = barEl.querySelector(".chess-clock") as HTMLElement | null;
if (!clockEl || !clockEl.dataset.seat) continue;
const seat = Number(clockEl.dataset.seat);
const ms = getRemainingTime(seat);
clockEl.textContent = formatTime(ms);
if (ms <= 30000 && ms > 0) {
clockEl.classList.add("low-time");
} else {
clockEl.classList.remove("low-time");
}
}
}
/**
* Render player profile, active turn indicator, captured pieces, and score
* Render player profile, active turn indicator, captured pieces, clock, and score
*/
function renderPlayerBar(barId: string, player: PlayerSnapshot | undefined, isActive: boolean, isMe: boolean) {
const el = document.getElementById(barId);
@@ -388,6 +434,13 @@ function renderPlayerBar(barId: string, player: PlayerSnapshot | undefined, isAc
? (S.winner === player.seat ? L("برنده 🏆", "Winner 🏆") : (S.winner === null ? L("تساوی 🤝", "Draw 🤝") : ""))
: (isActive ? (isMe ? L("نوبت شماست", "Your turn") : L("در حال تفکر…", "Thinking…")) : "");
// Chess clock
const remainingMs = getRemainingTime(player.seat);
const isLowTime = remainingMs <= 30000 && remainingMs > 0 && S.phase === "play";
const clockHtml = S.clocks
? `<div class="chess-clock ${isActive ? "active" : ""} ${isLowTime ? "low-time" : ""}" data-seat="${player.seat}">${formatTime(remainingMs)}</div>`
: "";
el.innerHTML = `
<div class="player-profile">
<div class="player-badge ${player.color}">
@@ -398,9 +451,12 @@ function renderPlayerBar(barId: string, player: PlayerSnapshot | undefined, isAc
<div class="player-status">${statusLabel}</div>
</div>
</div>
<div class="captured-tray">
${capturedPieces.map((p) => `<div class="mini-piece">${getPieceSvg(p.type as PieceType, p.color as PieceColor)}</div>`).join("")}
${advText}
<div class="player-clock-section">
<div class="captured-tray">
${capturedPieces.map((p) => `<div class="mini-piece">${getPieceSvg(p.type as PieceType, p.color as PieceColor)}</div>`).join("")}
${advText}
</div>
${clockHtml}
</div>
`;
}
@@ -513,6 +569,9 @@ function renderModalContainer() {
} else if (S.result_reason === "insufficient_material") {
title = L("تساوی — عدم کفایت مهره‌ها", "Draw — Insufficient Material");
subtitle = L("مهره کافی برای مات کردن در صفحه وجود ندارد", "Neither side has sufficient pieces to force checkmate");
} else if (S.result_reason === "timeout_draw") {
title = L("تساوی — پایان زمان و عدم کفایت مهره", "Draw — Timeout vs Insufficient Material");
subtitle = L("زمان حریف تمام شد اما مهره کافی برای مات کردن ندارید", "Opponent flagged but you lack mating material");
} else {
title = L("بازی مساوی شد", "Draw!");
subtitle = L("هیچ‌کدام از طرفین برنده نشد", "No side achieved victory");
@@ -520,9 +579,15 @@ function renderModalContainer() {
} else {
const winnerName = S.players[S.winner!]?.name;
title = `${winnerName} ${L("برنده شد! 🏆", "Wins! 🏆")}`;
subtitle = S.result_reason === "checkmate"
? L("با کیش و مات حریف", "By checkmate")
: (S.result_reason === "abandoned" ? L("به دلیل ترک حریف", "By abandonment") : "");
if (S.result_reason === "checkmate") {
subtitle = L("با کیش و مات حریف", "By checkmate");
} else if (S.result_reason === "timeout") {
subtitle = L("با اتمام زمان حریف", "On time");
} else if (S.result_reason === "abandoned") {
subtitle = L("به دلیل ترک حریف", "By abandonment");
} else {
subtitle = "";
}
}
container.innerHTML = `
+45
View File
@@ -158,6 +158,51 @@ html, body {
margin-inline-start: 4px;
}
/* ==========================================================================
Chess Clock
========================================================================== */
.player-clock-section {
display: flex;
align-items: center;
gap: 8px;
}
.chess-clock {
font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
font-size: 14px;
font-weight: 800;
padding: 4px 10px;
border-radius: 6px;
background: rgba(15, 16, 22, 0.85);
color: #94a3b8;
border: 1px solid rgba(255, 255, 255, 0.1);
letter-spacing: 0.5px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.5);
transition: all 0.2s ease;
min-width: 58px;
text-align: center;
}
.chess-clock.active {
background: rgba(30, 41, 59, 0.95);
color: #f8fafc;
border-color: rgba(245, 158, 11, 0.6);
box-shadow: 0 0 10px rgba(245, 158, 11, 0.25), inset 0 1px 2px rgba(0, 0, 0, 0.3);
}
.chess-clock.low-time {
background: rgba(127, 29, 29, 0.85);
color: #fca5a5;
border-color: rgba(239, 68, 68, 0.8);
animation: clock-pulse 1s infinite alternate;
}
@keyframes clock-pulse {
0% { transform: scale(1); box-shadow: 0 0 4px rgba(239, 68, 68, 0.4); }
100% { transform: scale(1.04); box-shadow: 0 0 12px rgba(239, 68, 68, 0.8); }
}
/* ==========================================================================
Chess Board & Cells
========================================================================== */