Feature added: Exit the game when opponent resigns
This commit is contained in:
@@ -7,6 +7,7 @@ const SOCKET_EVENTS = {
|
|||||||
CHESS_MOVE: "move",
|
CHESS_MOVE: "move",
|
||||||
CHESS_OPPONENT_MOVE: "opponent-move",
|
CHESS_OPPONENT_MOVE: "opponent-move",
|
||||||
USER_JOINED_ROOM: "user-joined-room",
|
USER_JOINED_ROOM: "user-joined-room",
|
||||||
|
USER_RESIGNED: "user-resigned",
|
||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
|||||||
+16
-2
@@ -1,7 +1,16 @@
|
|||||||
const socket = require("socket.io");
|
const socket = require("socket.io");
|
||||||
const { SOCKET_EVENTS } = require("./constants");
|
const { SOCKET_EVENTS } = require("./constants");
|
||||||
const { CHESS_MOVE, CHESS_OPPONENT_MOVE, CONNECTION, JOIN_ROOM, JOIN_ROOM_ERROR, JOIN_ROOM_SUCCESS, ROOM_FULL } =
|
const {
|
||||||
SOCKET_EVENTS;
|
CHESS_MOVE,
|
||||||
|
CHESS_OPPONENT_MOVE,
|
||||||
|
CONNECTION,
|
||||||
|
JOIN_ROOM,
|
||||||
|
JOIN_ROOM_ERROR,
|
||||||
|
JOIN_ROOM_SUCCESS,
|
||||||
|
ROOM_FULL,
|
||||||
|
USER_JOINED_ROOM,
|
||||||
|
USER_RESIGNED,
|
||||||
|
} = SOCKET_EVENTS;
|
||||||
// roomID => { timeLimit,gameHistory , players:[{username: {color}}] }
|
// roomID => { timeLimit,gameHistory , players:[{username: {color}}] }
|
||||||
let activeRooms = new Map();
|
let activeRooms = new Map();
|
||||||
|
|
||||||
@@ -63,6 +72,7 @@ function socketIOServerInit(server) {
|
|||||||
io.to(roomID).emit("new user joined the room");
|
io.to(roomID).emit("new user joined the room");
|
||||||
console.log(data, "joined");
|
console.log(data, "joined");
|
||||||
let room = getRoom(roomID);
|
let room = getRoom(roomID);
|
||||||
|
io.to(roomID).emit(USER_JOINED_ROOM, data.username);
|
||||||
socket.emit(result, room.gameHistory); // room joined successfully
|
socket.emit(result, room.gameHistory); // room joined successfully
|
||||||
} else {
|
} else {
|
||||||
socket.emit(result); // room is full
|
socket.emit(result); // room is full
|
||||||
@@ -78,6 +88,10 @@ function socketIOServerInit(server) {
|
|||||||
room.gameHistory.push(moveData);
|
room.gameHistory.push(moveData);
|
||||||
socket.to(roomID).emit(CHESS_OPPONENT_MOVE, moveData);
|
socket.to(roomID).emit(CHESS_OPPONENT_MOVE, moveData);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on(USER_RESIGNED, (roomID, username) => {
|
||||||
|
socket.to(roomID).emit(USER_RESIGNED, username);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
export const SOCKET_EVENTS = {
|
export const SOCKET_EVENTS = {
|
||||||
CONNECT:'connect',
|
CONNECT: "connect",
|
||||||
DISCONNECT:'disconnect',
|
DISCONNECT: "disconnect",
|
||||||
CONNECTION: "connection",
|
CONNECTION: "connection",
|
||||||
JOIN_ROOM: "join-room",
|
JOIN_ROOM: "join-room",
|
||||||
JOIN_ROOM_SUCCESS: "join-room-success",
|
JOIN_ROOM_SUCCESS: "join-room-success",
|
||||||
@@ -9,6 +9,7 @@ export const SOCKET_EVENTS = {
|
|||||||
CHESS_MOVE: "move",
|
CHESS_MOVE: "move",
|
||||||
CHESS_OPPONENT_MOVE: "opponent-move",
|
CHESS_OPPONENT_MOVE: "opponent-move",
|
||||||
USER_JOINED_ROOM: "user-joined-room",
|
USER_JOINED_ROOM: "user-joined-room",
|
||||||
|
USER_RESIGNED: "user-resigned",
|
||||||
};
|
};
|
||||||
|
|
||||||
export const DISPATCH_EVENTS = {
|
export const DISPATCH_EVENTS = {
|
||||||
@@ -17,4 +18,5 @@ export const DISPATCH_EVENTS = {
|
|||||||
CAPTURE_PIECE: "CAPTURE_PIECE",
|
CAPTURE_PIECE: "CAPTURE_PIECE",
|
||||||
JUMP_TO: "JUMP_TO",
|
JUMP_TO: "JUMP_TO",
|
||||||
SET_GAME_HISTORY: "SET_GAME_HISTORY",
|
SET_GAME_HISTORY: "SET_GAME_HISTORY",
|
||||||
|
END_GAME: "END_GAME",
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import React, { createContext, useReducer, useRef, useState } from 'react'
|
import React, { createContext, useReducer, useRef, useState } from 'react'
|
||||||
import { ChessModified, chessInit } from '../../utils/chess';
|
import { ChessModified, chessInit } from '../../utils/chess';
|
||||||
import { DISPATCH_EVENTS } from '../constants';
|
import { DISPATCH_EVENTS } from '../constants';
|
||||||
const { CAPTURE_PIECE, MOVE_PIECE, SELECT_PIECE, JUMP_TO, SET_GAME_HISTORY } = DISPATCH_EVENTS
|
const { CAPTURE_PIECE, MOVE_PIECE, SELECT_PIECE, JUMP_TO, SET_GAME_HISTORY, END_GAME } = DISPATCH_EVENTS
|
||||||
export const ChessGameContext = createContext();
|
export const ChessGameContext = createContext();
|
||||||
// myColor: null, chess: null, chessBoard: null, moveHints: null, selected: null, dispatch: null, handleOpponentMove: null, handleSquareClick: null, getSquareColor: null, isSquareMarked: null, selectPiece: null, handleDrop: null
|
// myColor: null, chess: null, chessBoard: null, moveHints: null, selected: null, dispatch: null, handleOpponentMove: null, handleSquareClick: null, getSquareColor: null, isSquareMarked: null, selectPiece: null, handleDrop: null
|
||||||
|
|
||||||
@@ -57,6 +57,10 @@ const reducer = (state, action) => {
|
|||||||
}
|
}
|
||||||
return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(state.chess.myColor), gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 }
|
return { ...state, chess: newChessObj, chessBoard: newChessObj.getBoard(state.chess.myColor), gameHistory: updatedGameHistory, currentIndex: updatedGameHistory.length - 1 }
|
||||||
}
|
}
|
||||||
|
case END_GAME:
|
||||||
|
{
|
||||||
|
return { ...state, hasGameEnded: true, gameEndedReason: action.val }
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
return state;
|
return state;
|
||||||
}
|
}
|
||||||
@@ -200,11 +204,15 @@ const ChessGameContextProvider = ({ children }) => {
|
|||||||
dispatch({ type: SET_GAME_HISTORY, val: fetchedGameHistory })
|
dispatch({ type: SET_GAME_HISTORY, val: fetchedGameHistory })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function endGame(reason) {
|
||||||
|
dispatch({ type: END_GAME, val: reason })
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ChessGameContext.Provider value={{
|
<ChessGameContext.Provider value={{
|
||||||
myColor, chessBoard, moveHints, selected, handleOpponentMove, handleSquareClick, getSquareColor, isSquareMarked,
|
myColor, chessBoard, moveHints, selected, handleOpponentMove, handleSquareClick, getSquareColor, isSquareMarked,
|
||||||
selectPiece, handleDrop, gameHistory, jumpTo, getChessBoard, currentIndex, goAhead, goBack, setGameHistory,
|
selectPiece, handleDrop, gameHistory, jumpTo, getChessBoard, currentIndex, goAhead, goBack, setGameHistory,
|
||||||
isTimerOn, hasGameEnded, gameEndedReason
|
isTimerOn, hasGameEnded, gameEndedReason,endGame
|
||||||
}}>
|
}}>
|
||||||
{children}
|
{children}
|
||||||
<audio src='/src/assets/move-self.mp3' ref={moveAudioRef} />
|
<audio src='/src/assets/move-self.mp3' ref={moveAudioRef} />
|
||||||
|
|||||||
@@ -9,15 +9,15 @@ import GameHistory from '../../components/GameHistory'
|
|||||||
import Timer from './Timer'
|
import Timer from './Timer'
|
||||||
import { useDisclosure } from '@mantine/hooks'
|
import { useDisclosure } from '@mantine/hooks'
|
||||||
import { SOCKET_EVENTS } from '../../constants'
|
import { SOCKET_EVENTS } from '../../constants'
|
||||||
const { CONNECT, DISCONNECT, CHESS_MOVE, CHESS_OPPONENT_MOVE, CONNECTION, JOIN_ROOM, JOIN_ROOM_ERROR, JOIN_ROOM_SUCCESS, ROOM_FULL, USER_JOINED_ROOM } = SOCKET_EVENTS;
|
const { CONNECT, DISCONNECT, CHESS_MOVE, CHESS_OPPONENT_MOVE, USER_RESIGNED, CONNECTION, JOIN_ROOM, JOIN_ROOM_ERROR, JOIN_ROOM_SUCCESS, ROOM_FULL, USER_JOINED_ROOM } = SOCKET_EVENTS;
|
||||||
|
|
||||||
const ChessGame = () => {
|
const ChessGame = () => {
|
||||||
const { setGameHistory, isTimerOn, setIsTimerOn, hasGameEnded, gameEndedReason } = useContext(ChessGameContext);
|
const { setGameHistory, isTimerOn, setIsTimerOn, hasGameEnded, gameEndedReason, endGame } = useContext(ChessGameContext);
|
||||||
const [gameEndedModalOpen, modalFunctions] = useDisclosure(true);
|
const [gameEndedModalOpen, modalFunctions] = useDisclosure(true);
|
||||||
|
|
||||||
const user = getUserData();
|
const user = getUserData();
|
||||||
let username = user.username;
|
let username = user.username;
|
||||||
let color = localStorage.getItem('myColor')
|
let color = localStorage.getItem('myColor');
|
||||||
const [hasJoinedRoom, setHasJoinedRoom] = useState(localStorage.getItem('socketid'));
|
const [hasJoinedRoom, setHasJoinedRoom] = useState(localStorage.getItem('socketid'));
|
||||||
const [isWaiting, setIsWaiting] = useState(true);
|
const [isWaiting, setIsWaiting] = useState(true);
|
||||||
const roomID = localStorage.getItem('roomID');
|
const roomID = localStorage.getItem('roomID');
|
||||||
@@ -25,6 +25,7 @@ const ChessGame = () => {
|
|||||||
const opponent = localStorage.getItem('opponent');
|
const opponent = localStorage.getItem('opponent');
|
||||||
|
|
||||||
const exitGame = () => {
|
const exitGame = () => {
|
||||||
|
// cleanup game related data
|
||||||
localStorage.removeItem('socketid');
|
localStorage.removeItem('socketid');
|
||||||
localStorage.removeItem('roomID');
|
localStorage.removeItem('roomID');
|
||||||
localStorage.removeItem('opponent');
|
localStorage.removeItem('opponent');
|
||||||
@@ -34,6 +35,12 @@ const ChessGame = () => {
|
|||||||
navigate('/play/friend');
|
navigate('/play/friend');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const resign = () => {
|
||||||
|
socket.emit(USER_RESIGNED,roomID, username);
|
||||||
|
endGame('RESIGN');
|
||||||
|
exitGame();
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
socket.connect();
|
socket.connect();
|
||||||
socket.on(CONNECT, () => {
|
socket.on(CONNECT, () => {
|
||||||
@@ -70,6 +77,14 @@ const ChessGame = () => {
|
|||||||
setIsWaiting(false);
|
setIsWaiting(false);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
socket.on(USER_RESIGNED, (roomID, username) => {
|
||||||
|
endGame('RESIGN');
|
||||||
|
})
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
socket.offAny();
|
||||||
|
socket.disconnect()
|
||||||
|
}
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
if (!hasJoinedRoom) return (
|
if (!hasJoinedRoom) return (
|
||||||
@@ -132,7 +147,7 @@ const ChessGame = () => {
|
|||||||
<GameHistory />
|
<GameHistory />
|
||||||
</Flex>
|
</Flex>
|
||||||
<Flex>
|
<Flex>
|
||||||
<Button onClick={exitGame} color='red'>Exit Game</Button>
|
<Button onClick={resign} color='red'>Exit Game</Button>
|
||||||
</Flex>
|
</Flex>
|
||||||
</Flex>
|
</Flex>
|
||||||
</MediaQuery>
|
</MediaQuery>
|
||||||
|
|||||||
Reference in New Issue
Block a user