fixed interval error

This commit is contained in:
Wenszel 2023-11-04 20:10:17 +01:00
parent b9ec78bc25
commit ae9ebcc246
4 changed files with 17211 additions and 22450 deletions

View File

@ -3,49 +3,50 @@ const { getPawnPositionAfterMove } = require('../utils/functions');
module.exports = (io, socket) => {
const req = socket.request;
/*
Function responsible for drawing number in range from 1 to 6 and returning it to the player
if current player can move with drawed number allow the player to make a move
else skip player's turn
*/
const rollDiceNumber = async () => {
const rolledNumber = Math.ceil(Math.random() * 6);
let room = await RoomModel.findOne({ _id: req.session.roomId }).exec();
room.rolledNumber = rolledNumber;
await RoomModel.findOneAndUpdate({ _id: req.session.roomId }, room).exec();
const getRoom = async () => {
return await RoomModel.findOne({ _id: req.session.roomId }).exec();
};
const updateRoom = async room => {
return await RoomModel.findOneAndUpdate({ _id: req.session.roomId }, room).exec();
};
const sendToPlayersRolledNumber = rolledNumber => {
io.to(req.session.roomId.toString()).emit('game:roll', rolledNumber);
const isPossible = await canPlayerMoveAnyPawn(req.session.roomId, req.session.color, rolledNumber);
if (!isPossible) {
room = changeMovingPlayer(room);
await RoomModel.findOneAndUpdate({ _id: req.session.roomId }, room).exec();
};
const sendToPlayersData = room => {
io.to(req.session.roomId.toString()).emit('room:data', JSON.stringify(room));
};
const rollDice = async () => {
const rolledNumber = Math.ceil(Math.random() * 6);
sendToPlayersRolledNumber(rolledNumber);
let room = await updateRoom({ rolledNumber: rolledNumber });
if (!canPlayerMove(room, rolledNumber)) {
room = changeMovingPlayer(room);
await updateRoom(room);
sendToPlayersData(room);
}
};
/*
Function responsible for check if any pawn of the player can move
if he cannot move, his turn will be skipped
Player's pawn can move if:
1) (if player's pawn is in base) if the rolled number is 1,6
2) (if player's pawn is near finish line) if the move does not go beyond the win line
Returns boolean
*/
const canPlayerMoveAnyPawn = async (roomId, playerColor, rolledNumber) => {
const canPlayerMove = (room, rolledNumber) => {
let isMovePossible = false;
const room = await RoomModel.findOne({ _id: roomId.toString() }).exec();
const playerPawns = room.pawns.filter(pawn => pawn.color === playerColor);
// Checking each player's pawn
const playerPawns = room.pawns.filter(pawn => pawn.color === req.session.color);
for (const pawn of playerPawns) {
// Checking the first condition
// (if player's pawn is in base) if the rolled number is 1,6
if (pawn.position === pawn.basePos && (rolledNumber === 6 || rolledNumber === 1)) {
isMovePossible = true;
}
// Checking the second condition
// (if player's pawn is near finish line) if the move does not go beyond the win line
if (pawn.position !== getPawnPositionAfterMove(rolledNumber, pawn) && pawn.position !== pawn.basePos) {
isMovePossible = true;
}
}
return isMovePossible;
};
const isMoveValid = async (pawn, room) => {
if (req.session.color !== pawn.color) {
return false;
@ -56,47 +57,39 @@ module.exports = (io, socket) => {
}
return true;
};
/*
Function responsible for skipping a player's turn, if he did not move within the allotted time
Function is used in timeouts that start after a player's move or after skipping his turn
*/
const skipPlayerTurn = async () => {
let room = await RoomModel.findOne({ _id: req.session.roomId }).exec();
let room = await getRoom();
room = changeMovingPlayer(room);
await RoomModel.findOneAndUpdate({ _id: req.session.roomId }, room).exec();
io.to(req.session.roomId.toString()).emit('room:data', JSON.stringify(room));
await updateRoom(room);
sendToPlayersData(room);
};
/*
Function responsible for moving the pawn by the number of spots that have been drawn
Props: pawnId - Id which is needed to find a pawn in the board. Id is the only thing that distinguishes pawns of the same color.
*/
const movePawn = async ({ pawnId }) => {
let room = await RoomModel.findOne({ _id: req.session.roomId }).exec();
const indexOfMovedPawn = room.pawns.findIndex(pawn => pawn._id == pawnId);
const newPositionOfMovedPawn = getPawnPositionAfterMove(room.rolledNumber, room.pawns[indexOfMovedPawn]);
if (!isMoveValid(room.pawns[indexOfMovedPawn], room)) return;
room.pawns[indexOfMovedPawn].position = newPositionOfMovedPawn;
// Looking for pawns in the same position as the new position of the pawn
const pawnsInTheSamePosition = room.pawns.filter(pawn => pawn.position === newPositionOfMovedPawn);
// Each pawn in this position is checked to see if it has the same color as the pawn that has now moved to this position, if so, it is moved to the base (captured)
let room = await getRoom();
const indexOfPawn = room.pawns.findIndex(pawn => pawn._id == pawnId);
if (!isMoveValid(room.pawns[indexOfPawn], room)) return;
const newPositionOfMovedPawn = getPawnPositionAfterMove(room.rolledNumber, room.pawns[indexOfPawn]);
room.pawns[indexOfPawn].position = newPositionOfMovedPawn;
room = beatPawns(newPositionOfMovedPawn, room);
room = changeMovingPlayer(room);
await updateRoom(room);
sendToPlayersData(room);
};
const beatPawns = (position, room) => {
const pawnsInTheSamePosition = room.pawns.filter(pawn => pawn.position === position);
pawnsInTheSamePosition.forEach(pawn => {
if (pawn.color !== req.session.color) {
const index = room.pawns.findIndex(i => i._id === pawn._id);
room.pawns[index].position = room.pawns[index].basePos;
}
});
room = changeMovingPlayer(room);
await RoomModel.findOneAndUpdate({ _id: req.session.roomId }, room).exec();
io.to(req.session.roomId.toString()).emit('room:data', JSON.stringify(room));
return room;
};
/*
Function responsible for changing the currently moving player in room object.
It changes the value of nowMoving for both players and sets a new turn-end time and erases the currently drawn number.
The function is used as an auxiliary function in other functions because many of them perform the operation of changing the currently moving player.
Args: room (object) from mongoDB
Returns: room object after changes
*/
const changeMovingPlayer = room => {
if (room.timeoutID) clearTimeout(room.timeoutID);
const playerIndex = room.players.findIndex(player => player.nowMoving === true);
room.players[playerIndex].nowMoving = false;
if (playerIndex + 1 === room.players.length) {
@ -106,11 +99,11 @@ module.exports = (io, socket) => {
}
room.nextMoveTime = Date.now() + 15000;
room.rolledNumber = null;
setTimeout(skipPlayerTurn, 15000);
room.timeoutID = setTimeout(skipPlayerTurn, 15000);
return room;
};
socket.on('game:roll', rollDiceNumber);
socket.on('game:roll', rollDice);
socket.on('game:move', movePawn);
socket.on('game:skip', skipPlayerTurn);
};

View File

@ -7,6 +7,7 @@ var RoomSchema = new Schema({
started: { type: Boolean, default: false },
full: { type: Boolean, default: false },
nextMoveTime: Number,
timeoutID: Number,
rolledNumber: Number,
players: [
{

39479
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -14,7 +14,7 @@
"react-dom": "^17.0.1",
"react-loading": "^2.0.3",
"react-router-dom": "^5.2.0",
"react-scripts": "4.0.3",
"react-scripts": "^5.0.1",
"socket.io": "^4.5.1",
"socket.io-client": "^4.5.1",
"web-vitals": "^1.1.0"