From 815c63009f0ac32747b3eafc242ffd3d15c06747 Mon Sep 17 00:00:00 2001 From: logsol Date: Sun, 22 Jul 2012 17:54:27 +0200 Subject: [PATCH] working towards usable new structure --- app/Bootstrap/Channel.js | 33 ++++++++++ app/Game/Client/GameController.js | 15 +++-- app/Game/Client/Networker.js | 13 ++-- app/Game/Client/Physics/Engine.js | 2 +- app/Game/Core/GameController.js | 7 +- .../{Server => Core}/NotificationCenter.js | 0 app/Game/Core/User.js | 8 +++ app/Game/Server/Channel.js | 18 +++-- app/Game/Server/CoordinatorLink.js | 20 ++++++ app/Game/Server/GameController.js | 14 ++-- app/Game/Server/User.js | 54 +++------------ app/Lobby/Coordinator.js | 36 ++++++---- app/Lobby/User.js | 66 +++++++++++++++++++ channel.js | 18 +++++ package.json | 3 +- 15 files changed, 223 insertions(+), 84 deletions(-) create mode 100755 app/Bootstrap/Channel.js mode change 100644 => 100755 app/Game/Client/Physics/Engine.js rename app/Game/{Server => Core}/NotificationCenter.js (100%) create mode 100755 app/Game/Core/User.js create mode 100755 app/Game/Server/CoordinatorLink.js create mode 100755 app/Lobby/User.js create mode 100755 channel.js diff --git a/app/Bootstrap/Channel.js b/app/Bootstrap/Channel.js new file mode 100755 index 0000000..0e5ce99 --- /dev/null +++ b/app/Bootstrap/Channel.js @@ -0,0 +1,33 @@ +define([ + "Game/Server/Channel" +], + +function(Channel) { + + function ChannelBootstrap(process) { + + var coordinatorLink = new CoordinatorLink(process); + var channel = null; + + process.on('message', function(message) { + + switch(message){ + case 'CREATE': + channel = new Channel(coordinatorLink); + break; + + case 'KILL': + channel.destroy(); + process.exit(0); + break; + + default: + coordinatorLink.receive(message); + break; + } + }); + } + + return ChannelBootstrap; + +}); \ No newline at end of file diff --git a/app/Game/Client/GameController.js b/app/Game/Client/GameController.js index d2f35f6..ca48ea1 100755 --- a/app/Game/Client/GameController.js +++ b/app/Game/Client/GameController.js @@ -3,24 +3,28 @@ define([ "Game/Client/Physics/Engine", "Game/Client/View/ViewController", "Game/Client/Control/KeyboardController", + "Game/Core/NotificationCenter", "Lib/Utilities/RequestAnimFrame" ], -function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimFrame) { +function(Parent, PhysicsEngine, ViewController, KeyboardController, NotificationCenter, requestAnimFrame) { function GameController () { + this.viewController = new ViewController(); + Parent.call(this, new PhysicsEngine()); this.me = null; this.keyboardController = null; - this.viewController = new ViewController(); - this.update(); + //NotificationCenter.on('me/joined', this.meJoined, this) + + //this.update(); } GameController.prototype = Object.create(Parent.prototype); - +/* GameController.prototype.getMe = function() { return this.me; } @@ -40,6 +44,7 @@ function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimF GameController.prototype.meJoined = function(user) { this.me = this.userJoined(user); + this.keyboardController = new KeyboardController(this.me, this); } @@ -66,6 +71,6 @@ function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimF } } - +*/ return GameController; }); diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index 721f76d..e0925e6 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -19,7 +19,7 @@ function(ProtocolHelper, GameController) { this.socketLink.on('connect', function() { self.onConnect(); }); - +/* this.socketLink.on('message', function(message) { self.onMessage(message); }); @@ -27,12 +27,13 @@ function(ProtocolHelper, GameController) { this.socketLink.on('disconnect', function() { self.onDisconnect(); }); +*/ } Networker.prototype.onConnect = function() { this.join('dungeon'); } - +/* Networker.prototype.onMessage = function(message) { var self = this; ProtocolHelper.runCommands(message, function(command, options) { @@ -44,7 +45,7 @@ function(ProtocolHelper, GameController) { if(this.gameController) this.gameController.destruct(); this.gameController = null; } - +*/ Networker.prototype.join = function(channelName){ this.sendCommand('join', channelName); } @@ -53,9 +54,9 @@ function(ProtocolHelper, GameController) { var message = ProtocolHelper.encodeCommand(command, options); this.socketLink.send(message); } - +/* Networker.prototype.onJoinSuccess = function(options) { - this.gameController = new GameController(this, options.id); + this.gameController = new GameController(options.id); this.gameController.loadLevel("default.json") console.log("Joined " + options.channelName); @@ -103,7 +104,7 @@ function(ProtocolHelper, GameController) { break; } } - +*/ return Networker; }); \ No newline at end of file diff --git a/app/Game/Client/Physics/Engine.js b/app/Game/Client/Physics/Engine.js old mode 100644 new mode 100755 index 7767e54..925ab33 --- a/app/Game/Client/Physics/Engine.js +++ b/app/Game/Client/Physics/Engine.js @@ -17,7 +17,7 @@ function(Parent, Settings, DomController, Box2D) { Engine.prototype.setupDebugDraw = function() { //var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE; - var debugSprite = Dom.getDebugCanvas().getContext("2d"); + var debugSprite = DomController.getDebugCanvas().getContext("2d"); // set debug draw var debugDraw = new Box2D.Dynamics.b2DebugDraw(); diff --git a/app/Game/Core/GameController.js b/app/Game/Core/GameController.js index 42c27c9..8af6a6a 100755 --- a/app/Game/Core/GameController.js +++ b/app/Game/Core/GameController.js @@ -7,7 +7,6 @@ define([ function(Engine, Level, Player) { function GameController(physicsEngine) { - console.log('constructor called'); this.players = {}; if (! physicsEngine instanceof Engine) { @@ -30,12 +29,16 @@ function(Engine, Level, Player) { this.level.loadLevelInToEngine(); } + /* + GameController.prototype.destroy = function() { for(var player in this.players) { this.players[player].destroy(); } } + + GameController.prototype.userJoined = function(user) { var player = new Player(user.id, this.physicsEngine); this.players[user.id] = player; @@ -48,5 +51,7 @@ function(Engine, Level, Player) { delete this.players[user.id]; } + */ + return GameController; }); diff --git a/app/Game/Server/NotificationCenter.js b/app/Game/Core/NotificationCenter.js similarity index 100% rename from app/Game/Server/NotificationCenter.js rename to app/Game/Core/NotificationCenter.js diff --git a/app/Game/Core/User.js b/app/Game/Core/User.js new file mode 100755 index 0000000..f661e18 --- /dev/null +++ b/app/Game/Core/User.js @@ -0,0 +1,8 @@ +define(function() { + + function User(id) { + this.id = id; + } + + return User; +}); \ No newline at end of file diff --git a/app/Game/Server/Channel.js b/app/Game/Server/Channel.js index 0bbec07..7ff2559 100755 --- a/app/Game/Server/Channel.js +++ b/app/Game/Server/Channel.js @@ -1,26 +1,32 @@ define([ "Game/Server/GameController", - "Game/Server/NotificationCenter" + "Game/Core/NotificationCenter" ], function(GameController, NotificationCenter) { - function Channel(name) { - this.name = name; + function Channel(coordinatorLink) { + + this.coordinatorLink = coordinatorLink; + + console.log('A CHANNEL WAS CREATED'); + /* this.users = {}; this.gameController = new GameController(); this.gameController.loadLevel("default.json"); - +*/ +/* var self = this; NotificationCenter.on("processGameCommandFromUser", function(topic, args) { self.processGameCommandFromUser.apply(self, args); }); +*/ } Channel.validateName = function(name){ return true; } - +/* Channel.prototype.addUser = function(user){ var userIds = Object.keys(this.users); @@ -56,7 +62,7 @@ function(GameController, NotificationCenter) { Channel.prototype.processGameCommandFromUser = function(command, options, user) { this.gameController.progressGameCommandFromUser(command, options, user); } - +*/ return Channel; }); \ No newline at end of file diff --git a/app/Game/Server/CoordinatorLink.js b/app/Game/Server/CoordinatorLink.js new file mode 100755 index 0000000..f0691e4 --- /dev/null +++ b/app/Game/Server/CoordinatorLink.js @@ -0,0 +1,20 @@ +define([ +], + +function() { + + function CoordinatorLink(process) { + this.process = process; + } + + CoordinatorLink.prototype.send = function(message) { + this.process.send(message); + }; + + CoordinatorLink.prototype.receive = function(message) { + throw 'This method is abstract and must be overwritten by Channel'; + }; + + return CoordinatorLink; + +}); \ No newline at end of file diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index 52628a2..5c35dd9 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -4,7 +4,7 @@ define([ "Game/Config/Settings", "Game/Core/Control/InputController", "Lib/Utilities/RequestAnimFrame", - "Game/Server/NotificationCenter" + "Game/Core/NotificationCenter" ], function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) { @@ -14,15 +14,15 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not this.inputControllers = {}; - this.update(); - this.updateWorld(); + //this.update(); + //this.updateWorld(); - NotificationCenter.on('user/joined', this.userJoined, this); - NotificationCenter.on('user/left', this.userLeft, this); + //NotificationCenter.on('user/joined', this.userJoined, this); + //NotificationCenter.on('user/left', this.userLeft, this); } GameController.prototype = Object.create(Parent.prototype); - +/* GameController.prototype.update = function() { requestAnimFrame(this.update.bind(this)); @@ -79,6 +79,6 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); } - +*/ return GameController; }); diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js index f77dbe3..b920088 100755 --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -1,12 +1,13 @@ define([ + "Game/Core/User", "Game/Core/Protocol/Helper", - "Game/Server/NotificationCenter" + "Game/Core/NotificationCenter" ], -function(ProtocolHelper, NotificationCenter) { - - function User(socketLink, coordinator) { +function(Parent, ProtocolHelper, NotificationCenter) { + function User(id, coordinator) { + Parent.call(this, id); this.id = socketLink.id; this.socketLink = socketLink; this.coordinator = coordinator; @@ -15,19 +16,14 @@ function(ProtocolHelper, NotificationCenter) { this.init(socketLink); } + User.prototype = Object.create(Parent.prototype); + User.prototype.init = function(socketLink){ var self = this; - socketLink.on('message', function(message){ - self.onMessage(message); - }); - - socketLink.on('disconnect', function(){ - self.onDisconnect(); - }); } - +/* User.prototype.setChannel = function(channel) { this.channel = channel; } @@ -38,44 +34,12 @@ function(ProtocolHelper, NotificationCenter) { this.socketLink.send(message); } - User.prototype.onMessage = function(message){ - var self = this; - ProtocolHelper.runCommands(message, function(command, options){ - self.processControlCommand(command, options); - }); - } - User.prototype.onDisconnect = function(){ - this.coordinator.removeUser(this); - } - - User.prototype.processControlCommand = function(command, options){ - switch(command) { - - case 'join': - this.coordinator.assignUserToChannel(this, options); - break; - - case 'leave': - this.coordinator.assignUserToLobby(this); - break; - - case 'gameCommand': - for(var gameCommand in options) { - //NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]); - //this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this); - } - break; - - default: - break; - } - } User.prototype.toString = function() { return "[User " + this.id + "]"; }; - +*/ return User; }); \ No newline at end of file diff --git a/app/Lobby/Coordinator.js b/app/Lobby/Coordinator.js index ab0ff8b..65d5339 100755 --- a/app/Lobby/Coordinator.js +++ b/app/Lobby/Coordinator.js @@ -1,9 +1,12 @@ define([ - "Game/Server/User", - "Game/Server/Channel" + "Lobby/User", + "Game/Server/Channel", + "node-fork" ], -function(User, Channel) { +function(User, Channel, nodeFork) { + + var fork = nodeFork.fork; function Coordinator() { this.channels = {}; @@ -16,16 +19,16 @@ function(User, Channel) { } Coordinator.prototype.assignUserToLobby = function(user){ - if(user.channel) { - user.channel.releaseUser(user); + if(user.channelProcess) { + //user.channel.releaseUser(user); -> generate message } this.lobbyUsers[user.id] = user; } Coordinator.prototype.assignUserToChannel = function(user, channelName){ - if(user.channel) { - user.channel.releaseUser(user); + if(user.channelProcess) { + //user.channel.releaseUser(user); -> generate message } if(!Channel.validateName(channelName)){ @@ -35,20 +38,29 @@ function(User, Channel) { var channel = this.channels[channelName]; if(!channel) { - channel = new Channel(channelName); + + try { + console.log('try'); + channel = fork('app/Bootstrap/Channel.js'); + channel.send('CREATE'); + channel.send('{setName:"' + channelName + '"}'); + } catch (err) { + throw 'Failed to fork channel ' + channelName + '! (' + err + ')'; + } + this.channels[channelName] = channel; } - channel.addUser(user); - user.setChannel(channel); + //channel.addUser(user); + //user.setChannel(channel); delete this.lobbyUsers[user.id]; } Coordinator.prototype.removeUser = function(user){ delete this.lobbyUsers[user.id]; - if(user.channel) { - user.channel.releaseUser(user); + if(user.channelProcess) { + //user.channel.releaseUser(user); -> generate message } } diff --git a/app/Lobby/User.js b/app/Lobby/User.js new file mode 100755 index 0000000..325842e --- /dev/null +++ b/app/Lobby/User.js @@ -0,0 +1,66 @@ +define([ + "Game/Core/User", + "Game/Core/Protocol/Helper" +], + +function(Parent, ProtocolHelper) { + + function User(socketLink, coordinator) { + Parent.call(this, socketLink.id); + + this.coordinator = coordinator; + this.channelProcess = null; + + var self = this; + + socketLink.on('message', function(message){ + self.onMessage(message); + }); + socketLink.on('disconnect', function(){ + self.onDisconnect(); + }); + } + + User.prototype = Object.create(Parent.prototype); + + User.prototype.setChannelProcess = function(channelProcess) { + this.channelProcess = channelProcess; + } + + User.prototype.onMessage = function(message){ + var self = this; + ProtocolHelper.runCommands(message, function(command, options){ + self.processControlCommand(command, options); + }); + } + + User.prototype.onDisconnect = function(){ + this.coordinator.removeUser(this); + } + + User.prototype.processControlCommand = function(command, options){ + switch(command) { + + case 'join': + this.coordinator.assignUserToChannel(this, options); + break; + + case 'leave': + this.coordinator.assignUserToLobby(this); + break; + + case 'gameCommand': + for(var gameCommand in options) { + //NotificationCenter.trigger("processGameCommandFromUser", [gameCommand, options[gameCommand], this]); + //this.channel.processGameCommandFromUser(gameCommand, options[gameCommand], this); + } + break; + + default: + break; + } + } + + return User; + +}); \ No newline at end of file diff --git a/channel.js b/channel.js new file mode 100755 index 0000000..183fe7a --- /dev/null +++ b/channel.js @@ -0,0 +1,18 @@ +console.log(requirejs); + + +var ree = require('requirejs'); + +console.log(ree); + +requirejs.config({ + baseUrl: 'app' +}); + +var inspector = {}; + +requirejs(["Bootstrap/Channel"], function(ChannelBootstrap) { + + var channelBootstrap = new ChannelBootstrap(process); + inspector.channelBootstrap = channelBootstrap; +}); \ No newline at end of file diff --git a/package.json b/package.json index 15afafd..0503987 100755 --- a/package.json +++ b/package.json @@ -17,7 +17,8 @@ "dependencies": { "socket.io": ">= 0.9.6", "node-static": ">= 0.6.0", - "requirejs": ">= 2.0.2" + "requirejs": ">= 2.0.2", + "node-fork": ">= 0.4.2" }, "devDependencies": {}, "optionalDependencies": {},