refactored backend
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const { getPawnPositionAfterMove } = require('../utils/functions');
|
||||
|
||||
const PawnSchema = new Schema({
|
||||
color: String,
|
||||
basePos: Number,
|
||||
position: Number,
|
||||
});
|
||||
|
||||
PawnSchema.methods.canMove = function (rolledNumber) {
|
||||
if (this.position === this.basePos && (rolledNumber === 6 || rolledNumber === 1)) {
|
||||
return true;
|
||||
}
|
||||
// (if player's pawn is near finish line) if the move does not go beyond the win line
|
||||
if (this.position !== getPawnPositionAfterMove(rolledNumber, this) && this.position !== this.basePos) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
module.exports = PawnSchema;
|
||||
@@ -0,0 +1,25 @@
|
||||
const mongoose = require('mongoose');
|
||||
|
||||
const Schema = mongoose.Schema;
|
||||
|
||||
const PlayerSchema = new Schema({
|
||||
sessionID: String,
|
||||
name: String,
|
||||
color: String,
|
||||
ready: { type: Boolean, default: false },
|
||||
nowMoving: { type: Boolean, default: false },
|
||||
});
|
||||
|
||||
PlayerSchema.methods.changeReadyStatus = function () {
|
||||
this.ready = !this.ready;
|
||||
};
|
||||
|
||||
PlayerSchema.methods.canMove = function (room, rolledNumber) {
|
||||
const playerPawns = room.getPlayerPawns(this.color);
|
||||
for (const pawn of playerPawns) {
|
||||
if (pawn.canMove(rolledNumber)) return true;
|
||||
}
|
||||
return false;
|
||||
};
|
||||
|
||||
module.exports = PlayerSchema;
|
||||
@@ -0,0 +1,114 @@
|
||||
const mongoose = require('mongoose');
|
||||
const { colors } = require('../utils/constants');
|
||||
const { getPawnPositionAfterMove, getStartPositions } = require('../utils/functions');
|
||||
const { makeRandomMove } = require('../handlers/handlersFunctions');
|
||||
const PawnSchema = require('./pawn');
|
||||
const PlayerSchema = require('./player');
|
||||
|
||||
const RoomSchema = new mongoose.Schema({
|
||||
name: String,
|
||||
private: { type: Boolean, default: true },
|
||||
password: String,
|
||||
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: { type: [PawnSchema], default: getStartPositions() },
|
||||
});
|
||||
|
||||
RoomSchema.methods.beatPawns = function (position, attackingPawnColor) {
|
||||
const pawnsOnPosition = this.pawns.filter(pawn => pawn.position === position);
|
||||
pawnsOnPosition.forEach(pawn => {
|
||||
if (pawn.color !== attackingPawnColor) {
|
||||
const index = this.getPawnIndex(pawn._id);
|
||||
this.pawns[index].position = this.pawns[index].basePos;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
RoomSchema.methods.changeMovingPlayer = function () {
|
||||
const playerIndex = this.players.findIndex(player => player.nowMoving === true);
|
||||
this.players[playerIndex].nowMoving = false;
|
||||
if (playerIndex + 1 === this.players.length) {
|
||||
this.players[0].nowMoving = true;
|
||||
} else {
|
||||
this.players[playerIndex + 1].nowMoving = true;
|
||||
}
|
||||
this.nextMoveTime = Date.now() + 15000;
|
||||
this.rolledNumber = null;
|
||||
if (this.timeoutID) clearTimeout(this.timeoutID);
|
||||
this.timeoutID = setTimeout(makeRandomMove, 15000, this._id.toString());
|
||||
};
|
||||
|
||||
RoomSchema.methods.movePawn = function (pawn) {
|
||||
const newPositionOfMovedPawn = getPawnPositionAfterMove(this.rolledNumber, pawn);
|
||||
this.changePositionOfPawn(pawn, newPositionOfMovedPawn);
|
||||
this.beatPawns(newPositionOfMovedPawn, pawn.color);
|
||||
};
|
||||
|
||||
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;
|
||||
this.timeoutID = setTimeout(makeRandomMove, 15000, this._id.toString());
|
||||
};
|
||||
|
||||
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, id) {
|
||||
if (this.full) return;
|
||||
this.players.push({
|
||||
sessionID: id,
|
||||
name: name,
|
||||
ready: false,
|
||||
color: colors[this.players.length],
|
||||
});
|
||||
};
|
||||
|
||||
RoomSchema.methods.getPawnIndex = function (pawnId) {
|
||||
return this.pawns.findIndex(pawn => pawn._id.toString() === pawnId.toString());
|
||||
};
|
||||
|
||||
RoomSchema.methods.getPawn = function (pawnId) {
|
||||
return this.pawns.find(pawn => pawn._id.toString() === pawnId.toString());
|
||||
};
|
||||
|
||||
RoomSchema.methods.getPlayerPawns = function (color) {
|
||||
return this.pawns.filter(pawn => pawn.color === color);
|
||||
};
|
||||
|
||||
RoomSchema.methods.getCurrentlyMovingPlayer = function () {
|
||||
return this.players.find(player => player.nowMoving === true);
|
||||
};
|
||||
|
||||
const Room = mongoose.model('Room', RoomSchema);
|
||||
|
||||
module.exports = Room;
|
||||
Reference in New Issue
Block a user