rename reason to endReason
This commit is contained in:
@@ -69,7 +69,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
|
||||
useEffect(() => {
|
||||
if (
|
||||
lobby.side === "s" ||
|
||||
lobby.reason ||
|
||||
lobby.endReason ||
|
||||
lobby.winner ||
|
||||
!lobby.pgn ||
|
||||
!lobby.white ||
|
||||
@@ -246,11 +246,11 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
|
||||
}
|
||||
|
||||
function isDraggablePiece({ piece }: { piece: string }) {
|
||||
return piece.startsWith(lobby.side) && !lobby.reason && !lobby.winner;
|
||||
return piece.startsWith(lobby.side) && !lobby.endReason && !lobby.winner;
|
||||
}
|
||||
|
||||
function onDrop(sourceSquare: Square, targetSquare: Square) {
|
||||
if (lobby.side === "s" || navFen || lobby.reason || lobby.winner) return false;
|
||||
if (lobby.side === "s" || navFen || lobby.endReason || lobby.winner) return false;
|
||||
|
||||
// premove
|
||||
if (lobby.side !== lobby.actualGame.turn()) return true;
|
||||
@@ -297,7 +297,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
|
||||
}
|
||||
|
||||
function onPieceDragBegin(_piece: string, sourceSquare: Square) {
|
||||
if (lobby.side !== lobby.actualGame.turn() || navFen || lobby.reason || lobby.winner) return;
|
||||
if (lobby.side !== lobby.actualGame.turn() || navFen || lobby.endReason || lobby.winner) return;
|
||||
|
||||
getMoveOptions(sourceSquare);
|
||||
}
|
||||
@@ -308,7 +308,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
|
||||
|
||||
function onSquareClick(square: Square) {
|
||||
updateCustomSquares({ rightClicked: {} });
|
||||
if (lobby.side !== lobby.actualGame.turn() || navFen || lobby.reason || lobby.winner) return;
|
||||
if (lobby.side !== lobby.actualGame.turn() || navFen || lobby.endReason || lobby.winner) return;
|
||||
|
||||
function resetFirstMove(square: Square) {
|
||||
setMoveFrom(square);
|
||||
@@ -494,7 +494,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
|
||||
function claimAbandoned(type: "win" | "draw") {
|
||||
if (
|
||||
lobby.side === "s" ||
|
||||
lobby.reason ||
|
||||
lobby.endReason ||
|
||||
lobby.winner ||
|
||||
!lobby.pgn ||
|
||||
abandonSeconds > 0 ||
|
||||
@@ -510,8 +510,8 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
|
||||
<div className="relative h-min">
|
||||
{/* overlay */}
|
||||
{(!lobby.white?.id || !lobby.black?.id) && (
|
||||
<div className="absolute top-0 right-0 bottom-0 z-10 flex h-full w-full items-center justify-center bg-black bg-opacity-70">
|
||||
<div className="bg-base-200 flex w-full items-center justify-center gap-4 py-4 px-2">
|
||||
<div className="absolute bottom-0 right-0 top-0 z-10 flex h-full w-full items-center justify-center bg-black bg-opacity-70">
|
||||
<div className="bg-base-200 flex w-full items-center justify-center gap-4 px-2 py-4">
|
||||
Waiting for opponent.
|
||||
{session?.user?.id !== lobby.white?.id && session?.user?.id !== lobby.black?.id && (
|
||||
<button
|
||||
@@ -622,7 +622,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
|
||||
</div>
|
||||
|
||||
<div className="relative h-60 w-full min-w-fit">
|
||||
{(lobby.reason ||
|
||||
{(lobby.endReason ||
|
||||
(lobby.pgn &&
|
||||
lobby.white &&
|
||||
session?.user?.id === lobby.white?.id &&
|
||||
@@ -634,8 +634,8 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
|
||||
lobby.white &&
|
||||
!lobby.white?.connected)) && (
|
||||
<div className="bg-neutral absolute w-full rounded-t-lg bg-opacity-95 p-2">
|
||||
{lobby.reason ? (
|
||||
lobby.reason === "abandoned" ? (
|
||||
{lobby.endReason ? (
|
||||
lobby.endReason === "abandoned" ? (
|
||||
lobby.winner === "draw" ? (
|
||||
"The game ended in a draw due to abandonment."
|
||||
) : (
|
||||
|
||||
@@ -60,7 +60,7 @@ export function initSocket(
|
||||
winnerName,
|
||||
winnerSide
|
||||
}: {
|
||||
reason: string;
|
||||
reason: Game["endReason"];
|
||||
winnerName?: string;
|
||||
winnerSide?: "white" | "black" | "draw";
|
||||
}) => {
|
||||
@@ -89,7 +89,7 @@ export function initSocket(
|
||||
}
|
||||
actions.updateLobby({
|
||||
type: "updateLobby",
|
||||
payload: { reason, winner: winnerSide || "draw" }
|
||||
payload: { endReason: reason, winner: winnerSide || "draw" }
|
||||
});
|
||||
actions.addMessage(m);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { 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";
|
||||
import { io } from "../server.js";
|
||||
@@ -127,7 +128,7 @@ export async function claimAbandoned(this: Socket, type: "win" | "draw") {
|
||||
return;
|
||||
}
|
||||
|
||||
game.reason = "abandoned";
|
||||
game.endReason = "abandoned";
|
||||
|
||||
if (type === "draw") {
|
||||
game.winner = "draw";
|
||||
@@ -138,7 +139,7 @@ export async function claimAbandoned(this: Socket, type: "win" | "draw") {
|
||||
}
|
||||
|
||||
const gameOver = {
|
||||
reason: game.reason,
|
||||
reason: game.endReason,
|
||||
winnerName: this.request.session.user.name,
|
||||
winnerSide: game.winner === "draw" ? undefined : game.winner
|
||||
};
|
||||
@@ -153,7 +154,7 @@ export async function getLatestGame(this: Socket) {
|
||||
|
||||
export async function sendMove(this: Socket, m: { from: string; to: string; promotion?: string }) {
|
||||
const game = activeGames.find((g) => g.code === Array.from(this.rooms)[1]);
|
||||
if (!game || game.reason || game.winner) return;
|
||||
if (!game || game.endReason || game.winner) return;
|
||||
const chess = new Chess();
|
||||
if (game.pgn) {
|
||||
chess.loadPgn(game.pgn);
|
||||
@@ -171,7 +172,7 @@ export async function sendMove(this: Socket, m: { from: string; to: string; prom
|
||||
|
||||
const newMove = chess.move(m);
|
||||
if (chess.isGameOver()) {
|
||||
let reason = "";
|
||||
let reason: Game["endReason"];
|
||||
if (chess.isCheckmate()) reason = "checkmate";
|
||||
else if (chess.isStalemate()) reason = "stalemate";
|
||||
else if (chess.isThreefoldRepetition()) reason = "repetition";
|
||||
@@ -191,7 +192,7 @@ export async function sendMove(this: Socket, m: { from: string; to: string; prom
|
||||
} else {
|
||||
game.winner = "draw";
|
||||
}
|
||||
game.reason = reason;
|
||||
game.endReason = reason;
|
||||
io.to(game.code as string).emit("gameOver", { reason, winnerName, winnerSide });
|
||||
}
|
||||
if (newMove) {
|
||||
|
||||
2
types/index.d.ts
vendored
2
types/index.d.ts
vendored
@@ -4,7 +4,7 @@ export interface Game {
|
||||
white?: User;
|
||||
black?: User;
|
||||
winner?: "white" | "black" | "draw";
|
||||
reason?: string;
|
||||
endReason?: "draw" | "checkmate" | "stalemate" | "repetition" | "insufficient" | "abandoned";
|
||||
host?: User;
|
||||
code?: string;
|
||||
unlisted?: boolean;
|
||||
|
||||
Reference in New Issue
Block a user