Added timers but it is not stable for now

This commit is contained in:
Moon Patel
2023-07-05 18:28:16 +05:30
parent a7a1d08b46
commit 313906eb5d
4 changed files with 54 additions and 19 deletions
+7 -2
View File
@@ -1,4 +1,4 @@
import React, { createContext, useReducer, useRef } 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 } = DISPATCH_EVENTS
@@ -71,6 +71,7 @@ const ChessGameContextProvider = ({ children }) => {
let myColor = localStorage.getItem('myColor'); let myColor = localStorage.getItem('myColor');
console.log('INSIDE CONTEXT PROVIDER'); console.log('INSIDE CONTEXT PROVIDER');
const [{ chess, chessBoard, moveHints, selected, gameHistory, currentIndex }, dispatch] = useReducer(reducer, myColor, chessGameStateInit); const [{ chess, chessBoard, moveHints, selected, gameHistory, currentIndex }, dispatch] = useReducer(reducer, myColor, chessGameStateInit);
const [isTimerOn, setIsTimerOn] = useState(true);
console.log(gameHistory); console.log(gameHistory);
@@ -111,11 +112,13 @@ const ChessGameContextProvider = ({ children }) => {
if (!type && selected && marked) { if (!type && selected && marked) {
dispatch({ type: MOVE_PIECE, val: { from: selected, to: square } }) dispatch({ type: MOVE_PIECE, val: { from: selected, to: square } })
emitToSocketCallback({ from: selected, to: square }) emitToSocketCallback({ from: selected, to: square })
setIsTimerOn(false)
captureAudioRef.current.play(); captureAudioRef.current.play();
} }
if (type && marked) { if (type && marked) {
dispatch({ type: CAPTURE_PIECE, val: { from: selected, to: square } }) dispatch({ type: CAPTURE_PIECE, val: { from: selected, to: square } })
emitToSocketCallback({ from: selected, to: square }) emitToSocketCallback({ from: selected, to: square })
setIsTimerOn(false);
moveAudioRef.current.play(); moveAudioRef.current.play();
} }
} else { } else {
@@ -130,10 +133,12 @@ const ChessGameContextProvider = ({ children }) => {
if (chess.get(to)) { if (chess.get(to)) {
dispatch({ type: CAPTURE_PIECE, val: { from: from, to: to } }); // capture piece dispatch({ type: CAPTURE_PIECE, val: { from: from, to: to } }); // capture piece
captureAudioRef.current.play(); captureAudioRef.current.play();
setIsTimerOn(false)
emitToSocketCallback(moveData); emitToSocketCallback(moveData);
} else { } else {
dispatch({ type: MOVE_PIECE, val: { from: from, to: to } }); // move piece dispatch({ type: MOVE_PIECE, val: { from: from, to: to } }); // move piece
moveAudioRef.current.play(); moveAudioRef.current.play();
setIsTimerOn(false)
emitToSocketCallback(moveData); emitToSocketCallback(moveData);
} }
} }
@@ -187,7 +192,7 @@ const ChessGameContextProvider = ({ children }) => {
return ( return (
<ChessGameContext.Provider value={{ <ChessGameContext.Provider value={{
myColor, chessBoard, moveHints, selected, handleOpponentMove, handleSquareClick, getSquareColor, isSquareMarked, selectPiece, handleDrop, gameHistory, jumpTo, getChessBoard, currentIndex, goAhead, goBack, setGameHistory myColor, chessBoard, moveHints, selected, handleOpponentMove, handleSquareClick, getSquareColor, isSquareMarked, selectPiece, handleDrop, gameHistory, jumpTo, getChessBoard, currentIndex, goAhead, goBack, setGameHistory, isTimerOn
}}> }}>
{children} {children}
<audio src='/src/assets/move-self.mp3' ref={moveAudioRef} /> <audio src='/src/assets/move-self.mp3' ref={moveAudioRef} />
+4 -1
View File
@@ -1,9 +1,12 @@
import React, { useEffect, useState } from 'react' import React, { useEffect, useState } from 'react'
const useCountDown = (timeLimit) => { const useCountDown = (timeLimit,isTimerOn) => {
const [timeLeft, setTimeLeft] = useState(timeLimit * 60 * 1000); const [timeLeft, setTimeLeft] = useState(timeLimit * 60 * 1000);
useEffect(() => { useEffect(() => {
if(!isTimerOn) {
return;
}
if (timeLeft > 0) { if (timeLeft > 0) {
const interval = setInterval(() => { const interval = setInterval(() => {
setTimeLeft((prev) => prev - 1000); setTimeLeft((prev) => prev - 1000);
+26 -16
View File
@@ -1,4 +1,4 @@
import { Avatar, Button, Flex, Image, Loader, MediaQuery, NavLink, Text, Title } from '@mantine/core' import { Avatar, Button, Flex, Group, Image, Loader, MediaQuery, NavLink, Text, Title } from '@mantine/core'
import React, { useContext, useEffect, useState } from 'react' import React, { useContext, useEffect, useState } from 'react'
import ChessBoard from '../Chess/ChessBoard' import ChessBoard from '../Chess/ChessBoard'
import { useNavigate, useParams } from 'react-router-dom' import { useNavigate, useParams } from 'react-router-dom'
@@ -6,15 +6,16 @@ import { socket } from '../../socket'
import { getUserData } from '../../../utils/auth' import { getUserData } from '../../../utils/auth'
import { ChessGameContext } from '../../context/chess-game-context' import { ChessGameContext } from '../../context/chess-game-context'
import GameHistory from '../../components/GameHistory' import GameHistory from '../../components/GameHistory'
import Timer from './Timer'
const ChessGame = () => { const ChessGame = () => {
const { gameHistory,setGameHistory } = useContext(ChessGameContext); const { gameHistory, setGameHistory, isTimerOn,setIsTimerOn } = useContext(ChessGameContext);
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');
const navigate = useNavigate(); const navigate = useNavigate();
const opponent = localStorage.getItem('opponent'); const opponent = localStorage.getItem('opponent');
@@ -57,7 +58,8 @@ const ChessGame = () => {
}) })
socket.on('opponent-move', (data) => { socket.on('opponent-move', (data) => {
console.log(data) console.log(data);
// setIsTimerOn(true);
}) })
}, []); }, []);
@@ -69,19 +71,27 @@ const ChessGame = () => {
return ( return (
<Flex gap="xl" miw={360} justify='center' align='center' wrap='nowrap' mt={{ base: '50px', sm: '0px' }} direction={{ base: 'column', lg: 'row' }}> <Flex gap="xl" miw={360} justify='center' align='center' wrap='nowrap' mt={{ base: '50px', sm: '0px' }} direction={{ base: 'column', lg: 'row' }}>
<Flex gap="xs" justify='center' align='start' wrap='nowrap' direction='column' > <Flex gap="xs" justify='center' align='start' wrap='nowrap' direction='column' >
<NavLink <div style={{display:'flex',justifyContent:'space-between',alignItems:'center'}}>
p="2px" <NavLink
label={opponent} style={{width:"500px"}}
icon={<Avatar radius="3px" children={opponent[0].toUpperCase()} />} p="2px"
description={"description"} label={opponent}
/> icon={<Avatar radius="3px" children={opponent[0].toUpperCase()} />}
description={"description"}
/>
{/* <Timer on={!isTimerOn} /> */}
</div>
<ChessBoard /> <ChessBoard />
<NavLink <div style={{display:'flex',justifyContent:'space-between',alignItems:'center'}}>
p="2px" <NavLink
label={username} style={{width:"500px"}}
icon={<Avatar radius="3px" />} p="2px"
description={"description"} label={username}
/> icon={<Avatar radius="3px" children={username[0].toUpperCase()} />}
description={"description"}
/>
{/* <Timer on={isTimerOn} /> */}
</div>
</Flex> </Flex>
<MediaQuery smallerThan="lg" styles={{ display: 'none' }}> <MediaQuery smallerThan="lg" styles={{ display: 'none' }}>
<Flex maw={450} sx={{ <Flex maw={450} sx={{
+17
View File
@@ -0,0 +1,17 @@
import React, { useContext } from 'react'
import useCountDown from '../../hooks/useCountDown'
import { Box } from '@mantine/core';
import { ChessGameContext } from '../../context/chess-game-context';
const Timer = ({ on }) => {
const { isTimerOn } = useContext(ChessGameContext)
const timeLimit = localStorage.getItem('timeLimit');
const [seconds, minutes] = useCountDown(timeLimit, on);
return (
<Box ff='monospace' sx={{ fontSize: '30px' }}>
{minutes}:{seconds}
</Box>
)
}
export default Timer