Initial Commit
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* 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 = `<div class="status">${L("در حال بارگذاری…", "Loading…")}</div>`;
|
||||
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<string, any> = {};
|
||||
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 ? `<span class="piece ${cell.color}">${pieceChar(cell.type, cell.color)}</span>` : "";
|
||||
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 `<button class="cell ${shade}${isSelected ? " selected" : ""}" data-from="${sq}" data-color="${cell.color || "null"}" title="${sq}" ${enabled ? "" : "disabled"}>${piece}</button>`;
|
||||
}).join("")).join("");
|
||||
|
||||
const selHint = selectedSquare
|
||||
? `<div class="hint">${L("مبدا", "From")}: ${selectedSquare} — ${L("یک خانه مقصد را بزنید (برای لغو، دوباره همان خانه)", "click a target square (click it again to cancel)")}</div>`
|
||||
: ``;
|
||||
|
||||
const promoPicker = pendingPromo
|
||||
? `<div class="promo"><span>${pendingPromo.from} → ${pendingPromo.to}: ${L("ارتقا به", "Promote to")}</span>
|
||||
${["q", "r", "b", "n"].map((t) => `<button class="promo-btn" data-promo="${t}" title="${t}">${pieceChar(t, myColor)}</button>`).join("")}
|
||||
<button class="promo-cancel" data-cancel-promo="1">${L("لغو", "Cancel")}</button></div>`
|
||||
: ``;
|
||||
|
||||
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 = `
|
||||
<div class="status">${statusText}</div>
|
||||
<div class="info">${S.moves_played !== undefined ? `حرکتها: ${S.moves_played}` : ""}</div>
|
||||
<div class="board">${boardHtml}</div>
|
||||
${selHint}
|
||||
${promoPicker}
|
||||
<div class="legend">${L("سفید حرکت میکند", "White moves")}</div>
|
||||
`;
|
||||
}
|
||||
|
||||
function pieceChar(type: string | null, color: string | null): string {
|
||||
if (!type) return "";
|
||||
// White uses outline glyphs, black uses filled glyphs.
|
||||
const white: Record<string, string> = { k: "♔", q: "♕", r: "♖", b: "♗", n: "♘", p: "♙" };
|
||||
const black: Record<string, string> = { 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();
|
||||
@@ -0,0 +1,18 @@
|
||||
html, body { margin: 0; height: 100%; background: #1a1a2e; color: #f1f3f5; font-family: system-ui, sans-serif; }
|
||||
#app { height: 100%; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px; padding: 16px; }
|
||||
.status { font-size: 22px; font-weight: 700; }
|
||||
.info { font-size: 14px; opacity: 0.8; }
|
||||
.board { display: grid; grid-template-columns: repeat(8, min(10vmin, 60px)); grid-template-rows: repeat(8, min(10vmin, 60px)); gap: 1px; border: 3px solid #c5a065; border-radius: 4px; overflow: hidden; }
|
||||
.cell { aspect-ratio: 1; font-size: min(6vmin, 36px); font-weight: 800; border: 0; border-radius: 0; color: #fff; cursor: pointer; display: flex; align-items: center; justify-content: center; }
|
||||
/* Square colours are assigned per square in JS (a1 is dark), so orientation flips keep them correct. */
|
||||
.cell.light { background: #d18b47; }
|
||||
.cell.dark { background: #8b5e3c; }
|
||||
.cell:disabled { cursor: default; opacity: 0.85; }
|
||||
.cell.selected { outline: 3px solid #ffd43b; outline-offset: -3px; }
|
||||
.hint { font-size: 14px; font-weight: 600; color: #ffd43b; }
|
||||
.promo { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 700; background: #2b2b45; border-radius: 10px; padding: 8px 12px; }
|
||||
.promo-btn { font-size: 30px; line-height: 1; border: 0; border-radius: 8px; background: #d18b47; color: #1a1a2e; cursor: pointer; padding: 4px 10px; }
|
||||
.promo-cancel { font-size: 13px; border: 0; border-radius: 8px; background: transparent; color: #f1f3f5; cursor: pointer; padding: 6px 8px; }
|
||||
.cell[data-color="w"] { color: #f0f0f0; text-shadow: 1px 1px 2px #333; }
|
||||
.cell[data-color="b"] { color: #222; text-shadow: 1px 1px 2px #ddd; }
|
||||
.legend { font-size: 12px; opacity: 0.7; }
|
||||
Reference in New Issue
Block a user