diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 0366f33..95bbf81 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -11,11 +11,18 @@ window.GarageServerIO = (function (window, socketio) { registerSocketEvents = function () { io.on('update', function(data) { - + if(garageServerGame.addPlayerInput) { + garageServerGame.addPlayerInput(data); + } }); io.on('ping', function(data) { }); + io.on('removePlayer', function(id) { + if(garageServerGame.removePlayer) { + garageServerGame.removePlayer(id); + } + }); }, startGarageServerGame = function (options) { @@ -23,15 +30,34 @@ window.GarageServerIO = (function (window, socketio) { }; function GarageServerGame (options) { - + this.players = []; + this.updates = []; } - GarageServerGame.prototype.update = function () { + GarageServerGame.prototype.addPlayerInput = function (input) { + this.updates.push(input); + }; + + GarageServerGame.prototype.sendPlayerInput = function (input) { + + }; + + GarageServerGame.prototype.removePlayer = function (id) { + for(var i = 0; i < this.players.length; i ++) { + if(this.players[i].id === id) { + this.players.splice(i, 1)[0]; + return; + } + } + }; + + GarageServerGame.prototype.updatePlayers = function () { }; return { - connectToGarageServer: connectToGarageServer + connectToGarageServer: connectToGarageServer, + startGarageServerGame: startGarageServerGame }; }) (window, io); \ No newline at end of file diff --git a/lib/server/garageserver.io.js b/lib/server/garageserver.io.js index 0bb5192..842d803 100644 --- a/lib/server/garageserver.io.js +++ b/lib/server/garageserver.io.js @@ -39,6 +39,7 @@ GarageServer.prototype.onPlayerConnect = function (socket, options) { GarageServer.prototype.onPlayerDisconnect = function (socket, options) { this.game.removePlayer(socket); + socket.broadcast.emit('removePlayer', socket.id); if(options.onPlayerDisconnect) { options.onPlayerDisconnect(socket); } diff --git a/lib/server/garageservergame.js b/lib/server/garageservergame.js index 5de07e5..19dce3d 100644 --- a/lib/server/garageservergame.js +++ b/lib/server/garageservergame.js @@ -48,6 +48,7 @@ GarageServerGame.prototype.removePlayer = function (client) { for(var i = 0; i < this.players.length; i ++) { if(this.players[i].id === client.id) { this.players.splice(i, 1)[0]; + return; } } };