Refactor out socket call from garageservergame on server

This commit is contained in:
Jeremiah Billmann
2013-07-16 21:27:46 -04:00
parent 2af51012c1
commit f5452bc984
2 changed files with 10 additions and 7 deletions
+6 -2
View File
@@ -29,10 +29,14 @@ api methods
sendPlayersEvent(data)
*/
function GarageServer(socketio, options) {
this.socketPath = '/garageserver.io';
var namespace = '/garageserver.io';
this.socketPath = namespace;
this.io = socketio;
this.game = new garageServerGame(options);
this.registerSocketEvents(options);
this.game = new garageServerGame(options, function (state) {
socketio.of(namespace).emit('update' ,state);
});
}
GarageServer.prototype.registerSocketEvents = function (options) {
+4 -5
View File
@@ -3,13 +3,14 @@ var playerController = require('./controllers/playercontroller'),
exports = module.exports = GarageServerGame;
function GarageServerGame(options) {
function GarageServerGame(options, broadcastCallback) {
this.playerController = new playerController();
this.entityController = new entityController();
this.options = options;
this.startTime = 0;
this.stateInterval = options.stateInterval ? options.stateInterval : 45;
this.stateIntervalId = 0;
this.broadcastCallback = broadcastCallback;
}
GarageServerGame.prototype.start = function () {
@@ -29,10 +30,8 @@ GarageServerGame.prototype.broadcastState = function () {
state.playerStates = this.getState(this.playerController);
state.entityStates = this.getState(this.entityController);
this.playerController.entities.forEach(function (player) {
player.client.emit('update', state);
});
this.broadcastCallback(state);
};
GarageServerGame.prototype.getState = function (controller) {