/** * Chess client — runs in sandboxed iframe. */ import "./style.css"; import { connectHost, type InitMsg } from "@3miz/game-client/iframe"; let S: any = null; let init: InitMsg | null = null; const L = (fa: string, en: string) => (init?.locale === "en" ? en : fa); const host = connectHost({ onInit(m) { init = m; document.documentElement.lang = m.locale; document.documentElement.dir = m.dir; render(); }, onState(env) { if (env.t === "snap") { S = env.body; // Drop any half-finished local selection once the turn moves on (e.g. timeout auto-move). if (S.turn !== S.me) { selectedSquare = ""; pendingPromo = null; } render(); } }, onAck(a) { if (a.status === "rejected") host.toast("حرکت پذیرفته نشد", "Move not accepted", "warn"); }, }); function render() { const app = document.getElementById("app")!; if (!S) { app.innerHTML = `
${L("در حال بارگذاری…", "Loading…")}
`; return; } const me = S.me; const myColor = me !== null && me !== undefined ? S.players[me]?.color : null; // Orientation: your own back rank is always at the bottom. // White (and observers) see rank 8 at the top, files a→h; Black sees rank 1 at the top, files h→a. const flipped = myColor === "b"; const bySquare: Record = {}; for (const cell of S.board) bySquare[cell.square] = cell; const files = flipped ? [7, 6, 5, 4, 3, 2, 1, 0] : [0, 1, 2, 3, 4, 5, 6, 7]; const ranks = flipped ? [0, 1, 2, 3, 4, 5, 6, 7] : [7, 6, 5, 4, 3, 2, 1, 0]; const isMyTurn = S.phase === "play" && S.turn === me; const boardHtml = ranks.map((r) => files.map((f) => { const sq = String.fromCharCode(97 + f) + (r + 1); const cell = bySquare[sq] || { square: sq, type: null, color: null }; const piece = cell.color ? `${pieceChar(cell.type, cell.color)}` : ""; const selectable = isMyTurn && !!myColor && cell.color === myColor; const isSelected = selectedSquare === sq; const enabled = !selectedSquare ? selectable : isMyTurn; // Standard colouring: a1 is dark; shade = parity of (file + rank). const shade = (f + r) % 2 === 0 ? "dark" : "light"; return ``; }).join("")).join(""); const selHint = selectedSquare ? `
${L("مبدا", "From")}: ${selectedSquare} — ${L("یک خانه مقصد را بزنید (برای لغو، دوباره همان خانه)", "click a target square (click it again to cancel)")}
` : ``; const promoPicker = pendingPromo ? `
${pendingPromo.from} → ${pendingPromo.to}: ${L("ارتقا به", "Promote to")} ${["q", "r", "b", "n"].map((t) => ``).join("")}
` : ``; const statusText = S.phase === "over" ? (S.result_reason === "abandoned" ? L("بازی رها شد", "Abandoned") : S.winner === null ? (S.result_reason === "stalemate" ? L("پات — مساوی!", "Stalemate — draw!") : L("مساوی!", "Draw!")) : (S.result_reason === "checkmate" ? `${S.players[S.winner]?.name} ${L("مات کرد! 🏆", "wins by checkmate 🏆")}` : `${S.players[S.winner]?.name} ${L("برنده شد 🏆", "wins 🏆")}`)) : ((S.turn === me ? L("نوبت شماست", "Your turn") : `${L("نوبت", "Turn")}: ${S.players[S.turn]?.name}`) + (S.check ? ` · ${L("کیش!", "Check!")}` : "")); app.innerHTML = `
${statusText}
${S.moves_played !== undefined ? `حرکت‌ها: ${S.moves_played}` : ""}
${boardHtml}
${selHint} ${promoPicker}
${L("سفید حرکت می‌کند", "White moves")}
`; } function pieceChar(type: string | null, color: string | null): string { if (!type) return ""; // White uses outline glyphs, black uses filled glyphs. const white: Record = { k: "♔", q: "♕", r: "♖", b: "♗", n: "♘", p: "♙" }; const black: Record = { k: "♚", q: "♛", r: "♜", b: "♝", n: "♞", p: "♟" }; return color === "b" ? (black[type] ?? "") : (white[type] ?? ""); } let selectedSquare = ""; let pendingPromo: { from: string; to: string } | null = null; document.addEventListener("click", (e) => { const el = e.target as HTMLElement; // Promotion choice buttons. const promoBtn = el.closest("[data-promo]") as HTMLElement | null; if (promoBtn && pendingPromo) { host.cmd("move", { from: pendingPromo.from, to: pendingPromo.to, promotion: promoBtn.dataset.promo }); pendingPromo = null; selectedSquare = ""; render(); return; } if (el.closest("[data-cancel-promo]")) { pendingPromo = null; render(); return; } const b = el.closest("[data-from]") as HTMLElement | null; if (!b) return; const sq = b.dataset.from!; if (pendingPromo) return; // choose a promotion piece (or cancel) first if (selectedSquare) { if (selectedSquare === sq) { selectedSquare = ""; render(); return; } // Clicking another of my own pieces switches the selection instead of sending a bad move. const clicked = (S?.board || []).find((c: any) => c.square === sq); const myColor = S?.me !== null && S?.me !== undefined ? S?.players[S.me]?.color : null; if (clicked?.color && myColor && clicked.color === myColor) { selectedSquare = sq; render(); return; } // Pawn stepping onto the last rank must promote: ask which piece first. const moving = (S?.board || []).find((c: any) => c.square === selectedSquare); const lastRank = myColor === "b" ? "1" : "8"; if (moving?.type === "p" && sq[1] === lastRank) { pendingPromo = { from: selectedSquare, to: sq }; selectedSquare = ""; render(); return; } host.cmd("move", { from: selectedSquare, to: sq }); selectedSquare = ""; render(); } else { // Select one of my own pieces when it is my turn. const cellData = (S?.board || []).find((c: any) => c.square === sq); const myColor = S?.me !== null && S?.me !== undefined ? S?.players[S.me]?.color : null; const isMyColor = !!myColor && cellData?.color === myColor; if (isMyColor && S?.phase === "play" && S?.turn === S?.me) { selectedSquare = sq; render(); } } }); render();