Game state persistency implemented. Game state is

now saved on page refresh.
The game state is stored on the server and when the user refreshes
the page the game state is fetched from the server on socket
re-connection

Next step:
stop user from making any move when the use is viewing past moves
This commit is contained in:
Moon Patel
2023-07-04 16:50:40 +05:30
parent 1d26cca8d8
commit aa6e463b60
7 changed files with 59 additions and 16 deletions
+8 -2
View File
@@ -1,11 +1,14 @@
import React, { useContext, useState } from 'react'
import Piece from './Piece';
import { socket } from '../socket';
import { Box, Flex } from '@mantine/core';
import { Box, Flex, Modal } from '@mantine/core';
import { useDroppable } from '@dnd-kit/core'
import { ChessGameContext } from '../context/chess-game-context';
import { SOCKET_EVENTS } from '../constants';
const { CHESS_MOVE } = SOCKET_EVENTS
const Cell = ({ cell }) => {
let roomID = localStorage.getItem('roomID');
let { square, type, color } = cell;
const { getSquareColor, isSquareMarked, handleSquareClick } = useContext(ChessGameContext)
const [isDropped, setIsDropped] = useState(false);
@@ -14,7 +17,10 @@ const Cell = ({ cell }) => {
let marked = isSquareMarked(square);
const handleClick = () => {
handleSquareClick(square);
handleSquareClick(square, (moveData) => {
// moveData contains fen string, from, to squares of the move
socket.emit(CHESS_MOVE, roomID, moveData);
});
}
let content = marked ? <Mark /> : <Piece cell={cell} />;