handle invalid move exceptioin & support promotion
This commit is contained in:
@@ -39,10 +39,8 @@ const Board = () => {
|
|||||||
|
|
||||||
socket.on("receivedLatestGame", (latestGame: Game) => {
|
socket.on("receivedLatestGame", (latestGame: Game) => {
|
||||||
if (latestGame.pgn) {
|
if (latestGame.pgn) {
|
||||||
const loadSuccess = game.loadPgn(latestGame.pgn);
|
game.loadPgn(latestGame.pgn);
|
||||||
if (loadSuccess) {
|
forceUpdate();
|
||||||
forceUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if (latestGame.black?.id === session?.user.id) {
|
if (latestGame.black?.id === session?.user.id) {
|
||||||
if (side !== "b") setSide("b");
|
if (side !== "b") setSide("b");
|
||||||
@@ -80,16 +78,21 @@ const Board = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function makeMove(m: { from: string; to: string; promotion?: string }) {
|
function makeMove(m: { from: string; to: string; promotion?: string }) {
|
||||||
const result = game.move({ from: m.from, to: m.to });
|
try {
|
||||||
if (result) {
|
const result = game.move(m);
|
||||||
setOptionSquares({
|
if (result) {
|
||||||
[m.from]: { background: "rgba(255, 255, 0, 0.4)" },
|
setOptionSquares({
|
||||||
[m.to]: { background: "rgba(255, 255, 0, 0.4)" }
|
[m.from]: { background: "rgba(255, 255, 0, 0.4)" },
|
||||||
});
|
[m.to]: { background: "rgba(255, 255, 0, 0.4)" }
|
||||||
} else {
|
});
|
||||||
|
return result;
|
||||||
|
} else {
|
||||||
|
throw new Error("invalid move");
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
setOptionSquares({});
|
setOptionSquares({});
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isDraggablePiece({ piece }: { piece: string }) {
|
function isDraggablePiece({ piece }: { piece: string }) {
|
||||||
@@ -100,13 +103,15 @@ const Board = () => {
|
|||||||
function onDrop(sourceSquare: Square, targetSquare: Square) {
|
function onDrop(sourceSquare: Square, targetSquare: Square) {
|
||||||
if (side !== game.turn()) return false;
|
if (side !== game.turn()) return false;
|
||||||
|
|
||||||
const move = makeMove({
|
const moveDetails = {
|
||||||
from: sourceSquare,
|
from: sourceSquare,
|
||||||
to: targetSquare,
|
to: targetSquare,
|
||||||
promotion: "q"
|
promotion: "q"
|
||||||
});
|
};
|
||||||
if (move === null) return false; // illegal move
|
|
||||||
socket?.emit("sendMove", { from: move.from, to: move.to });
|
const move = makeMove(moveDetails);
|
||||||
|
if (!move) return false; // illegal move
|
||||||
|
socket?.emit("sendMove", moveDetails);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -163,16 +168,18 @@ const Board = () => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const move = makeMove({
|
const moveDetails = {
|
||||||
from: moveFrom as Square,
|
from: moveFrom as Square,
|
||||||
to: square,
|
to: square,
|
||||||
promotion: "q"
|
promotion: "q"
|
||||||
});
|
};
|
||||||
if (move === null) {
|
|
||||||
|
const move = makeMove(moveDetails);
|
||||||
|
if (!move) {
|
||||||
resetFirstMove(square);
|
resetFirstMove(square);
|
||||||
} else {
|
} else {
|
||||||
setMoveFrom("");
|
setMoveFrom("");
|
||||||
socket?.emit("sendMove", { from: move.from, to: move.to });
|
socket?.emit("sendMove", moveDetails);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -127,7 +127,7 @@ export async function sendMove(this: Socket, m: { from: string; to: string; prom
|
|||||||
this.emit("receivedLatestGame", game);
|
this.emit("receivedLatestGame", game);
|
||||||
} else {
|
} else {
|
||||||
game.pgn = chess.pgn();
|
game.pgn = chess.pgn();
|
||||||
this.to(game.code as string).emit("receivedMove", { from: m.from, to: m.to });
|
this.to(game.code as string).emit("receivedMove", m);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user