From 4f4bbf73e833cd4cf578a151ca458e24c578c5ee Mon Sep 17 00:00:00 2001 From: logsol Date: Mon, 17 Mar 2014 21:46:19 +0100 Subject: [PATCH] nc refuckulating --- app/Game/Channel/Channel.js | 2 +- app/Game/Channel/GameController.js | 12 +- app/Game/Channel/GameObjects/Doll.js | 2 +- app/Game/Channel/GameObjects/Items/RagDoll.js | 2 +- app/Game/Channel/PipeToServer.js | 2 +- app/Game/Channel/Player.js | 8 +- app/Game/Channel/User.js | 11 +- app/Game/Client/Player.js | 10 +- app/Game/Client/View/DomController.js | 2 +- app/Game/Client/View/ViewManager.js | 2 +- app/Game/Client/View/Views/AbstractView.js | 4 +- app/Game/Core/Physics/Engine.js | 2 +- app/Lib/Utilities/NotificationCenter.js | 133 ++++++++++++++---- app/Server/Coordinator.js | 2 +- app/Server/PipeToChannel.js | 2 +- app/Server/User.js | 4 +- 16 files changed, 142 insertions(+), 58 deletions(-) diff --git a/app/Game/Channel/Channel.js b/app/Game/Channel/Channel.js index d9628a9..bbf6f45 100755 --- a/app/Game/Channel/Channel.js +++ b/app/Game/Channel/Channel.js @@ -31,7 +31,7 @@ Nc.on('broadcastControlCommand', this.broadcastControlCommand, this); Nc.on('broadcastControlCommandExcept', this.broadcastControlCommandExcept, this); - Nc.on('broadcastGameCommand', this.broadcastGameCommand, this); + Nc.on(Nc.ns.channel.to.client.gameCommand.broadcast, this.broadcastGameCommand, this); Nc.on('broadcastGameCommandExcept', this.broadcastGameCommandExcept, this); console.checkpoint('channel ' + this.name + ' created'); diff --git a/app/Game/Channel/GameController.js b/app/Game/Channel/GameController.js index 9131742..2c3b785 100755 --- a/app/Game/Channel/GameController.js +++ b/app/Game/Channel/GameController.js @@ -22,8 +22,8 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N Nc.on('user/joined', this.onUserJoined, this); Nc.on('user/left', this.onUserLeft, this); - Nc.on('user/resetLevel', this.onResetLevel, this); - Nc.on('user/clientReady', this.onClientReady, this); + Nc.on(Nc.ns.channel.events.user.level.reset, this.onResetLevel, this); + Nc.on(Nc.ns.channel.events.user.client.ready, this.onClientReady, this); Nc.on('player/killed', this.onPlayerKilled, this); console.checkpoint('starting game controller for channel ' + channel.name); @@ -84,7 +84,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N y: spawnPoint.y }; - Nc.trigger("broadcastGameCommand", "spawnPlayer", options); + Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "spawnPlayer", options); }, respawnTime * 1000); }; @@ -93,7 +93,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N var update = this.getWorldUpdateObject(false); if(Object.getOwnPropertyNames(update).length > 0) { - Nc.trigger("broadcastGameCommand", 'worldUpdate', update); + Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'worldUpdate', update); } setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); @@ -184,12 +184,12 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N userId: userId } - Nc.trigger('user/' + userId + "/gameCommand", "clientReadyResponse", options); + Nc.trigger(Nc.ns.channel.to.client.user.gameCommand.send + userId, "clientReadyResponse", options); }; GameController.prototype.onResetLevel = function(userId) { Parent.prototype.onResetLevel.call(this); - Nc.trigger("broadcastGameCommand", "resetLevel", true); + Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "resetLevel", true); for (var key in this.players) { this.spawnPlayer(this.players[key]); } diff --git a/app/Game/Channel/GameObjects/Doll.js b/app/Game/Channel/GameObjects/Doll.js index 8349094..b73d06b 100755 --- a/app/Game/Channel/GameObjects/Doll.js +++ b/app/Game/Channel/GameObjects/Doll.js @@ -63,7 +63,7 @@ function (Parent, Item, Box2D, Nc) { self.player.addDamage(damage.Length() * 2, player); } - Nc.trigger("engine/addToWorldQueue", callback) + Nc.trigger(Nc.ns.channel.engine.worldQueue.add, callback) } } diff --git a/app/Game/Channel/GameObjects/Items/RagDoll.js b/app/Game/Channel/GameObjects/Items/RagDoll.js index 848abcb..0f19f47 100644 --- a/app/Game/Channel/GameObjects/Items/RagDoll.js +++ b/app/Game/Channel/GameObjects/Items/RagDoll.js @@ -35,7 +35,7 @@ function (Parent, Settings, Nc) { }; RagDoll.prototype.destroy = function() { - Nc.trigger("broadcastGameCommand", 'removeGameObject', { + Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'removeGameObject', { type: 'animated', uid: this.uid }); diff --git a/app/Game/Channel/PipeToServer.js b/app/Game/Channel/PipeToServer.js index 113ceca..b8827f9 100755 --- a/app/Game/Channel/PipeToServer.js +++ b/app/Game/Channel/PipeToServer.js @@ -12,7 +12,7 @@ function (Nc, Channel) { this.channel = null; this.process = process; - Nc.on('process/message', this.send, this); + Nc.on(Nc.ns.channel.to.server.controlCommand.send, this.send, this); process.on('message', function (message, handle) { diff --git a/app/Game/Channel/Player.js b/app/Game/Channel/Player.js index 188160c..f05de54 100755 --- a/app/Game/Channel/Player.js +++ b/app/Game/Channel/Player.js @@ -43,7 +43,7 @@ function (Parent, Nc) { options.action = "throw"; options.x = x; options.y = y; - Nc.trigger("broadcastGameCommand", "handActionResponse", options); + Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "handActionResponse", options); } } else { // grab @@ -51,7 +51,7 @@ function (Parent, Nc) { this.grab(item); options.action = "grab"; - Nc.trigger("broadcastGameCommand", "handActionResponse", options); + Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "handActionResponse", options); } } }; @@ -85,7 +85,7 @@ function (Parent, Nc) { Parent.prototype.kill.call(this, killedByPlayer, ragDollId); this.broadcastStats(); - Nc.trigger("broadcastGameCommand", "playerKill", { + Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "playerKill", { playerId: this.id, killedByPlayerId: killedByPlayer.id, ragDollId: ragDollId @@ -102,7 +102,7 @@ function (Parent, Nc) { }; Player.prototype.broadcastStats = function() { - Nc.trigger("broadcastGameCommand", "updateStats", { + Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "updateStats", { playerId: this.id, stats: this.stats }); diff --git a/app/Game/Channel/User.js b/app/Game/Channel/User.js index 486b8de..374f896 100755 --- a/app/Game/Channel/User.js +++ b/app/Game/Channel/User.js @@ -14,7 +14,7 @@ function(Parent, Nc, ProtocolHelper, ProtocolParser) { this.isReady = false; var self = this; - Nc.on('user/' + this.id + "/joinSuccess", function(options) { + Nc.on(Nc.ns.channel.to.client.user.controlCommand.joinSuccess + this.id, function(options) { self.sendControlCommand("joinSuccess", options); }); @@ -22,9 +22,12 @@ function(Parent, Nc, ProtocolHelper, ProtocolParser) { ProtocolHelper.applyCommand(message.data, self); }); + /* + couldnt find trigger for this while refactoring Nc.on('user/' + this.id + "/gameCommand", function(command, options) { self.sendGameCommand(command, options); }); + */ } User.prototype = Object.create(Parent.prototype); @@ -43,10 +46,10 @@ function(Parent, Nc, ProtocolHelper, ProtocolParser) { } // FIXME: move this to Protocol helper as a function if(command.hasOwnProperty("resetLevel")) { - Nc.trigger("user/resetLevel", this.id); + Nc.trigger(Nc.ns.channel.events.user.level.reset, this.id); } else if(command.hasOwnProperty("clientReady")) { this.isReady = true; - Nc.trigger("user/clientReady", this.id); + Nc.trigger(Nc.ns.channel.events.user.client.ready, this.id); } else { this.player.playerController.applyCommand(command); } @@ -60,7 +63,7 @@ function(Parent, Nc, ProtocolHelper, ProtocolParser) { var recipient = "user/" + this.id; var data = ProtocolHelper.encodeCommand(command, options); - Nc.trigger("process/message", recipient, data); + Nc.trigger(Nc.ns.channel.to.server.controlCommand.send, recipient, data); }; User.prototype.sendGameCommand = function(command, options) { diff --git a/app/Game/Client/Player.js b/app/Game/Client/Player.js index b21171c..8155f82 100755 --- a/app/Game/Client/Player.js +++ b/app/Game/Client/Player.js @@ -41,7 +41,7 @@ function (Parent, Nc, Settings) { var callback = function(playerInfoView) { self.playerInfoView = playerInfoView; } - Nc.trigger("view/createAndAddPlayerInfo", callback, options); + Nc.trigger(Nc.ns.client.view.playerInfo.createAndAdd, callback, options); }; Player.prototype.onHealthChange = function() { @@ -69,15 +69,15 @@ function (Parent, Nc, Settings) { healthFactor: this.stats.health / 100, visible: this.playerInfoViewVisible }; - Nc.trigger("view/updatePlayerInfo", this.playerInfoView, options); + Nc.trigger(Nc.ns.client.view.playerInfo.update, this.playerInfoView, options); this.playerInfoViewVisibleTimeout = setTimeout(function() { self.playerInfoViewVisible = false; - Nc.trigger("view/updatePlayerInfo", self.playerInfoView, {visible: self.playerInfoViewVisible}); + Nc.trigger(Nc.ns.client.view.playerInfo.update, self.playerInfoView, {visible: self.playerInfoViewVisible}); }, Settings.HEALTH_DISPLAY_TIME * 1000); } else { - Nc.trigger("view/updatePlayerInfo", this.playerInfoView, {visible: this.playerInfoViewVisible}); + Nc.trigger(Nc.ns.client.view.playerInfo.update, this.playerInfoView, {visible: this.playerInfoViewVisible}); } }; @@ -96,7 +96,7 @@ function (Parent, Nc, Settings) { x: position.x * Settings.RATIO, y: position.y * Settings.RATIO, } - Nc.trigger("view/updatePlayerInfo", this.playerInfoView, options); + Nc.trigger(Nc.ns.client.view.playerInfo.update, this.playerInfoView, options); } }; diff --git a/app/Game/Client/View/DomController.js b/app/Game/Client/View/DomController.js index fb0a054..4969f42 100755 --- a/app/Game/Client/View/DomController.js +++ b/app/Game/Client/View/DomController.js @@ -13,7 +13,7 @@ function (Settings, Nc, Stats, Screenfull) { this.stats = null; this.ping = null; - Nc.on("view/ready", this.initDevTools, this); + Nc.on(Nc.ns.client.view.events.ready, this.initDevTools, this); } DomController.prototype.initDevTools = function() { diff --git a/app/Game/Client/View/ViewManager.js b/app/Game/Client/View/ViewManager.js index a71f1fd..3e3da85 100755 --- a/app/Game/Client/View/ViewManager.js +++ b/app/Game/Client/View/ViewManager.js @@ -36,7 +36,7 @@ function (Settings, Exception, AbstractView, PixiView, Nc) { throw new Exception("In the view", Settings.VIEW_CONTROLLER + 'View', "this.setCanvas(canvas) has not been called with a valid HTMLCanvasElement!"); } - Nc.trigger("view/ready", view); + Nc.trigger(Nc.ns.client.view.events.ready, view); return view; } diff --git a/app/Game/Client/View/Views/AbstractView.js b/app/Game/Client/View/Views/AbstractView.js index 32cc69d..70ad741 100755 --- a/app/Game/Client/View/Views/AbstractView.js +++ b/app/Game/Client/View/Views/AbstractView.js @@ -23,8 +23,8 @@ function (DomController, Settings, Exception, Nc) { Nc.on("view/toggleInfo", this.onToggleInfo, this); - Nc.on("view/createAndAddPlayerInfo", this.onCreateAndAddPlayerInfo, this); - Nc.on("view/updatePlayerInfo", this.onUpdatePlayerInfo, this); + Nc.on(Nc.ns.client.view.playerInfo.createAndAdd, this.onCreateAndAddPlayerInfo, this); + Nc.on(Nc.ns.client.view.playerInfo.update, this.onUpdatePlayerInfo, this); Nc.on("view/removePlayerInfo", this.onRemovePlayerInfo, this); Nc.on("view/updateLoader", this.onUpdateLoader, this); diff --git a/app/Game/Core/Physics/Engine.js b/app/Game/Core/Physics/Engine.js index 67ef4a0..e6945e4 100755 --- a/app/Game/Core/Physics/Engine.js +++ b/app/Game/Core/Physics/Engine.js @@ -17,7 +17,7 @@ function (Settings, Box2D, CollisionDetector, Nc) { this.lastStep = Date.now(); this.worldQueue = []; - Nc.on("engine/addToWorldQueue", this.addToWorldQueue, this); + Nc.on(Nc.ns.channel.engine.worldQueue.add, this.addToWorldQueue, this); } Engine.prototype.getWorld = function () { diff --git a/app/Lib/Utilities/NotificationCenter.js b/app/Lib/Utilities/NotificationCenter.js index 76d3d3f..307eb66 100755 --- a/app/Lib/Utilities/NotificationCenter.js +++ b/app/Lib/Utilities/NotificationCenter.js @@ -1,80 +1,159 @@ define([ + "Lib/Utilities/Exception" ], -function () { +function (Exception) { + + function populate(obj, path) { + path = path || "Nc.ns"; + for(var key in obj) { + if(!obj.hasOwnProperty(key)) continue; + if(obj[key] === null) { + obj[key] = path + "." + key; + } else { + obj[key] = populate(obj[key], path + "." + key); + } + } + return obj; + } function NotificationCenter () { this.topics = {}; this.subUid = -1; -/* + var i = 0; - this.nc = { + this.ns = { client: { view: { mesh: { - create: i++, - add: i++, - remove: i++, - update: i++ + create: null, + add: null, + remove: null, + update: null }, playerInfo: { - createAndAdd: i++, - remove: i++, - update: i++ + createAndAdd: null, + remove: null, + update: null }, preloadBar: { - update: i++ + update: null }, fullScreen: { - change: i++ + change: null }, debugMode: { - toggle: i++ + toggle: null }, gameInfo: { - toggle: i++ - } + toggle: null + }, events: { - ready: i++ + ready: null } }, input: { handAction: { - request: i++ + request: null }, xy: { - change: i++ + change: null } }, server: { gameCommand: { - send: i++ + send: null } } }, core: { game: { gameObject: { - add: i++, - remove: i++ - } + add: null, + remove: null + }, events: { level: { - loaded: i++ + loaded: null } } } }, channel: { - pipeToServer: function(v) { return v + "-ns.channel.pipeToServer")} + events: { + controlCommand: null, + user: { + joined: null, + left: null, + client: { + ready: null + }, + level: { + reset: null + } + } + }, + engine: { + worldQueue: { + add: null + } + }, + to: { + server: { + send: null + }, + client: { + user: { + gameCommand: { + send: null + }, + controlCommand: { + joinSuccess: null + } + }, + gameCommand: { + broadcast: null + } + } + } + }, + server: { + events: { + controlCommand: null, + user: { + joined: null, + left: null + } + }, + to: { + client: { + message: { + send: null + } + } + } } - }; - */ + + populate(this.ns); + } + + NotificationCenter.prototype.validate = function(topic) { + if (typeof topic === 'object') { + throw new Exception("Topic bad format " + JSON.stringify(topic)); + } + + if (topic.indexOf("Nc.ns") !== 0) { + throw new Exception("Topic bad format, does not begin with Nc.ns. : " + topic); + } + }; + NotificationCenter.prototype.trigger = function (topic /*, arguments*/) { + this.validate(topic); + if (!this.topics[topic]) { console.warn("No such topic " + topic + ". Could not trigger. arguments: " + arguments.join); } @@ -91,6 +170,8 @@ function () { NotificationCenter.prototype.on = function (topic, func, context) { + this.validate(topic); + if (!this.topics[topic]) { this.topics[topic] = []; } diff --git a/app/Server/Coordinator.js b/app/Server/Coordinator.js index 317534d..e6cfa95 100644 --- a/app/Server/Coordinator.js +++ b/app/Server/Coordinator.js @@ -11,7 +11,7 @@ function (User, Channel, PipeToChannel, Nc, Settings) { function Coordinator() { this.channelPipes = {}; - Nc.on('coordinator/message', this.onMessage, this); + Nc.on(Nc.ns.server.events.controlCommand + "coordinator", this.onMessage, this); console.checkpoint('create Coordinator'); } diff --git a/app/Server/PipeToChannel.js b/app/Server/PipeToChannel.js index e23285e..22792e1 100755 --- a/app/Server/PipeToChannel.js +++ b/app/Server/PipeToChannel.js @@ -47,7 +47,7 @@ function (Nc, childProcess) { } PipeToChannel.prototype.onMessage = function (message) { - Nc.trigger(message.recipient + '/message', message.data); + Nc.trigger(Nc.ns.server.events.controlCommand + message.recipient, message.data); } return PipeToChannel; diff --git a/app/Server/User.js b/app/Server/User.js index 40b96a4..d920e97 100644 --- a/app/Server/User.js +++ b/app/Server/User.js @@ -16,7 +16,7 @@ function (Parent, ProtocolHelper, Nc) { socketLink.on('message', this.onMessage.bind(this)); socketLink.on('disconnect', this.onDisconnect.bind(this)); - Nc.on("user/" + this.socketLink.id + "/message", this.socketLink.send, this.socketLink); + Nc.on(Nc.ns.server.to.client.message.send + id, this.socketLink.send, this.socketLink); } User.prototype = Object.create(Parent.prototype); @@ -73,7 +73,7 @@ function (Parent, ProtocolHelper, Nc) { User.prototype.onPing = function(timestamp) { var message = ProtocolHelper.encodeCommand("pong", timestamp); - Nc.trigger("user/" + this.socketLink.id + "/message", message); + Nc.trigger(Nc.ns.server.to.client.message.send + id, message); }; return User;