added handler for ready button
This commit is contained in:
parent
159b96a678
commit
5eea80b804
@ -1,9 +1,19 @@
|
|||||||
const RoomModel = require('../schemas/room');
|
const RoomModel = require('../schemas/room');
|
||||||
const { colors } = require('../utils/constants');
|
const { colors } = require('../utils/constants');
|
||||||
const { getStartPositions } = require('../utils/functions');
|
const { getStartPositions } = require('../utils/functions');
|
||||||
|
/*
|
||||||
|
Function handle all player's requests to server
|
||||||
|
file constains functions:
|
||||||
|
1. login
|
||||||
|
with helper functions:
|
||||||
|
- addPlayerToExistingRoom
|
||||||
|
- createNewRoom
|
||||||
|
2. ready
|
||||||
|
*/
|
||||||
module.exports = (io, socket) => {
|
module.exports = (io, socket) => {
|
||||||
const req = socket.request;
|
const req = socket.request;
|
||||||
|
|
||||||
|
// Function responsible for adding a player to an existing room or creating a new one
|
||||||
const login = data => {
|
const login = data => {
|
||||||
// When new player login to game we are looking for not full and not started room to put player there
|
// When new player login to game we are looking for not full and not started room to put player there
|
||||||
RoomModel.findOne({ full: false, started: false }, function (err, room) {
|
RoomModel.findOne({ full: false, started: false }, function (err, room) {
|
||||||
@ -17,7 +27,39 @@ module.exports = (io, socket) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Function responsible for changing the player's readiness
|
||||||
|
const ready = () => {
|
||||||
|
const { roomId, playerId } = req.session;
|
||||||
|
// Finds player room
|
||||||
|
RoomModel.findOne({ _id: roomId }, function (err, room) {
|
||||||
|
if (err) return err;
|
||||||
|
// Finds index of player in players array
|
||||||
|
const index = room.players.findIndex(player => player._id.toString() == playerId.toString());
|
||||||
|
// Changes player's readiness to the opposite
|
||||||
|
room.players[index].ready = !room.players[index].ready;
|
||||||
|
// If two players are ready starts game by setting the room properties
|
||||||
|
if (room.players.filter(player => player.ready).length >= 2) {
|
||||||
|
room.started = true;
|
||||||
|
room.nextMoveTime = Date.now() + 15000;
|
||||||
|
room.players.forEach(player => (player.ready = true));
|
||||||
|
room.players[0].nowMoving = true;
|
||||||
|
}
|
||||||
|
RoomModel.findOneAndUpdate(
|
||||||
|
{
|
||||||
|
_id: roomId,
|
||||||
|
},
|
||||||
|
room,
|
||||||
|
(err, updatedRoom) => {
|
||||||
|
if (err) return err;
|
||||||
|
// Sends to all players in room game data
|
||||||
|
io.to(roomId).emit('room:data', JSON.stringify(updatedRoom));
|
||||||
|
}
|
||||||
|
);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
socket.on('player:login', login);
|
socket.on('player:login', login);
|
||||||
|
socket.on('player:ready', ready);
|
||||||
|
|
||||||
function createNewRoom(data) {
|
function createNewRoom(data) {
|
||||||
const room = new RoomModel({
|
const room = new RoomModel({
|
||||||
|
|||||||
@ -1,54 +0,0 @@
|
|||||||
var express = require("express");
|
|
||||||
var router = express.Router();
|
|
||||||
var RoomModel = require("../schemas/room");
|
|
||||||
var changeReadyState = (req, res, exit) => {
|
|
||||||
RoomModel.findOne({ _id: req.session.roomId }, function (err, doc) {
|
|
||||||
if (err) {
|
|
||||||
res.status(500).send(err);
|
|
||||||
} else {
|
|
||||||
//finds player by id and changes ready state
|
|
||||||
let updatedPlayers = doc.players;
|
|
||||||
let index = updatedPlayers.findIndex(
|
|
||||||
(player) => player._id.toString() == req.session.playerId.toString()
|
|
||||||
);
|
|
||||||
if (!exit) updatedPlayers[index].ready = !updatedPlayers[index].ready;
|
|
||||||
else updatedPlayers[index].ready = false;
|
|
||||||
const updatedDoc = {
|
|
||||||
players: updatedPlayers,
|
|
||||||
};
|
|
||||||
if (updatedPlayers.filter((player) => player.ready).length >= 2) {
|
|
||||||
updatedDoc.started = true;
|
|
||||||
updatedDoc.players.forEach((player) => (player.ready = true));
|
|
||||||
updatedDoc.nextMoveTime = Date.now() + 15000;
|
|
||||||
updatedDoc.players[0].nowMoving = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
RoomModel.findOneAndUpdate(
|
|
||||||
{
|
|
||||||
_id: req.session.roomId,
|
|
||||||
},
|
|
||||||
updatedDoc,
|
|
||||||
function (err, doc) {
|
|
||||||
if (err) {
|
|
||||||
console.log(err);
|
|
||||||
} else {
|
|
||||||
console.log("Updated Docs : ", doc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
);
|
|
||||||
res.status(200).send("Ready!");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
//changing status of player to ready for game
|
|
||||||
router.post("/ready", function (req, res) {
|
|
||||||
changeReadyState(req, res, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
//deleting user in case he left before game started
|
|
||||||
router.post("/exit", function (req, res) {
|
|
||||||
// changeReadyState(req,res, true)
|
|
||||||
});
|
|
||||||
|
|
||||||
module.exports = router;
|
|
||||||
@ -38,6 +38,7 @@ const Gameboard = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
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: '...' });
|
||||||
|
|||||||
@ -1,23 +1,22 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState, useContext } from 'react';
|
||||||
|
import { SocketContext } from '../../App';
|
||||||
import Switch from '@material-ui/core/Switch';
|
import Switch from '@material-ui/core/Switch';
|
||||||
import axios from 'axios';
|
|
||||||
|
|
||||||
const ReadyButton = () => {
|
const ReadyButton = () => {
|
||||||
|
const socket = useContext(SocketContext);
|
||||||
const [checked, setChecked] = useState(false)
|
const [checked, setChecked] = useState(false);
|
||||||
|
|
||||||
const handleCheckboxChange = () => {
|
const handleCheckboxChange = () => {
|
||||||
axios.post('/player/ready',{},{withCredentials: true});
|
socket.emit('player:ready');
|
||||||
setChecked(!checked);
|
setChecked(!checked);
|
||||||
}
|
};
|
||||||
|
|
||||||
return(
|
|
||||||
<div className="ready-container">
|
|
||||||
<Switch onClick={handleCheckboxChange} checked={checked}/>
|
|
||||||
<label>{checked ? 'I want to play' : 'Im waiting' }</label>
|
|
||||||
</div>
|
|
||||||
)
|
|
||||||
|
|
||||||
}
|
return (
|
||||||
|
<div className='ready-container'>
|
||||||
|
<Switch onClick={handleCheckboxChange} checked={checked} />
|
||||||
|
<label>{checked ? 'I want to play' : 'Im waiting'}</label>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default ReadyButton;
|
export default ReadyButton;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user