Skip to content

Commit

Permalink
Merge 4026f04 into b900880
Browse files Browse the repository at this point in the history
  • Loading branch information
kenware authored Jul 24, 2018
2 parents b900880 + 4026f04 commit 0f6eaaa
Show file tree
Hide file tree
Showing 12 changed files with 3,856 additions and 3,977 deletions.
43 changes: 31 additions & 12 deletions app/controllers/answers.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ const Answer = mongoose.model('Answer');


/**
* Find answer by id
* @description Get answers and pass it to req
*
* @param {object} req
* @param {object} res
* @param {Function} next
* @param {object} id
*
* @returns {Array} returns the answer array
*/
export const answer = (req, res, next, id) => {
Answer.load(id, (err, answerObject) => {
Expand All @@ -19,14 +26,26 @@ export const answer = (req, res, next, id) => {
};

/**
* Show an answer
* @description Get games leaderboard
*
* @param {object} req
* @param {object} res
* @param {Function} next
* @param {object} id
*
* @returns {Array} returns the game array
*/
export const show = (req, res) => {
res.jsonp(req.answer);
};

/**
* List of Answers
* @description Get games leaderboard
*
* @param {object} req
* @param {object} res
*
* @returns {Array} returns the game array
*/
export const all = (req, res) => {
Answer.find({ official: true }).select('-_id').exec((err, answers) => {
Expand All @@ -41,15 +60,15 @@ export const all = (req, res) => {
};

/**
* List of Answers (for Game class)
* @description Get games leaderboard
*
* @param {object} cb
* @param {object} region
*
* @returns {Array} returns the game array
*/
export const allAnswersForGame = (cb) => {
Answer.find({ official: true }).select('-_id').exec((err, answers) => {
if (err) {
console.log(err);
} else {
cb(answers);
}
export const allAnswersForGame = (cb, region) => {
Answer.find({ official: true, region }).select('-_id').exec((err, answers) => {
cb(answers);
});
};

92 changes: 56 additions & 36 deletions app/controllers/questions.js
Original file line number Diff line number Diff line change
@@ -1,55 +1,75 @@
/**
* Module dependencies.
*/
var mongoose = require('mongoose'),
async = require('async'),
Question = mongoose.model('Question'),
_ = require('underscore');
import mongoose from 'mongoose';

const Question = mongoose.model('Question');

/**
* Find question by id
* @description returns grray or error on failed game load
*
* @param {object} req
* @param {object} res
* @param {Function} next
* @param {object} id
*
* @returns {Array} returns the game array
*/
exports.question = function(req, res, next, id) {
Question.load(id, function(err, question) {
if (err) return next(err);
if (!question) return next(new Error('Failed to load question ' + id));
req.question = question;
next();
});
exports.question = (req, res, next, id) => {
Question.load(id, (err, question) => {
if (err) return next(err);
if (!question) return next(new Error(`Failed to load question ${id}`));
req.question = question;
next();
});
};

/**
* Show an question
*/
exports.show = function(req, res) {
res.jsonp(req.question);
* @description pass questions to req
*
* @param {object} req
* @param {object} res
* @param {Function} next
* @param {object} id
*
* @returns {Array} returns the game array
*/
exports.show = (req, res) => {
res.jsonp(req.question);
};

/**
* List of Questions
* @description Get all games questions
*
* @param {object} req
* @param {object} res
* @param {Function} next
* @param {object} id
*
* @returns {Array} returns the game array
*/
exports.all = function(req, res) {
Question.find({official:true, numAnswers: {$lt : 3}}).select('-_id').exec(function(err, questions) {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(questions);
}
});
exports.all = (req, res) => {
Question.find({ official: true, numAnswers: { $lt: 3 } }).select('-_id').exec((err, questions) => {
if (err) {
res.render('error', {
status: 500
});
} else {
res.jsonp(questions);
}
});
};

/**
* List of Questions (for Game class)
* @description Get games leaderboard
*
* @param {object} cb
* @param {object} region
*
* @returns {Array} returns the game array according to region
*/
exports.allQuestionsForGame = function(cb) {
Question.find({official:true, numAnswers: {$lt : 3}}).select('-_id').exec(function(err, questions) {
if (err) {
console.log(err);
} else {
cb(questions);
}
});
exports.allQuestionsForGame = (cb, region) => {
Question.find({ official: true, region, numAnswers: { $lt: 3 } }).select('-_id').exec((err, questions) => {
cb(questions);
});
};
26 changes: 15 additions & 11 deletions config/socket/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ Game.prototype.assignGuestNames = function () {
});
};

Game.prototype.prepareGame = function () {
Game.prototype.prepareGame = function (region) {
this.state = 'game in progress';
this.io.sockets.in(this.gameID).emit('prepareGame',
{
Expand All @@ -147,8 +147,8 @@ Game.prototype.prepareGame = function () {
});
const self = this;
async.parallel([
this.getQuestions,
this.getAnswers
this.getQuestions(region),
this.getAnswers(region)
], (err, results) => {
[self.questions, self.answers] = results;
self.startGame();
Expand Down Expand Up @@ -352,16 +352,20 @@ Game.prototype.stateDissolveGame = function () {
this.persistGame();
};

Game.prototype.getQuestions = function (cb) {
questions.allQuestionsForGame((data) => {
cb(null, data);
});
Game.prototype.getQuestions = function (region) {
return function (cb) {
questions.allQuestionsForGame((data) => {
cb(null, data);
}, region);
};
};

Game.prototype.getAnswers = function (cb) {
allAnswersForGame((data) => {
cb(null, data);
});
Game.prototype.getAnswers = function (region) {
return function (cb) {
allAnswersForGame((data) => {
cb(null, data);
}, region);
};
};

Game.prototype.shuffleCards = function (cards) {
Expand Down
21 changes: 19 additions & 2 deletions config/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,13 @@ export default (io) => {
}
const game = new Game(uniqueRoom, io);
allPlayers[socket.id] = true;

// const isUserExist = game.players.map(user => user.userId === player.userId);
const isUserExist = game.players.find(user => user.userId === player.userId);
if (isUserExist && player.userId !== 'unauthenticated') {
return io.sockets.socket(socket.id).emit('userExist');
}

game.players.push(player);
allGames[uniqueRoom] = game;
socket.join(game.gameID);
Expand All @@ -87,6 +94,11 @@ export default (io) => {
gameIDStr = gameID.toString();
game = new Game(gameIDStr, io);
allPlayers[socket.id] = true;
// const isUserExist = game.players.map(user => user.userId === player.userId);
const isUserExist = game.players.find(user => user.userId === player.userId);
if (isUserExist && player.userId !== 'unauthenticated') {
return io.sockets.socket(socket.id).emit('userExist');
}
game.players.push(player);
allGames[gameID] = game;
gamesNeedingPlayers.push(game);
Expand Down Expand Up @@ -139,6 +151,11 @@ export default (io) => {
|| game.players[0].socket.id !== socket.id)) {
// Put player into the requested game
allPlayers[socket.id] = true;
// const isUserExist = game.players.map(user => user.userId === player.userId);
const isUserExist = game.players.find(user => user.userId === player.userId);
if (isUserExist && player.userId !== 'unauthenticated') {
return io.sockets.socket(socket.id).emit('userExist');
}
game.players.push(player);
socket.join(game.gameID);
socket.gameID = game.gameID;
Expand Down Expand Up @@ -226,7 +243,7 @@ export default (io) => {
allGames[socket.gameID].beginRound(allGames[socket.gameID]);
});

socket.on('startGame', () => {
socket.on('startGame', (region) => {
if (allGames[socket.gameID]) {
const thisGame = allGames[socket.gameID];
if (thisGame.players.length >= thisGame.playerMinLimit) {
Expand All @@ -236,7 +253,7 @@ export default (io) => {
return gamesNeedingPlayers.splice(index, 1);
}
});
thisGame.prepareGame();
thisGame.prepareGame(region);
thisGame.sendNotification('The game has begun!');
}
}
Expand Down
Loading

0 comments on commit 0f6eaaa

Please sign in to comment.