diff --git a/app.js b/app.js new file mode 100755 index 0000000..c49b7d4 --- /dev/null +++ b/app.js @@ -0,0 +1,26 @@ +var requirejs = require('requirejs'); + +var inspector = {}; + +requirejs.config({ + nodeRequire: require, + baseUrl: 'lib' +}); + +var port = process.argv[2] + || process.env.PORT + || process.env.npm_package_config_port; + +var options = { + port: port, + rootDirectory: './', + caching: false, + logLevel: process.argv[3] || 0 +}; + +requirejs(["Bootstrap/Server"], function(Server) { + var server = new Server(options); + inspector.server = server; +}); + +exports = module.exports = inspector; \ No newline at end of file diff --git a/app/Bootstrap/HttpServer.js b/app/Bootstrap/HttpServer.js old mode 100644 new mode 100755 index 20b24ef..f6bb85d --- a/app/Bootstrap/HttpServer.js +++ b/app/Bootstrap/HttpServer.js @@ -1,4 +1,9 @@ -define(['http', 'node-static'], function(http, nodeStatic) { +define([ + 'http', + 'node-static' +], + +function(http, nodeStatic) { function HttpServer(options) { options.port = options.port || 1234; diff --git a/app/Bootstrap/Server.js b/app/Bootstrap/Server.js new file mode 100755 index 0000000..91ee84f --- /dev/null +++ b/app/Bootstrap/Server.js @@ -0,0 +1,16 @@ +define([ + "Server/HttpServer", + "Server/Socket", + "Server/Coordinator" +], + +function(HttpServer, Socket, Coordinator) { + + function Server(options) { + this.coordinator = new Coordinator(); + this.httpServer = new HttpServer(options); + this.socket = new Socket(httpServer.getServer(), options, coordinator); + } + + return Server; +}); \ No newline at end of file diff --git a/app/Bootstrap/Socket.js b/app/Bootstrap/Socket.js old mode 100644 new mode 100755 index 36e117b..e4f7c3e --- a/app/Bootstrap/Socket.js +++ b/app/Bootstrap/Socket.js @@ -1,4 +1,8 @@ -define(['socket.io'], function(io) { +define([ + 'socket.io' +], + +function(io) { function Socket(server, options, coordinator) { options.logLevel = typeof options.logLevel != 'undefined' diff --git a/app/Bootstrap/server.js b/app/Bootstrap/server.js deleted file mode 100644 index df57cf0..0000000 --- a/app/Bootstrap/server.js +++ /dev/null @@ -1,36 +0,0 @@ -var requirejs = require('requirejs'); - -var inspector = {}; - -requirejs.config({ - nodeRequire: require, - baseUrl: 'lib' -}); - -var requirements = [ - "Server/HttpServer", - "Server/Socket", - "Server/Coordinator" -]; - -var port = process.argv[2] - || process.env.PORT - || process.env.npm_package_config_port; - -requirejs(requirements, function(HttpServer, Socket, Coordinator) { - - var options = { - port: port, - rootDirectory: './', - caching: false, - logLevel: process.argv[3] - }; - - var coordinator = new Coordinator(); - var httpServer = new HttpServer(options); - var socket = new Socket(httpServer.getServer(), options, coordinator); - - inspector.coordinator = coordinator; -}); - -exports = module.exports = inspector; diff --git a/app/Game/Server/GameController.js b/app/Game/Server/GameController.js index eea1e3b..3c4f6e6 100755 --- a/app/Game/Server/GameController.js +++ b/app/Game/Server/GameController.js @@ -10,20 +10,20 @@ define([ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, requestAnimFrame) { - function ServerProcessor (serverGame) { - this.serverGame = serverGame; + function GameController (channel) { + this.channel = channel; this.players = {}; this.init(); } - ServerProcessor.prototype.init = function() { + GameController.prototype.init = function() { this.physicsEngine = this.factory.new(PhysicsEngine); this.update(); this.updateWorld(); } - ServerProcessor.prototype.loadLevel = function(path) { + GameController.prototype.loadLevel = function(path) { if (this.level) { this.level.unload(); } @@ -32,11 +32,11 @@ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, request this.level.loadLevelInToEngine(); } - ServerProcessor.prototype.getPhysicsEngine = function() { + GameController.prototype.getPhysicsEngine = function() { return this.physicsEngine; } - ServerProcessor.prototype.update = function() { + GameController.prototype.update = function() { requestAnimFrame(this.update.bind(this)); @@ -46,11 +46,13 @@ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, request } } - ServerProcessor.prototype.destruct = function() { + GameController.prototype.destruct = function() { } - ServerProcessor.prototype.createPlayerWithId = function(id) { + GameController.prototype.createPlayerForUser = function(user) { + var id = user.id; + var player = new Player(this.physicsEngine, id, null); this.players[id] = { player: player, @@ -61,20 +63,24 @@ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, request this.physicsEngine.setCollisionDetector(player); } - ServerProcessor.prototype.progressGameCommandFromId = function(command, options, id) { + GameController.prototype.progressGameCommandFromId = function(command, options, id) { var inputController = this.players[id].inputController; if (typeof inputController[command] == 'function') { inputController[command](options); } } - ServerProcessor.prototype.userIdLeft = function(id) { + GameController.prototype.userIdLeft = function(id) { var player = this.players[id].player; player.destroy(); delete this.players[id]; } - ServerProcessor.prototype.updateWorld = function() { + GameController.prototype.updateClientsWorld = function(update_world) { + this.channel.sendCommandToAllUsers('gameCommand', {worldUpdate: update_world}); + } + + GameController.prototype.updateWorld = function() { var update = {}; var isUpdateNeeded = false; @@ -102,5 +108,5 @@ function(PhysicsEngine, Settings, Player, Box2D, Level, InputController, request setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL); } - return ServerProcessor; + return GameController; }); diff --git a/app/Game/Server/User.js b/app/Game/Server/User.js old mode 100644 new mode 100755 index f30c9d9..7c8818c --- a/app/Game/Server/User.js +++ b/app/Game/Server/User.js @@ -1,4 +1,4 @@ -define(["Protocol/Helper"], function(ProtocolHelper) { +define(["Game/Core/Protocol/Helper"], function(ProtocolHelper) { function User(socketLink, coordinator) { diff --git a/app/Lobby/Coordinator.js b/app/Lobby/Coordinator.js old mode 100644 new mode 100755 index 3a71f80..ab0ff8b --- a/app/Lobby/Coordinator.js +++ b/app/Lobby/Coordinator.js @@ -1,4 +1,9 @@ -define(["Server/User", "Server/Channel", "Server/Factory"], function(User, Channel, Factory) { +define([ + "Game/Server/User", + "Game/Server/Channel" +], + +function(User, Channel) { function Coordinator() { this.channels = {}; @@ -30,8 +35,7 @@ define(["Server/User", "Server/Channel", "Server/Factory"], function(User, Chann var channel = this.channels[channelName]; if(!channel) { - var factory = new Factory(); - channel = factory.new(Channel, channelName); + channel = new Channel(channelName); this.channels[channelName] = channel; }