added playerHandler tests

This commit is contained in:
Wenszel 2023-11-18 18:35:33 +01:00
parent e702b912f7
commit b70693afdd
7 changed files with 2109 additions and 116 deletions

View File

@ -1,121 +1,58 @@
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');
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 handleLogin = async data => {
const login = data => { const room = await RoomModel.findOne({ full: false, started: false });
// 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) {
if (room) { if (room) {
// If there is one adds player to it
addPlayerToExistingRoom(room, data); addPlayerToExistingRoom(room, data);
} else { } else {
// If not creates new room and add player to it
createNewRoom(data); createNewRoom(data);
} }
});
}; };
// Function responsible for changing the player's readiness const handleReady = async () => {
const ready = () => {
const { roomId, playerId } = req.session; const { roomId, playerId } = req.session;
// Finds player room const room = await RoomModel.findOne({ _id: roomId });
RoomModel.findOne({ _id: roomId }, function (err, room) { room.getPlayer(playerId).changeReadyStatus();
if (err) return err; if (room.canStartGame()) {
// Finds index of player in players array room.startGame();
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( await RoomModel.findOneAndUpdate({ _id: roomId }, room);
{ io.to(roomId).emit('room:data', JSON.stringify(room));
_id: roomId,
},
room,
err => {
if (err) return err;
// Sends to all players in room game data
io.to(roomId.toString()).emit('room:data', JSON.stringify(room));
}
);
});
}; };
socket.on('player:login', login); const createNewRoom = async data => {
socket.on('player:ready', ready); const room = new RoomModel();
room.addPlayer(data.name);
await room.save();
reloadSession(room);
};
const addPlayerToExistingRoom = async (room, data) => {
room.addPlayer(data.name);
if (room.isFull()) {
room.startGame();
}
await RoomModel.findOneAndUpdate({ _id: room._id }, room);
reloadSession(room);
};
function createNewRoom(data) {
const room = new RoomModel({
createDate: new Date(),
players: [
{
name: data.name,
color: colors[0],
},
],
pawns: getStartPositions(),
});
// Saves new room to database
room.save().then(() => {
// Since it is not bound to an HTTP request, the session must be manually reloaded and saved // Since it is not bound to an HTTP request, the session must be manually reloaded and saved
const reloadSession = room => {
req.session.reload(err => { req.session.reload(err => {
if (err) return socket.disconnect(); if (err) return socket.disconnect();
// Saving session data
req.session.roomId = room._id.toString(); req.session.roomId = room._id.toString();
req.session.playerId = room.players[0]._id.toString(); req.session.playerId = room.players[room.players.length - 1]._id.toString();
req.session.color = room.players[0].color; req.session.color = colors[room.players.length - 1];
req.session.save(); req.session.save();
// Sending data to the user, after which player will be redirected to the game
socket.join(room._id.toString()); socket.join(room._id.toString());
socket.emit('player:data', JSON.stringify(req.session)); socket.emit('player:data', JSON.stringify(req.session));
}); });
});
}
function addPlayerToExistingRoom(room, data) {
// Adding a new user to the room
room.players.push({
name: data.name,
ready: false,
color: colors[room.players.length],
});
let updatedRoom = { players: room.players };
// Checking if the room is full
if (room.players.length === 4) {
// Changes the properties of the room to the state to start the game
updatedRoom = {
...updatedRoom,
full: true,
started: true,
nextMoveTime: Date.now() + 15000,
pawns: getStartPositions(),
}; };
updatedRoom.players.forEach(player => (player.ready = true));
updatedRoom.players[0].nowMoving = true; socket.on('player:login', handleLogin);
} socket.on('player:ready', handleReady);
// Updates a room in the database
RoomModel.findOneAndUpdate({ _id: room._id }, updatedRoom).then(() => {
// Since it is not bound to an HTTP request, the session must be manually reloaded and saved
req.session.reload(err => {
if (err) return socket.disconnect();
// Saving session data
req.session.roomId = room._id.toString();
req.session.playerId = updatedRoom.players[updatedRoom.players.length - 1]._id.toString();
req.session.color = colors[updatedRoom.players.length - 1];
req.session.save();
socket.join(room._id.toString());
// Sending data to the user, after which player will be redirected to the game
socket.emit('player:data', JSON.stringify(req.session));
});
});
}
}; };

