rename reason to endReason

This commit is contained in:
Nathaniel Tampus
2023-04-01 20:58:29 +08:00
parent 86f880c181
commit 8d19c88c83
4 changed files with 20 additions and 19 deletions

View File

@@ -69,7 +69,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
useEffect(() => { useEffect(() => {
if ( if (
lobby.side === "s" || lobby.side === "s" ||
lobby.reason || lobby.endReason ||
lobby.winner || lobby.winner ||
!lobby.pgn || !lobby.pgn ||
!lobby.white || !lobby.white ||
@@ -246,11 +246,11 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
} }
function isDraggablePiece({ piece }: { piece: string }) { 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) { 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 // premove
if (lobby.side !== lobby.actualGame.turn()) return true; 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) { 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); getMoveOptions(sourceSquare);
} }
@@ -308,7 +308,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
function onSquareClick(square: Square) { function onSquareClick(square: Square) {
updateCustomSquares({ rightClicked: {} }); 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) { function resetFirstMove(square: Square) {
setMoveFrom(square); setMoveFrom(square);
@@ -494,7 +494,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
function claimAbandoned(type: "win" | "draw") { function claimAbandoned(type: "win" | "draw") {
if ( if (
lobby.side === "s" || lobby.side === "s" ||
lobby.reason || lobby.endReason ||
lobby.winner || lobby.winner ||
!lobby.pgn || !lobby.pgn ||
abandonSeconds > 0 || abandonSeconds > 0 ||
@@ -510,8 +510,8 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
<div className="relative h-min"> <div className="relative h-min">
{/* overlay */} {/* overlay */}
{(!lobby.white?.id || !lobby.black?.id) && ( {(!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="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 py-4 px-2"> <div className="bg-base-200 flex w-full items-center justify-center gap-4 px-2 py-4">
Waiting for opponent. Waiting for opponent.
{session?.user?.id !== lobby.white?.id && session?.user?.id !== lobby.black?.id && ( {session?.user?.id !== lobby.white?.id && session?.user?.id !== lobby.black?.id && (
<button <button
@@ -622,7 +622,7 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
</div> </div>
<div className="relative h-60 w-full min-w-fit"> <div className="relative h-60 w-full min-w-fit">
{(lobby.reason || {(lobby.endReason ||
(lobby.pgn && (lobby.pgn &&
lobby.white && lobby.white &&
session?.user?.id === lobby.white?.id && session?.user?.id === lobby.white?.id &&
@@ -634,8 +634,8 @@ export default function GamePage({ initialLobby }: { initialLobby: Game }) {
lobby.white && lobby.white &&
!lobby.white?.connected)) && ( !lobby.white?.connected)) && (
<div className="bg-neutral absolute w-full rounded-t-lg bg-opacity-95 p-2"> <div className="bg-neutral absolute w-full rounded-t-lg bg-opacity-95 p-2">
{lobby.reason ? ( {lobby.endReason ? (
lobby.reason === "abandoned" ? ( lobby.endReason === "abandoned" ? (
lobby.winner === "draw" ? ( lobby.winner === "draw" ? (
"The game ended in a draw due to abandonment." "The game ended in a draw due to abandonment."
) : ( ) : (

View File

@@ -60,7 +60,7 @@ export function initSocket(
winnerName, winnerName,
winnerSide winnerSide
}: { }: {
reason: string; reason: Game["endReason"];
winnerName?: string; winnerName?: string;
winnerSide?: "white" | "black" | "draw"; winnerSide?: "white" | "black" | "draw";
}) => { }) => {
@@ -89,7 +89,7 @@ export function initSocket(
} }
actions.updateLobby({ actions.updateLobby({
type: "updateLobby", type: "updateLobby",
payload: { reason, winner: winnerSide || "draw" } payload: { endReason: reason, winner: winnerSide || "draw" }
}); });
actions.addMessage(m); actions.addMessage(m);
} }

View File

@@ -1,4 +1,5 @@
import { activeGames } from "../db/models/game.model.js"; import { activeGames } from "../db/models/game.model.js";
import type { Game } from "@chessu/types";
import type { DisconnectReason, Socket } from "socket.io"; import type { DisconnectReason, Socket } from "socket.io";
import { Chess } from "chess.js"; import { Chess } from "chess.js";
import { io } from "../server.js"; import { io } from "../server.js";
@@ -127,7 +128,7 @@ export async function claimAbandoned(this: Socket, type: "win" | "draw") {
return; return;
} }
game.reason = "abandoned"; game.endReason = "abandoned";
if (type === "draw") { if (type === "draw") {
game.winner = "draw"; game.winner = "draw";
@@ -138,7 +139,7 @@ export async function claimAbandoned(this: Socket, type: "win" | "draw") {
} }
const gameOver = { const gameOver = {
reason: game.reason, reason: game.endReason,
winnerName: this.request.session.user.name, winnerName: this.request.session.user.name,
winnerSide: game.winner === "draw" ? undefined : game.winner 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 }) { 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]); 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(); const chess = new Chess();
if (game.pgn) { if (game.pgn) {
chess.loadPgn(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); const newMove = chess.move(m);
if (chess.isGameOver()) { if (chess.isGameOver()) {
let reason = ""; let reason: Game["endReason"];
if (chess.isCheckmate()) reason = "checkmate"; if (chess.isCheckmate()) reason = "checkmate";
else if (chess.isStalemate()) reason = "stalemate"; else if (chess.isStalemate()) reason = "stalemate";
else if (chess.isThreefoldRepetition()) reason = "repetition"; else if (chess.isThreefoldRepetition()) reason = "repetition";
@@ -191,7 +192,7 @@ export async function sendMove(this: Socket, m: { from: string; to: string; prom
} else { } else {
game.winner = "draw"; game.winner = "draw";
} }
game.reason = reason; game.endReason = reason;
io.to(game.code as string).emit("gameOver", { reason, winnerName, winnerSide }); io.to(game.code as string).emit("gameOver", { reason, winnerName, winnerSide });
} }
if (newMove) { if (newMove) {

2
types/index.d.ts vendored
View File

@@ -4,7 +4,7 @@ export interface Game {
white?: User; white?: User;
black?: User; black?: User;
winner?: "white" | "black" | "draw"; winner?: "white" | "black" | "draw";
reason?: string; endReason?: "draw" | "checkmate" | "stalemate" | "repetition" | "insufficient" | "abandoned";
host?: User; host?: User;
code?: string; code?: string;
unlisted?: boolean; unlisted?: boolean;