From 3afc2fa66ecde074e1f2166c685b6405fc822764 Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 22 Jul 2012 15:01:50 +0200 Subject: [PATCH] fixed server bugs --- app/Game/Core/GameController.js | 7 ++++--- app/Game/Server/Channel.js | 4 ++-- app/Game/Server/GameController.js | 14 ++++++++++---- app/Game/Server/NotificationCenter.js | 11 +++++++---- app/Game/Server/User.js | 15 +-------------- 5 files changed, 24 insertions(+), 27 deletions(-) diff --git a/app/Game/Core/GameController.js b/app/Game/Core/GameController.js index 5f26592..42c27c9 100755 --- a/app/Game/Core/GameController.js +++ b/app/Game/Core/GameController.js @@ -1,9 +1,10 @@ define([ "Game/Core/Physics/Engine", - "Game/Core/Loader/Level" + "Game/Core/Loader/Level", + "Game/Core/Player" ], -function(Engine, Level) { +function(Engine, Level, Player) { function GameController(physicsEngine) { console.log('constructor called'); @@ -36,7 +37,7 @@ function(Engine, Level) { } GameController.prototype.userJoined = function(user) { - var player = new Player(id, this.physicsEngine); + var player = new Player(user.id, this.physicsEngine); this.players[user.id] = player; return player; } diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index 867e85a..0bbec07 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -25,11 +25,11 @@ function(GameController, NotificationCenter) { var userIds = Object.keys(this.users); this.users[user.id] = user; - + user.sendCommand('joinSuccess', {channelName: this.name, id: user.id, userIds: userIds}); this.sendCommandToAllUsersExcept('userJoined', user.id, user); - this.gameController.createPlayerForUser(user) + NotificationCenter.trigger('user/joined', user); } Channel.prototype.releaseUser = function(user) { diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index e4aa2cc..52628a2 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -16,6 +16,9 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not this.update(); this.updateWorld(); + + NotificationCenter.on('user/joined', this.userJoined, this); + NotificationCenter.on('user/left', this.userLeft, this); } GameController.prototype = Object.create(Parent.prototype); @@ -26,13 +29,16 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not this.physicsEngine.update(); for(var id in this.players) { - this.players[id].player.update(); + this.players[id].update(); } } GameController.prototype.userJoined = function(user) { - var player = Parent.prototype.userJoined.call(this, user); - this.inputControllers[player.id] = new InputController(player); + Parent.prototype.userJoined.call(this, user); + + var id = user.id; + var player = this.players[id]; + this.inputControllers[id] = new InputController(player); } GameController.prototype.userLeft = function(user) { @@ -68,7 +74,7 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not } while (body = body.GetNext()); if(isUpdateNeeded) { - NotificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]); + //NotificationCenter.trigger("sendCommandToAllUsers", ['gameCommand', {worldUpdate:update}]); } setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); diff --git a/app/Game/Server/NotificationCenter.js b/app/Game/Server/NotificationCenter.js index b243c28..7401613 100755 --- a/app/Game/Server/NotificationCenter.js +++ b/app/Game/Server/NotificationCenter.js @@ -8,21 +8,23 @@ function() { this.subUid = -1; } - NotificationCenter.prototype.trigger = function(topic, args) { + NotificationCenter.prototype.trigger = function(topic) { if (!this.topics[topic]) { throw "No such topic " + topic + ". Could not trigger."; } + var args = Array.prototype.slice.call(arguments, 1); var subscribers = this.topics[topic]; var len = subscribers ? subscribers.length : 0; while (len--) { - subscribers[len].func(topic, args); + var subscriber = subscribers[len]; + subscriber.func.apply(subscriber.context, args); } } - NotificationCenter.prototype.on = function(topic, func) { + NotificationCenter.prototype.on = function(topic, func, context) { if (!this.topics[topic]) { this.topics[topic] = []; @@ -31,7 +33,8 @@ function() { var token = ( ++this.subUid ).toString(); this.topics[topic].push({ token: token, - func: func + func: func, + context: context }); return token; diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index 1498356..f77dbe3 100755 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -29,20 +29,7 @@ function(ProtocolHelper, NotificationCenter) { } User.prototype.setChannel = function(channel) { - if (NotificationCenter) { - NotificationCenter.off("updateClientsWorld"); - } - this.channel = channel; - - // Use the right factory and nc - NotificationCenter = this.channel.notificationCenter; - this.factory = this.channel.factory; - - var self = this; - NotificationCenter.on("sendCommandToAllUsers", function(topic, args) { - self.sendCommand.apply(self, args); - }); } User.prototype.sendCommand = function(command, options) { @@ -75,7 +62,7 @@ function(ProtocolHelper, NotificationCenter) { case 'gameCommand': for(var gameCommand in options) { - NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]); + //NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]); //this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this); } break;