Skip to content

Commit

Permalink
Merge branch 'develop' into invite-players-and-connet-as-friends-#158…
Browse files Browse the repository at this point in the history
…457046
  • Loading branch information
Onah Benjamin Ifeanyi committed Oct 16, 2018
2 parents fe15157 + 94754af commit 292d628
Show file tree
Hide file tree
Showing 9 changed files with 279 additions and 426 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
4 changes: 2 additions & 2 deletions config/socket/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,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 @@ -256,7 +256,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 292d628

Please sign in to comment.