diff --git a/client/garageserver.io.js b/client/garageserver.io.js index 983ba40..646622a 100644 --- a/client/garageserver.io.js +++ b/client/garageserver.io.js @@ -7,12 +7,18 @@ options = { onEntityUpdate: function (state), onPlayerRemove: function (id), onEntityRemove: function (id), + onEvent: function (data), onWorldState: function (state), onPing: function (pingDelay), onUpdatePlayerPhysics: function (state, inputs, deltaTime), onInterpolation: function(previousState, targetState, amount) logging: true } +api methods + initializeGarageServer(path, options) + addInput({}) + getStates(function (playerStates, entityStates)) + getId() : 0 */ var GarageServerIO = (function (socketio) { @@ -164,8 +170,8 @@ var GarageServerIO = (function (socketio) { _playerController = new PlayerController(), _entityController = new EntityController(), - initializeGarageServer = function (path, opts) { - _options = opts; + initializeGarageServer = function (path, options) { + _options = options; _socket = _io.connect(path + '/garageserver.io'); registerSocketEvents(); }, @@ -243,6 +249,14 @@ var GarageServerIO = (function (socketio) { console.log('garageserver.io:: socket removeEntity ' + id); } }); + _socket.on('event', function(data) { + if (_options.onEvent) { + _options.onEvent(data); + } + if (_options.logging) { + console.log('garageserver.io:: socket event ' + data); + } + }); }, getId = function () { diff --git a/src/server/garageserver.io.js b/src/server/garageserver.io.js index 64e656c..41d228f 100644 --- a/src/server/garageserver.io.js +++ b/src/server/garageserver.io.js @@ -15,8 +15,19 @@ options = { onPlayerDisconnect: function (socket), onPing: function (socket, data) } +api methods + createGarageServer(io, options) + start() + stop() + getPlayers() : [player] : { id: '', state: {}, inputs: [{}], stateHistory: [{ state, executionTime }] } + getEntities() : [entity] : { id: '', state: {}, stateHistory: [{ state, executionTime }] } + updatePlayerState(id, state) + updateEntityState(id, state) + addEntity(id) + removeEntity(id) + sendPlayerEvent(id, data) + sendPlayersEvent(data) */ - function GarageServer(socketio, options) { this.socketPath = '/garageserver.io'; this.io = socketio; @@ -127,6 +138,14 @@ GarageServer.prototype.removeEntity = function (id) { this.game.removeEntity(id); }; +GarageServer.prototype.sendPlayerEvent = function (id, data) { + this.game.sendPlayerEvent(id, data); +}; + +GarageServer.prototype.sendPlayersEvent = function (data) { + this.io.of(this.socketPath).emit('event', data); +}; + exports.createGarageServer = function (io, options) { return new GarageServer(io, options); }; \ No newline at end of file diff --git a/src/server/garageservergame.js b/src/server/garageservergame.js index 8a3f6ae..6219366 100644 --- a/src/server/garageservergame.js +++ b/src/server/garageservergame.js @@ -20,7 +20,7 @@ GarageServerGame.prototype.start = function () { }; GarageServerGame.prototype.stop = function () { - clearInterval(this.stateIntervalId); + clearInterval(this.stateIntervalId); }; GarageServerGame.prototype.broadcastState = function () { @@ -100,4 +100,13 @@ GarageServerGame.prototype.removeEntity = function (id) { GarageServerGame.prototype.addPlayerInput = function (id, input, sequence, time) { this.playerController.addInput(id, input, sequence, time); +}; + +GarageServerGame.prototype.sendPlayerEvent = function (id, data) { + this.playerController.entities.some(function (player) { + if (player.id === id) { + player.client.emit('event', data); + return true; + } + }); }; \ No newline at end of file