diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index c7868e9..484aa05 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -21,8 +21,7 @@ function (ProtocolHelper, GameController) { }); this.socketLink.on('message', function (message) { - //self.onMessage(message); - console.log('Message from server: ', message); + self.onMessage(message); }); this.socketLink.on('disconnect', function () { @@ -49,11 +48,13 @@ function (ProtocolHelper, GameController) { Networker.prototype.onJoinSuccess = function (options) { - this.gameController = new GameController(); - this.gameController.loadLevel("default.json"); - /* - console.log("Joined " + options.channelName); + //this.gameController = new GameController(); + //this.gameController.loadLevel("default.json"); + + + console.log("Joined " + options.channelName); + /* if (options.userIds && options.userIds.length > 0) { for(var i = 0; i < options.userIds.length; i++) { this.gameController.userJoined(options.userIds[i]) @@ -67,7 +68,7 @@ function (ProtocolHelper, GameController) { this.socketLink.send(message); } -/* + Networker.prototype.onMessage = function (message) { var self = this; @@ -78,19 +79,21 @@ function (ProtocolHelper, GameController) { } Networker.prototype.onUserJoined = function (userId) { - this.gameController.userJoined(userId); + //this.gameController.userJoined(userId); console.log("User " + userId + " joined"); } - +/* Networker.prototype.sendGameCommand = function (command, options) { this.sendCommand('gameCommand', ProtocolHelper.assemble(command, options)); } - +*/ Networker.prototype.onUserLeft = function (userId) { - this.gameController.userLeft(userId); + //this.gameController.userLeft(userId); + console.log("User " + userId + " left"); } Networker.prototype.processControlCommand = function (command, options) { + switch(command) { case 'joinSuccess': this.onJoinSuccess(options); @@ -98,7 +101,7 @@ function (ProtocolHelper, GameController) { case 'gameCommand': for(var gameCommand in options) { - this.gameController.processGameCommand(gameCommand, options[gameCommand]); + //this.gameController.processGameCommand(gameCommand, options[gameCommand]); } break; @@ -114,7 +117,7 @@ function (ProtocolHelper, GameController) { break; } } -*/ + return Networker; }); \ No newline at end of file diff --git a/app/Game/Core/Protocol/Helper.js b/app/Game/Core/Protocol/Helper.js index 3fd164b..90cbb7a 100755 --- a/app/Game/Core/Protocol/Helper.js +++ b/app/Game/Core/Protocol/Helper.js @@ -17,7 +17,12 @@ function (Parser) { } Helper.runCommands = function (message, callback) { - var commands = Parser.decode(message); + var commands; + if (typeof message == "string") { + commands = Parser.decode(message); + } else { + commands = message; + } for(var command in commands) { callback(command, commands[command]); diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index 694f8ef..2233d99 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -1,10 +1,11 @@ define([ "Game/Server/GameController", "Game/Core/NotificationCenter", - "Game/Server/User" + "Game/Server/User", + "Game/Core/Protocol/Helper" ], - function (GameController, NotificationCenter, User) { + function (GameController, NotificationCenter, User, ProtocolHelper) { function Channel (pipeToLobby, name) { @@ -33,23 +34,35 @@ // Messages look like: // {channel: {setName: 'foo'}} // {user: {jupm: null}, id: 12} - NotificationCenter.on('channel/message', function (message){ + NotificationCenter.on('channel/message', function (message) { switch(message.recipient) { case 'user': - self.forward(self.users[message.id], message.data); + console.log(message); + var user = self.users[message.id]; + ProtocolHelper.runCommands(message.data, function (command, options) { + user[command].call(user, options); + }); break; + case 'id': // Do nothing, it is needed by the user break; + case 'channel': - self.forward(self, message.data); + ProtocolHelper.runCommands(message.data, function (command, options) { + self[command].call(self, options); + }); break; + default: throw 'unknown recipient'; break; } }); + NotificationCenter.on('channel/users/all', this.sendControlCommandToAllUsers, this); + NotificationCenter.on('channel/users/all/except', this.sendControlCommandToAllUsersExcept, this); + console.checkpoint('channel ' + name + ' created'); } @@ -57,49 +70,36 @@ return true; } - Channel.prototype.forward = function (target, message) { - for(var command in message) { - if(typeof target[command] == 'function') { - target[command].call(target, message[command]); - } else { - throw 'trying to call undefined function ' + target[command]; - } - } - }; - - Channel.prototype.addUser = function (userId) { var user = new User(userId, this); this.users[user.id] = user; + NotificationCenter.trigger('user/' + user.id + "/joinSuccess", {userId: user.id, channelName: this.name}); NotificationCenter.trigger('user/joined', user); } -/* - Channel.prototype.send = function(recipient, message) { - this.pipeToLobby.send(recipient, message); - }*/ -/* - Channel.prototype.releaseUser = function (user) { - this.gameController.userIdLeft(user.id); - this.sendCommandToAllUsersExcept("userLeft", user.id, user); + Channel.prototype.releaseUser = function (userId) { + var user = this.users[userId]; + //this.gameController.userIdLeft(user.id); + + this.sendControlCommandToAllUsersExcept("userLeft", user.id, user); delete this.users[user.id]; } - Channel.prototype.sendCommandToAllUsers = function (command, options) { + Channel.prototype.sendControlCommandToAllUsers = function (command, options) { for(var id in this.users) { - this.users[id].sendCommand(command, options); + this.users[id].sendControlCommand(command, options); } } - Channel.prototype.sendCommandToAllUsersExcept = function (command, options, except_user) { + Channel.prototype.sendControlCommandToAllUsersExcept = function (command, options, exceptUser) { for(var id in this.users) { - if (id != except_user.id) { - this.users[id].sendCommand(command, options); + if (id != exceptUser.id) { + this.users[id].sendControlCommand(command, options); } } } - +/* Channel.prototype.processGameCommandFromUser = function (command, options, user) { this.gameController.progressGameCommandFromUser(command, options, user); } diff --git a/app/Game/Server/PipeToLobby.js b/app/Game/Server/PipeToLobby.js index 48eef97..f40f738 100755 --- a/app/Game/Server/PipeToLobby.js +++ b/app/Game/Server/PipeToLobby.js @@ -12,7 +12,7 @@ function (NotificationCenter, Channel) { this.channel = null; this.process = process; - NotificationCenter.on('net/send', this.send, this); + NotificationCenter.on('process/message', this.send, this); process.on('message', function (message, handle) { diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index f2e5ac9..4ff79f9 100644 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -13,17 +13,28 @@ function(Parent, NotificationCenter, ProtocolHelper) { var self = this; NotificationCenter.on('user/joined', function(user) { - self.sendCommand("joined", true); + if(user.id != self.id) { + self.sendControlCommand("userJoined", user.id); + } + }); + + NotificationCenter.on('user/' + this.id + "/joinSuccess", function(options) { + self.sendControlCommand("joinSuccess", options); }); } User.prototype = Object.create(Parent.prototype); - User.prototype.sendCommand = function(command, options) { + User.prototype.sendControlCommand = function(command, options) { var recipient = "user/" + this.id; var data = ProtocolHelper.encodeCommand(command, options); - NotificationCenter.trigger("net/send", recipient, data); + NotificationCenter.trigger("process/message", recipient, data); + }; + + User.prototype.sendGameCommand = function(command, options) { + var data = ProtocolHelper.encodeCommand(command, options); + this.sendControlCommand("gameCommand", data); }; return User; diff --git a/app/Lobby/Coordinator.js b/app/Lobby/Coordinator.js index bbb21be..8e08f2c 100755 --- a/app/Lobby/Coordinator.js +++ b/app/Lobby/Coordinator.js @@ -53,7 +53,7 @@ function (User, Channel, PipeToChannel, NotificationCenter) { Coordinator.prototype.removeUser = function (user) { - //user.channel.send('user/' + user.id + '/left'); + NotificationCenter.trigger('user/left', user); //NotificationCenter.off('channel/' + user.channel.channelName + '/user/' + user.id); delete this.lobbyUsers[user.id]; @@ -82,7 +82,7 @@ function (User, Channel, PipeToChannel, NotificationCenter) { }, this); NotificationCenter.on('user/left', function (user) { - + channelPipe.send('channel', { releaseUser: user.id }); }, this); return channelPipe;