1887
backend/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -9,5 +9,13 @@
"express-session": "^1.17.1", "express-session": "^1.17.1",
"mongoose": "^5.12.0", "mongoose": "^5.12.0",
"socket.io": "^4.5.1" "socket.io": "^4.5.1"
},
"devDependencies": {
"chai": "^4.3.10",
"mocha": "^10.2.0",
"socket.io-client": "^4.7.2"
},
"scripts": {
"test": "mocha tests/**/*.js"
} }
} }

View File

@ -9,4 +9,8 @@ const PlayerSchema = new Schema({
nowMoving: { type: Boolean, default: false }, nowMoving: { type: Boolean, default: false },
}); });
PlayerSchema.methods.changeReadyStatus = function () {
this.ready = !this.ready;
};
module.exports = PlayerSchema; module.exports = PlayerSchema;

View File

@ -1,18 +1,19 @@
const mongoose = require('mongoose'); const mongoose = require('mongoose');
const { getPawnPositionAfterMove } = require('../utils/functions'); const { colors } = require('../utils/constants');
const { getPawnPositionAfterMove, getStartPositions } = require('../utils/functions');
const Schema = mongoose.Schema; const Schema = mongoose.Schema;
const PawnSchema = require('./pawn'); const PawnSchema = require('./pawn');
const PlayerSchema = require('./player'); const PlayerSchema = require('./player');
const RoomSchema = new Schema({ const RoomSchema = new Schema({
createDate: Date, createDate: { type: Date, default: Date.now },
started: { type: Boolean, default: false }, started: { type: Boolean, default: false },
full: { type: Boolean, default: false }, full: { type: Boolean, default: false },
nextMoveTime: Number, nextMoveTime: Number,
timeoutID: Number, timeoutID: Number,
rolledNumber: Number, rolledNumber: Number,
players: [PlayerSchema], players: [PlayerSchema],
pawns: [PawnSchema], pawns: { type: [PawnSchema], default: getStartPositions() },
}); });
RoomSchema.methods.beatPawns = function (position, attackingPawnColor) { RoomSchema.methods.beatPawns = function (position, attackingPawnColor) {
@ -48,13 +49,44 @@ RoomSchema.methods.getPawnsThatCanMove = function () {
const movingPlayer = this.getCurrentlyMovingPlayer(); const movingPlayer = this.getCurrentlyMovingPlayer();
const playerPawns = this.getPlayerPawns(movingPlayer.color); const playerPawns = this.getPlayerPawns(movingPlayer.color);
return playerPawns.filter(pawn => pawn.canMove(this.rolledNumber)); return playerPawns.filter(pawn => pawn.canMove(this.rolledNumber));
} };
RoomSchema.methods.changePositionOfPawn = function (pawn, newPosition) { RoomSchema.methods.changePositionOfPawn = function (pawn, newPosition) {
const pawnIndex = this.getPawnIndex(pawn._id); const pawnIndex = this.getPawnIndex(pawn._id);
this.pawns[pawnIndex].position = newPosition; this.pawns[pawnIndex].position = newPosition;
}; };
RoomSchema.methods.canStartGame = function () {
return this.players.filter(player => player.ready).length >= 2;
};
RoomSchema.methods.startGame = function () {
this.started = true;
this.pawns = getStartPositions();
this.nextMoveTime = Date.now() + 15000;
this.players.forEach(player => (player.ready = true));
this.players[0].nowMoving = true;
};
RoomSchema.methods.isFull = function () {
if (this.players.length === 4) {
this.full = true;
}
return this.full;
};
RoomSchema.methods.getPlayer = function (playerId) {
return this.players.find(player => player._id.toString() === playerId.toString());
};
RoomSchema.methods.addPlayer = function (name) {
this.players.push({
name: name,
ready: false,
color: colors[this.players.length],
});
};
RoomSchema.methods.getPawnIndex = function (pawnId) { RoomSchema.methods.getPawnIndex = function (pawnId) {
return this.pawns.findIndex(pawn => pawn._id.toString() === pawnId.toString()); return this.pawns.findIndex(pawn => pawn._id.toString() === pawnId.toString());
}; };

View File

@ -91,3 +91,5 @@ if (process.env.NODE_ENV === 'production') {
res.sendFile('/app/build/index.html'); res.sendFile('/app/build/index.html');
}); });
} }
module.exports = { server };

