added timer

This commit is contained in:
Wenszel
2022-06-01 21:07:40 +02:00
parent 3d79bf66df
commit 57398bd46c
3 changed files with 48 additions and 14 deletions
@@ -1,16 +1,30 @@
import React from 'react';
import React, { useState, useEffect, useContext } from 'react';
import { SocketContext } from '../../App';
const NameContainer = ({ player, time, color }) => {
const getRemainingTime = () => {
return Math.round((time - Date.now()) / 1000) + 1;
const [remainingTime, setRemainingTime] = useState();
const socket = useContext(SocketContext);
const countdown = () => {
if (remainingTime <= 0) {
socket.emit('game:skip');
} else {
setRemainingTime(Math.ceil((time - Date.now()) / 1000));
}
};
useEffect(() => {
socket.on('game:skip', () => {
setRemainingTime(15);
});
const interval = setInterval(countdown, 1000);
return () => clearInterval(interval);
}, [countdown]);
return (
<div
className={`name-container ${color}`}
style={player.ready ? { backgroundColor: color } : { backgroundColor: 'lightgrey' }}
>
{player.name}
{player.nowMoving ? <div className='timer'> {getRemainingTime()} </div> : null}
<p>{player.name}</p>
{player.nowMoving ? <div className='timer'> {remainingTime} </div> : null}
</div>
);
};
+2 -5
View File
@@ -36,17 +36,14 @@ canvas {
border-radius: 5px;
color: white;
font-weight: bold;
display: flex;
text-align: center;
justify-content: center;
align-items: center;
}
.timer {
background-color: darkblue;
color: white;
position: relative;
top: -24px;
left: 32px;
bottom: 60px;
left: 82px;
border: 1px solid white;
padding: 1px;
width: 30px;