From 8ef25e592383f5daa8632ddfe89519c98cd53b50 Mon Sep 17 00:00:00 2001 From: Moon Patel Date: Mon, 16 Oct 2023 02:08:06 +0530 Subject: [PATCH] fix: piece cannot be captured by clicking --- frontend/src/context/chess-game-context.jsx | 16 ++++++--- frontend/src/pages/Chess/ChessBoard.jsx | 40 +++++++++++++-------- 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/frontend/src/context/chess-game-context.jsx b/frontend/src/context/chess-game-context.jsx index b6b08c4..8a45ce4 100644 --- a/frontend/src/context/chess-game-context.jsx +++ b/frontend/src/context/chess-game-context.jsx @@ -2,12 +2,12 @@ import React, { createContext, useReducer, useRef, useState } from 'react' import PropTypes from 'prop-types'; -import { socket, socketBot } from '../socket'; +import { socket } from '../socket'; import { ChessModified, chessInit } from '../utils/chess'; import { DISPATCH_EVENTS, SOCKET_EVENTS } from '../constants'; const { CAPTURE_PIECE, MOVE_PIECE, SELECT_PIECE, JUMP_TO, SET_GAME_HISTORY, END_GAME } = DISPATCH_EVENTS -const { GAME_END, CHESS_MOVE } = SOCKET_EVENTS; +const { GAME_END } = SOCKET_EVENTS; export const ChessGameContext = createContext(); @@ -145,10 +145,12 @@ const ChessGameContextProvider = ({ children }) => { if (chessRef.current.turn() === myColor) { if (type && color === myColor) { - return dispatch({ type: SELECT_PIECE, val: square }); + selectPiece({square,color}); + return; } if (!type && selectedRef.current && marked) { - dispatch({ type: MOVE_PIECE, val: { from: selected, to: square, callback } }) + let moveData = { from: selectedRef.current, to: square }; + dispatch({ type: MOVE_PIECE, val: { from: selectedRef.current, to: square, callback } }) emitToSocketCallback({ from: selectedRef.current, to: square }) setIsTimerOn(false) moveAudioRef.current.play(); @@ -231,11 +233,15 @@ const ChessGameContextProvider = ({ children }) => { socket.emit(GAME_END, roomID); } + function getPieceColor(square) { + return chessRef.current.get(square).color + } + return ( {children}