edited ready button
This commit is contained in:
parent
4551885c57
commit
4083b0f5bd
@ -49,10 +49,10 @@ module.exports = (io, socket) => {
|
|||||||
_id: roomId,
|
_id: roomId,
|
||||||
},
|
},
|
||||||
room,
|
room,
|
||||||
(err, updatedRoom) => {
|
err => {
|
||||||
if (err) return err;
|
if (err) return err;
|
||||||
// Sends to all players in room game data
|
// Sends to all players in room game data
|
||||||
io.to(roomId).emit('room:data', JSON.stringify(updatedRoom));
|
io.to(roomId).emit('room:data', JSON.stringify(room));
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|||||||
@ -16,7 +16,6 @@ function App() {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const socket = io('http://localhost:8080', { withCredentials: true });
|
const socket = io('http://localhost:8080', { withCredentials: true });
|
||||||
socket.on('player:data', data => {
|
socket.on('player:data', data => {
|
||||||
console.log(data);
|
|
||||||
data = JSON.parse(data);
|
data = JSON.parse(data);
|
||||||
setPlayerData(data);
|
setPlayerData(data);
|
||||||
data.roomId != null ? setRedirect(true) : setRedirect(false);
|
data.roomId != null ? setRedirect(true) : setRedirect(false);
|
||||||
|
|||||||
@ -14,6 +14,7 @@ const Gameboard = () => {
|
|||||||
// Game logic data
|
// Game logic data
|
||||||
const [rolledNumber, setRolledNumber] = useState(null);
|
const [rolledNumber, setRolledNumber] = useState(null);
|
||||||
const [time, setTime] = useState();
|
const [time, setTime] = useState();
|
||||||
|
const [isReady, setIsReady] = useState();
|
||||||
const [nowMoving, setNowMoving] = useState(false);
|
const [nowMoving, setNowMoving] = useState(false);
|
||||||
const [started, setStarted] = useState(false);
|
const [started, setStarted] = useState(false);
|
||||||
|
|
||||||
@ -33,7 +34,6 @@ const Gameboard = () => {
|
|||||||
socket.emit('room:data', context.roomId);
|
socket.emit('room:data', context.roomId);
|
||||||
socket.on('room:data', data => {
|
socket.on('room:data', data => {
|
||||||
data = JSON.parse(data);
|
data = JSON.parse(data);
|
||||||
//console.log(data);
|
|
||||||
// Filling navbar with empty player nick container
|
// Filling navbar with empty player nick container
|
||||||
while (data.players.length !== 4) {
|
while (data.players.length !== 4) {
|
||||||
data.players.push({ name: '...' });
|
data.players.push({ name: '...' });
|
||||||
@ -48,7 +48,9 @@ const Gameboard = () => {
|
|||||||
setNowMoving(false);
|
setNowMoving(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const currentPlayer = data.players.find(player => player._id === context.playerId);
|
||||||
checkWin();
|
checkWin();
|
||||||
|
setIsReady(currentPlayer.ready);
|
||||||
setPlayers(data.players);
|
setPlayers(data.players);
|
||||||
setPawns(data.pawns);
|
setPawns(data.pawns);
|
||||||
setTime(data.nextMoveTime);
|
setTime(data.nextMoveTime);
|
||||||
@ -63,7 +65,7 @@ const Gameboard = () => {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<Navbar players={players} started={started} time={time} />
|
<Navbar players={players} started={started} time={time} isReady={isReady} />
|
||||||
{nowMoving ? <Dice nowMoving={nowMoving} rolledNumberCallback={rolledNumberCallback} /> : null}
|
{nowMoving ? <Dice nowMoving={nowMoving} rolledNumberCallback={rolledNumberCallback} /> : null}
|
||||||
<Map pawns={pawns} nowMoving={nowMoving} rolledNumber={rolledNumber} />
|
<Map pawns={pawns} nowMoving={nowMoving} rolledNumber={rolledNumber} />
|
||||||
</>
|
</>
|
||||||
|
|||||||
@ -1,15 +1,15 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import NameContainer from './navbar-components/NameContainer'
|
import NameContainer from './navbar-components/NameContainer';
|
||||||
import ReadyButton from './navbar-components/ReadyButton'
|
import ReadyButton from './navbar-components/ReadyButton';
|
||||||
|
|
||||||
const Navbar = ({ players, started, time }) => {
|
const Navbar = ({ players, started, time, isReady }) => {
|
||||||
return(
|
return (
|
||||||
<div className = "navbar-container">
|
<div className='navbar-container'>
|
||||||
{players.map((player, index) =>
|
{players.map((player, index) => (
|
||||||
<NameContainer key = {index} player = {player} time = {time}/>
|
<NameContainer key={index} player={player} time={time} />
|
||||||
)}
|
))}
|
||||||
{started ? null : <ReadyButton/>}
|
{started ? null : <ReadyButton isReady={isReady} />}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
export default Navbar;
|
export default Navbar;
|
||||||
@ -1,19 +1,20 @@
|
|||||||
import React, { useState, useContext } from 'react';
|
import React, { useState, useContext, useEffect, useLayoutEffect } from 'react';
|
||||||
import { SocketContext } from '../../App';
|
import { SocketContext } from '../../App';
|
||||||
import Switch from '@material-ui/core/Switch';
|
import Switch from '@material-ui/core/Switch';
|
||||||
|
|
||||||
const ReadyButton = () => {
|
const ReadyButton = ({ isReady }) => {
|
||||||
const socket = useContext(SocketContext);
|
const socket = useContext(SocketContext);
|
||||||
const [checked, setChecked] = useState(false);
|
const [checked, setChecked] = useState();
|
||||||
|
|
||||||
const handleCheckboxChange = () => {
|
const handleCheckboxChange = () => {
|
||||||
socket.emit('player:ready');
|
socket.emit('player:ready');
|
||||||
setChecked(!checked);
|
setChecked(!checked);
|
||||||
};
|
};
|
||||||
|
useEffect(() => {
|
||||||
|
setChecked(isReady);
|
||||||
|
});
|
||||||
return (
|
return (
|
||||||
<div className='ready-container'>
|
<div className='ready-container'>
|
||||||
<Switch onClick={handleCheckboxChange} checked={checked} />
|
<Switch onChange={handleCheckboxChange} checked={checked || false} />
|
||||||
<label>{checked ? 'I want to play' : 'Im waiting'}</label>
|
<label>{checked ? 'I want to play' : 'Im waiting'}</label>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user