save finished games to db

This commit is contained in:
Nathaniel Tampus
2023-04-02 00:00:09 +08:00
parent 5088e25769
commit 75eb21c337
2 changed files with 37 additions and 27 deletions

View File

@@ -58,11 +58,13 @@ export function initSocket(
({
reason,
winnerName,
winnerSide
winnerSide,
id
}: {
reason: Game["endReason"];
winnerName?: string;
winnerSide?: "white" | "black" | "draw";
id: number;
}) => {
const m = {
author: { name: "server" }
@@ -89,7 +91,7 @@ export function initSocket(
}
actions.updateLobby({
type: "updateLobby",
payload: { endReason: reason, winner: winnerSide || "draw" }
payload: { endReason: reason, winner: winnerSide || "draw", id }
});
actions.addMessage(m);
}

View File

@@ -1,4 +1,4 @@
import { activeGames } from "../db/models/game.model.js";
import GameModel, { activeGames } from "../db/models/game.model.js";
import type { Game } from "@chessu/types";
import type { DisconnectReason, Socket } from "socket.io";
import { Chess } from "chess.js";
@@ -138,10 +138,14 @@ export async function claimAbandoned(this: Socket, type: "win" | "draw") {
game.winner = "black";
}
const { id } = (await GameModel.create(game)) as Game;
game.id = id;
const gameOver = {
reason: game.endReason,
winnerName: this.request.session.user.name,
winnerSide: game.winner === "draw" ? undefined : game.winner
winnerSide: game.winner === "draw" ? undefined : game.winner,
id
};
io.to(game.code as string).emit("gameOver", gameOver);
@@ -171,32 +175,36 @@ export async function sendMove(this: Socket, m: { from: string; to: string; prom
}
const newMove = chess.move(m);
if (chess.isGameOver()) {
let reason: Game["endReason"];
if (chess.isCheckmate()) reason = "checkmate";
else if (chess.isStalemate()) reason = "stalemate";
else if (chess.isThreefoldRepetition()) reason = "repetition";
else if (chess.isInsufficientMaterial()) reason = "insufficient";
else if (chess.isDraw()) reason = "draw";
const winnerSide =
reason === "checkmate" ? (prevTurn === "w" ? "white" : "black") : undefined;
const winnerName =
reason === "checkmate"
? winnerSide === "white"
? game.white?.name
: game.black?.name
: undefined;
if (reason === "checkmate") {
game.winner = winnerSide;
} else {
game.winner = "draw";
}
game.endReason = reason;
io.to(game.code as string).emit("gameOver", { reason, winnerName, winnerSide });
}
if (newMove) {
game.pgn = chess.pgn();
if (chess.isGameOver()) {
let reason: Game["endReason"];
if (chess.isCheckmate()) reason = "checkmate";
else if (chess.isStalemate()) reason = "stalemate";
else if (chess.isThreefoldRepetition()) reason = "repetition";
else if (chess.isInsufficientMaterial()) reason = "insufficient";
else if (chess.isDraw()) reason = "draw";
const winnerSide =
reason === "checkmate" ? (prevTurn === "w" ? "white" : "black") : undefined;
const winnerName =
reason === "checkmate"
? winnerSide === "white"
? game.white?.name
: game.black?.name
: undefined;
if (reason === "checkmate") {
game.winner = winnerSide;
} else {
game.winner = "draw";
}
game.endReason = reason;
const { id } = (await GameModel.create(game)) as Game; // save game to db
game.id = id;
io.to(game.code as string).emit("gameOver", { reason, winnerName, winnerSide, id });
}
this.to(game.code as string).emit("receivedMove", m);
} else {
throw new Error("invalid move");