socket implementation and multiplayer feature added in frontend

This commit is contained in:
Moon Patel
2023-07-02 14:45:49 +05:30
parent 16326766b7
commit ceb6a6993c
13 changed files with 215 additions and 64 deletions
+16 -12
View File
@@ -8,27 +8,31 @@ const Cell = ({ cell, chess, marked, dispatch }) => {
const { square, type, color } = cell;
const { isOver, setNodeRef } = useDroppable({ id: square });
const [isDropped, setIsDropped] = useState(false);
let squareColor = chess.squareColor(square) === 'light' ? "w" : "b";
console.log(chess.turn() !== localStorage.getItem('my_color'))
const handleClick = () => {
if (type && chess.turn() === chess.myColor) {
return dispatch({ type: 'SELECT_PIECE', val: square });
}
console.log(type, chess.selected, marked)
if (!type && chess.selected && marked) {
console.log(square)
dispatch({ type: 'MOVE_PIECE', val: { from: chess.selected, to: square } })
}
if (type && marked) {
dispatch({ type: 'CAPTURE_PIECE', val: { from: chess.selected, to: square } })
if (chess.turn() !== localStorage.getItem('my_color')) return;
if (chess.myColor === color) {
if (type && chess.turn() === chess.myColor) {
return dispatch({ type: 'SELECT_PIECE', val: square });
}
console.log(type, chess.selected, marked)
if (!type && chess.selected && marked) {
console.log(square)
dispatch({ type: 'MOVE_PIECE', val: { from: chess.selected, to: square } })
}
if (type && marked) {
dispatch({ type: 'CAPTURE_PIECE', val: { from: chess.selected, to: square } })
}
}
}
let content;
content = marked ? <Mark /> : <Piece cell={cell} dispatch={dispatch} />;
let squareColor = chess.squareColor(square) === "dark" ? "gray" : "white";
return (
<Flex ref={setNodeRef} onClick={handleClick} w="75px" h="75px" bg={squareColor} >
<Flex ref={setNodeRef} onClick={handleClick} w="75px" h="75px" bg={squareColor === 'w' ? "white" : "gray"} >
{content}
</Flex>
)