fix(chess): prevent duplicate players and fix move detection
Some checks failed
Deploy to Production / test (push) Failing after 38s
Some checks failed
Deploy to Production / test (push) Failing after 38s
- Prevent same player from joining as both white and black - Add validation to reject duplicate players in RoomManager - Fix spectator status not resetting when joining as player - Use ref to track latest chess state in ChessBoard for accurate move validation
This commit is contained in:
@@ -17,6 +17,12 @@ export function ChessBoard({ state, myPlayerId, isSpectator, onAction, players }
|
||||
const [promotionTo, setPromotionTo] = useState<string | null>(null);
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const [boardWidth, setBoardWidth] = useState(400);
|
||||
|
||||
// Track latest state in ref to avoid stale closures
|
||||
const chessRef = useRef(chess);
|
||||
useEffect(() => {
|
||||
chessRef.current = chess;
|
||||
}, [chess]);
|
||||
|
||||
// Responsive board sizing
|
||||
useEffect(() => {
|
||||
@@ -56,7 +62,7 @@ export function ChessBoard({ state, myPlayerId, isSpectator, onAction, players }
|
||||
function onDrop(sourceSquare: string, targetSquare: string): boolean {
|
||||
if (isSpectator || !isMyTurn) return false;
|
||||
|
||||
const testGame = new Chess(chess.fen);
|
||||
const testGame = new Chess(chessRef.current.fen);
|
||||
|
||||
// Check if this is a promotion move
|
||||
const piece = testGame.get(sourceSquare as any);
|
||||
|
||||
@@ -56,13 +56,16 @@ export function useGameRoom(roomId: string, userId: string, role?: string) {
|
||||
return {
|
||||
...prev,
|
||||
spectators: [...prev.spectators.filter(s => s.discordId !== msg.player.discordId), msg.player],
|
||||
isSpectator: isMe ? true : prev.isSpectator,
|
||||
isSpectator: isMe || prev.isSpectator,
|
||||
roomStatus: prev.roomStatus === "connecting" ? "waiting" : prev.roomStatus,
|
||||
};
|
||||
}
|
||||
|
||||
const isMe = msg.player.discordId === userId;
|
||||
return {
|
||||
...prev,
|
||||
players: [...prev.players.filter(p => p.discordId !== msg.player.discordId), msg.player],
|
||||
isSpectator: isMe ? false : prev.isSpectator,
|
||||
roomStatus: prev.roomStatus === "connecting" ? "waiting" : prev.roomStatus,
|
||||
};
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user