Permalink
Browse files

created gameScore field in game model, implemented scoring functions …

…and show score
  • Loading branch information...
coreyarch1234 committed Jun 25, 2017
1 parent 3f60100 commit 345b2ef94cd9d7189761054c45b80c7dd7f426ac
Showing with 86 additions and 4 deletions.
  1. +1 −0 .gitignore
  2. +15 −2 public/scripts/sockets.js
  3. +62 −2 sockets/game.js
  4. +8 −0 views/games/show.handlebars
View
@@ -1,3 +1,4 @@
node_modules
npm-debug.log
.env
TASKS.md
View
@@ -95,14 +95,27 @@ $(function() {
socket.on('subscribe:is_correct?', function(data, cb) {
if (data.isCorrect) {
$('#correct-answer-alert').text("Nice Job! The correct answer was: " + data.answer);
// $('#score-display').text(data.score);
$('#correct-answer-alert').text("Nice Job! Your score is: " + data.score);
// $('#correct-answer-alert').text("Nice Job! The correct answer was: " + data.answer);
} else {
$('#correct-answer-alert').text("The correct answer was: " + data.answer);
// $('#score-display').text(data.score);
$('#correct-answer-alert').text("Your score is: " + data.score);
// $('#correct-answer-alert').text("The correct answer was: " + data.answer);
}
cb();
});
socket.on('subscribe:answered', function(data) {
// Show updated score
// for (var i = 0; i < data.users.length; i++){
// if (data.users[i].sockId === socket.id) {
// // return data.users[i];
// $('#correct-answer-alert').text(data.users[i].score);
// }
// }
// $('#correct-answer-alert').text(data.score);
// console.log(data.score)
// update dom to reflect # of people who have answered the question
socket.emit('publish:next_question?', data);
});
View
@@ -5,6 +5,44 @@ module.exports = function(io) {
var excessArray = [];
var maxPlayers = 2;
//Scoring
var users = []
//Add a user with a specific ID
function addUser(sockId, score = 12) {
var newUser = {
sockId: sockId,
score: score
}
users.push(newUser)
}
// increase or decrease the score of the specified player
function increaseScore(sockId) {
for (var i = 0; i < users.length; i++){
if (users[i].sockId === sockId) {
users[i].score++;
}
}
}
function decreaseScore(sockId) {
for (var i = 0; i < users.length; i++){
if (users[i].sockId === sockId) {
users[i].score--;
}
}
}
//Find user with a specific id
function findUser(sockId) {
for (var i = 0; i < users.length; i++){
if (users[i].sockId === sockId) {
return users[i];
}
}
}
var Game = require('../models/game/game.js');
var Question = require('../models/question/question.js');
var Answer = require('../models/answer/answer.js');
@@ -26,6 +64,9 @@ module.exports = function(io) {
console.log("User '" + socket.id + "' joined...");
console.log("user is now in : " + socket.room);
//Add a user to users array to keep track of score
addUser(socket.id);
if (participants[code] == undefined) {
participants[code] = 1;
} else {
@@ -51,13 +92,32 @@ module.exports = function(io) {
var status = {} // Data we want to send to the client/user
if (answerChosen == answerCorrect) {
status = { isCorrect: true, answer: answerCorrect }
//Increase score of specific player
increaseScore(socket.id);
var user = findUser(socket.id);
// console.log("Printing users:")
// console.log(users.length)
// for (var i = 0; i < users.length; i++){
// console.log("users")
// console.log(users[i].sockId)
// }
status = { isCorrect: true, answer: answerCorrect, score: user.score }
} else {
status = { isCorrect: false, answer: answerCorrect }
//Decrease score of specific player
decreaseScore(socket.id);
var user = findUser(socket.id);
status = { isCorrect: false, answer: answerCorrect, score: user.score }
}
// MARK: Updating the users' dom (hidden field) with whether they were correct or incorrect
socket.emit('subscribe:is_correct?', status, function() {
//Find user to pass in score to response object
// var user = findUser(socket.id);
// console.log("The user found is: ");
// console.log(user)
// console.log("The users are");
// console.log(users);
// var user_score = user.score;
Game.findById(question.game, function(err, game) {
if (err) { return error }
var response = {
@@ -47,6 +47,14 @@
<!-- <input class="answer" type="submit" value="D">
<input class="answer" type="submit" value="E"> -->
</div>
<!-- Score display -->
<h1>Score: </h1>
<!-- <div class="answer-alert" id="score-display" style="display: none;">
</div> -->
<!-- Display if they got question right or wrong -->
<div class="answer-alert" id="correct-answer-alert" style="display: none;">
</div>

0 comments on commit 345b2ef

Please sign in to comment.