Added support for different chess piece styles

This commit is contained in:
2026-11-06 23:06:34 +03:30
parent 9921e64598
commit 43a95b7afb
312 changed files with 868 additions and 15 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "chess-client",
"version": "1.7.0",
"version": "1.8.0",
"private": true,
"type": "module",
"scripts": {
+78 -1
View File
@@ -5,7 +5,7 @@
import "./style.css";
import { connectHost, type InitMsg } from "@3miz/game-client/iframe";
import { getPieceSvg, type PieceType, type PieceColor } from "./pieces";
import { getPieceSvg, setPieceTheme, getPieceTheme, AVAILABLE_THEMES, type PieceType, type PieceColor } from "./pieces";
import { VfxEngine } from "./vfx";
import { getLegalMoves, type CellInfo } from "./rules-hint";
@@ -47,12 +47,27 @@ let legalTargets: { target: string; isCapture: boolean }[] = [];
let pendingPromo: { from: string; to: string } | null = null;
let lastMove: { from: string; to: string } | null = null;
let confettiTriggered = false;
let isThemeModalOpen = false;
// Load saved piece theme from localStorage if present
try {
const savedTheme = localStorage.getItem("chess_piece_theme");
if (savedTheme) {
setPieceTheme(savedTheme);
}
} catch {
// Ignore in iframe restricted environments
}
const L = (fa: string, en: string) => (init?.locale === "en" ? en : fa);
const host = connectHost({
onInit(m) {
init = m;
// Check if table option piece_theme was configured
if (m.options && typeof m.options.piece_theme === "string") {
setPieceTheme(m.options.piece_theme);
}
document.documentElement.lang = m.locale;
document.documentElement.dir = m.dir;
if (!document.getElementById("board-grid")) {
@@ -441,6 +456,10 @@ function renderPlayerBar(barId: string, player: PlayerSnapshot | undefined, isAc
? `<div class="chess-clock ${isActive ? "active" : ""} ${isLowTime ? "low-time" : ""}" data-seat="${player.seat}">${formatTime(remainingMs)}</div>`
: "";
const themeBtnHtml = isMe
? `<button class="theme-settings-btn" id="open-theme-btn" title="${L("انتخاب طرح و استایل مهره‌ها", "Select Piece Theme")}">🎨</button>`
: "";
el.innerHTML = `
<div class="player-profile">
<div class="player-badge ${player.color}">
@@ -457,8 +476,16 @@ function renderPlayerBar(barId: string, player: PlayerSnapshot | undefined, isAc
${advText}
</div>
${clockHtml}
${themeBtnHtml}
</div>
`;
if (isMe) {
el.querySelector("#open-theme-btn")?.addEventListener("click", () => {
isThemeModalOpen = true;
renderModalContainer();
});
}
}
/**
@@ -556,6 +583,56 @@ function renderModalContainer() {
return;
}
if (isThemeModalOpen) {
const currentTheme = getPieceTheme();
container.innerHTML = `
<div class="modal-backdrop">
<div class="theme-modal-card">
<div class="theme-modal-header">
<div class="theme-modal-title">🎨 ${L("انتخاب طرح و استایل مهره‌ها", "Select Piece Theme")}</div>
<button class="theme-modal-close" id="close-theme-modal">✕</button>
</div>
<div class="theme-grid">
${AVAILABLE_THEMES.map((theme) => {
const isSelected = theme.id === currentTheme;
const wKingSvg = getPieceSvg("k", "w", theme.id);
const bKnightSvg = getPieceSvg("n", "b", theme.id);
return `
<div class="theme-option-card ${isSelected ? "selected" : ""}" data-theme-id="${theme.id}">
<div class="theme-preview-pieces">
<div class="theme-mini-piece">${wKingSvg}</div>
<div class="theme-mini-piece">${bKnightSvg}</div>
</div>
<div class="theme-name">${init?.locale === "en" ? theme.name : theme.nameFa}</div>
</div>
`;
}).join("")}
</div>
</div>
</div>
`;
container.querySelector("#close-theme-modal")?.addEventListener("click", () => {
isThemeModalOpen = false;
renderModalContainer();
});
container.querySelectorAll("[data-theme-id]").forEach((card) => {
card.addEventListener("click", () => {
const tid = (card as HTMLElement).dataset.themeId;
if (tid) {
setPieceTheme(tid);
try {
localStorage.setItem("chess_piece_theme", tid);
} catch {}
isThemeModalOpen = false;
updateUI();
}
});
});
return;
}
if (S.phase === "over") {
const isWinner = S.winner !== null && S.winner === S.me;
const isDraw = S.winner === null;
+67 -6
View File
@@ -239,6 +239,55 @@ function buildKingSvg(isWhite: boolean): string {
</svg>`;
}
import { THEME_SVGS } from "./themes-data";
export interface ThemeInfo {
id: string;
name: string;
nameFa: string;
}
export const AVAILABLE_THEMES: ThemeInfo[] = [
{ id: "imperial", name: "Imperial 3miz", nameFa: "امپریال سه‌میز (پیش‌فرض)" },
{ id: "cburnett", name: "cburnett (Lichess)", nameFa: "سی‌برنت (استاندارد لیچس)" },
{ id: "merida", name: "Merida", nameFa: "مریدا (Merida)" },
{ id: "staunty", name: "Staunty", nameFa: "استاونتی (Staunty)" },
{ id: "alpha", name: "Alpha", nameFa: "آلفا (Alpha)" },
{ id: "california", name: "California", nameFa: "کالیفرنیا (California)" },
{ id: "cardinal", name: "Cardinal", nameFa: "کاردینال (Cardinal)" },
{ id: "dubrovny", name: "Dubrovny", nameFa: "دوبروفنی (Dubrovny)" },
{ id: "governor", name: "Governor", nameFa: "گاورنر (Governor)" },
{ id: "maestro", name: "Maestro", nameFa: "ماسترو (Maestro)" },
{ id: "fresca", name: "Fresca", nameFa: "فرسکا (Fresca)" },
{ id: "kosal", name: "Kosal", nameFa: "کوسال (Kosal)" },
{ id: "leipzig", name: "Leipzig", nameFa: "لایپزیگ (Leipzig)" },
{ id: "chess7", name: "Chess7", nameFa: "چس سون (Chess7)" },
{ id: "chessnut", name: "Chessnut", nameFa: "چس‌نات (Chessnut)" },
{ id: "companion", name: "Companion", nameFa: "کامپنیون (Companion)" },
{ id: "fantasy", name: "Fantasy", nameFa: "فانتزی (Fantasy)" },
{ id: "anarcandy", name: "Anarcandy", nameFa: "آنارکندی (شکلاتی)" },
{ id: "pixel", name: "Pixel", nameFa: "پیکسل (Pixel)" },
{ id: "papercut", name: "Papercut", nameFa: "پیپرکات (Papercut)" },
{ id: "spatial", name: "Spatial", nameFa: "اسپشیال ۳بعدی (Spatial)" },
{ id: "tatiana", name: "Tatiana", nameFa: "تاتیانا (Tatiana)" },
{ id: "kiwen-suwi", name: "Kiwen Suwi", nameFa: "کیون سوی (Kiwen Suwi)" },
{ id: "riohacha", name: "Riohacha", nameFa: "ریوهاچا (Riohacha)" },
{ id: "letter", name: "Letter", nameFa: "حروفی (Letter)" },
{ id: "xkcd", name: "xkcd", nameFa: "کمیک (xkcd)" },
];
let currentPieceTheme = "imperial";
export function setPieceTheme(themeId: string) {
if (themeId === "imperial" || THEME_SVGS[themeId]) {
currentPieceTheme = themeId;
}
}
export function getPieceTheme(): string {
return currentPieceTheme;
}
const PIECE_BUILDERS: Record<PieceType, (isWhite: boolean) => string> = {
p: buildPawnSvg,
n: buildKnightSvg,
@@ -251,14 +300,26 @@ const PIECE_BUILDERS: Record<PieceType, (isWhite: boolean) => string> = {
// Cached SVG strings for instant performance
const PIECE_CACHE: Record<string, string> = {};
export function getPieceSvg(type: PieceType | null, color: PieceColor | null): string {
export function getPieceSvg(type: PieceType | null, color: PieceColor | null, theme: string = currentPieceTheme): string {
if (!type || !color) return "";
const key = `${type}-${color}`;
if (!PIECE_CACHE[key]) {
const builder = PIECE_BUILDERS[type];
if (builder) {
PIECE_CACHE[key] = builder(color === "w");
const key = `${theme}-${type}-${color}`;
if (PIECE_CACHE[key]) return PIECE_CACHE[key];
if (theme !== "imperial" && THEME_SVGS[theme]) {
const pieceKey = `${color}${type.toUpperCase()}`; // e.g. "wK", "bP"
const rawSvg = THEME_SVGS[theme][pieceKey];
if (rawSvg) {
// Ensure SVG has proper class and attributes
let svg = rawSvg.replace("<svg ", `<svg class="piece-svg piece-${color}" `);
PIECE_CACHE[key] = svg;
return svg;
}
}
// Fallback to Imperial theme
const builder = PIECE_BUILDERS[type];
if (builder) {
PIECE_CACHE[key] = builder(color === "w");
}
return PIECE_CACHE[key] ?? "";
}
+132
View File
@@ -715,3 +715,135 @@ html, body {
0% { transform: translate(-50%, -50%) scale(0.4); opacity: 0; }
100% { transform: translate(-50%, -50%) scale(1); opacity: 1; }
}
/* ==========================================================================
Piece Theme Settings & Modal
========================================================================== */
.theme-settings-btn {
background: rgba(30, 32, 41, 0.7);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 6px;
color: var(--text-muted);
cursor: pointer;
padding: 4px 8px;
display: flex;
align-items: center;
justify-content: center;
font-size: 15px;
transition: all 0.2s ease;
}
.theme-settings-btn:hover {
background: rgba(51, 65, 85, 0.8);
color: var(--gold-accent);
border-color: rgba(245, 158, 11, 0.4);
transform: scale(1.05);
}
.theme-modal-card {
background: var(--bg-card);
border: 1px solid rgba(255, 255, 255, 0.12);
border-radius: 14px;
padding: 20px 24px;
display: flex;
flex-direction: column;
gap: 16px;
box-shadow: 0 16px 40px rgba(0, 0, 0, 0.8);
width: min(92vw, 480px);
max-height: 82vh;
animation: modal-spring 0.26s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.theme-modal-header {
display: flex;
align-items: center;
justify-content: space-between;
}
.theme-modal-title {
font-size: 16px;
font-weight: 700;
color: var(--gold-light);
display: flex;
align-items: center;
gap: 8px;
}
.theme-modal-close {
background: transparent;
border: none;
color: var(--text-muted);
cursor: pointer;
font-size: 18px;
padding: 4px 8px;
border-radius: 4px;
transition: color 0.15s ease;
}
.theme-modal-close:hover {
color: #f8fafc;
}
.theme-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(130px, 1fr));
gap: 10px;
overflow-y: auto;
max-height: 55vh;
padding-inline-end: 4px;
}
.theme-option-card {
background: rgba(20, 22, 30, 0.85);
border: 1.5px solid rgba(255, 255, 255, 0.08);
border-radius: 10px;
padding: 10px 8px;
display: flex;
flex-direction: column;
align-items: center;
gap: 8px;
cursor: pointer;
transition: all 0.2s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.theme-option-card:hover {
border-color: rgba(245, 158, 11, 0.5);
background: rgba(30, 34, 48, 0.95);
transform: translateY(-2px);
}
.theme-option-card.selected {
border-color: var(--gold-accent);
background: rgba(245, 158, 11, 0.12);
box-shadow: 0 0 12px rgba(245, 158, 11, 0.3);
}
.theme-preview-pieces {
display: flex;
align-items: center;
justify-content: center;
gap: 6px;
width: 100%;
height: 38px;
}
.theme-mini-piece {
width: 32px;
height: 32px;
display: flex;
align-items: center;
justify-content: center;
}
.theme-name {
font-size: 12px;
font-weight: 700;
color: var(--text-main);
text-align: center;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
width: 100%;
}
File diff suppressed because one or more lines are too long