diff --git a/client.js b/client.js
index 98abfec..57fad09 100644
--- a/client.js
+++ b/client.js
@@ -21,11 +21,11 @@ function onConnect () {
Chuck.init();
}
-function onMessage (packet) {
- packet = JSON.parse(packet);
+function onMessage (message) {
+ var commands = JSON.parse(message);
- if (packet && packet.m) {
- processServerCommand(packet);
+ for(var command in commands) {
+ processControlCommand(type, command[type]);
}
}
@@ -33,16 +33,16 @@ function onDisconnect () {
console.log('client disconnected');
}
-function processServerCommand(packet){
- switch(packet.m) {
- case 'join':
+function processControlCommand(command, options){
+ switch(command) {
+ case 'joined':
break;
case 'nick':
break;
case 'gameCommand':
- Chuck.processGameCommand(packet.d);
+ Chuck.processGameCommand(options);
break;
default:
diff --git a/index.html b/index.html
deleted file mode 100644
index 4695ec5..0000000
--- a/index.html
+++ /dev/null
@@ -1,33 +0,0 @@
-
-
-
- Chuck
-
-
-
-
-
-
- hello chuck
-
-
-
-
diff --git a/lib/Client/Consumer.js b/lib/Client/Consumer.js
new file mode 100644
index 0000000..8cdd227
--- /dev/null
+++ b/lib/Client/Consumer.js
@@ -0,0 +1,12 @@
+define(function() {
+
+ function Consumer() {
+ }
+
+ Consumer.prototype.init = function(){
+ return null;
+ }
+
+ return Consumer;
+
+});
\ No newline at end of file
diff --git a/lib/Protocol/Parser.js b/lib/Protocol/Parser.js
new file mode 100644
index 0000000..d673194
--- /dev/null
+++ b/lib/Protocol/Parser.js
@@ -0,0 +1,15 @@
+define(function() {
+
+ function Parser() {
+ }
+
+ Parser.prototype.encode = function(message){
+ return JSON.stringify(message);
+ }
+
+ Parser.prototype.decode = function(message){
+ return JSON.parse(message);
+ }
+
+ return Parser;
+});
\ No newline at end of file
diff --git a/lib/Server/Channel.js b/lib/Server/Channel.js
new file mode 100644
index 0000000..cd505f0
--- /dev/null
+++ b/lib/Server/Channel.js
@@ -0,0 +1,22 @@
+define(function() {
+
+ function Channel(name) {
+ this.name = name;
+ this.users = {};
+ }
+
+ Channel.prototype.validateName = function(name){
+ return true;
+ }
+
+ Channel.prototype.addUser = function(user){
+ this.users[user.id] = user;
+ }
+
+ Channel.prototype.releaseUser = function(user){
+ delete this.users[user.id];
+ }
+
+ return Channel;
+
+});
\ No newline at end of file
diff --git a/lib/Server/Coordinator.js b/lib/Server/Coordinator.js
new file mode 100644
index 0000000..1e9765b
--- /dev/null
+++ b/lib/Server/Coordinator.js
@@ -0,0 +1,45 @@
+define(["Server/User", "Server/Channel"], function(User, Channel) {
+
+ function Coordinator() {
+ this.channels = {};
+ this.lobbyUsers = {};
+ }
+
+ Coordinator.prototype.createUser = function(socketLink){
+ var user = new User(socketLink, this);
+ this.assignUserToLobby(user);
+ }
+
+ Coordinator.prototype.assignUserToLobby = function(user){
+ if(user.channel) {
+ user.channel.releaseUser(user);
+ }
+ this.lobbyUsers[user.id] = user;
+ }
+
+ Coordinator.prototype.assignUserToChannel = function(user, channelName){
+ if(user.channel) {
+ user.channel.releaseUser(user);
+ }
+
+ if(!Channel.validateName(channelName)){
+ //TODO send validation error
+ return false;
+ }
+
+ var channel = this.channels[channelName];
+ if(!channel) {
+ channel = new Channel(channelName);
+ this.channels[channelName] = channel;
+ }
+
+ channel.addUser(user);
+ user.setChannel(channel);
+
+ delete this.lobbyUsers[user.id];
+ }
+
+
+ return Coordinator;
+
+});
\ No newline at end of file
diff --git a/lib/Server/HttpServer.js b/lib/Server/HttpServer.js
new file mode 100644
index 0000000..47aa491
--- /dev/null
+++ b/lib/Server/HttpServer.js
@@ -0,0 +1,66 @@
+define(['http', 'node-static'], function(http, nodeStatic) {
+
+ function HttpServer(options) {
+ options.port = options.port || 1234;
+ options.caching = options.caching || true;
+ options.rootDirectory = options.rootDirectory || './';
+
+ this.server = null;
+
+ this.init(options);
+ }
+
+ HttpServer.prototype.init = function(options) {
+ var self = this;
+
+ var fileServer = new nodeStatic.Server(options.rootDirectory, { cache: options.caching });
+
+ this.server = http.createServer(
+ function(req, res){
+ req.addListener('end', function () {
+ switch(true) {
+ case req.url == '/':
+ fileServer.serveFile('./static/html/index.html', 200, {}, req, res);
+ break;
+
+ case req.url == '/client.js':
+ fileServer.serveFile('./client.js', 200, {}, req, res);
+ break;
+
+ case req.url == '/require.js':
+ fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res);
+ break;
+
+ case new RegExp(/^\/lib/).test(req.url):
+ fileServer.serve(req, res, function(){
+ self.handleFileError(res)
+ });
+ break;
+
+ case new RegExp(/^\/static/).test(req.url):
+ fileServer.serve(req, res, function(){
+ self.handleFileError(res)
+ });
+ break;
+
+ default:
+ self.handleFileError(res);
+ break;
+ }
+ });
+ }
+ );
+ this.server.listen(options.port);
+ }
+
+ HttpServer.prototype.getServer = function(){
+ return this.server;
+ }
+
+ HttpServer.prototype.handleFileError = function (res){
+ res.writeHead(404, {'Content-Type': 'text/html'});
+ res.end('404 not ... found
');
+ }
+
+ return HttpServer;
+});
\ No newline at end of file
diff --git a/lib/Server/Socket.js b/lib/Server/Socket.js
new file mode 100644
index 0000000..c903dc8
--- /dev/null
+++ b/lib/Server/Socket.js
@@ -0,0 +1,29 @@
+define(['socket.io'], function(io) {
+
+ function Socket(server, coordinator) {
+ this.coordinator = coordinator;
+ this.socket = io.listen(server);
+
+ this.init(server);
+ }
+
+ Socket.prototype.init = function(){
+
+ var self = this;
+
+ this.socket.configure('development', function(){
+ this.set('log level', 0);
+ });
+
+ this.socket.on('connection', function(user){
+ self.onConnection(user);
+ });
+ }
+
+ Socket.prototype.onConnection = function(socketLink){
+ this.coordinator.createUser(socketLink);
+ }
+
+ return Socket;
+
+});
\ No newline at end of file
diff --git a/lib/Server/User.js b/lib/Server/User.js
new file mode 100644
index 0000000..0395c8a
--- /dev/null
+++ b/lib/Server/User.js
@@ -0,0 +1,66 @@
+define(["Protocol/Parser"], function(Parser) {
+
+ function User(socketLink, coordinator) {
+
+ this.id = socketLink.id;
+ this.socketLink = socketLink;
+ this.coordinator = coordinator;
+ this.channel = null;
+
+ this.init(socketLink);
+ }
+
+ User.prototype.init = function(socketLink){
+
+ socketLink.on('message', function(message){
+ this.onMessage(message);
+ });
+
+ socketLink.on('disconnect', function(){
+ this.onDisconnect();
+ });
+ }
+
+ User.prototype.setChannel = function(channel){
+ this.channel = channel;
+ }
+
+ User.prototype.send = function(message){
+ message = Parser.encode(message);
+ this.socketLink.send(message);
+ }
+
+ User.prototype.onMessage = function(){
+ var commands = Parser.decode(message);
+ for(var command in commands) {
+ this.processControlCommand(command, commands[command]);
+ }
+ }
+
+ User.prototype.onDisconnect = function(){
+ return null;
+ }
+
+ 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':
+ //this.channel.game.processGameCommand(options);
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ return User;
+
+});
\ No newline at end of file
diff --git a/server.js b/server.js
index 717a8a9..94ef7d4 100644
--- a/server.js
+++ b/server.js
@@ -1,99 +1,40 @@
-var http = require('http'),
- io = require('socket.io'),
- nodeStatic = require('node-static')
- requirejs = require('requirejs');
+var requirejs = require('requirejs');
+
+var inspector = {};
requirejs.config({
nodeRequire: require,
baseUrl: 'lib'
});
+var requirements = [
+ "Server/HttpServer",
+ "Server/Socket",
+ "Server/Coordinator"
+];
+requirejs(requirements, function(HttpServer, Socket, Coordinator) {
+
+ var options = {
+ port: 1234,
+ rootDirectory: './',
+ caching: false
+ };
+
+ var coordinator = new Coordinator();
+ var httpServer = new HttpServer(options);
+ var socket = new Socket(httpServer.getServer(), coordinator);
+
+ inspector.coordinator = coordinator;
+});
+
+exports = module.exports = inspector;
+
+/*
+belongs to channel.js
var chuck;
requirejs(["Chuck/Chuck"], function(Chuck) {
Chuck.init();
+ chuck = Chuck;
});
-
-var clients = [];
-
-// Setting up http server
-var fileServer = new nodeStatic.Server('./', { cache: false });
-
-function handleFileError(res){
- res.writeHead(404, {'Content-Type': 'text/html'});
- res.end('404 not ... found
');
-}
-
-var server = http.createServer(
- function(req, res){
- req.addListener('end', function () {
- switch(true) {
- case req.url == '/':
- fileServer.serveFile('./static/html/index.html', 200, {}, req, res);
- break;
-
- case req.url == '/client.js':
- fileServer.serveFile('./client.js', 200, {}, req, res);
- break;
-
- case req.url == '/require.js':
- fileServer.serveFile('./node_modules/requirejs/require.js', 200, {}, req, res);
- break;
-
- case new RegExp(/^\/lib/).test(req.url):
- fileServer.serve(req, res, function(){
- handleFileError(res)
- });
- break;
-
- case new RegExp(/^\/static/).test(req.url):
- fileServer.serve(req, res, function(){
- handleFileError(res)
- });
- break;
-
- default:
- handleFileError(res);
- break;
- }
- });
- }
-);
-server.listen(1234);
-
-var socket = io.listen(server);
-
-socket.configure('development', function(){
- socket.set('log level', 0);
-});
-
-socket.on('connection', function(client) {
- console.log('client connected');
- clients.push(client);
- console.log("Total clients: " + clients.length);
-
- client.send(JSON.stringify({"startId" : clients.length}));
-
- client.on('message', function(packet){
- packet = JSON.parse(packet);
-
- if(packet && packet.m){
- switch(packet.m){
- case 'jump':
- jump();
- updateWorld(client);
- break;
- case 'ping':
- pong(client, packet.d);
- break;
- default:
- break;
- }
- }
- //updateWorld();
- });
-
- client.on('disconnect', function(){
- console.log("disconnect");
- });
-});
\ No newline at end of file
+*/
\ No newline at end of file