added tests
This commit is contained in:
commit
fcc6fcde28
@ -1,122 +1,58 @@
|
||||
const RoomModel = require('../schemas/room');
|
||||
const { colors } = require('../utils/constants');
|
||||
const { getStartPositions } = require('../utils/functions');
|
||||
|
||||
module.exports = (io, socket) => {
|
||||
const req = socket.request;
|
||||
|
||||
// Function responsible for adding a player to an existing room or creating a new one
|
||||
const login = data => {
|
||||
// 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 there is one adds player to it
|
||||
addPlayerToExistingRoom(room, data);
|
||||
} else {
|
||||
// If not creates new room and add player to it
|
||||
createNewRoom(data);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
// 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 => {
|
||||
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);
|
||||
socket.on('player:ready', ready);
|
||||
|
||||
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
|
||||
req.session.reload(err => {
|
||||
if (err) return socket.disconnect();
|
||||
// Saving session data
|
||||
req.session.roomId = room._id.toString();
|
||||
req.session.playerId = room.players[0]._id.toString();
|
||||
req.session.color = room.players[0].color;
|
||||
req.session.save();
|
||||
// Sending data to the user, after which player will be redirected to the game
|
||||
socket.join(room._id.toString());
|
||||
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;
|
||||
const handleLogin = async data => {
|
||||
const room = await RoomModel.findOne({ full: false, started: false });
|
||||
if (room) {
|
||||
addPlayerToExistingRoom(room, data);
|
||||
} else {
|
||||
createNewRoom(data);
|
||||
}
|
||||
// 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));
|
||||
socket.emit('room:data', JSON.stringify(updatedRoom));
|
||||
});
|
||||
};
|
||||
|
||||
const handleReady = async () => {
|
||||
const { roomId, playerId } = req.session;
|
||||
const room = await RoomModel.findOne({ _id: roomId });
|
||||
room.getPlayer(playerId).changeReadyStatus();
|
||||
if (room.canStartGame()) {
|
||||
room.startGame();
|
||||
}
|
||||
await RoomModel.findOneAndUpdate({ _id: roomId }, room);
|
||||
io.to(roomId).emit('room:data', JSON.stringify(room));
|
||||
};
|
||||
|
||||
const createNewRoom = async data => {
|
||||
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);
|
||||
};
|
||||
|
||||
// Since it is not bound to an HTTP request, the session must be manually reloaded and saved
|
||||
const reloadSession = room => {
|
||||
req.session.reload(err => {
|
||||
if (err) return socket.disconnect();
|
||||
req.session.roomId = room._id.toString();
|
||||
req.session.playerId = room.players[room.players.length - 1]._id.toString();
|
||||
req.session.color = colors[room.players.length - 1];
|
||||
req.session.save();
|
||||
socket.join(room._id.toString());
|
||||
socket.emit('player:data', JSON.stringify(req.session));
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
socket.on('player:login', handleLogin);
|
||||
socket.on('player:ready', handleReady);
|
||||
};
|
||||
|
||||
@ -2,36 +2,17 @@ const RoomModel = require('../schemas/room');
|
||||
|
||||
module.exports = (io, socket) => {
|
||||
const req = socket.request;
|
||||
const getData = () => {
|
||||
RoomModel.findOne({ _id: req.session.roomId }, function (err, room) {
|
||||
if (!room) return err;
|
||||
if (room.nextMoveTime <= Date.now()) {
|
||||
changeCurrentMovingPlayer();
|
||||
} else {
|
||||
io.to(req.session.roomId.toString()).emit('room:data', JSON.stringify(room));
|
||||
}
|
||||
});
|
||||
|
||||
const getData = async () => {
|
||||
let room = await RoomModel.findOne({ _id: req.session.roomId });
|
||||
// Handle the situation when the server crashes and any player reconnects after the time has expired
|
||||
// Typically, the responsibility for changing players is managed by gameHandler.js.
|
||||
if (room.nextMoveTime <= Date.now()) {
|
||||
room.changeMovingPlayer();
|
||||
await RoomModel.findOneAndUpdate({ _id: req.session.roomId }, room);
|
||||
}
|
||||
io.to(req.session.roomId).emit('room:data', JSON.stringify(room));
|
||||
};
|
||||
|
||||
socket.on('room:data', getData);
|
||||
|
||||
function changeCurrentMovingPlayer() {
|
||||
RoomModel.findOne({ _id: req.session.roomId }, function (err, room) {
|
||||
if (!room) return err;
|
||||
const index = room.players.findIndex(player => player.nowMoving === true);
|
||||
const roomSize = room.players.length;
|
||||
room.players[index].nowMoving = false;
|
||||
if (index + 1 === roomSize) {
|
||||
room.players[0].nowMoving = true;
|
||||
} else {
|
||||
room.players[index + 1].nowMoving = true;
|
||||
}
|
||||
room.nextMoveTime = Date.now() + 15000;
|
||||
if (this.timeoutID) clearTimeout(this.timeoutID);
|
||||
this.timeoutID = null;
|
||||
RoomModel.findOneAndUpdate({ _id: req.session.roomId }, room, function (err, updatedRoom) {
|
||||
io.to(req.session.roomId).emit('room:data', JSON.stringify(updatedRoom));
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
1887
backend/package-lock.json
generated
1887
backend/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@ -9,5 +9,13 @@
|
||||
"express-session": "^1.17.1",
|
||||
"mongoose": "^5.12.0",
|
||||
"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"
|
||||
}
|
||||
}
|
||||
|
||||
@ -9,4 +9,8 @@ const PlayerSchema = new Schema({
|
||||
nowMoving: { type: Boolean, default: false },
|
||||
});
|
||||
|
||||
PlayerSchema.methods.changeReadyStatus = function () {
|
||||
this.ready = !this.ready;
|
||||
};
|
||||
|
||||
module.exports = PlayerSchema;
|
||||
|
||||
@ -1,18 +1,19 @@
|
||||
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 PawnSchema = require('./pawn');
|
||||
const PlayerSchema = require('./player');
|
||||
|
||||
const RoomSchema = new Schema({
|
||||
createDate: Date,
|
||||
createDate: { type: Date, default: Date.now },
|
||||
started: { type: Boolean, default: false },
|
||||
full: { type: Boolean, default: false },
|
||||
nextMoveTime: Number,
|
||||
timeoutID: Number,
|
||||
rolledNumber: Number,
|
||||
players: [PlayerSchema],
|
||||
pawns: [PawnSchema],
|
||||
pawns: { type: [PawnSchema], default: getStartPositions() },
|
||||
});
|
||||
|
||||
RoomSchema.methods.beatPawns = function (position, attackingPawnColor) {
|
||||
@ -49,13 +50,44 @@ RoomSchema.methods.getPawnsThatCanMove = function () {
|
||||
const movingPlayer = this.getCurrentlyMovingPlayer();
|
||||
const playerPawns = this.getPlayerPawns(movingPlayer.color);
|
||||
return playerPawns.filter(pawn => pawn.canMove(this.rolledNumber));
|
||||
}
|
||||
};
|
||||
|
||||
RoomSchema.methods.changePositionOfPawn = function (pawn, newPosition) {
|
||||
const pawnIndex = this.getPawnIndex(pawn._id);
|
||||
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.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) {
|
||||
if (this.full) return;
|
||||
this.players.push({
|
||||
name: name,
|
||||
ready: false,
|
||||
color: colors[this.players.length],
|
||||
});
|
||||
};
|
||||
|
||||
RoomSchema.methods.getPawnIndex = function (pawnId) {
|
||||
return this.pawns.findIndex(pawn => pawn._id.toString() === pawnId.toString());
|
||||
};
|
||||
|
||||
@ -91,3 +91,5 @@ if (process.env.NODE_ENV === 'production') {
|
||||
res.sendFile('/app/build/index.html');
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { server };
|
||||
123
backend/tests/handlers/player.test.js
Normal file
123
backend/tests/handlers/player.test.js
Normal 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();
|
||||
});
|
||||
});
|
||||
});
|
||||
71
backend/tests/schemas/room.test.js
Normal file
71
backend/tests/schemas/room.test.js
Normal file
@ -0,0 +1,71 @@
|
||||
const { expect } = require('chai');
|
||||
const RoomModel = require('../../schemas/room');
|
||||
const { getPawnPositionAfterMove, getStartPositions } = require('../../utils/functions');
|
||||
describe('Testing room model methods', function () {
|
||||
const room = new RoomModel();
|
||||
|
||||
beforeEach(function () {
|
||||
room.players = [];
|
||||
room.pawns = getStartPositions();
|
||||
});
|
||||
it('should correctly beat pawn', function () {
|
||||
room.addPlayer('test1', 'red');
|
||||
room.addPlayer('test2', 'blue');
|
||||
room.pawns.forEach(pawn => {
|
||||
pawn.position = getPawnPositionAfterMove(1, pawn);
|
||||
});
|
||||
room.beatPawns(16, 'green');
|
||||
room.pawns.forEach(pawn => {
|
||||
if (pawn.color != 'red') {
|
||||
expect(pawn.position).to.not.equal(pawn.basePos);
|
||||
} else {
|
||||
expect(pawn.position).to.equal(pawn.basePos);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
it('should correctly beat multiple pawns', function () {
|
||||
room.pawns[0].position = 16;
|
||||
room.pawns[1].position = 16;
|
||||
room.beatPawns(16, 'green');
|
||||
room.pawns.forEach(pawn => {
|
||||
expect(pawn.position).to.equal(pawn.basePos);
|
||||
});
|
||||
});
|
||||
|
||||
it('should correctly change moving player from last to first', function () {
|
||||
room.addPlayer('test1', 'red');
|
||||
room.addPlayer('test2', 'blue');
|
||||
room.players[1].nowMoving = true;
|
||||
room.changeMovingPlayer();
|
||||
expect(room.players[0].nowMoving).to.equal(true);
|
||||
});
|
||||
|
||||
it('should correctly change moving player from first to second', function () {
|
||||
room.addPlayer('test1', 'red');
|
||||
room.addPlayer('test2', 'blue');
|
||||
room.players[0].nowMoving = true;
|
||||
room.changeMovingPlayer();
|
||||
expect(room.players[1].nowMoving).to.equal(true);
|
||||
});
|
||||
|
||||
it('should correctly returns pawns that can move', function () {
|
||||
room.addPlayer('test1', 'red');
|
||||
room.addPlayer('test2', 'blue');
|
||||
room.players[0].nowMoving = true;
|
||||
room.pawns[0].position = 16;
|
||||
room.rolledNumber = 2;
|
||||
const pawnsThatCanMove = room.getPawnsThatCanMove();
|
||||
expect(pawnsThatCanMove.length).to.equal(1);
|
||||
});
|
||||
|
||||
it('should given rolled 6 correctly returns pawns that can move', function () {
|
||||
room.addPlayer('test1', 'red');
|
||||
room.addPlayer('test2', 'blue');
|
||||
room.players[0].nowMoving = true;
|
||||
room.pawns[0].position = 16;
|
||||
room.rolledNumber = 6;
|
||||
const pawnsThatCanMove = room.getPawnsThatCanMove();
|
||||
expect(pawnsThatCanMove.length).to.equal(4);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue
Block a user