Skip to content

Commit

Permalink
messaging works reconnecting broken
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentwoo committed Apr 12, 2012
1 parent 7ffe6e4 commit d768450
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 27 deletions.
19 changes: 6 additions & 13 deletions client/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ var socket
, me
, lastMsg
, preventRefresh = false
, fixwrap;
, fixwrap
, SERVER_EVENTS = ['init', 'join', 'rejoin', 'taken', 'setHash', 'remaining', 'puzzled',
'add', 'hint', 'start', 'win', 'msg'];

$(function() {
fixwrap = $('#fixwrap');
Expand All @@ -29,18 +31,9 @@ function startGame() {
}, 1500);
});

socket.on('init', init);
socket.on('join', join);
socket.on('rejoin', rejoin);
socket.on('taken', taken);
socket.on('setHash', setHash);
socket.on('remaining', remaining);
socket.on('puzzled', puzzled);
socket.on('add', add);
socket.on('hint', hint);
socket.on('start', start);
socket.on('win', win);
socket.on('msg', msg);
SERVER_EVENTS.forEach(function(event) {
socket.on(event, window[event]);
});

socket.on('disconnect', socket_disconnect);
socket.on('reconnect', socket_reconnect);
Expand Down
30 changes: 17 additions & 13 deletions game.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var CLIENT_MSGS = ['init', 'take', 'hint', 'msg'];
var CLIENT_EVENTS = ['init', 'take', 'hint', 'msg'];

var Game = function(io, hash, minPlayers) {
this.io = io;
Expand Down Expand Up @@ -56,7 +56,7 @@ Game.prototype.firstAvailablePlayerSlot = function() {
Game.prototype.getPlayerIdx = function(socket) {
for (var i = 0; i < this.players.length; i++) {
if (this.players[i] !== null &&
this.players[i].socket.sessionId === socket.sessionId)
this.players[i].socket.id === socket.id)
return i;
}
return -1;
Expand All @@ -79,14 +79,11 @@ Game.prototype.playerData = function() {
Game.prototype.registerClient = function(socket, sess) {
if (this.numPlayers() >= this.players.length) return false;
var self = this;
socket.join(this.hash);
for (var msg in CLIENT_MSGS) {
socket.on(msg, this.handleClientMessage(msg, socket));
}

for (var i = 0; i < this.players.length; i++) {
var player = this.players[i];
if (player === null) continue;
if (player.socket.sessionId === socket.sessionId || player.sess === sess) {
if (player.socket.id === socket.id || player.sess === sess) {
if (!player.online) {
this.broadcast('rejoin', i);
this.sendMsg({event: true, msg: 'Player ' + (i + 1) + ' has reconnected.'});
Expand All @@ -98,6 +95,10 @@ Game.prototype.registerClient = function(socket, sess) {
return true;
}
}
//socket.join(this.hash);
CLIENT_EVENTS.forEach(function(event) {
socket.on(event, self.handleClientMessage(event, socket));
});

var playerIdx = this.firstAvailablePlayerSlot();
this.broadcast('join', playerIdx);
Expand All @@ -124,18 +125,19 @@ Game.prototype.unregisterClient = function(socket, gameOver) {
this.broadcast('leave', playerIdx);
this.updateRemaining();
this.sendMsg({event: true, msg: 'Player ' + (playerIdx + 1) + ' has disconnected.'});
//socket.leave(this.hash);
setTimeout( function delayGameover() {
if (self.numPlayers() === 0) gameOver();
}, 3600000);
}

Game.prototype.handleClientMessage = function(message, socket) {
var func = this[message];
var player = this.getPlayerIdx(socket);
Game.prototype.handleClientMessage = function(event, socket) {
var self = this;
return function(message) {
console.log('receiving ' + message + ' event with payload' + message);
var player = self.getPlayerIdx(socket);
console.log('receiving ' + event + ' from player ' + player + ' with payload ' + message);
if (player === -1) return;
func.call(this, player, message);
self[event].call(self, player, message);
};
}

Expand All @@ -147,7 +149,9 @@ Game.prototype.updateRemaining = function() {
Game.prototype.broadcast = function(event, message) {
console.log(this.hash + ' broadcasting: ');
console.log(message);
this.io.sockets.in(this.hash).emit(event, message);
this.players.forEach( function(player) {
if (player !== null) player.socket.emit(event, message);
});
}

Game.prototype.sendMsg = function(msg) {
Expand Down
2 changes: 1 addition & 1 deletion server.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ function getLatestPublicGame() {
io.sockets.on('connection', function(socket){
var game = null;
socket.on('init', function(message){
console.log(message);
console.log('connecting socket ' + socket.id);
game = getGame(message.game);
game.registerClient(socket, message.sess);
(game.handleClientMessage('init', socket)).call(game, message);
Expand Down

0 comments on commit d768450

Please sign in to comment.