server-side move validation improvements
This commit is contained in:
@@ -87,7 +87,17 @@ export async function sendMove(this: Socket, m: { from: string; to: string; prom
|
||||
if (game.pgn) {
|
||||
chess.loadPgn(game.pgn);
|
||||
}
|
||||
|
||||
try {
|
||||
const prevTurn = chess.turn();
|
||||
|
||||
if (
|
||||
(prevTurn === "b" && this.request.session.id !== game.black?.id) ||
|
||||
(prevTurn === "w" && this.request.session.id !== game.white?.id)
|
||||
) {
|
||||
throw new Error("not turn to move");
|
||||
}
|
||||
|
||||
const newMove = chess.move(m);
|
||||
if (chess.isGameOver()) {
|
||||
let reason = "";
|
||||
@@ -112,11 +122,15 @@ export async function sendMove(this: Socket, m: { from: string; to: string; prom
|
||||
}
|
||||
io.to(game.code as string).emit("gameOver", { reason, winnerName, winnerSide });
|
||||
}
|
||||
if (newMove === null) {
|
||||
this.emit("receivedLatestGame", game);
|
||||
} else {
|
||||
if (newMove) {
|
||||
game.pgn = chess.pgn();
|
||||
this.to(game.code as string).emit("receivedMove", m);
|
||||
} else {
|
||||
throw new Error("invalid move");
|
||||
}
|
||||
} catch (e) {
|
||||
console.log("sendMove error: " + e);
|
||||
this.emit("receivedLatestGame", game);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user