View File

@ -0,0 +1,123 @@
const { io } = require('socket.io-client');
const { expect } = require('chai');
const { server } = require('../../server');
const mongoose = require('mongoose');
const CONNECTION_URI = require('../../credentials.js');
const socketURL = 'http://localhost:8080';
const options = {
transports: ['websocket'],
'force new connection': true,
};
describe('Testing player socket handlers', function () {
let firstPlayer, secondPlayer;
before(async function () {
await mongoose.connect(CONNECTION_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
firstPlayer = io.connect(socketURL, options);
secondPlayer = io.connect(socketURL, options);
await assertDatabaseIsClear();
});
const assertDatabaseIsClear = async () => {
const collectionInfo = await mongoose.connection.db.listCollections({ name: 'rooms' }).next();
if (collectionInfo) await mongoose.connection.collections.rooms.drop();
};
beforeEach(function (done) {
firstPlayer.off('room:data');
secondPlayer.off('room:data');
firstPlayer.off('player:data');
secondPlayer.off('player:data');
done();
});
after(function (done) {
if (firstPlayer.connected) {
firstPlayer.disconnect();
}
if (secondPlayer.connected) {
secondPlayer.disconnect();
}
server.close();
assertDatabaseIsClear();
done();
});
it('should return credentials when joining room', function (done) {
firstPlayer.emit('player:login', { name: 'test1' });
firstPlayer.on('player:data', data => {
data = JSON.parse(data);
expect(data).to.have.property('roomId');
expect(data).to.have.property('playerId');
expect(data).to.have.property('color');
expect(data.color).to.equal('red');
done();
});
});
it('should correctly join player to room', function (done) {
firstPlayer.emit('room:data');
firstPlayer.on('room:data', data => {
data = JSON.parse(data);
expect(data.players[0].name).to.equal('test1');
done();
});
});
it('should correctly join player to existing room', function (done) {
secondPlayer.emit('player:login', { name: 'test2' });
secondPlayer.on('player:data', data => {
data = JSON.parse(data);
expect(data.color).to.equal('blue');
secondPlayer.emit('room:data');
});
secondPlayer.on('room:data', data => {
data = JSON.parse(data);
expect(data.players[1].name).to.equal('test2');
done();
});
});
it('should correctly change player ready status to true', function (done) {
firstPlayer.emit('player:ready');
firstPlayer.on('room:data', data => {
data = JSON.parse(data);
const player = data.players.find(player => player.name === 'test1');
expect(player.ready).to.equal(true);
done();
});
});
it('should correctly change player ready status to false', function (done) {
firstPlayer.emit('player:ready');
firstPlayer.on('room:data', data => {
data = JSON.parse(data);
const player = data.players.find(player => player.name === 'test1');
expect(player.ready).to.equal(false);
done();
});
});
it('should correctly change second player ready status to true', function (done) {
secondPlayer.emit('player:ready');
secondPlayer.on('room:data', data => {
data = JSON.parse(data);
const player = data.players.find(player => player.name === 'test2');
expect(player.ready).to.equal(true);
done();
});
});
it('should start game', function (done) {
firstPlayer.emit('player:ready');
firstPlayer.on('room:data', data => {
data = JSON.parse(data);
expect(data.started).to.equal(true);
done();
});
});
});