feat(games): implement chess game plugin with full UI
Some checks failed
Deploy to Production / test (push) Failing after 32s
Add chess as the first game plugin using the existing multiplayer framework. Server-side game logic uses chess.js with server-authoritative clock management. Client uses react-chessboard v5 with cburnett piece set, drag-and-drop + click-to-move, configurable time controls (bullet/blitz/rapid/classical/none), draw offers, resignation, and timeout detection. Extends the game framework with room creation options to support per-game configuration. Includes 57 tests covering all code paths. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@@ -117,7 +117,7 @@ export class GameServer {
|
||||
|
||||
switch (msg.type) {
|
||||
case "CREATE_ROOM": {
|
||||
const result = this.roomManager.createRoom(msg.gameType, discordId);
|
||||
const result = this.roomManager.createRoom(msg.gameType, discordId, msg.options);
|
||||
if (!result.ok) {
|
||||
ws.send(JSON.stringify({ type: "ERROR", message: result.error }));
|
||||
return;
|
||||
|
||||
@@ -33,7 +33,7 @@ export class RoomManager {
|
||||
private cleanupTimers = new Map<string, Timer>();
|
||||
readonly emitter = mitt<RoomEvents>();
|
||||
|
||||
createRoom(gameSlug: string, hostId: string): CreateResult {
|
||||
createRoom(gameSlug: string, hostId: string, options?: Record<string, unknown>): CreateResult {
|
||||
const plugin = gameRegistry.get(gameSlug);
|
||||
if (!plugin) return { ok: false, error: `Unknown game type: ${gameSlug}` };
|
||||
|
||||
@@ -47,6 +47,7 @@ export class RoomManager {
|
||||
state: null,
|
||||
status: "waiting",
|
||||
createdAt: Date.now(),
|
||||
options,
|
||||
};
|
||||
|
||||
this.rooms.set(id, room);
|
||||
@@ -85,7 +86,7 @@ export class RoomManager {
|
||||
room.players.push(playerId);
|
||||
|
||||
if (room.players.length >= plugin.maxPlayers) {
|
||||
room.state = plugin.createInitialState(room.players);
|
||||
room.state = plugin.createInitialState(room.players, room.options);
|
||||
room.status = "playing";
|
||||
this.scheduleCleanup(roomId, ROOM_CONFIG.PLAYING_MAX_MS);
|
||||
|
||||
@@ -185,7 +186,7 @@ export class RoomManager {
|
||||
room.players.push(adminId);
|
||||
}
|
||||
|
||||
room.state = plugin.createInitialState(room.players);
|
||||
room.state = plugin.createInitialState(room.players, room.options);
|
||||
room.status = "playing";
|
||||
this.scheduleCleanup(roomId, ROOM_CONFIG.PLAYING_MAX_MS);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ export interface Room {
|
||||
state: unknown;
|
||||
status: "waiting" | "playing" | "finished";
|
||||
createdAt: number;
|
||||
options?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface RoomSummary {
|
||||
@@ -28,7 +29,7 @@ export interface PlayerInfo {
|
||||
}
|
||||
|
||||
export const GameWsClientSchema = z.discriminatedUnion("type", [
|
||||
z.object({ type: z.literal("CREATE_ROOM"), gameType: z.string() }),
|
||||
z.object({ type: z.literal("CREATE_ROOM"), gameType: z.string(), options: z.looseObject({}).optional() }),
|
||||
z.object({
|
||||
type: z.literal("JOIN_ROOM"),
|
||||
roomId: z.string(),
|
||||
|
||||
@@ -18,6 +18,11 @@ import type { WsConnectionData } from "./games/GameServer";
|
||||
import { getSession } from "./routes/auth.routes";
|
||||
import { GameWsClientSchema } from "./games/types";
|
||||
|
||||
// Register game plugins
|
||||
import { gameRegistry } from "@shared/games/registry";
|
||||
import { chessPlugin } from "@shared/games/chess/chess.plugin";
|
||||
gameRegistry.register(chessPlugin);
|
||||
|
||||
const WS_CONFIG = {
|
||||
MAX_CONNECTIONS: 200,
|
||||
MAX_PAYLOAD_BYTES: 16384,
|
||||
|
||||
15
bun.lock
@@ -6,6 +6,7 @@
|
||||
"name": "app",
|
||||
"dependencies": {
|
||||
"@napi-rs/canvas": "^0.1.89",
|
||||
"chess.js": "^1.4.0",
|
||||
"discord.js": "^14.25.1",
|
||||
"dotenv": "^17.2.3",
|
||||
"drizzle-orm": "^0.44.7",
|
||||
@@ -26,10 +27,12 @@
|
||||
"version": "0.1.0",
|
||||
"dependencies": {
|
||||
"@imgly/background-removal": "^1.7.0",
|
||||
"chess.js": "^1.4.0",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"lucide-react": "^0.564.0",
|
||||
"react": "^19.1.0",
|
||||
"react-chessboard": "^5.10.0",
|
||||
"react-dom": "^19.1.0",
|
||||
"react-router-dom": "^7.13.2",
|
||||
"tailwind-merge": "^3.4.0",
|
||||
@@ -99,6 +102,14 @@
|
||||
|
||||
"@discordjs/ws": ["@discordjs/ws@1.2.3", "", { "dependencies": { "@discordjs/collection": "^2.1.0", "@discordjs/rest": "^2.5.1", "@discordjs/util": "^1.1.0", "@sapphire/async-queue": "^1.5.2", "@types/ws": "^8.5.10", "@vladfrangu/async_event_emitter": "^2.2.4", "discord-api-types": "^0.38.1", "tslib": "^2.6.2", "ws": "^8.17.0" } }, "sha512-wPlQDxEmlDg5IxhJPuxXr3Vy9AjYq5xCvFWGJyD7w7Np8ZGu+Mc+97LCoEc/+AYCo2IDpKioiH0/c/mj5ZR9Uw=="],
|
||||
|
||||
"@dnd-kit/accessibility": ["@dnd-kit/accessibility@3.1.1", "", { "dependencies": { "tslib": "^2.0.0" }, "peerDependencies": { "react": ">=16.8.0" } }, "sha512-2P+YgaXF+gRsIihwwY1gCsQSYnu9Zyj2py8kY5fFvUM1qm2WA2u639R6YNVfU4GWr+ZM5mqEsfHZZLoRONbemw=="],
|
||||
|
||||
"@dnd-kit/core": ["@dnd-kit/core@6.3.1", "", { "dependencies": { "@dnd-kit/accessibility": "^3.1.1", "@dnd-kit/utilities": "^3.2.2", "tslib": "^2.0.0" }, "peerDependencies": { "react": ">=16.8.0", "react-dom": ">=16.8.0" } }, "sha512-xkGBRQQab4RLwgXxoqETICr6S5JlogafbhNsidmrkVv2YRs5MLwpjoF2qpiGjQt8S9AoxtIV603s0GIUpY5eYQ=="],
|
||||
|
||||
"@dnd-kit/modifiers": ["@dnd-kit/modifiers@9.0.0", "", { "dependencies": { "@dnd-kit/utilities": "^3.2.2", "tslib": "^2.0.0" }, "peerDependencies": { "@dnd-kit/core": "^6.3.0", "react": ">=16.8.0" } }, "sha512-ybiLc66qRGuZoC20wdSSG6pDXFikui/dCNGthxv4Ndy8ylErY0N3KVxY2bgo7AWwIbxDmXDg3ylAFmnrjcbVvw=="],
|
||||
|
||||
"@dnd-kit/utilities": ["@dnd-kit/utilities@3.2.2", "", { "dependencies": { "tslib": "^2.0.0" }, "peerDependencies": { "react": ">=16.8.0" } }, "sha512-+MKAJEOfaBe5SmV6t34p80MMKhjvUz0vRrvVJbPT0WElzaOJ/1xs+D+KDv+tD/NE5ujfrChEcshd4fLn0wpiqg=="],
|
||||
|
||||
"@drizzle-team/brocli": ["@drizzle-team/brocli@0.10.2", "", {}, "sha512-z33Il7l5dKjUgGULTqBsQBQwckHh5AbIuxhdsIxDDiZAzBOrZO6q9ogcWC65kU382AfynTfgNumVcNIjuIua6w=="],
|
||||
|
||||
"@esbuild-kit/core-utils": ["@esbuild-kit/core-utils@3.3.2", "", { "dependencies": { "esbuild": "~0.18.20", "source-map-support": "^0.5.21" } }, "sha512-sPRAnw9CdSsRmEtnsl2WXWdyquogVpB3yZ3dgwJfe8zrOzTsV7cJvmwrKVa+0ma5BoiGJ+BoqkMvawbayKUsqQ=="],
|
||||
@@ -337,6 +348,8 @@
|
||||
|
||||
"caniuse-lite": ["caniuse-lite@1.0.30001769", "", {}, "sha512-BCfFL1sHijQlBGWBMuJyhZUhzo7wer5sVj9hqekB/7xn0Ypy+pER/edCYQm4exbXj4WiySGp40P8UuTh6w1srg=="],
|
||||
|
||||
"chess.js": ["chess.js@1.4.0", "", {}, "sha512-BBJgrrtKQOzFLonR0l+k64A98NLemPwNsCskwb+29bRwobUa4iTm51E1kwGPbWXAcfdDa18nad6vpPPKPWarqw=="],
|
||||
|
||||
"class-variance-authority": ["class-variance-authority@0.7.1", "", { "dependencies": { "clsx": "^2.1.1" } }, "sha512-Ka+9Trutv7G8M6WT6SeiRWz792K5qEqIGEGzXKhAE6xOWAY6pPH8U+9IY3oCMv6kqTmLsv7Xh/2w2RigkePMsg=="],
|
||||
|
||||
"clsx": ["clsx@2.1.1", "", {}, "sha512-eYm0QWBtUrBWZWG0d386OGAw16Z995PiOVo2B7bjWSbHedGl5e0ZWaq65kOGgUSNesEIDkB9ISbTg/JK9dhCZA=="],
|
||||
@@ -473,6 +486,8 @@
|
||||
|
||||
"react": ["react@19.2.4", "", {}, "sha512-9nfp2hYpCwOjAN+8TZFGhtWEwgvWHXqESH8qT89AT/lWklpLON22Lc8pEtnpsZz7VmawabSU0gCjnj8aC0euHQ=="],
|
||||
|
||||
"react-chessboard": ["react-chessboard@5.10.0", "", { "dependencies": { "@dnd-kit/core": "^6.3.1", "@dnd-kit/modifiers": "^9.0.0" }, "peerDependencies": { "react": "^19.0.0", "react-dom": "^19.0.0" } }, "sha512-Y3PgaCVhnDG3IaQfu86OzTSEIEAUtuU5XwmHWnx3tcFOX7lSoAq81ZFX3MBj6y5a6FzDMTczMVmkkrV2CzTrIw=="],
|
||||
|
||||
"react-dom": ["react-dom@19.2.4", "", { "dependencies": { "scheduler": "^0.27.0" }, "peerDependencies": { "react": "^19.2.4" } }, "sha512-AXJdLo8kgMbimY95O2aKQqsz2iWi9jMgKJhRBAxECE4IFxfcazB2LmzloIoibJI3C12IlY20+KFaLv+71bUJeQ=="],
|
||||
|
||||
"react-refresh": ["react-refresh@0.17.0", "", {}, "sha512-z6F7K9bV85EfseRCp2bzrpyQ0Gkw1uLoCel9XBVWPg/TjRj94SkJzUTGfOa4bs7iJvBWtQG0Wq7wnI0syw3EBQ=="],
|
||||
|
||||
@@ -40,6 +40,7 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@napi-rs/canvas": "^0.1.89",
|
||||
"chess.js": "^1.4.0",
|
||||
"discord.js": "^14.25.1",
|
||||
"dotenv": "^17.2.3",
|
||||
"drizzle-orm": "^0.44.7",
|
||||
|
||||
@@ -10,10 +10,12 @@
|
||||
},
|
||||
"dependencies": {
|
||||
"@imgly/background-removal": "^1.7.0",
|
||||
"chess.js": "^1.4.0",
|
||||
"class-variance-authority": "^0.7.1",
|
||||
"clsx": "^2.1.1",
|
||||
"lucide-react": "^0.564.0",
|
||||
"react": "^19.1.0",
|
||||
"react-chessboard": "^5.10.0",
|
||||
"react-dom": "^19.1.0",
|
||||
"react-router-dom": "^7.13.2",
|
||||
"tailwind-merge": "^3.4.0",
|
||||
|
||||
1
panel/public/pieces/bB.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill="none" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><g fill="#000" stroke-linecap="butt"><path d="M9 36c3.4-1 10.1.4 13.5-2 3.4 2.4 10.1 1 13.5 2 0 0 1.6.5 3 2-.7 1-1.6 1-3 .5-3.4-1-10.1.5-13.5-1-3.4 1.5-10.1 0-13.5 1-1.4.5-2.3.5-3-.5 1.4-2 3-2 3-2z"/><path d="M15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2z"/><path d="M25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z"/></g><path stroke="#ececec" stroke-linejoin="miter" d="M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5"/></g></svg>
|
||||
|
After Width: | Height: | Size: 683 B |
1
panel/public/pieces/bK.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill="none" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path stroke-linejoin="miter" d="M22.5 11.6V6"/><path fill="#000" stroke-linecap="butt" stroke-linejoin="miter" d="M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5"/><path fill="#000" d="M11.5 37a22.3 22.3 0 0 0 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10z"/><path stroke-linejoin="miter" d="M20 8h5"/><path stroke="#ececec" d="M32 29.5s8.5-4 6-9.7C34.1 14 25 18 22.5 24.6v2.1-2.1C20 18 9.9 14 7 19.9c-2.5 5.6 4.8 9 4.8 9"/><path stroke="#ececec" d="M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0"/></g></svg>
|
||||
|
After Width: | Height: | Size: 783 B |
1
panel/public/pieces/bN.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill="none" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path fill="#000" d="M22 10c10.5 1 16.5 8 16 29H15c0-9 10-6.5 8-21"/><path fill="#000" d="M24 18c.38 2.91-5.55 7.37-8 9-3 2-2.82 4.34-5 4-1.04-.94 1.41-3.04 0-3-1 0 .19 1.23-1 2-1 0-4 1-4-4 0-2 6-12 6-12s1.89-1.9 2-3.5c-.73-1-.5-2-.5-3 1-1 3 2.5 3 2.5h2s.78-2 2.5-3c1 0 1 3 1 3"/><path fill="#ececec" stroke="#ececec" d="M9.5 25.5a.5.5 0 1 1-1 0 .5.5 0 1 1 1 0m5.43-9.75a.5 1.5 30 1 1-.86-.5.5 1.5 30 1 1 .86.5"/><path fill="#ececec" stroke="none" d="m24.55 10.4-.45 1.45.5.15c3.15 1 5.65 2.49 7.9 6.75S35.75 29.06 35.25 39l-.05.5h2.25l.05-.5c.5-10.06-.88-16.85-3.25-21.34s-5.79-6.64-9.19-7.16z"/></g></svg>
|
||||
|
After Width: | Height: | Size: 782 B |
1
panel/public/pieces/bP.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><path stroke="#000" stroke-linecap="round" stroke-width="1.5" d="M22.5 9a4 4 0 0 0-3.22 6.38 6.48 6.48 0 0 0-.87 10.65c-3 1.06-7.41 5.55-7.41 13.47h23c0-7.92-4.41-12.41-7.41-13.47a6.46 6.46 0 0 0-.87-10.65A4.01 4.01 0 0 0 22.5 9z"/></svg>
|
||||
|
After Width: | Height: | Size: 298 B |
1
panel/public/pieces/bQ.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><g stroke="none"><circle cx="6" cy="12" r="2.75"/><circle cx="14" cy="9" r="2.75"/><circle cx="22.5" cy="8" r="2.75"/><circle cx="31" cy="9" r="2.75"/><circle cx="39" cy="12" r="2.75"/></g><path stroke-linecap="butt" d="M9 26c8.5-1.5 21-1.5 27 0l2.5-12.5L31 25l-.3-14.1-5.2 13.6-3-14.5-3 14.5-5.2-13.6L14 25 6.5 13.5z"/><path stroke-linecap="butt" d="M9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z"/><path fill="none" stroke-linecap="butt" d="M11 38.5a35 35 1 0 0 23 0"/><path fill="none" stroke="#ececec" d="M11 29a35 35 1 0 1 23 0m-21.5 2.5h20m-21 3a35 35 1 0 0 22 0m-23 3a35 35 1 0 0 24 0"/></g></svg>
|
||||
|
After Width: | Height: | Size: 910 B |
1
panel/public/pieces/bR.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path stroke-linecap="butt" d="M9 39h27v-3H9zm3.5-7 1.5-2.5h17l1.5 2.5zm-.5 4v-4h21v4z"/><path stroke-linecap="butt" stroke-linejoin="miter" d="M14 29.5v-13h17v13z"/><path stroke-linecap="butt" d="M14 16.5 11 14h23l-3 2.5zM11 14V9h4v2h5V9h5v2h5V9h4v5z"/><path fill="none" stroke="#ececec" stroke-linejoin="miter" stroke-width="1" d="M12 35.5h21m-20-4h19m-18-2h17m-17-13h17M11 14h23"/></g></svg>
|
||||
|
After Width: | Height: | Size: 557 B |
1
panel/public/pieces/wB.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill="none" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><g fill="#fff" stroke-linecap="butt"><path d="M9 36c3.39-.97 10.11.43 13.5-2 3.39 2.43 10.11 1.03 13.5 2 0 0 1.65.54 3 2-.68.97-1.65.99-3 .5-3.39-.97-10.11.46-13.5-1-3.39 1.46-10.11.03-13.5 1-1.35.49-2.32.47-3-.5 1.35-1.94 3-2 3-2z"/><path d="M15 32c2.5 2.5 12.5 2.5 15 0 .5-1.5 0-2 0-2 0-2.5-2.5-4-2.5-4 5.5-1.5 6-11.5-5-15.5-11 4-10.5 14-5 15.5 0 0-2.5 1.5-2.5 4 0 0-.5.5 0 2z"/><path d="M25 8a2.5 2.5 0 1 1-5 0 2.5 2.5 0 1 1 5 0z"/></g><path stroke-linejoin="miter" d="M17.5 26h10M15 30h15m-7.5-14.5v5M20 18h5"/></g></svg>
|
||||
|
After Width: | Height: | Size: 700 B |
1
panel/public/pieces/wK.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill="none" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path stroke-linejoin="miter" d="M22.5 11.63V6M20 8h5"/><path fill="#fff" stroke-linecap="butt" stroke-linejoin="miter" d="M22.5 25s4.5-7.5 3-10.5c0 0-1-2.5-3-2.5s-3 2.5-3 2.5c-1.5 3 3 10.5 3 10.5"/><path fill="#fff" d="M11.5 37c5.5 3.5 15.5 3.5 21 0v-7s9-4.5 6-10.5c-4-6.5-13.5-3.5-16 4V27v-3.5c-3.5-7.5-13-10.5-16-4-3 6 5 10 5 10z"/><path d="M11.5 30c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0m-21 3.5c5.5-3 15.5-3 21 0"/></g></svg>
|
||||
|
After Width: | Height: | Size: 610 B |
1
panel/public/pieces/wN.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill="none" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path fill="#fff" d="M22 10c10.5 1 16.5 8 16 29H15c0-9 10-6.5 8-21"/><path fill="#fff" d="M24 18c.38 2.91-5.55 7.37-8 9-3 2-2.82 4.34-5 4-1.042-.94 1.41-3.04 0-3-1 0 .19 1.23-1 2-1 0-4.003 1-4-4 0-2 6-12 6-12s1.89-1.9 2-3.5c-.73-.994-.5-2-.5-3 1-1 3 2.5 3 2.5h2s.78-1.992 2.5-3c1 0 1 3 1 3"/><path fill="#000" d="M9.5 25.5a.5.5 0 1 1-1 0 .5.5 0 1 1 1 0m5.433-9.75a.5 1.5 30 1 1-.866-.5.5 1.5 30 1 1 .866.5"/></g></svg>
|
||||
|
After Width: | Height: | Size: 593 B |
1
panel/public/pieces/wP.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><path fill="#fff" stroke="#000" stroke-linecap="round" stroke-width="1.5" d="M22.5 9c-2.21 0-4 1.79-4 4 0 .89.29 1.71.78 2.38C17.33 16.5 16 18.59 16 21c0 2.03.94 3.84 2.41 5.03-3 1.06-7.41 5.55-7.41 13.47h23c0-7.92-4.41-12.41-7.41-13.47 1.47-1.19 2.41-3 2.41-5.03 0-2.41-1.33-4.5-3.28-5.62.49-.67.78-1.49.78-2.38 0-2.21-1.79-4-4-4z"/></svg>
|
||||
|
After Width: | Height: | Size: 400 B |
1
panel/public/pieces/wQ.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill="#fff" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path d="M8 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0m16.5-4.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0M41 12a2 2 0 1 1-4 0 2 2 0 1 1 4 0M16 8.5a2 2 0 1 1-4 0 2 2 0 1 1 4 0M33 9a2 2 0 1 1-4 0 2 2 0 1 1 4 0"/><path stroke-linecap="butt" d="M9 26c8.5-1.5 21-1.5 27 0l2-12-7 11V11l-5.5 13.5-3-15-3 15-5.5-14V25L7 14z"/><path stroke-linecap="butt" d="M9 26c0 2 1.5 2 2.5 4 1 1.5 1 1 .5 3.5-1.5 1-1.5 2.5-1.5 2.5-1.5 1.5.5 2.5.5 2.5 6.5 1 16.5 1 23 0 0 0 1.5-1 0-2.5 0 0 .5-1.5-1-2.5-.5-2.5-.5-2 .5-3.5 1-2 2.5-2 2.5-4-8.5-1.5-18.5-1.5-27 0z"/><path fill="none" d="M11.5 30c3.5-1 18.5-1 22 0M12 33.5c6-1 15-1 21 0"/></g></svg>
|
||||
|
After Width: | Height: | Size: 772 B |
1
panel/public/pieces/wR.svg
Normal file
@@ -0,0 +1 @@
|
||||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 45 45"><g fill="#fff" fill-rule="evenodd" stroke="#000" stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"><path stroke-linecap="butt" d="M9 39h27v-3H9zm3-3v-4h21v4zm-1-22V9h4v2h5V9h5v2h5V9h4v5"/><path d="m34 14-3 3H14l-3-3"/><path stroke-linecap="butt" stroke-linejoin="miter" d="M31 17v12.5H14V17"/><path d="m31 29.5 1.5 2.5h-20l1.5-2.5"/><path fill="none" stroke-linejoin="miter" d="M11 14h23"/></g></svg>
|
||||
|
After Width: | Height: | Size: 476 B |
@@ -2,6 +2,7 @@ import { useState, useEffect } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
import { useWebSocket } from "../lib/useWebSocket";
|
||||
import { gameUIRegistry } from "./registry";
|
||||
import { ChevronLeft } from "lucide-react";
|
||||
|
||||
// Mirrors RoomSummary in api/src/games/types.ts — keep in sync
|
||||
interface RoomSummary {
|
||||
@@ -15,12 +16,27 @@ interface RoomSummary {
|
||||
status: "waiting" | "playing" | "finished";
|
||||
}
|
||||
|
||||
const CHESS_TIME_CONTROLS = [
|
||||
{ key: "bullet_1_0", label: "1+0", category: "Bullet" },
|
||||
{ key: "bullet_2_1", label: "2+1", category: "Bullet" },
|
||||
{ key: "blitz_3_0", label: "3+0", category: "Blitz" },
|
||||
{ key: "blitz_3_2", label: "3+2", category: "Blitz" },
|
||||
{ key: "blitz_5_0", label: "5+0", category: "Blitz" },
|
||||
{ key: "blitz_5_3", label: "5+3", category: "Blitz" },
|
||||
{ key: "rapid_10_0", label: "10+0", category: "Rapid" },
|
||||
{ key: "rapid_15_10", label: "15+10", category: "Rapid" },
|
||||
{ key: "classical_30_0", label: "30+0", category: "Classical" },
|
||||
{ key: "none", label: "None", category: "No Clock" },
|
||||
] as const;
|
||||
|
||||
export function GameLobby() {
|
||||
const { send, subscribe, connected } = useWebSocket();
|
||||
const navigate = useNavigate();
|
||||
const [rooms, setRooms] = useState<RoomSummary[]>([]);
|
||||
const [filter, setFilter] = useState<string | null>(null);
|
||||
const [showCreate, setShowCreate] = useState(false);
|
||||
const [configGame, setConfigGame] = useState<string | null>(null);
|
||||
const [chessTimeControl, setChessTimeControl] = useState("blitz_5_3");
|
||||
|
||||
const gameTypes = gameUIRegistry.list();
|
||||
|
||||
@@ -42,11 +58,21 @@ export function GameLobby() {
|
||||
const filteredRooms = filter ? rooms.filter(r => r.gameSlug === filter) : rooms;
|
||||
const activeRooms = filteredRooms.filter(r => r.status !== "finished");
|
||||
|
||||
function createRoom(gameSlug: string) {
|
||||
function handleGameSelect(gameSlug: string) {
|
||||
if (gameSlug === "chess") {
|
||||
setConfigGame("chess");
|
||||
return;
|
||||
}
|
||||
send({ type: "CREATE_ROOM", gameType: gameSlug });
|
||||
setShowCreate(false);
|
||||
}
|
||||
|
||||
function createChessRoom() {
|
||||
send({ type: "CREATE_ROOM", gameType: "chess", options: { timeControl: chessTimeControl } });
|
||||
setShowCreate(false);
|
||||
setConfigGame(null);
|
||||
}
|
||||
|
||||
return (
|
||||
<div>
|
||||
<div className="flex items-center justify-between gap-3 mb-4 md:mb-6">
|
||||
@@ -136,23 +162,73 @@ export function GameLobby() {
|
||||
</div>
|
||||
|
||||
{showCreate && (
|
||||
<div className="fixed inset-0 z-50 flex items-end sm:items-center justify-center bg-black/50" onClick={() => setShowCreate(false)}>
|
||||
<div className="fixed inset-0 z-50 flex items-end sm:items-center justify-center bg-black/50" onClick={() => { setShowCreate(false); setConfigGame(null); }}>
|
||||
<div className="bg-surface-container-highest rounded-xl p-6 w-full sm:max-w-sm shadow-[0_20px_40px_rgba(0,0,0,0.5)]" onClick={e => e.stopPropagation()}>
|
||||
<h2 className="font-display text-base font-semibold mb-4">Create a Room</h2>
|
||||
<div className="space-y-2">
|
||||
{gameTypes.map(g => (
|
||||
{configGame === "chess" ? (
|
||||
<>
|
||||
<button
|
||||
key={g.slug}
|
||||
onClick={() => createRoom(g.slug)}
|
||||
className="w-full flex items-center gap-3 rounded-xl bg-raised px-4 py-3 text-sm font-medium hover:bg-surface-container-high transition-colors"
|
||||
onClick={() => setConfigGame(null)}
|
||||
className="flex items-center gap-1 text-xs text-text-tertiary hover:text-foreground transition-colors mb-3"
|
||||
>
|
||||
<span className="text-lg">{g.icon}</span>
|
||||
<span>{g.name}</span>
|
||||
<ChevronLeft className="w-3.5 h-3.5" /> Back
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
<h2 className="font-display text-base font-semibold mb-1">{"\u265A"} Chess</h2>
|
||||
<p className="text-xs text-text-tertiary mb-4">Choose your time control</p>
|
||||
|
||||
<div className="space-y-3">
|
||||
{["Bullet", "Blitz", "Rapid", "Classical", "No Clock"].map(category => {
|
||||
const controls = CHESS_TIME_CONTROLS.filter(tc => tc.category === category);
|
||||
return (
|
||||
<div key={category}>
|
||||
<div className="text-[10px] font-label font-semibold text-text-disabled uppercase tracking-wider mb-1.5">
|
||||
{category}
|
||||
</div>
|
||||
<div className="flex flex-wrap gap-1.5">
|
||||
{controls.map(tc => (
|
||||
<button
|
||||
key={tc.key}
|
||||
onClick={() => setChessTimeControl(tc.key)}
|
||||
className={`px-3 py-1.5 rounded-lg text-sm font-medium transition-colors ${
|
||||
chessTimeControl === tc.key
|
||||
? "bg-primary/15 text-primary ring-1 ring-primary/30"
|
||||
: "bg-raised text-text-tertiary hover:text-foreground"
|
||||
}`}
|
||||
>
|
||||
{tc.label}
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
|
||||
<button
|
||||
onClick={createChessRoom}
|
||||
className="mt-5 w-full rounded-xl bg-primary text-on-primary px-4 py-2.5 text-sm font-label font-semibold hover:opacity-90 transition-colors"
|
||||
>
|
||||
Create Room
|
||||
</button>
|
||||
</>
|
||||
) : (
|
||||
<>
|
||||
<h2 className="font-display text-base font-semibold mb-4">Create a Room</h2>
|
||||
<div className="space-y-2">
|
||||
{gameTypes.map(g => (
|
||||
<button
|
||||
key={g.slug}
|
||||
onClick={() => handleGameSelect(g.slug)}
|
||||
className="w-full flex items-center gap-3 rounded-xl bg-raised px-4 py-3 text-sm font-medium hover:bg-surface-container-high transition-colors"
|
||||
>
|
||||
<span className="text-lg">{g.icon}</span>
|
||||
<span>{g.name}</span>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
<button
|
||||
onClick={() => setShowCreate(false)}
|
||||
onClick={() => { setShowCreate(false); setConfigGame(null); }}
|
||||
className="mt-4 w-full text-center text-sm text-text-tertiary hover:text-foreground transition-colors"
|
||||
>
|
||||
Cancel
|
||||
|
||||
544
panel/src/games/chess/ChessGame.tsx
Normal file
@@ -0,0 +1,544 @@
|
||||
import { useState, useEffect, useCallback, useMemo, useRef } from "react";
|
||||
import { Chessboard } from "react-chessboard";
|
||||
import { Chess } from "chess.js";
|
||||
import type { Square } from "chess.js";
|
||||
import type { GameUIProps } from "../registry";
|
||||
import { Flag, Handshake, X, Check, Clock } from "lucide-react";
|
||||
import type { PieceRenderObject } from "react-chessboard";
|
||||
|
||||
// ── Piece images (cburnett set from Lichess, CC BY-SA) ──
|
||||
|
||||
const PIECE_KEYS = ["wP","wR","wN","wB","wQ","wK","bP","bR","bN","bB","bQ","bK"] as const;
|
||||
|
||||
const chessPieces: PieceRenderObject = Object.fromEntries(
|
||||
PIECE_KEYS.map(key => [
|
||||
key,
|
||||
(props?: { svgStyle?: React.CSSProperties }) => (
|
||||
<img
|
||||
src={`/pieces/${key}.svg`}
|
||||
alt={key}
|
||||
style={{ width: "100%", height: "100%", ...props?.svgStyle }}
|
||||
draggable={false}
|
||||
/>
|
||||
),
|
||||
])
|
||||
);
|
||||
|
||||
/** Returns the image URL for a piece key like "wQ" or "bN" */
|
||||
function pieceImgSrc(color: "white" | "black", type: string): string {
|
||||
return `/pieces/${color === "white" ? "w" : "b"}${type.toUpperCase()}.svg`;
|
||||
}
|
||||
|
||||
// ── Types matching server ChessPlayerView / ChessSpectatorView ──
|
||||
|
||||
interface ChessClockView {
|
||||
white: number;
|
||||
black: number;
|
||||
increment: number;
|
||||
activeColor: "white" | "black" | null;
|
||||
}
|
||||
|
||||
interface ChessViewBase {
|
||||
fen: string;
|
||||
pgn: string;
|
||||
turn: "white" | "black";
|
||||
clock: ChessClockView | null;
|
||||
drawOffer: "white" | "black" | null;
|
||||
result: "white" | "black" | "draw" | null;
|
||||
resultReason: string | null;
|
||||
moveHistory: { from: string; to: string; san: string; color: "w" | "b" }[];
|
||||
isCheck: boolean;
|
||||
}
|
||||
|
||||
interface PlayerView extends ChessViewBase {
|
||||
myColor: "white" | "black";
|
||||
legalMoves: { from: string; to: string; promotion?: string }[];
|
||||
}
|
||||
|
||||
interface SpectatorView extends ChessViewBase {
|
||||
players: { white: string; black: string };
|
||||
}
|
||||
|
||||
function isPlayerView(state: unknown): state is PlayerView {
|
||||
return typeof state === "object" && state !== null && "myColor" in state;
|
||||
}
|
||||
|
||||
// ── Clock Display ──
|
||||
|
||||
function formatTime(ms: number): string {
|
||||
if (ms <= 0) return "0:00";
|
||||
const totalSeconds = Math.ceil(ms / 1000);
|
||||
const minutes = Math.floor(totalSeconds / 60);
|
||||
const seconds = totalSeconds % 60;
|
||||
if (minutes >= 60) {
|
||||
const hours = Math.floor(minutes / 60);
|
||||
const mins = minutes % 60;
|
||||
return `${hours}:${mins.toString().padStart(2, "0")}:${seconds.toString().padStart(2, "0")}`;
|
||||
}
|
||||
return `${minutes}:${seconds.toString().padStart(2, "0")}`;
|
||||
}
|
||||
|
||||
function ChessClock({ time, isActive, isLow, label }: {
|
||||
time: number;
|
||||
isActive: boolean;
|
||||
isLow: boolean;
|
||||
label: string;
|
||||
}) {
|
||||
return (
|
||||
<div className={`flex items-center gap-2 px-3 py-2 rounded-lg font-mono text-lg transition-colors ${
|
||||
isActive
|
||||
? isLow
|
||||
? "bg-destructive/15 text-destructive"
|
||||
: "bg-primary/15 text-primary"
|
||||
: "bg-card text-text-tertiary"
|
||||
}`}>
|
||||
<Clock className="w-4 h-4 opacity-60" />
|
||||
<span className="font-semibold tabular-nums">{formatTime(time)}</span>
|
||||
<span className="text-xs opacity-50 font-label">{label}</span>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ── Move History ──
|
||||
|
||||
function MoveHistory({ moves, containerRef }: {
|
||||
moves: { san: string; color: "w" | "b" }[];
|
||||
containerRef: React.RefObject<HTMLDivElement | null>;
|
||||
}) {
|
||||
const pairs: { number: number; white?: string; black?: string }[] = [];
|
||||
for (let i = 0; i < moves.length; i++) {
|
||||
const moveNum = Math.floor(i / 2) + 1;
|
||||
if (i % 2 === 0) {
|
||||
pairs.push({ number: moveNum, white: moves[i]!.san });
|
||||
} else {
|
||||
const pair = pairs[pairs.length - 1];
|
||||
if (pair) pair.black = moves[i]!.san;
|
||||
}
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (containerRef.current) {
|
||||
containerRef.current.scrollTop = containerRef.current.scrollHeight;
|
||||
}
|
||||
}, [moves.length]);
|
||||
|
||||
if (pairs.length === 0) {
|
||||
return (
|
||||
<div className="text-xs text-text-disabled text-center py-4">
|
||||
No moves yet
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-[auto_1fr_1fr] gap-x-2 gap-y-0.5 text-sm">
|
||||
{pairs.map(p => (
|
||||
<div key={p.number} className="contents">
|
||||
<span className="text-text-disabled text-xs tabular-nums pr-1">{p.number}.</span>
|
||||
<span className={`font-mono ${p.white ? "text-foreground" : ""}`}>{p.white ?? ""}</span>
|
||||
<span className={`font-mono ${p.black ? "text-foreground" : ""}`}>{p.black ?? ""}</span>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
// ── Main Component ──
|
||||
|
||||
export function ChessGame({ state, myPlayerId, isSpectator, onAction, players }: GameUIProps) {
|
||||
const view = state as PlayerView | SpectatorView;
|
||||
const playerView = isPlayerView(state) ? state as PlayerView : null;
|
||||
const myColor = playerView?.myColor ?? "white";
|
||||
const boardOrientation = isSpectator ? "white" : myColor;
|
||||
const isMyTurn = playerView ? view.turn === playerView.myColor : false;
|
||||
const isGameOver = view.result !== null;
|
||||
const moveHistoryRef = useRef<HTMLDivElement>(null);
|
||||
|
||||
// Selected square for click-to-move
|
||||
const [selectedSquare, setSelectedSquare] = useState<Square | null>(null);
|
||||
const [promotionSquare, setPromotionSquare] = useState<{ from: Square; to: Square } | null>(null);
|
||||
|
||||
// Live clock state (client-side countdown for smooth display)
|
||||
const [liveClock, setLiveClock] = useState(view.clock);
|
||||
const lastServerClock = useRef(view.clock);
|
||||
|
||||
// Sync from server
|
||||
useEffect(() => {
|
||||
lastServerClock.current = view.clock;
|
||||
setLiveClock(view.clock);
|
||||
}, [view.clock?.white, view.clock?.black, view.clock?.activeColor]);
|
||||
|
||||
// Client-side clock tick (100ms intervals for smooth countdown)
|
||||
useEffect(() => {
|
||||
if (!liveClock || !liveClock.activeColor || isGameOver) return;
|
||||
|
||||
const interval = setInterval(() => {
|
||||
setLiveClock(prev => {
|
||||
if (!prev || !prev.activeColor) return prev;
|
||||
const active = prev.activeColor;
|
||||
const remaining = Math.max(0, prev[active] - 100);
|
||||
if (remaining <= 0 && !isSpectator && active !== myColor) {
|
||||
// Opponent ran out — claim timeout
|
||||
onAction({ type: "claim_timeout" });
|
||||
}
|
||||
return { ...prev, [active]: remaining };
|
||||
});
|
||||
}, 100);
|
||||
|
||||
return () => clearInterval(interval);
|
||||
}, [liveClock?.activeColor, isGameOver, isSpectator, myColor, onAction]);
|
||||
|
||||
// Compute legal move squares for highlighting
|
||||
const legalMovesForSquare = useMemo(() => {
|
||||
if (!playerView || !selectedSquare || isGameOver || !isMyTurn) return [];
|
||||
return playerView.legalMoves
|
||||
.filter(m => m.from === selectedSquare)
|
||||
.map(m => m.to);
|
||||
}, [playerView, selectedSquare, isGameOver, isMyTurn]);
|
||||
|
||||
// Build custom square styles
|
||||
const customSquareStyles = useMemo(() => {
|
||||
const styles: Record<string, React.CSSProperties> = {};
|
||||
|
||||
// Highlight selected square
|
||||
if (selectedSquare) {
|
||||
styles[selectedSquare] = {
|
||||
backgroundColor: "rgba(233, 195, 73, 0.35)",
|
||||
};
|
||||
}
|
||||
|
||||
// Highlight legal moves
|
||||
for (const sq of legalMovesForSquare) {
|
||||
// Check if a piece occupies the target (capture indicator)
|
||||
const game = new Chess(view.fen);
|
||||
const targetPiece = game.get(sq as Square);
|
||||
styles[sq] = targetPiece
|
||||
? {
|
||||
background: "radial-gradient(circle, transparent 55%, rgba(233, 195, 73, 0.45) 55%)",
|
||||
}
|
||||
: {
|
||||
background: "radial-gradient(circle, rgba(233, 195, 73, 0.35) 25%, transparent 25%)",
|
||||
};
|
||||
}
|
||||
|
||||
// Highlight last move
|
||||
const lastMove = view.moveHistory.length > 0 ? view.moveHistory[view.moveHistory.length - 1] : undefined;
|
||||
if (lastMove) {
|
||||
styles[lastMove.from] = {
|
||||
...styles[lastMove.from],
|
||||
backgroundColor: "rgba(233, 195, 73, 0.15)",
|
||||
};
|
||||
styles[lastMove.to] = {
|
||||
...styles[lastMove.to],
|
||||
backgroundColor: "rgba(233, 195, 73, 0.2)",
|
||||
};
|
||||
}
|
||||
|
||||
// Highlight king in check
|
||||
if (view.isCheck && !isGameOver) {
|
||||
const game = new Chess(view.fen);
|
||||
// Find the king of the side in check
|
||||
const board = game.board();
|
||||
for (const row of board) {
|
||||
for (const piece of row) {
|
||||
if (piece && piece.type === "k" && piece.color === (view.turn === "white" ? "w" : "b")) {
|
||||
styles[piece.square] = {
|
||||
...styles[piece.square],
|
||||
background: "radial-gradient(circle, rgba(220, 38, 38, 0.7) 0%, rgba(220, 38, 38, 0.2) 60%, transparent 70%)",
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return styles;
|
||||
}, [selectedSquare, legalMovesForSquare, view.fen, view.moveHistory, view.isCheck, view.turn, isGameOver]);
|
||||
|
||||
// Handle square click (for click-to-move)
|
||||
const onSquareClick = useCallback((square: Square) => {
|
||||
if (isSpectator || isGameOver || !isMyTurn || !playerView) return;
|
||||
|
||||
// If clicking a legal move target, make the move
|
||||
if (selectedSquare && legalMovesForSquare.includes(square)) {
|
||||
// Check for promotion
|
||||
const game = new Chess(view.fen);
|
||||
const piece = game.get(selectedSquare);
|
||||
const isPromotion = piece?.type === "p" &&
|
||||
((piece.color === "w" && square[1] === "8") ||
|
||||
(piece.color === "b" && square[1] === "1"));
|
||||
|
||||
if (isPromotion) {
|
||||
setPromotionSquare({ from: selectedSquare, to: square });
|
||||
} else {
|
||||
onAction({ type: "move", from: selectedSquare, to: square });
|
||||
}
|
||||
setSelectedSquare(null);
|
||||
return;
|
||||
}
|
||||
|
||||
// Select a piece (must be our color)
|
||||
const game = new Chess(view.fen);
|
||||
const piece = game.get(square);
|
||||
if (piece && piece.color === (myColor === "white" ? "w" : "b")) {
|
||||
setSelectedSquare(square);
|
||||
} else {
|
||||
setSelectedSquare(null);
|
||||
}
|
||||
}, [isSpectator, isGameOver, isMyTurn, playerView, selectedSquare, legalMovesForSquare, view.fen, myColor, onAction]);
|
||||
|
||||
// Handle drag-and-drop (react-chessboard v5 API)
|
||||
const onPieceDrop = useCallback(({ sourceSquare, targetSquare }: { piece: unknown; sourceSquare: string; targetSquare: string | null }): boolean => {
|
||||
if (isSpectator || isGameOver || !isMyTurn || !playerView || !targetSquare) return false;
|
||||
|
||||
// Check if this is a legal move
|
||||
const isLegal = playerView.legalMoves.some(m =>
|
||||
m.from === sourceSquare && m.to === targetSquare
|
||||
);
|
||||
if (!isLegal) return false;
|
||||
|
||||
// Check for promotion
|
||||
const game = new Chess(view.fen);
|
||||
const sourcePiece = game.get(sourceSquare as Square);
|
||||
const isPromotion = sourcePiece?.type === "p" &&
|
||||
((sourcePiece.color === "w" && targetSquare[1] === "8") ||
|
||||
(sourcePiece.color === "b" && targetSquare[1] === "1"));
|
||||
|
||||
if (isPromotion) {
|
||||
setPromotionSquare({ from: sourceSquare as Square, to: targetSquare as Square });
|
||||
return false; // Don't visually drop yet; wait for promotion choice
|
||||
}
|
||||
|
||||
onAction({ type: "move", from: sourceSquare, to: targetSquare });
|
||||
setSelectedSquare(null);
|
||||
return true;
|
||||
}, [isSpectator, isGameOver, isMyTurn, playerView, view.fen, onAction]);
|
||||
|
||||
// Handle promotion selection
|
||||
const handlePromotion = useCallback((promoType: string) => {
|
||||
if (!promotionSquare) return;
|
||||
onAction({ type: "move", from: promotionSquare.from, to: promotionSquare.to, promotion: promoType });
|
||||
setPromotionSquare(null);
|
||||
}, [promotionSquare, onAction]);
|
||||
|
||||
// Only allow dragging own pieces when it's your turn (v5 API)
|
||||
const canDragPiece = useCallback(({ piece }: { isSparePiece: boolean; piece: { pieceType: string }; square: string | null }): boolean => {
|
||||
if (isSpectator || isGameOver || !isMyTurn) return false;
|
||||
const pieceColor = piece.pieceType[0] === "w" ? "white" : "black";
|
||||
return pieceColor === myColor;
|
||||
}, [isSpectator, isGameOver, isMyTurn, myColor]);
|
||||
|
||||
// Clear selection when turn changes
|
||||
useEffect(() => {
|
||||
setSelectedSquare(null);
|
||||
setPromotionSquare(null);
|
||||
}, [view.turn]);
|
||||
|
||||
// Resolve player names
|
||||
const getPlayerName = (color: "white" | "black") => {
|
||||
if (isPlayerView(state)) {
|
||||
const id = color === (state as PlayerView).myColor ? myPlayerId : players.find(p => p.discordId !== myPlayerId)?.discordId;
|
||||
return players.find(p => p.discordId === id)?.username ?? (color === myColor ? "You" : "Opponent");
|
||||
}
|
||||
const spectatorState = state as SpectatorView;
|
||||
return players.find(p => p.discordId === spectatorState.players[color])?.username ?? color;
|
||||
};
|
||||
|
||||
// Determine opponent/bottom player for layout
|
||||
const topColor: "white" | "black" = boardOrientation === "white" ? "black" : "white";
|
||||
const bottomColor: "white" | "black" = boardOrientation;
|
||||
|
||||
// Draw offer UI for the receiving player
|
||||
const showDrawOffer = playerView && view.drawOffer && view.drawOffer !== playerView.myColor && !isGameOver;
|
||||
const showDrawButton = playerView && !view.drawOffer && !isGameOver;
|
||||
const pendingDrawOffer = playerView && view.drawOffer === playerView.myColor;
|
||||
|
||||
return (
|
||||
<div className="flex flex-col lg:flex-row gap-4 items-start">
|
||||
{/* Board + Clocks Column */}
|
||||
<div className="flex flex-col gap-2 w-full lg:w-auto">
|
||||
{/* Top player info + clock */}
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
<div className={`w-3 h-3 rounded-full shrink-0 ${topColor === "white" ? "bg-white" : "bg-zinc-800 ring-1 ring-zinc-600"}`} />
|
||||
<span className={`text-sm font-medium truncate ${view.turn === topColor && !isGameOver ? "text-primary" : "text-text-secondary"}`}>
|
||||
{getPlayerName(topColor)}
|
||||
</span>
|
||||
{view.turn === topColor && !isGameOver && (
|
||||
<span className="w-1.5 h-1.5 rounded-full bg-primary animate-pulse" />
|
||||
)}
|
||||
</div>
|
||||
{liveClock && (
|
||||
<ChessClock
|
||||
time={liveClock[topColor]}
|
||||
isActive={liveClock.activeColor === topColor}
|
||||
isLow={liveClock[topColor] < 30_000 && liveClock.activeColor === topColor}
|
||||
label={topColor === "white" ? "W" : "B"}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Chess Board */}
|
||||
<div className="relative rounded-xl overflow-hidden shadow-[0_8px_32px_rgba(0,0,0,0.4)] w-full max-w-[min(100%,560px)]">
|
||||
<Chessboard options={{
|
||||
position: view.fen,
|
||||
boardOrientation: boardOrientation,
|
||||
pieces: chessPieces,
|
||||
onPieceDrop,
|
||||
onSquareClick: ({ square }: { piece: unknown; square: string }) => onSquareClick(square as Square),
|
||||
canDragPiece,
|
||||
squareStyles: customSquareStyles,
|
||||
darkSquareStyle: { backgroundColor: "#2a3a5c" },
|
||||
lightSquareStyle: { backgroundColor: "#c8cad6" },
|
||||
boardStyle: { borderRadius: "0" },
|
||||
dropSquareStyle: { boxShadow: "inset 0 0 1px 4px rgba(233, 195, 73, 0.6)" },
|
||||
animationDurationInMs: 200,
|
||||
allowDragging: !isSpectator && !isGameOver,
|
||||
}} />
|
||||
|
||||
{/* Custom Promotion Dialog */}
|
||||
{promotionSquare && (
|
||||
<div className="absolute inset-0 z-10 flex items-center justify-center bg-black/50">
|
||||
<div className="bg-surface-container-highest rounded-xl p-3 shadow-[0_20px_40px_rgba(0,0,0,0.5)]">
|
||||
<div className="text-xs font-label text-text-tertiary mb-2 text-center">Promote to</div>
|
||||
<div className="flex gap-1.5">
|
||||
{["q", "r", "b", "n"].map(piece => (
|
||||
<button
|
||||
key={piece}
|
||||
onClick={() => handlePromotion(piece)}
|
||||
className="w-14 h-14 flex items-center justify-center rounded-lg bg-raised hover:bg-primary/20 transition-colors p-2"
|
||||
>
|
||||
<img
|
||||
src={pieceImgSrc(myColor, piece)}
|
||||
alt={piece}
|
||||
className="w-full h-full"
|
||||
draggable={false}
|
||||
/>
|
||||
</button>
|
||||
))}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Bottom player info + clock */}
|
||||
<div className="flex items-center justify-between gap-3">
|
||||
<div className="flex items-center gap-2 min-w-0">
|
||||
<div className={`w-3 h-3 rounded-full shrink-0 ${bottomColor === "white" ? "bg-white" : "bg-zinc-800 ring-1 ring-zinc-600"}`} />
|
||||
<span className={`text-sm font-medium truncate ${view.turn === bottomColor && !isGameOver ? "text-primary" : "text-text-secondary"}`}>
|
||||
{getPlayerName(bottomColor)}
|
||||
{!isSpectator && " (You)"}
|
||||
</span>
|
||||
{view.turn === bottomColor && !isGameOver && (
|
||||
<span className="w-1.5 h-1.5 rounded-full bg-primary animate-pulse" />
|
||||
)}
|
||||
</div>
|
||||
{liveClock && (
|
||||
<ChessClock
|
||||
time={liveClock[bottomColor]}
|
||||
isActive={liveClock.activeColor === bottomColor}
|
||||
isLow={liveClock[bottomColor] < 30_000 && liveClock.activeColor === bottomColor}
|
||||
label={bottomColor === "white" ? "W" : "B"}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Side Panel */}
|
||||
<div className="flex flex-col gap-3 w-full lg:w-72 lg:min-h-[400px]">
|
||||
{/* Game Status */}
|
||||
{isGameOver && (
|
||||
<div className={`rounded-xl px-4 py-3 ${
|
||||
view.result === "draw"
|
||||
? "bg-warning/10 border border-warning/20"
|
||||
: "bg-primary/10 border border-primary/20"
|
||||
}`}>
|
||||
<div className={`text-sm font-display font-semibold ${
|
||||
view.result === "draw" ? "text-warning" : "text-primary"
|
||||
}`}>
|
||||
{view.result === "draw"
|
||||
? "Draw"
|
||||
: `${getPlayerName(view.result!)} wins`}
|
||||
</div>
|
||||
<div className="text-xs text-text-tertiary mt-0.5">{view.resultReason}</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Turn Indicator */}
|
||||
{!isGameOver && !isSpectator && (
|
||||
<div className={`rounded-xl px-4 py-2.5 text-sm font-medium ${
|
||||
isMyTurn
|
||||
? "bg-success/10 text-success border border-success/20"
|
||||
: "bg-card text-text-tertiary"
|
||||
}`}>
|
||||
{isMyTurn ? "Your turn" : "Waiting for opponent..."}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Draw Offer Banner */}
|
||||
{showDrawOffer && (
|
||||
<div className="rounded-xl bg-info/10 border border-info/20 px-4 py-3">
|
||||
<div className="text-sm text-info font-medium mb-2">Draw offered</div>
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={() => onAction({ type: "accept_draw" })}
|
||||
className="flex-1 flex items-center justify-center gap-1.5 rounded-lg bg-success/15 text-success px-3 py-1.5 text-xs font-semibold hover:bg-success/25 transition-colors"
|
||||
>
|
||||
<Check className="w-3.5 h-3.5" /> Accept
|
||||
</button>
|
||||
<button
|
||||
onClick={() => onAction({ type: "decline_draw" })}
|
||||
className="flex-1 flex items-center justify-center gap-1.5 rounded-lg bg-destructive/15 text-destructive px-3 py-1.5 text-xs font-semibold hover:bg-destructive/25 transition-colors"
|
||||
>
|
||||
<X className="w-3.5 h-3.5" /> Decline
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{pendingDrawOffer && (
|
||||
<div className="rounded-xl bg-info/10 border border-info/20 px-4 py-2.5 text-xs text-info">
|
||||
Draw offer sent — waiting for response...
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Action Buttons */}
|
||||
{!isSpectator && !isGameOver && (
|
||||
<div className="flex gap-2">
|
||||
<button
|
||||
onClick={() => onAction({ type: "resign" })}
|
||||
className="flex-1 flex items-center justify-center gap-1.5 rounded-xl bg-raised px-3 py-2 text-xs font-label font-medium text-text-tertiary hover:text-destructive hover:bg-destructive/10 transition-colors"
|
||||
>
|
||||
<Flag className="w-3.5 h-3.5" /> Resign
|
||||
</button>
|
||||
{showDrawButton && (
|
||||
<button
|
||||
onClick={() => onAction({ type: "offer_draw" })}
|
||||
className="flex-1 flex items-center justify-center gap-1.5 rounded-xl bg-raised px-3 py-2 text-xs font-label font-medium text-text-tertiary hover:text-info hover:bg-info/10 transition-colors"
|
||||
>
|
||||
<Handshake className="w-3.5 h-3.5" /> Draw
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Move History */}
|
||||
<div className="rounded-xl bg-card flex flex-col flex-1 min-h-0">
|
||||
<div className="px-4 py-2.5 text-xs font-label font-semibold text-text-tertiary border-b border-border">
|
||||
Moves
|
||||
</div>
|
||||
<div ref={moveHistoryRef} className="px-4 py-2 overflow-y-auto max-h-60 lg:max-h-none lg:flex-1">
|
||||
<MoveHistory moves={view.moveHistory} containerRef={moveHistoryRef} />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Clock Info (if applicable) */}
|
||||
{liveClock && liveClock.increment > 0 && (
|
||||
<div className="text-[10px] text-text-disabled text-center">
|
||||
+{liveClock.increment / 1000}s increment per move
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -32,3 +32,15 @@ export const gameUIRegistry = {
|
||||
return Array.from(plugins.values());
|
||||
},
|
||||
};
|
||||
|
||||
// ── Register game UI plugins ──
|
||||
|
||||
import { ChessGame } from "./chess/ChessGame";
|
||||
|
||||
gameUIRegistry.register({
|
||||
slug: "chess",
|
||||
name: "Chess",
|
||||
icon: "\u265A",
|
||||
maxPlayers: 2,
|
||||
component: ChessGame,
|
||||
});
|
||||
|
||||
584
shared/games/chess/chess.plugin.test.ts
Normal file
@@ -0,0 +1,584 @@
|
||||
import { describe, it, expect, beforeEach, spyOn } from "bun:test";
|
||||
import { chessPlugin } from "./chess.plugin";
|
||||
import type { ChessState, ChessAction, ChessPlayerView, ChessSpectatorView } from "./chess.types";
|
||||
import type { GameResult } from "../types";
|
||||
|
||||
// ── Helpers ──
|
||||
|
||||
function createState(overrides?: Partial<Parameters<typeof chessPlugin.createInitialState>[1]>): ChessState {
|
||||
// Use a fixed seed by mocking Math.random so white/black assignment is deterministic
|
||||
const spy = spyOn(Math, "random").mockReturnValue(0.1); // < 0.5 → players[0] = white
|
||||
const state = chessPlugin.createInitialState(["alice", "bob"], overrides);
|
||||
spy.mockRestore();
|
||||
return state;
|
||||
}
|
||||
|
||||
function act(state: ChessState, action: ChessAction, playerId: string): ChessState {
|
||||
const result = chessPlugin.handleAction(state, action, playerId);
|
||||
if (!result.ok) throw new Error(`Action failed: ${result.error}`);
|
||||
return result.state;
|
||||
}
|
||||
|
||||
function tryAct(state: ChessState, action: ChessAction, playerId: string): GameResult<ChessState> {
|
||||
return chessPlugin.handleAction(state, action, playerId);
|
||||
}
|
||||
|
||||
/** Play a Scholar's Mate: 1. e4 e5 2. Qh5 Nc6 3. Bc4 Nf6 4. Qxf7# */
|
||||
function playScholarsMate(state: ChessState): ChessState {
|
||||
const white = state.players.white;
|
||||
const black = state.players.black;
|
||||
state = act(state, { type: "move", from: "e2", to: "e4" }, white);
|
||||
state = act(state, { type: "move", from: "e7", to: "e5" }, black);
|
||||
state = act(state, { type: "move", from: "d1", to: "h5" }, white);
|
||||
state = act(state, { type: "move", from: "b8", to: "c6" }, black);
|
||||
state = act(state, { type: "move", from: "f1", to: "c4" }, white);
|
||||
state = act(state, { type: "move", from: "g8", to: "f6" }, black);
|
||||
state = act(state, { type: "move", from: "h5", to: "f7" }, white); // Checkmate
|
||||
return state;
|
||||
}
|
||||
|
||||
// ── Tests ──
|
||||
|
||||
describe("chessPlugin", () => {
|
||||
describe("createInitialState", () => {
|
||||
it("should create a valid starting position", () => {
|
||||
const state = createState();
|
||||
expect(state.fen).toBe("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
|
||||
expect(state.result).toBeNull();
|
||||
expect(state.drawOffer).toBeNull();
|
||||
expect(state.moveHistory).toEqual([]);
|
||||
});
|
||||
|
||||
it("should assign players to white and black", () => {
|
||||
const state = createState();
|
||||
expect(state.players.white).toBe("alice");
|
||||
expect(state.players.black).toBe("bob");
|
||||
});
|
||||
|
||||
it("should randomize color assignment", () => {
|
||||
const spy = spyOn(Math, "random").mockReturnValue(0.9); // > 0.5 → swap
|
||||
const state = chessPlugin.createInitialState(["alice", "bob"]);
|
||||
spy.mockRestore();
|
||||
expect(state.players.white).toBe("bob");
|
||||
expect(state.players.black).toBe("alice");
|
||||
});
|
||||
|
||||
it("should default to blitz 5+3 time control", () => {
|
||||
const state = createState();
|
||||
expect(state.clock).not.toBeNull();
|
||||
expect(state.clock!.white).toBe(5 * 60_000);
|
||||
expect(state.clock!.black).toBe(5 * 60_000);
|
||||
expect(state.clock!.increment).toBe(3_000);
|
||||
});
|
||||
|
||||
it("should apply custom time control from options", () => {
|
||||
const state = createState({ timeControl: "bullet_1_0" });
|
||||
expect(state.clock!.white).toBe(60_000);
|
||||
expect(state.clock!.increment).toBe(0);
|
||||
});
|
||||
|
||||
it("should create no clock when timeControl is 'none'", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
expect(state.clock).toBeNull();
|
||||
});
|
||||
|
||||
it("should fall back to blitz 5+3 for unknown time control", () => {
|
||||
const state = createState({ timeControl: "invalid_key" });
|
||||
expect(state.clock!.white).toBe(5 * 60_000);
|
||||
expect(state.clock!.increment).toBe(3_000);
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAction — move", () => {
|
||||
let state: ChessState;
|
||||
|
||||
beforeEach(() => {
|
||||
state = createState({ timeControl: "none" });
|
||||
});
|
||||
|
||||
it("should accept a legal move", () => {
|
||||
const result = tryAct(state, { type: "move", from: "e2", to: "e4" }, "alice");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
// After 1. e4, FEN should show pawn on e4 (4th rank) and it's black's turn
|
||||
expect(result.state.fen).toContain("4P3"); // Pawn on e4 in FEN notation
|
||||
expect(result.state.fen).toContain(" b "); // Black to move
|
||||
expect(result.state.moveHistory).toHaveLength(1);
|
||||
expect(result.state.moveHistory[0]!.san).toBe("e4");
|
||||
expect(result.state.moveHistory[0]!.color).toBe("w");
|
||||
}
|
||||
});
|
||||
|
||||
it("should reject a move when it's not your turn", () => {
|
||||
const result = tryAct(state, { type: "move", from: "e7", to: "e5" }, "bob");
|
||||
// bob is black, white moves first — but bob is trying to move a black piece on white's turn
|
||||
// Actually, the check is playerColor !== turn, so bob (black) can't move when turn is white
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("It's not your turn");
|
||||
});
|
||||
|
||||
it("should reject an illegal move", () => {
|
||||
const result = tryAct(state, { type: "move", from: "e2", to: "e5" }, "alice");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("Illegal move");
|
||||
});
|
||||
|
||||
it("should reject a move from a nonsense square", () => {
|
||||
const result = tryAct(state, { type: "move", from: "z9", to: "z8" }, "alice");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("Illegal move");
|
||||
});
|
||||
|
||||
it("should detect checkmate", () => {
|
||||
const final = playScholarsMate(state);
|
||||
expect(final.result).toBe("white");
|
||||
expect(final.resultReason).toBe("Checkmate");
|
||||
});
|
||||
|
||||
it("should clear a pending draw offer on move", () => {
|
||||
state = act(state, { type: "offer_draw" }, "alice");
|
||||
expect(state.drawOffer).toBe("white");
|
||||
state = act(state, { type: "move", from: "e2", to: "e4" }, "alice");
|
||||
expect(state.drawOffer).toBeNull();
|
||||
});
|
||||
|
||||
it("should track move history for both colors", () => {
|
||||
state = act(state, { type: "move", from: "e2", to: "e4" }, "alice");
|
||||
state = act(state, { type: "move", from: "e7", to: "e5" }, "bob");
|
||||
expect(state.moveHistory).toHaveLength(2);
|
||||
expect(state.moveHistory[0]!.color).toBe("w");
|
||||
expect(state.moveHistory[1]!.color).toBe("b");
|
||||
});
|
||||
|
||||
it("should handle pawn promotion", () => {
|
||||
// Set up a position where white pawn is on the 7th rank
|
||||
const promoState: ChessState = {
|
||||
...state,
|
||||
fen: "8/P7/8/8/8/8/8/4K2k w - - 0 1", // White pawn on a7, kings
|
||||
};
|
||||
const result = tryAct(promoState, { type: "move", from: "a7", to: "a8", promotion: "q" }, "alice");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.state.fen).toContain("Q"); // Promoted to queen
|
||||
}
|
||||
});
|
||||
|
||||
it("should detect stalemate", () => {
|
||||
// Classic stalemate: black king in corner, no legal moves
|
||||
const stalemateState: ChessState = {
|
||||
...state,
|
||||
fen: "k7/2Q5/1K6/8/8/8/8/8 w - - 0 1",
|
||||
};
|
||||
// White plays Qc8 — but that's check not stalemate. Use Qb7 stalemate pattern.
|
||||
// Actually use: king a8, white queen a6, white king c8 → Qa7# is checkmate not stalemate
|
||||
// Classic stalemate: Ka1, Qb3 with Kc2
|
||||
const state2: ChessState = {
|
||||
...state,
|
||||
fen: "k7/8/1K6/8/8/8/8/2Q5 w - - 0 1", // Kc1 → Qc7 creates stalemate? No.
|
||||
};
|
||||
// Let's use a known stalemate position where it's black to move and has no legal moves
|
||||
const state3: ChessState = {
|
||||
...state,
|
||||
fen: "k7/2Q5/K7/8/8/8/8/8 b - - 0 1", // Black to move, king on a8, blocked
|
||||
};
|
||||
// In this position it's actually stalemate for black — black king has no legal moves
|
||||
// But wait, Qa7 is not played, the queen is on c7.
|
||||
// Ka8 can go to b8 (not attacked by Qc7? Qc7 attacks b8? c7 queen attacks b8 yes).
|
||||
// Ka8 also blocked by Ka6. So a8 king: a7 attacked by K and Q, b8 attacked by Q, b7 attacked by Q.
|
||||
// That IS stalemate — black king has no legal moves and is not in check.
|
||||
// But we need white to deliver the stalemate on their move, then the position detection happens.
|
||||
// Actually, chess.js checks stalemate from the new position. So we need a position where
|
||||
// after white's move, it's black's turn and black is stalemated.
|
||||
|
||||
// Simpler: use position where white's move leads to stalemate
|
||||
const stalemateSetup: ChessState = {
|
||||
...state,
|
||||
fen: "k7/8/1K6/2Q5/8/8/8/8 w - - 0 1",
|
||||
};
|
||||
// White plays Qc7 → position is k7/2Q5/1K6/8/8/8/8/8 b - - 1 1
|
||||
// Black king on a8: a7 attacked by K+Q, b8 attacked by Q, b7 attacked by K → stalemate!
|
||||
const result = tryAct(stalemateSetup, { type: "move", from: "c5", to: "c7" }, "alice");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.state.result).toBe("draw");
|
||||
expect(result.state.resultReason).toBe("Stalemate");
|
||||
}
|
||||
});
|
||||
|
||||
it("should detect insufficient material", () => {
|
||||
// King vs King — after a capture leaves only kings
|
||||
const kvk: ChessState = {
|
||||
...state,
|
||||
fen: "8/8/8/8/8/8/1p6/K1k5 b - - 0 1", // Black pawn can't create insufficiency easily
|
||||
};
|
||||
// Simpler: just set up K vs K directly after a capture
|
||||
// Actually, let's set up KN vs K where white captures last black piece
|
||||
const setup: ChessState = {
|
||||
...state,
|
||||
fen: "8/8/8/8/8/2n5/8/KN5k w - - 0 1",
|
||||
};
|
||||
// White Nb1 captures Nc3 → only K+N vs K (insufficient material? Actually K+N vs K IS insufficient)
|
||||
const result = tryAct(setup, { type: "move", from: "b1", to: "c3" }, "alice");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.state.result).toBe("draw");
|
||||
expect(result.state.resultReason).toBe("Insufficient material");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAction — move with clock", () => {
|
||||
it("should deduct time and add increment on move", () => {
|
||||
const state = createState({ timeControl: "blitz_5_3" });
|
||||
const now = Date.now();
|
||||
// Simulate 1 second passing
|
||||
const stateWithOldClock: ChessState = {
|
||||
...state,
|
||||
clock: { ...state.clock!, lastMoveAt: now - 1000 },
|
||||
};
|
||||
const result = tryAct(stateWithOldClock, { type: "move", from: "e2", to: "e4" }, "alice");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
// White had 300_000ms, spent ~1000ms, got 3000ms increment
|
||||
// Should be approximately 300_000 - 1000 + 3000 = 302_000
|
||||
expect(result.state.clock!.white).toBeGreaterThan(300_000);
|
||||
expect(result.state.clock!.white).toBeLessThanOrEqual(302_100); // small tolerance
|
||||
}
|
||||
});
|
||||
|
||||
it("should trigger timeout if player's clock has expired when they try to move", () => {
|
||||
const state = createState({ timeControl: "bullet_1_0" });
|
||||
const stateWithExpiredClock: ChessState = {
|
||||
...state,
|
||||
clock: { ...state.clock!, white: 500, lastMoveAt: Date.now() - 1000 },
|
||||
};
|
||||
// White tries to move but their clock is at 500ms with 1000ms elapsed → expired
|
||||
const result = tryAct(stateWithExpiredClock, { type: "move", from: "e2", to: "e4" }, "alice");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.state.result).toBe("black"); // Black wins on time
|
||||
expect(result.state.resultReason).toBe("Timeout");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAction — resign", () => {
|
||||
it("should let white resign (black wins)", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const result = tryAct(state, { type: "resign" }, "alice");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.state.result).toBe("black");
|
||||
expect(result.state.resultReason).toBe("Resignation");
|
||||
}
|
||||
});
|
||||
|
||||
it("should let black resign (white wins)", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const result = tryAct(state, { type: "resign" }, "bob");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.state.result).toBe("white");
|
||||
expect(result.state.resultReason).toBe("Resignation");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAction — draw offers", () => {
|
||||
let state: ChessState;
|
||||
|
||||
beforeEach(() => {
|
||||
state = createState({ timeControl: "none" });
|
||||
});
|
||||
|
||||
it("should allow offering a draw", () => {
|
||||
const result = tryAct(state, { type: "offer_draw" }, "alice");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) expect(result.state.drawOffer).toBe("white");
|
||||
});
|
||||
|
||||
it("should reject duplicate draw offer from same player", () => {
|
||||
state = act(state, { type: "offer_draw" }, "alice");
|
||||
const result = tryAct(state, { type: "offer_draw" }, "alice");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("You already offered a draw");
|
||||
});
|
||||
|
||||
it("should allow opponent to accept a draw", () => {
|
||||
state = act(state, { type: "offer_draw" }, "alice");
|
||||
const result = tryAct(state, { type: "accept_draw" }, "bob");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.state.result).toBe("draw");
|
||||
expect(result.state.resultReason).toBe("Agreement");
|
||||
expect(result.state.drawOffer).toBeNull();
|
||||
}
|
||||
});
|
||||
|
||||
it("should reject accepting when no draw offer exists", () => {
|
||||
const result = tryAct(state, { type: "accept_draw" }, "bob");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("No draw offer to accept");
|
||||
});
|
||||
|
||||
it("should reject accepting your own draw offer", () => {
|
||||
state = act(state, { type: "offer_draw" }, "alice");
|
||||
const result = tryAct(state, { type: "accept_draw" }, "alice");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("No draw offer to accept");
|
||||
});
|
||||
|
||||
it("should allow opponent to decline a draw", () => {
|
||||
state = act(state, { type: "offer_draw" }, "alice");
|
||||
const result = tryAct(state, { type: "decline_draw" }, "bob");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) expect(result.state.drawOffer).toBeNull();
|
||||
});
|
||||
|
||||
it("should reject declining when no draw offer exists", () => {
|
||||
const result = tryAct(state, { type: "decline_draw" }, "bob");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("No draw offer to decline");
|
||||
});
|
||||
|
||||
it("should reject declining your own draw offer", () => {
|
||||
state = act(state, { type: "offer_draw" }, "alice");
|
||||
const result = tryAct(state, { type: "decline_draw" }, "alice");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("No draw offer to decline");
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAction — claim_timeout", () => {
|
||||
it("should reject when there is no clock", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const result = tryAct(state, { type: "claim_timeout" }, "alice");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("No clock in this game");
|
||||
});
|
||||
|
||||
it("should reject when opponent still has time", () => {
|
||||
const state = createState({ timeControl: "blitz_5_3" });
|
||||
const result = tryAct(state, { type: "claim_timeout" }, "bob");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("Opponent still has time remaining");
|
||||
});
|
||||
|
||||
it("should succeed when opponent's time has expired", () => {
|
||||
const state = createState({ timeControl: "bullet_1_0" });
|
||||
// White's clock is 60_000ms — set lastMoveAt far in the past
|
||||
const expiredState: ChessState = {
|
||||
...state,
|
||||
clock: { ...state.clock!, lastMoveAt: Date.now() - 120_000 },
|
||||
};
|
||||
// It's white's turn, so white's clock is ticking. Bob (black) claims timeout.
|
||||
const result = tryAct(expiredState, { type: "claim_timeout" }, "bob");
|
||||
expect(result.ok).toBe(true);
|
||||
if (result.ok) {
|
||||
expect(result.state.result).toBe("black");
|
||||
expect(result.state.resultReason).toBe("Timeout");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("handleAction — guards", () => {
|
||||
it("should reject any action after game is over", () => {
|
||||
let state = createState({ timeControl: "none" });
|
||||
state = act(state, { type: "resign" }, "alice");
|
||||
expect(state.result).toBe("black");
|
||||
|
||||
const moveResult = tryAct(state, { type: "move", from: "e2", to: "e4" }, "bob");
|
||||
expect(moveResult.ok).toBe(false);
|
||||
if (!moveResult.ok) expect(moveResult.error).toBe("Game is already over");
|
||||
|
||||
const resignResult = tryAct(state, { type: "resign" }, "bob");
|
||||
expect(resignResult.ok).toBe(false);
|
||||
|
||||
const drawResult = tryAct(state, { type: "offer_draw" }, "bob");
|
||||
expect(drawResult.ok).toBe(false);
|
||||
});
|
||||
|
||||
it("should reject actions from a non-player", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const result = tryAct(state, { type: "move", from: "e2", to: "e4" }, "charlie");
|
||||
expect(result.ok).toBe(false);
|
||||
if (!result.ok) expect(result.error).toBe("You are not a player in this game");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getPlayerView", () => {
|
||||
it("should return the correct color for each player", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const aliceView = chessPlugin.getPlayerView(state, "alice") as ChessPlayerView;
|
||||
const bobView = chessPlugin.getPlayerView(state, "bob") as ChessPlayerView;
|
||||
expect(aliceView.myColor).toBe("white");
|
||||
expect(bobView.myColor).toBe("black");
|
||||
});
|
||||
|
||||
it("should include legal moves only for the active player", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const whiteView = chessPlugin.getPlayerView(state, "alice") as ChessPlayerView;
|
||||
const blackView = chessPlugin.getPlayerView(state, "bob") as ChessPlayerView;
|
||||
expect(whiteView.legalMoves.length).toBeGreaterThan(0); // White has 20 opening moves
|
||||
expect(blackView.legalMoves).toEqual([]); // Not black's turn
|
||||
});
|
||||
|
||||
it("should return 20 legal moves in starting position", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const view = chessPlugin.getPlayerView(state, "alice") as ChessPlayerView;
|
||||
expect(view.legalMoves.length).toBe(20); // 16 pawn moves + 4 knight moves
|
||||
});
|
||||
|
||||
it("should return no legal moves after game is over", () => {
|
||||
let state = createState({ timeControl: "none" });
|
||||
state = playScholarsMate(state);
|
||||
const view = chessPlugin.getPlayerView(state, "alice") as ChessPlayerView;
|
||||
expect(view.legalMoves).toEqual([]);
|
||||
});
|
||||
|
||||
it("should detect check in the view", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
// After 1. e4 e5 2. Qh5 — queen attacks f7, not check yet.
|
||||
// Use a direct check position: white queen on e7 checking black king on e8
|
||||
const checkState: ChessState = {
|
||||
...state,
|
||||
fen: "rnbqkbnr/ppppQppp/8/8/4P3/8/PPPP1PPP/RNB1KBNR b KQkq - 0 1",
|
||||
};
|
||||
const view = chessPlugin.getPlayerView(checkState, "bob") as ChessPlayerView;
|
||||
expect(view.isCheck).toBe(true);
|
||||
});
|
||||
|
||||
it("should include clock with activeColor when game is in progress", () => {
|
||||
const state = createState({ timeControl: "blitz_5_3" });
|
||||
const view = chessPlugin.getPlayerView(state, "alice") as ChessPlayerView;
|
||||
expect(view.clock).not.toBeNull();
|
||||
expect(view.clock!.activeColor).toBe("white");
|
||||
});
|
||||
|
||||
it("should set clock activeColor to null when game is over", () => {
|
||||
let state = createState({ timeControl: "blitz_5_3" });
|
||||
state = act(state, { type: "resign" }, "alice");
|
||||
const view = chessPlugin.getPlayerView(state, "alice") as ChessPlayerView;
|
||||
expect(view.clock!.activeColor).toBeNull();
|
||||
});
|
||||
|
||||
it("should default to white for an unknown player", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const view = chessPlugin.getPlayerView(state, "unknown") as ChessPlayerView;
|
||||
expect(view.myColor).toBe("white");
|
||||
});
|
||||
});
|
||||
|
||||
describe("getSpectatorView", () => {
|
||||
it("should include both player identifiers", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const view = chessPlugin.getSpectatorView(state) as ChessSpectatorView;
|
||||
expect(view.players.white).toBe("alice");
|
||||
expect(view.players.black).toBe("bob");
|
||||
});
|
||||
|
||||
it("should include current turn", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const view = chessPlugin.getSpectatorView(state) as ChessSpectatorView;
|
||||
expect(view.turn).toBe("white");
|
||||
});
|
||||
|
||||
it("should include clock data when present", () => {
|
||||
const state = createState({ timeControl: "rapid_10_0" });
|
||||
const view = chessPlugin.getSpectatorView(state) as ChessSpectatorView;
|
||||
expect(view.clock).not.toBeNull();
|
||||
expect(view.clock!.increment).toBe(0);
|
||||
});
|
||||
|
||||
it("should have null clock when time control is none", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const view = chessPlugin.getSpectatorView(state) as ChessSpectatorView;
|
||||
expect(view.clock).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe("isGameOver", () => {
|
||||
it("should return null when game is in progress", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
expect(chessPlugin.isGameOver!(state)).toBeNull();
|
||||
});
|
||||
|
||||
it("should return winner for white victory", () => {
|
||||
let state = createState({ timeControl: "none" });
|
||||
state = act(state, { type: "resign" }, "bob"); // Bob (black) resigns
|
||||
const result = chessPlugin.isGameOver!(state);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.winner).toBe("alice"); // White player ID
|
||||
expect(result!.reason).toBe("Resignation");
|
||||
});
|
||||
|
||||
it("should return winner for black victory", () => {
|
||||
let state = createState({ timeControl: "none" });
|
||||
state = act(state, { type: "resign" }, "alice"); // Alice (white) resigns
|
||||
const result = chessPlugin.isGameOver!(state);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.winner).toBe("bob"); // Black player ID
|
||||
});
|
||||
|
||||
it("should return null winner for draw", () => {
|
||||
let state = createState({ timeControl: "none" });
|
||||
state = act(state, { type: "offer_draw" }, "alice");
|
||||
state = act(state, { type: "accept_draw" }, "bob");
|
||||
const result = chessPlugin.isGameOver!(state);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.winner).toBeNull();
|
||||
expect(result!.reason).toBe("Agreement");
|
||||
});
|
||||
|
||||
it("should return checkmate result with correct winner", () => {
|
||||
let state = createState({ timeControl: "none" });
|
||||
state = playScholarsMate(state);
|
||||
const result = chessPlugin.isGameOver!(state);
|
||||
expect(result).not.toBeNull();
|
||||
expect(result!.winner).toBe("alice"); // White wins by checkmate
|
||||
expect(result!.reason).toBe("Checkmate");
|
||||
});
|
||||
});
|
||||
|
||||
describe("onPlayerDisconnect", () => {
|
||||
it("should award victory to opponent when player disconnects", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const result = chessPlugin.onPlayerDisconnect!(state, "alice");
|
||||
expect(result.result).toBe("black");
|
||||
expect(result.resultReason).toBe("Opponent disconnected");
|
||||
});
|
||||
|
||||
it("should award victory to white when black disconnects", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const result = chessPlugin.onPlayerDisconnect!(state, "bob");
|
||||
expect(result.result).toBe("white");
|
||||
expect(result.resultReason).toBe("Opponent disconnected");
|
||||
});
|
||||
|
||||
it("should not change state if game is already over", () => {
|
||||
let state = createState({ timeControl: "none" });
|
||||
state = act(state, { type: "resign" }, "alice");
|
||||
const result = chessPlugin.onPlayerDisconnect!(state, "bob");
|
||||
expect(result.result).toBe("black"); // Original result preserved
|
||||
expect(result.resultReason).toBe("Resignation"); // Not overwritten
|
||||
});
|
||||
|
||||
it("should not change state for unknown player", () => {
|
||||
const state = createState({ timeControl: "none" });
|
||||
const result = chessPlugin.onPlayerDisconnect!(state, "unknown");
|
||||
expect(result.result).toBeNull(); // No change
|
||||
});
|
||||
});
|
||||
|
||||
describe("plugin metadata", () => {
|
||||
it("should have correct slug and name", () => {
|
||||
expect(chessPlugin.slug).toBe("chess");
|
||||
expect(chessPlugin.name).toBe("Chess");
|
||||
});
|
||||
|
||||
it("should require exactly 2 players", () => {
|
||||
expect(chessPlugin.minPlayers).toBe(2);
|
||||
expect(chessPlugin.maxPlayers).toBe(2);
|
||||
});
|
||||
});
|
||||
});
|
||||
303
shared/games/chess/chess.plugin.ts
Normal file
@@ -0,0 +1,303 @@
|
||||
import { Chess } from "chess.js";
|
||||
import type { GamePlugin, GameResult, GameOverResult } from "../types";
|
||||
import type {
|
||||
ChessState, ChessAction, ChessPlayerView,
|
||||
ChessSpectatorView, ChessClock,
|
||||
} from "./chess.types";
|
||||
import { TIME_CONTROLS } from "./chess.types";
|
||||
|
||||
function colorOfPlayer(state: ChessState, playerId: string): "white" | "black" | null {
|
||||
if (state.players.white === playerId) return "white";
|
||||
if (state.players.black === playerId) return "black";
|
||||
return null;
|
||||
}
|
||||
|
||||
function currentTurn(fen: string): "white" | "black" {
|
||||
// FEN active color field is the second space-separated token
|
||||
return fen.split(" ")[1] === "w" ? "white" : "black";
|
||||
}
|
||||
|
||||
function applyClockTick(clock: ChessClock, turn: "white" | "black"): ChessClock {
|
||||
const now = Date.now();
|
||||
const elapsed = now - clock.lastMoveAt;
|
||||
const remaining = Math.max(0, clock[turn] - elapsed);
|
||||
return { ...clock, [turn]: remaining, lastMoveAt: now };
|
||||
}
|
||||
|
||||
function addIncrement(clock: ChessClock, color: "white" | "black"): ChessClock {
|
||||
return { ...clock, [color]: clock[color] + clock.increment };
|
||||
}
|
||||
|
||||
export const chessPlugin: GamePlugin<ChessState, ChessAction> = {
|
||||
slug: "chess",
|
||||
name: "Chess",
|
||||
minPlayers: 2,
|
||||
maxPlayers: 2,
|
||||
|
||||
createInitialState(players: string[], options?: Record<string, unknown>): ChessState {
|
||||
const game = new Chess();
|
||||
const timeControlKey = (options?.timeControl as string) ?? "blitz_5_3";
|
||||
const tc = TIME_CONTROLS[timeControlKey] ?? TIME_CONTROLS.blitz_5_3;
|
||||
|
||||
// Randomly assign colors
|
||||
const shuffled = Math.random() < 0.5 ? [players[0], players[1]] : [players[1], players[0]];
|
||||
|
||||
const clock: ChessClock | null = tc.time > 0
|
||||
? { white: tc.time, black: tc.time, increment: tc.increment, lastMoveAt: Date.now() }
|
||||
: null;
|
||||
|
||||
return {
|
||||
fen: game.fen(),
|
||||
pgn: game.pgn(),
|
||||
players: { white: shuffled[0], black: shuffled[1] },
|
||||
clock,
|
||||
drawOffer: null,
|
||||
result: null,
|
||||
resultReason: null,
|
||||
moveHistory: [],
|
||||
};
|
||||
},
|
||||
|
||||
handleAction(state: ChessState, action: ChessAction, playerId: string): GameResult<ChessState> {
|
||||
if (state.result) return { ok: false, error: "Game is already over" };
|
||||
|
||||
const playerColor = colorOfPlayer(state, playerId);
|
||||
if (!playerColor) return { ok: false, error: "You are not a player in this game" };
|
||||
|
||||
switch (action.type) {
|
||||
case "move": {
|
||||
const turn = currentTurn(state.fen);
|
||||
if (playerColor !== turn) return { ok: false, error: "It's not your turn" };
|
||||
|
||||
// Check clock timeout before allowing move
|
||||
if (state.clock) {
|
||||
const ticked = applyClockTick(state.clock, turn);
|
||||
if (ticked[turn] <= 0) {
|
||||
return {
|
||||
ok: true,
|
||||
state: {
|
||||
...state,
|
||||
clock: ticked,
|
||||
result: turn === "white" ? "black" : "white",
|
||||
resultReason: "Timeout",
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const game = new Chess(state.fen);
|
||||
try {
|
||||
const move = game.move({ from: action.from, to: action.to, promotion: action.promotion });
|
||||
if (!move) return { ok: false, error: "Illegal move" };
|
||||
} catch {
|
||||
return { ok: false, error: "Illegal move" };
|
||||
}
|
||||
|
||||
let newClock = state.clock;
|
||||
if (newClock) {
|
||||
newClock = applyClockTick(newClock, turn);
|
||||
newClock = addIncrement(newClock, turn);
|
||||
}
|
||||
|
||||
const moveEntry = {
|
||||
from: action.from,
|
||||
to: action.to,
|
||||
san: game.history().slice(-1)[0],
|
||||
color: turn === "white" ? "w" as const : "b" as const,
|
||||
};
|
||||
|
||||
let result: ChessState["result"] = null;
|
||||
let resultReason: string | null = null;
|
||||
|
||||
if (game.isCheckmate()) {
|
||||
result = turn;
|
||||
resultReason = "Checkmate";
|
||||
} else if (game.isStalemate()) {
|
||||
result = "draw";
|
||||
resultReason = "Stalemate";
|
||||
} else if (game.isThreefoldRepetition()) {
|
||||
result = "draw";
|
||||
resultReason = "Threefold repetition";
|
||||
} else if (game.isInsufficientMaterial()) {
|
||||
result = "draw";
|
||||
resultReason = "Insufficient material";
|
||||
} else if (game.isDraw()) {
|
||||
result = "draw";
|
||||
resultReason = "Draw (50-move rule)";
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true,
|
||||
state: {
|
||||
...state,
|
||||
fen: game.fen(),
|
||||
pgn: game.pgn(),
|
||||
clock: newClock,
|
||||
drawOffer: null, // any pending draw offer is cleared on move
|
||||
result,
|
||||
resultReason,
|
||||
moveHistory: [...state.moveHistory, moveEntry],
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
case "resign": {
|
||||
return {
|
||||
ok: true,
|
||||
state: {
|
||||
...state,
|
||||
result: playerColor === "white" ? "black" : "white",
|
||||
resultReason: "Resignation",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
case "offer_draw": {
|
||||
if (state.drawOffer === playerColor) return { ok: false, error: "You already offered a draw" };
|
||||
return {
|
||||
ok: true,
|
||||
state: { ...state, drawOffer: playerColor },
|
||||
};
|
||||
}
|
||||
|
||||
case "accept_draw": {
|
||||
const opponentColor = playerColor === "white" ? "black" : "white";
|
||||
if (state.drawOffer !== opponentColor) return { ok: false, error: "No draw offer to accept" };
|
||||
return {
|
||||
ok: true,
|
||||
state: {
|
||||
...state,
|
||||
result: "draw",
|
||||
resultReason: "Agreement",
|
||||
drawOffer: null,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
case "decline_draw": {
|
||||
const opponentColor = playerColor === "white" ? "black" : "white";
|
||||
if (state.drawOffer !== opponentColor) return { ok: false, error: "No draw offer to decline" };
|
||||
return {
|
||||
ok: true,
|
||||
state: { ...state, drawOffer: null },
|
||||
};
|
||||
}
|
||||
|
||||
case "claim_timeout": {
|
||||
if (!state.clock) return { ok: false, error: "No clock in this game" };
|
||||
const opponentColor = playerColor === "white" ? "black" : "white";
|
||||
const ticked = applyClockTick(state.clock, opponentColor);
|
||||
if (ticked[opponentColor] > 0) return { ok: false, error: "Opponent still has time remaining" };
|
||||
return {
|
||||
ok: true,
|
||||
state: {
|
||||
...state,
|
||||
clock: ticked,
|
||||
result: playerColor,
|
||||
resultReason: "Timeout",
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
default:
|
||||
return { ok: false, error: "Unknown action type" };
|
||||
}
|
||||
},
|
||||
|
||||
getPlayerView(state: ChessState, playerId: string): ChessPlayerView {
|
||||
const myColor = colorOfPlayer(state, playerId) ?? "white";
|
||||
const turn = currentTurn(state.fen);
|
||||
|
||||
let clockView: ChessPlayerView["clock"] = null;
|
||||
if (state.clock) {
|
||||
// Compute live remaining time for display
|
||||
const elapsed = state.result ? 0 : Date.now() - state.clock.lastMoveAt;
|
||||
const activeRemaining = Math.max(0, state.clock[turn] - elapsed);
|
||||
const inactiveColor = turn === "white" ? "black" : "white";
|
||||
clockView = {
|
||||
white: turn === "white" ? activeRemaining : state.clock.white,
|
||||
black: turn === "black" ? activeRemaining : state.clock.black,
|
||||
increment: state.clock.increment,
|
||||
activeColor: state.result ? null : turn,
|
||||
};
|
||||
}
|
||||
|
||||
// Compute legal moves for the current player
|
||||
const game = new Chess(state.fen);
|
||||
const legalMoves = myColor === turn && !state.result
|
||||
? game.moves({ verbose: true }).map(m => ({
|
||||
from: m.from,
|
||||
to: m.to,
|
||||
...(m.promotion ? { promotion: m.promotion } : {}),
|
||||
}))
|
||||
: [];
|
||||
|
||||
return {
|
||||
fen: state.fen,
|
||||
pgn: state.pgn,
|
||||
myColor,
|
||||
turn,
|
||||
clock: clockView,
|
||||
drawOffer: state.drawOffer,
|
||||
result: state.result,
|
||||
resultReason: state.resultReason,
|
||||
moveHistory: state.moveHistory,
|
||||
isCheck: game.isCheck(),
|
||||
legalMoves,
|
||||
};
|
||||
},
|
||||
|
||||
getSpectatorView(state: ChessState): ChessSpectatorView {
|
||||
const turn = currentTurn(state.fen);
|
||||
const game = new Chess(state.fen);
|
||||
|
||||
let clockView: ChessSpectatorView["clock"] = null;
|
||||
if (state.clock) {
|
||||
const elapsed = state.result ? 0 : Date.now() - state.clock.lastMoveAt;
|
||||
const activeRemaining = Math.max(0, state.clock[turn] - elapsed);
|
||||
clockView = {
|
||||
white: turn === "white" ? activeRemaining : state.clock.white,
|
||||
black: turn === "black" ? activeRemaining : state.clock.black,
|
||||
increment: state.clock.increment,
|
||||
activeColor: state.result ? null : turn,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
fen: state.fen,
|
||||
pgn: state.pgn,
|
||||
players: state.players,
|
||||
turn,
|
||||
clock: clockView,
|
||||
drawOffer: state.drawOffer,
|
||||
result: state.result,
|
||||
resultReason: state.resultReason,
|
||||
moveHistory: state.moveHistory,
|
||||
isCheck: game.isCheck(),
|
||||
};
|
||||
},
|
||||
|
||||
isGameOver(state: ChessState): GameOverResult | null {
|
||||
if (!state.result) return null;
|
||||
|
||||
let winner: string | null = null;
|
||||
if (state.result === "white") winner = state.players.white;
|
||||
else if (state.result === "black") winner = state.players.black;
|
||||
|
||||
return {
|
||||
winner,
|
||||
reason: state.resultReason ?? "Game over",
|
||||
};
|
||||
},
|
||||
|
||||
onPlayerDisconnect(state: ChessState, playerId: string): ChessState {
|
||||
if (state.result) return state;
|
||||
const color = colorOfPlayer(state, playerId);
|
||||
if (!color) return state;
|
||||
return {
|
||||
...state,
|
||||
result: color === "white" ? "black" : "white",
|
||||
resultReason: "Opponent disconnected",
|
||||
};
|
||||
},
|
||||
};
|
||||
69
shared/games/chess/chess.types.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
export interface ChessClock {
|
||||
white: number; // ms remaining
|
||||
black: number; // ms remaining
|
||||
increment: number; // ms added per move
|
||||
lastMoveAt: number; // timestamp of last move (for computing elapsed time)
|
||||
}
|
||||
|
||||
export interface ChessState {
|
||||
fen: string;
|
||||
pgn: string;
|
||||
players: { white: string; black: string };
|
||||
clock: ChessClock | null;
|
||||
drawOffer: "white" | "black" | null;
|
||||
result: "white" | "black" | "draw" | null;
|
||||
resultReason: string | null;
|
||||
moveHistory: { from: string; to: string; san: string; color: "w" | "b" }[];
|
||||
}
|
||||
|
||||
export type ChessAction =
|
||||
| { type: "move"; from: string; to: string; promotion?: string }
|
||||
| { type: "resign" }
|
||||
| { type: "offer_draw" }
|
||||
| { type: "accept_draw" }
|
||||
| { type: "decline_draw" }
|
||||
| { type: "claim_timeout" };
|
||||
|
||||
export interface ChessPlayerView {
|
||||
fen: string;
|
||||
pgn: string;
|
||||
myColor: "white" | "black";
|
||||
turn: "white" | "black";
|
||||
clock: { white: number; black: number; increment: number; activeColor: "white" | "black" | null } | null;
|
||||
drawOffer: "white" | "black" | null;
|
||||
result: "white" | "black" | "draw" | null;
|
||||
resultReason: string | null;
|
||||
moveHistory: { from: string; to: string; san: string; color: "w" | "b" }[];
|
||||
isCheck: boolean;
|
||||
legalMoves: { from: string; to: string; promotion?: string }[];
|
||||
}
|
||||
|
||||
export interface ChessSpectatorView {
|
||||
fen: string;
|
||||
pgn: string;
|
||||
players: { white: string; black: string };
|
||||
turn: "white" | "black";
|
||||
clock: { white: number; black: number; increment: number; activeColor: "white" | "black" | null } | null;
|
||||
drawOffer: "white" | "black" | null;
|
||||
result: "white" | "black" | "draw" | null;
|
||||
resultReason: string | null;
|
||||
moveHistory: { from: string; to: string; san: string; color: "w" | "b" }[];
|
||||
isCheck: boolean;
|
||||
}
|
||||
|
||||
export interface ChessRoomOptions {
|
||||
timeControl: "bullet_1_0" | "bullet_2_1" | "blitz_3_0" | "blitz_3_2" | "blitz_5_0" | "blitz_5_3" | "rapid_10_0" | "rapid_15_10" | "classical_30_0" | "none";
|
||||
}
|
||||
|
||||
export const TIME_CONTROLS: Record<string, { name: string; time: number; increment: number }> = {
|
||||
bullet_1_0: { name: "Bullet 1+0", time: 1 * 60_000, increment: 0 },
|
||||
bullet_2_1: { name: "Bullet 2+1", time: 2 * 60_000, increment: 1_000 },
|
||||
blitz_3_0: { name: "Blitz 3+0", time: 3 * 60_000, increment: 0 },
|
||||
blitz_3_2: { name: "Blitz 3+2", time: 3 * 60_000, increment: 2_000 },
|
||||
blitz_5_0: { name: "Blitz 5+0", time: 5 * 60_000, increment: 0 },
|
||||
blitz_5_3: { name: "Blitz 5+3", time: 5 * 60_000, increment: 3_000 },
|
||||
rapid_10_0: { name: "Rapid 10+0", time: 10 * 60_000, increment: 0 },
|
||||
rapid_15_10: { name: "Rapid 15+10", time: 15 * 60_000, increment: 10_000 },
|
||||
classical_30_0:{ name: "Classical 30+0", time: 30 * 60_000, increment: 0 },
|
||||
none: { name: "No Clock", time: 0, increment: 0 },
|
||||
};
|
||||
@@ -4,7 +4,7 @@ export interface GamePlugin<TState = unknown, TAction = unknown> {
|
||||
minPlayers: number;
|
||||
maxPlayers: number;
|
||||
|
||||
createInitialState(players: string[]): TState;
|
||||
createInitialState(players: string[], options?: Record<string, unknown>): TState;
|
||||
handleAction(state: TState, action: TAction, playerId: string): GameResult<TState>;
|
||||
getPlayerView(state: TState, playerId: string): unknown;
|
||||
getSpectatorView(state: TState): unknown;
|
||||
|
||||