From 9954946c12a29380118e48b90f40caf204d0ccdd Mon Sep 17 00:00:00 2001 From: Wenszel Date: Tue, 7 Jun 2022 08:50:04 +0200 Subject: [PATCH] edited countdown --- package-lock.json | 17 ++++---- package.json | 1 + src/components/Navbar.jsx | 2 +- .../navbar-components/NameContainer.jsx | 42 ++++++++++++++++--- .../navbar-components/ReadyButton.jsx | 2 +- 5 files changed, 48 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 235bba9..3180f1e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@testing-library/react": "^11.2.5", "@testing-library/user-event": "^12.8.3", "axios": "^0.21.1", + "prop-types": "^15.8.1", "react": "^17.0.1", "react-beforeunload": "^2.4.0", "react-dom": "^17.0.1", @@ -16043,13 +16044,13 @@ } }, "node_modules/prop-types": { - "version": "15.7.2", - "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", - "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", + "version": "15.8.1", + "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz", + "integrity": "sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==", "dependencies": { "loose-envify": "^1.4.0", "object-assign": "^4.1.1", - "react-is": "^16.8.1" + "react-is": "^16.13.1" } }, "node_modules/prop-types/node_modules/react-is": { @@ -34536,13 +34537,13 @@ } }, "prop-types": { - "version": "15.7.2", - "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.7.2.tgz", - "integrity": "sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==", + "version": "15.8.1", + "resolved": "https://registry.npmjs.org/prop-types/-/prop-types-15.8.1.tgz", + "integrity": "sha512-oj87CgZICdulUohogVAR7AjlC0327U4el4L6eAvOqCeudMDVU0NThNaV+b9Df4dXgSP1gXMTnPdhfe/2qDH5cg==", "requires": { "loose-envify": "^1.4.0", "object-assign": "^4.1.1", - "react-is": "^16.8.1" + "react-is": "^16.13.1" }, "dependencies": { "react-is": { diff --git a/package.json b/package.json index 17bd903..46b7cfc 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "@testing-library/react": "^11.2.5", "@testing-library/user-event": "^12.8.3", "axios": "^0.21.1", + "prop-types": "^15.8.1", "react": "^17.0.1", "react-beforeunload": "^2.4.0", "react-dom": "^17.0.1", diff --git a/src/components/Navbar.jsx b/src/components/Navbar.jsx index 14a3ac1..f41f642 100644 --- a/src/components/Navbar.jsx +++ b/src/components/Navbar.jsx @@ -10,7 +10,7 @@ const Navbar = ({ players, started, time, isReady, rolledNumber, nowMoving, roll
{players.map((player, index) => (
- + { + +/* + Component responsible for: + - displaying the player's name + - informing players about the readiness of other players by changing the color of container from gray to the player's color + - counting time to the end of the move + + Props: + - player (object): + The player to whom the container belongs + Player's properties used in this component: + - ready (boolean): + is the player ready for the start of the game, if so, change color from gray to the player's color + when the game is started all players are ready not matter if they clicked ready button before + - nowMoving (boolean) is this player move now, if true display timer + - name (string) + - time (number) - time remaining until the move is made in milliseconds +*/ + +const NameContainer = ({ player, time }) => { const [remainingTime, setRemainingTime] = useState(); const socket = useContext(SocketContext); + // Function responsible for counting down to the end of time every second const countdown = () => { + // If the time if over emit information to server if (remainingTime <= 0) { - socket.emit('game:skip'); - } else { - setRemainingTime(Math.ceil((time - Date.now()) / 1000)); + return socket.emit('game:skip'); } + setRemainingTime(Math.ceil((time - Date.now()) / 1000)); }; + useEffect(() => { + // Starts the countdown from the beginning if the server returned information about skipping the turn socket.on('game:skip', () => { setRemainingTime(15); }); + setRemainingTime(Math.ceil((time - Date.now()) / 1000)); const interval = setInterval(countdown, 1000); return () => clearInterval(interval); }, [countdown]); + return (

{player.name}

{player.nowMoving ?
{remainingTime}
: null} @@ -29,4 +54,9 @@ const NameContainer = ({ player, time, color }) => { ); }; +NameContainer.propTypes = { + player: PropTypes.object, + time: PropTypes.number, +}; + export default NameContainer; diff --git a/src/components/navbar-components/ReadyButton.jsx b/src/components/navbar-components/ReadyButton.jsx index abb55aa..62ea815 100644 --- a/src/components/navbar-components/ReadyButton.jsx +++ b/src/components/navbar-components/ReadyButton.jsx @@ -1,4 +1,4 @@ -import React, { useState, useContext, useEffect, useLayoutEffect } from 'react'; +import React, { useState, useContext, useEffect } from 'react'; import { SocketContext } from '../../App'; import Switch from '@material-ui/core/Switch';