Permalink
Browse files

[rewrite] Making code make logical sense, purging hacky bits, bug fixes.

  • Loading branch information...
Saturn-V committed Apr 24, 2017
1 parent 0ccb3d3 commit caa370b089e3c3a8631cf15776a913dc57493d0e
Showing with 25 additions and 32 deletions.
  1. +1 −10 public/scripts/games.js
  2. +8 −7 public/scripts/sockets.js
  3. +0 −1 server.js
  4. +16 −14 sockets/game.js
View
@@ -1,17 +1,9 @@
//CREATING GAME POST REQUESTS AND THE LIKE GO HERE
function generateCode() {
// return Math.round((Math.pow(36, 4 + 1) - Math.random() * Math.pow(36, 4))).toString(36).slice(1);
return 'WFER'
return Math.round((Math.pow(36, 4 + 1) - Math.random() * Math.pow(36, 4))).toString(36).slice(1);
}
$(function() {
$('#join-game').click(function(event) {
var code = $('game-code').val();
// if game code exists in global variable of game connected-users
// take user to /:code
// else
// u tryna join a fake ass room bruh
});
$('#create-game').click(function(event) {
var name = $('#game-name').val();
@@ -29,7 +21,6 @@ $(function() {
alert(error.message);
},
success: function(game) {
// alert('Game Created succesfuly - Redirecting');
window.location.href = '/' + game.code;
}
});
View
@@ -1,15 +1,15 @@
// ANYTHING CORRESPONDING TO THE GAME SHOW PAGE SHOULD GO HERE.
var socket = io();
var questionIndex = 0
var gameCode = ""
var questionIndex = 0;
var gameCode = "";
//ONCE WE HIT THE GAMES SHOW PAGE, GRAB GAME CODE AND EMIT TO SERVER
$(function() {
$('.question').first().show().addClass('current-question');
gameCode = window.location.href.split('/')[3];
socket.emit('join_room', gameCode, function() { });
socket.emit('join_room', gameCode);
//ONCE USER CLICKS ON AN ANSWER, GRAB TEXT AND EMIT DATA TO SERVER
//
$('body').on('click', '.answer', function(e) {
e.preventDefault();
@@ -51,6 +51,7 @@ $(function() {
} else {
console.log('Waiting for the rest of the players to answer')
// FEATURE: Update all clients with who has just created an answer
}
});
@@ -60,7 +61,6 @@ $(function() {
$('#answer-input').hide(); // hide input
$('#fake-answer').val(""); // clear input
$('#answers').show(); // display answers
var answer = $('.answer').first()
@@ -71,6 +71,8 @@ $(function() {
answer.val(answers[i]);
answer = answer.next();
}
$('#answers').show(); // display answers
}, 1000);
});
@@ -85,8 +87,7 @@ $(function() {
socket.on('room:update_answered', function(data) {
// update dom to reflect # of people who have answered the question
// cb();
socket.emit('room:next_question', data.game);
socket.emit('room:next_question', data);
});
socket.on('room:next_question', function() {
View
@@ -37,7 +37,6 @@ app.get('/', function(req, res){
app.use('/', games)
// Socket Logic
var sockets = require('./sockets/game.js');
sockets(io);
View
@@ -2,6 +2,7 @@ module.exports = function(io) {
var loadedAnswer = [];
var participants = {};
var participantIdsDict = {};
var participantIds = [];
var Game = require('../models/game/game.js');
@@ -33,9 +34,6 @@ module.exports = function(io) {
console.log("Participants: ");
console.log(participants);
cb();
// INCREMENT THE # OF PARTICIPANTS
});
@@ -64,25 +62,31 @@ module.exports = function(io) {
if (err) { return error }
var response = {
participantIds: participantIds,
game: game
game: game,
socketId: socket.id
}
io.in(socket.room).emit('room:update_answered', response);
});
});
})
});
socket.on('room:next_question', function(game) {
console.log('Question passed for context: ' + game.code);
participantIds.push(socket.id);
socket.on('room:next_question', function(data) {
console.log('Question passed for context: ' + data.game.code);
//Check my socket id with everyone else's...if my socket id is there twice
if (!(data.socketId in participantIdsDict)) {
participantIdsDict[data.socketId] = true;
participantIds.push(data.socketId);
}
console.log('participantIds: ' + participantIds);
if ((participantIds.length) == participants[game.code]*4) {
participantIds = []
if ((participantIds.length) == participants[data.game.code]) {
participantIdsDict = {};
participantIds = [];
io.in(socket.room).emit('room:next_question');
} else {
console.log("'" + socket.id + "' has selected their answer...")
console.log("Awaiting " + (participants[game.code] - participantIds.length));
console.log("Awaiting " + (participants[data.game.code] - participantIds.length));
}
});
@@ -112,9 +116,7 @@ module.exports = function(io) {
} else {
console.log("'" + socket.id + "' has entered their answer...")
console.log("Awaiting " + (participants[data.code] - answers.length));
var response = {
ready: false
}
var response = { ready: false }
cb(response)
}
})

0 comments on commit caa370b

Please sign in to comment.