diff --git a/frontend/src/components/Challenges.jsx b/frontend/src/components/Challenges.jsx index 558d634..19fa210 100644 --- a/frontend/src/components/Challenges.jsx +++ b/frontend/src/components/Challenges.jsx @@ -1,7 +1,7 @@ import { Button, Group, Stack, Text, Title } from '@mantine/core'; import React, { useEffect, useState } from 'react' import { useNavigate } from 'react-router-dom'; -import { getUserData } from '../../utils/auth' +import { getAuthToken, getUserData } from '../../utils/auth' const Challenges = () => { const navigate = useNavigate(); @@ -15,7 +15,11 @@ const Challenges = () => { const fetchData = async () => { let url = `${import.meta.env.VITE_BACKEND_HOST}/api/user/${username}/challenges`; try { - response = await fetch(url, { signal: abortController.signal }) + response = await fetch(url, { + signal: abortController.signal, headers: { + 'Authorization': `Bearer ${getAuthToken()}` + } + }) const data = await response.json(); if (data.success) { setChallenges(data.challenges); diff --git a/frontend/src/constants.js b/frontend/src/constants.js index c9548bc..5adac31 100644 --- a/frontend/src/constants.js +++ b/frontend/src/constants.js @@ -1,4 +1,6 @@ export const SOCKET_EVENTS = { + CONNECT:'connect', + DISCONNECT:'disconnect', CONNECTION: "connection", JOIN_ROOM: "join-room", JOIN_ROOM_SUCCESS: "join-room-success", diff --git a/frontend/src/context/chess-game-context.jsx b/frontend/src/context/chess-game-context.jsx index 4adb607..f94987f 100644 --- a/frontend/src/context/chess-game-context.jsx +++ b/frontend/src/context/chess-game-context.jsx @@ -22,7 +22,11 @@ const reducer = (state, action) => { let updatedGameHistory = state.gameHistory; let { san, after } = newChessObj.move(action.val); updatedGameHistory.push({ move: san, fen: after }); - return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 }; + if (newChessObj.isCheckmate()) { + return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1, hasGameEnded: true, gameEndedReason: 'CHECKMATE' }; + } else { + return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 }; + } } case CAPTURE_PIECE: { @@ -31,7 +35,11 @@ const reducer = (state, action) => { let updatedGameHistory = state.gameHistory; let { san, after } = newChessObj.move(action.val); updatedGameHistory.push({ move: san, fen: after }); - return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 }; + if (newChessObj.isCheckmate()) { + return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1, hasGameEnded: true, gameEndedReason: 'CHECKMATE' }; + } else { + return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(localStorage.getItem('myColor')), moveHints: [], selected: null, gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 }; + } } case JUMP_TO: { @@ -61,8 +69,10 @@ function chessGameStateInit(myColor) { let gameHistory = []; let selected = null; let currentIndex = -1; + let hasGameEnded = false; + let gameEndedReason = ""; - return { chess, chessBoard, moveHints, selected, gameHistory, currentIndex }; + return { chess, chessBoard, moveHints, selected, gameHistory, currentIndex, hasGameEnded, gameEndedReason }; } // the ChessGameContextProvider seperates the game logic from the ChessBoard component and exposes @@ -70,7 +80,7 @@ function chessGameStateInit(myColor) { const ChessGameContextProvider = ({ children }) => { let myColor = localStorage.getItem('myColor'); console.log('INSIDE CONTEXT PROVIDER'); - const [{ chess, chessBoard, moveHints, selected, gameHistory, currentIndex }, dispatch] = useReducer(reducer, myColor, chessGameStateInit); + const [{ chess, chessBoard, moveHints, selected, gameHistory, currentIndex, hasGameEnded, gameEndedReason }, dispatch] = useReducer(reducer, myColor, chessGameStateInit); const [isTimerOn, setIsTimerOn] = useState(true); console.log(gameHistory); @@ -192,7 +202,9 @@ const ChessGameContextProvider = ({ children }) => { return ( {children}