Skip to content

Instantly share code, notes, and snippets.

@brandon-barker
Created September 2, 2013 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandon-barker/6416710 to your computer and use it in GitHub Desktop.
Save brandon-barker/6416710 to your computer and use it in GitHub Desktop.
socket.io event listeners
io.sockets.on('connection', function (socket) {
socket.on('addPlayer', function(player) {
players[socket.id] = player;
players[socket.id].id = socket.id;
console.log("Player " + player.userName + " with id: " + socket.id + " has joined.");
for (var key in players) {
console.log("Players: " + key + " : " + players[key].userName);
}
if (Object.size(players) == 2) {
io.sockets.emit('ready', true);
}
socket.emit('playerId', socket.id);
});
socket.on('logPoints', function(player) {
if (players[socket.id] != null) {
players[socket.id].score = player.score;
console.log(players[socket.id].userName + ' score: ' + players[socket.id].score);
}
});
socket.on('answerQuestion', function (roundNumber) {
round = roundNumber;
var imdbRound = getRound(roundNumber);
imdbRound.answers++;
if (imdbRound.answers >= 2) {
if (round < 8) {
// Move on to the next round
io.sockets.emit('nextRound', true);
} else {
// Game over!
console.log('Game over!');
console.log(players);
io.sockets.emit('gameOver', players);
}
}
});
socket.on('disconnect', function() {
if (players[socket.id] != null) {
start = false;
imdb.clearRounds();
round = 1;
console.log("Player " + players[socket.id].userName + " with id: " + socket.id + " has left.");
io.sockets.emit('playerLeft', players[socket.id]);
delete players[socket.id];
for (var key in players) {
console.log("Remaining players: " + key + " : " + players[key].userName);
}
if (Object.size(players) < 2) {
io.sockets.emit('ready', false);
}
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment