From da913c4709241881d7e8b5c05adadee097b996c0 Mon Sep 17 00:00:00 2001 From: Jeena Date: Tue, 25 Feb 2014 00:55:16 +0100 Subject: [PATCH] added lobby, nickname fixes #44, channel name --- app/Bootstrap/HttpServer.js | 29 ++++-- app/Game/Client/Networker.js | 8 +- app/Lobby/Api.js | 52 ++++++++++ app/Lobby/Coordinator.js | 10 ++ app/Lobby/PipeToChannel.js | 1 + app/Lobby/User.js | 6 +- client.js | 2 +- server.js | 2 +- static/html/game.html | 61 ++++++++++++ static/html/index.html | 182 +++++++++++++++++++++++++---------- 10 files changed, 285 insertions(+), 68 deletions(-) create mode 100644 app/Lobby/Api.js create mode 100755 static/html/game.html mode change 100755 => 100644 static/html/index.html diff --git a/app/Bootstrap/HttpServer.js b/app/Bootstrap/HttpServer.js index 22583a2..e333b45 100755 --- a/app/Bootstrap/HttpServer.js +++ b/app/Bootstrap/HttpServer.js @@ -1,16 +1,18 @@ define([ 'http', - 'node-static' + 'node-static', + 'Lobby/Api' ], -function (http, nodeStatic) { +function (http, nodeStatic, Api) { - function HttpServer (options) { + function HttpServer (options, coordinator) { options.port = options.port || 1234; options.caching = typeof options.caching != 'undefined' ? options.caching : 3600; options.rootDirectory = options.rootDirectory || './'; this.server = null; + this.api = new Api(coordinator); this.init(options); } @@ -22,24 +24,30 @@ function (http, nodeStatic) { this.server = http.createServer( function (req, res) { - - req.addListener('data', function() { // doesn't work on Jeenas computer without this - //console.log("data") + + var fullBody = ''; + req.addListener('data', function(chunk) { // doesn't work on Jeenas computer without this + fullBody += chunk.toString(); }); req.addListener('error', function(err) { console.log(''); }); + req.addListener('end', function () { switch(true) { case req.url == '/': - fileServer.serveFile('./static/html/index.html', 200, {}, req, res); console.checkpoint('HTTP Server serves index'); break; + case req.url == '/game.html': + fileServer.serveFile('./static/html/game.html', 200, {}, req, res); + console.checkpoint('HTTP Server serves game'); + break; + case req.url == '/client.js': fileServer.serveFile('./client.js', 200, {}, req, res); break; @@ -52,6 +60,13 @@ function (http, nodeStatic) { fileServer.serveFile('./node_modules/screenfull/dist/screenfull.js', 200, {}, req, res); break; + case req.url == '/api': + self.api.handleCall(fullBody); + var status = self.api.isError ? 400 : 200; + res.writeHead(status, {"Content-Type": self.api.getContentType()}); + res.end(self.api.getOutput()); + break; + case new RegExp(/^\/app/).test(req.url): fileServer.serve(req, res, function () { self.handleFileError(res) diff --git a/app/Game/Client/Networker.js b/app/Game/Client/Networker.js index 5ee400d..760d2aa 100755 --- a/app/Game/Client/Networker.js +++ b/app/Game/Client/Networker.js @@ -34,7 +34,12 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do Networker.prototype.onConnect = function () { console.log('connected.') - this.sendCommand('join', 'dungeon'); + var channel = JSON.parse(localStorage["channel"]); + if(channel.name) { + this.sendCommand('join', channel.name); + } else { + window.location.href = "/"; + } } Networker.prototype.onDisconnect = function () { @@ -45,6 +50,7 @@ function (ProtocolHelper, GameController, User, NotificationCenter, Settings, Do } Networker.prototype.onJoinSuccess = function (options) { + console.log("join success") this.userId = options.userId; this.gameController = new GameController(); diff --git a/app/Lobby/Api.js b/app/Lobby/Api.js new file mode 100644 index 0000000..d52594a --- /dev/null +++ b/app/Lobby/Api.js @@ -0,0 +1,52 @@ +define([ + "Lib/Utilities/NotificationCenter", + "Lib/Utilities/Protocol/Helper" +], + +function (NotificationCenter, ProtocolHelper) { + + function Api(coordinator) { + this.coordinator = coordinator; + this.isError = false; + this.output = null; + } + + Api.prototype.handleCall = function(queryParameters) { + + var command; + try { + var message = JSON.parse(queryParameters); + command = message.command; + } catch(e) { + console.error(e) + } + + var output = null; + switch(command) { + case "getChannels": + output = this.coordinator.getChannels(); + break; + default: + this.isError = true; + output = "Command not found"; + break; + } + + this.output = output; + } + + Api.prototype.getOutput = function() { + var output = {}; + var key = this.isError ? "error" : "success"; + output[key] = this.output; + return JSON.stringify(output); + + }; + + Api.prototype.getContentType = function() { + return "application/json"; + }; + + return Api; + +}); \ No newline at end of file diff --git a/app/Lobby/Coordinator.js b/app/Lobby/Coordinator.js index c0399b5..36e7a68 100755 --- a/app/Lobby/Coordinator.js +++ b/app/Lobby/Coordinator.js @@ -92,6 +92,16 @@ function (User, Channel, PipeToChannel, NotificationCenter) { return channelPipe; }; + Coordinator.prototype.getChannels = function(options) { + var list = []; + for (var channelName in this.channelPipes) { + list.push({ + name: channelName + }); + } + return list; + }; + return Coordinator; }); \ No newline at end of file diff --git a/app/Lobby/PipeToChannel.js b/app/Lobby/PipeToChannel.js index 3b98ba8..3e7b5b8 100755 --- a/app/Lobby/PipeToChannel.js +++ b/app/Lobby/PipeToChannel.js @@ -13,6 +13,7 @@ function (NotificationCenter, childProcess) { try { this.channelPipe = fork('channel.js'); + console.log(this.channelPipe) } catch (err) { throw 'Failed to fork channel! (' + err + ')'; } diff --git a/app/Lobby/User.js b/app/Lobby/User.js index 8a15316..e0dc2a8 100755 --- a/app/Lobby/User.js +++ b/app/Lobby/User.js @@ -20,11 +20,7 @@ function (Parent, ProtocolHelper, NotificationCenter) { } User.prototype = Object.create(Parent.prototype); - - User.prototype.setChannelProcess = function (channelProcess) { - this.channelProcess = channelProcess; - } - + // Socket callbacks diff --git a/client.js b/client.js index bf6cc0d..6bc6999 100755 --- a/client.js +++ b/client.js @@ -27,7 +27,7 @@ function (Networker, SocketIO, Settings, Exception, PIXI) { "flashsocket" ] }; - var socket = SocketIO.connect(location.href, options); + var socket = SocketIO.connect("/", options); var networker = new Networker(socket); inspector.networker = networker; inspector.settings = Settings; diff --git a/server.js b/server.js index 28c88d4..21dd3ef 100755 --- a/server.js +++ b/server.js @@ -28,7 +28,7 @@ requirejs([ function (HttpServer, Socket, Coordinator) { var coordinator = new Coordinator(); - var httpServer = new HttpServer(options); + var httpServer = new HttpServer(options, coordinator); var socket = new Socket(httpServer.getServer(), options, coordinator); inspector = { diff --git a/static/html/game.html b/static/html/game.html new file mode 100755 index 0000000..e043031 --- /dev/null +++ b/static/html/game.html @@ -0,0 +1,61 @@ + + + + + Chuck + + + +
+ + + diff --git a/static/html/index.html b/static/html/index.html old mode 100755 new mode 100644 index e043031..eda77ba --- a/static/html/index.html +++ b/static/html/index.html @@ -1,61 +1,137 @@ - - - Chuck - - - -
- - - + function populate(list) { + var html = ""; + for (var i = 0; i < list.length; i++) { + var channel = list[i]; + html += "