diff --git a/backend/socket.js b/backend/socket.js index 4c2739f..5a35593 100644 --- a/backend/socket.js +++ b/backend/socket.js @@ -18,6 +18,19 @@ const { // roomID => { timeLimit,gameHistory , players:{'b': {username,userid}, 'w':{username,userid} } } let activeRooms = new Map(); +// chess - chess object +// moveData - {from,to} => lan notation +function isValidMove(chess,moveData) { + let newChess = new Chess(); + newChess.loadPgn(chess.pgn()); + try { + newChess.move(moveData); + return true; + } catch(err) { + return false; + } +} + function createRoom(roomID, timeLimit) { console.log(roomID, "created"); activeRooms.set(roomID, { timeLimit, players: {}, gameHistory: [] }); @@ -73,12 +86,25 @@ function socketIOServerInit(server) { console.log(evt); }); + socket.on('INIT', async (data) => { + if(data.color === 'b') { + console.log(data.color); + const botMove = await nextMove({position:chess.fen()}); + console.log({ from: botMove.substring(0, 2), to: botMove.substring(2) }); + chess.move({from:botMove.substring(0,2),to:botMove.substring(2)}); + setTimeout(() => { + socket.emit("CHESS_BOT_MOVE",{from:botMove.substring(0,2),to:botMove.substring(2)}) + }, 500); + } + }); + socket.on("disconnect", (reason) => { console.log(id, "disconnected due to", reason); }); socket.on(CHESS_MOVE, async (roomID, moveData) => { console.log("CHESS_MOVE"); + if(!isValidMove(chess,moveData)) return; chess.move(moveData); let move = moveData.from + moveData.to; console.log(move); diff --git a/frontend/src/App.jsx b/frontend/src/App.jsx index 74a3160..fa343de 100644 --- a/frontend/src/App.jsx +++ b/frontend/src/App.jsx @@ -13,7 +13,7 @@ import AuthenticationPage from './pages/Authentication/Authentication' import ChallengeFriend, { playFriendAction } from './pages/Play/ChallengeFriend' import Profile, { action as profileAction } from './pages/Settings/Profile' import { getAuthToken, getUserData } from './utils/auth' -import Computer from './pages/Play/Computer' +import Computer, { playComputerAction } from './pages/Play/Computer' import ComputerGame from './pages/Play/ComputerGame' import MultiplayerGame from './pages/Play/MultiplayerGame' @@ -29,7 +29,7 @@ const router = createBrowserRouter([{ { index: true, element: }, { path: 'friend/:friend_username', element: , action: playFriendAction }, { path: 'friend', element: }, - { path: 'computer', element: }, + { path: 'computer', element: , action: playComputerAction }, { path: 'online', element:
Online
} ] }, diff --git a/frontend/src/pages/Play/ChessGameComputer.jsx b/frontend/src/pages/Play/ChessGameComputer.jsx index 97d6ac1..587e781 100644 --- a/frontend/src/pages/Play/ChessGameComputer.jsx +++ b/frontend/src/pages/Play/ChessGameComputer.jsx @@ -3,7 +3,7 @@ import React, { useContext, useEffect } from 'react' import { ChessGameContext } from '../../context/chess-game-context'; import { socketBot as socket } from '../../socket'; import { getUserData } from '../../utils/auth'; -import { Avatar, Button, Flex, Image, MediaQuery, Modal, NavLink, Text, Title } from '@mantine/core'; +import { Avatar, Button, Flex, MediaQuery, Modal, NavLink, Text, Title } from '@mantine/core'; import ChessBoard from '../Chess/ChessBoard'; import GameHistory from '../../components/GameHistory'; import { useNavigate } from 'react-router-dom'; @@ -22,6 +22,7 @@ const ChessGameComputer = () => { useEffect(() => { socket.connect(); + socket.emit('INIT', {color}); socket.onAny(evt => { console.log("event", evt); @@ -42,11 +43,11 @@ const ChessGameComputer = () => { const exitGame = () => { console.log("Ending game"); socket.disconnect(); + localStorage.removeItem('myColor'); navigate("/play/computer"); } const pieceDropCallback = (moveData) => { - console.log("Hello"); socket.emit(CHESS_MOVE, roomID, moveData); } const pieceClickCallback = (moveData) => { @@ -83,16 +84,16 @@ const ChessGameComputer = () => { { // TODO: handle isWaiting state - false ? - <> - - - - - - - - : + // false ? + // <> + // + // + // + // + // + // + // + // : }
diff --git a/frontend/src/pages/Play/Computer.jsx b/frontend/src/pages/Play/Computer.jsx index e6a5f04..d00b9cf 100644 --- a/frontend/src/pages/Play/Computer.jsx +++ b/frontend/src/pages/Play/Computer.jsx @@ -1,31 +1,60 @@ -import { Button, Card, Flex, Image, TextInput, Title } from '@mantine/core' -import { IconSearch } from '@tabler/icons-react' -import React from 'react' -import { Link } from 'react-router-dom' +import React from "react"; +import { Button, Card, Flex, Image, Select, Text, Title } from "@mantine/core"; +import { Form, redirect } from "react-router-dom"; const Computer = () => { + return ( - + Play with Computer - - - - + +
+