fix(chess): robust client-side validation and admin self-play support
Some checks failed
Deploy to Production / test (push) Failing after 35s

Wrap chess.js move validation in try-catch for invalid moves. Fix admin
self-play by detecting when both players share the same ID and allowing
either color's pieces to be dragged on the current turn.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
syntaxbullet
2026-04-02 14:27:33 +02:00
parent 87b66cd65d
commit 24211dca14

View File

@@ -25,9 +25,12 @@ export function ChessBoard({ state, myPlayerId, isSpectator, onAction, players }
return <div className="text-text-tertiary text-sm">Waiting for game to start...</div>;
}
const myColor = chess.players.white === myPlayerId ? "white" : chess.players.black === myPlayerId ? "black" : null;
const isWhite = chess.players.white === myPlayerId;
const isBlack = chess.players.black === myPlayerId;
const isBothSides = isWhite && isBlack; // admin self-play
const myColor = isWhite ? "white" : isBlack ? "black" : null;
const turn = game.turn() === "w" ? "white" : "black";
const isMyTurn = myColor === turn && !isSpectator;
const isMyTurn = (isBothSides || myColor === turn) && !isSpectator;
const boardOrientation = myColor ?? "white";
const opponentId = myColor === "white" ? chess.players.black : chess.players.white;
@@ -50,7 +53,12 @@ export function ChessBoard({ state, myPlayerId, isSpectator, onAction, players }
}
}
const move = testGame.move({ from: sourceSquare, to: targetSquare });
let move;
try {
move = testGame.move({ from: sourceSquare, to: targetSquare });
} catch {
return false;
}
if (!move) return false;
onAction({ type: "move", from: sourceSquare, to: targetSquare });
@@ -70,6 +78,10 @@ export function ChessBoard({ state, myPlayerId, isSpectator, onAction, players }
function isDraggablePiece({ piece }: { piece: string }): boolean {
if (isSpectator || !isMyTurn) return false;
if (isBothSides) {
const pieceColor = piece[0] === "w" ? "white" : "black";
return pieceColor === turn;
}
const pieceColor = piece[0] === "w" ? "white" : "black";
return pieceColor === myColor;
}