working towards usable new structure

This commit is contained in:
logsol 2012-07-22 17:54:27 +02:00
parent 3afc2fa66e
commit 815c63009f
15 changed files with 223 additions and 84 deletions

33
app/Bootstrap/Channel.js Executable file
View file

@ -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;
});

View file

@ -3,24 +3,28 @@ define([
"Game/Client/Physics/Engine", "Game/Client/Physics/Engine",
"Game/Client/View/ViewController", "Game/Client/View/ViewController",
"Game/Client/Control/KeyboardController", "Game/Client/Control/KeyboardController",
"Game/Core/NotificationCenter",
"Lib/Utilities/RequestAnimFrame" "Lib/Utilities/RequestAnimFrame"
], ],
function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimFrame) { function(Parent, PhysicsEngine, ViewController, KeyboardController, NotificationCenter, requestAnimFrame) {
function GameController () { function GameController () {
this.viewController = new ViewController();
Parent.call(this, new PhysicsEngine()); Parent.call(this, new PhysicsEngine());
this.me = null; this.me = null;
this.keyboardController = null; this.keyboardController = null;
this.viewController = new ViewController(); //NotificationCenter.on('me/joined', this.meJoined, this)
this.update();
//this.update();
} }
GameController.prototype = Object.create(Parent.prototype); GameController.prototype = Object.create(Parent.prototype);
/*
GameController.prototype.getMe = function() { GameController.prototype.getMe = function() {
return this.me; return this.me;
} }
@ -40,6 +44,7 @@ function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimF
GameController.prototype.meJoined = function(user) { GameController.prototype.meJoined = function(user) {
this.me = this.userJoined(user); this.me = this.userJoined(user);
this.keyboardController = new KeyboardController(this.me, this); this.keyboardController = new KeyboardController(this.me, this);
} }
@ -66,6 +71,6 @@ function(Parent, PhysicsEngine, ViewController, KeyboardController, requestAnimF
} }
} }
*/
return GameController; return GameController;
}); });

View file

@ -19,7 +19,7 @@ function(ProtocolHelper, GameController) {
this.socketLink.on('connect', function() { this.socketLink.on('connect', function() {
self.onConnect(); self.onConnect();
}); });
/*
this.socketLink.on('message', function(message) { this.socketLink.on('message', function(message) {
self.onMessage(message); self.onMessage(message);
}); });
@ -27,12 +27,13 @@ function(ProtocolHelper, GameController) {
this.socketLink.on('disconnect', function() { this.socketLink.on('disconnect', function() {
self.onDisconnect(); self.onDisconnect();
}); });
*/
} }
Networker.prototype.onConnect = function() { Networker.prototype.onConnect = function() {
this.join('dungeon'); this.join('dungeon');
} }
/*
Networker.prototype.onMessage = function(message) { Networker.prototype.onMessage = function(message) {
var self = this; var self = this;
ProtocolHelper.runCommands(message, function(command, options) { ProtocolHelper.runCommands(message, function(command, options) {
@ -44,7 +45,7 @@ function(ProtocolHelper, GameController) {
if(this.gameController) this.gameController.destruct(); if(this.gameController) this.gameController.destruct();
this.gameController = null; this.gameController = null;
} }
*/
Networker.prototype.join = function(channelName){ Networker.prototype.join = function(channelName){
this.sendCommand('join', channelName); this.sendCommand('join', channelName);
} }
@ -53,9 +54,9 @@ function(ProtocolHelper, GameController) {
var message = ProtocolHelper.encodeCommand(command, options); var message = ProtocolHelper.encodeCommand(command, options);
this.socketLink.send(message); this.socketLink.send(message);
} }
/*
Networker.prototype.onJoinSuccess = function(options) { Networker.prototype.onJoinSuccess = function(options) {
this.gameController = new GameController(this, options.id); this.gameController = new GameController(options.id);
this.gameController.loadLevel("default.json") this.gameController.loadLevel("default.json")
console.log("Joined " + options.channelName); console.log("Joined " + options.channelName);
@ -103,7 +104,7 @@ function(ProtocolHelper, GameController) {
break; break;
} }
} }
*/
return Networker; return Networker;
}); });

2
app/Game/Client/Physics/Engine.js Normal file → Executable file
View file

@ -17,7 +17,7 @@ function(Parent, Settings, DomController, Box2D) {
Engine.prototype.setupDebugDraw = function() { Engine.prototype.setupDebugDraw = function() {
//var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE; //var debugSprite = Settings.DEBUG_DRAW_CANVAS_SPRITE;
var debugSprite = Dom.getDebugCanvas().getContext("2d"); var debugSprite = DomController.getDebugCanvas().getContext("2d");
// set debug draw // set debug draw
var debugDraw = new Box2D.Dynamics.b2DebugDraw(); var debugDraw = new Box2D.Dynamics.b2DebugDraw();

View file

@ -7,7 +7,6 @@ define([
function(Engine, Level, Player) { function(Engine, Level, Player) {
function GameController(physicsEngine) { function GameController(physicsEngine) {
console.log('constructor called');
this.players = {}; this.players = {};
if (! physicsEngine instanceof Engine) { if (! physicsEngine instanceof Engine) {
@ -30,12 +29,16 @@ function(Engine, Level, Player) {
this.level.loadLevelInToEngine(); this.level.loadLevelInToEngine();
} }
/*
GameController.prototype.destroy = function() { GameController.prototype.destroy = function() {
for(var player in this.players) { for(var player in this.players) {
this.players[player].destroy(); this.players[player].destroy();
} }
} }
GameController.prototype.userJoined = function(user) { GameController.prototype.userJoined = function(user) {
var player = new Player(user.id, this.physicsEngine); var player = new Player(user.id, this.physicsEngine);
this.players[user.id] = player; this.players[user.id] = player;
@ -48,5 +51,7 @@ function(Engine, Level, Player) {
delete this.players[user.id]; delete this.players[user.id];
} }
*/
return GameController; return GameController;
}); });

8
app/Game/Core/User.js Executable file
View file

@ -0,0 +1,8 @@
define(function() {
function User(id) {
this.id = id;
}
return User;
});

View file

@ -1,26 +1,32 @@
define([ define([
"Game/Server/GameController", "Game/Server/GameController",
"Game/Server/NotificationCenter" "Game/Core/NotificationCenter"
], ],
function(GameController, NotificationCenter) { function(GameController, NotificationCenter) {
function Channel(name) { function Channel(coordinatorLink) {
this.name = name;
this.coordinatorLink = coordinatorLink;
console.log('A CHANNEL WAS CREATED');
/*
this.users = {}; this.users = {};
this.gameController = new GameController(); this.gameController = new GameController();
this.gameController.loadLevel("default.json"); this.gameController.loadLevel("default.json");
*/
/*
var self = this; var self = this;
NotificationCenter.on("processGameCommandFromUser", function(topic, args) { NotificationCenter.on("processGameCommandFromUser", function(topic, args) {
self.processGameCommandFromUser.apply(self, args); self.processGameCommandFromUser.apply(self, args);
}); });
*/
} }
Channel.validateName = function(name){ Channel.validateName = function(name){
return true; return true;
} }
/*
Channel.prototype.addUser = function(user){ Channel.prototype.addUser = function(user){
var userIds = Object.keys(this.users); var userIds = Object.keys(this.users);
@ -56,7 +62,7 @@ function(GameController, NotificationCenter) {
Channel.prototype.processGameCommandFromUser = function(command, options, user) { Channel.prototype.processGameCommandFromUser = function(command, options, user) {
this.gameController.progressGameCommandFromUser(command, options, user); this.gameController.progressGameCommandFromUser(command, options, user);
} }
*/
return Channel; return Channel;
}); });

View file

@ -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;
});

View file

@ -4,7 +4,7 @@ define([
"Game/Config/Settings", "Game/Config/Settings",
"Game/Core/Control/InputController", "Game/Core/Control/InputController",
"Lib/Utilities/RequestAnimFrame", "Lib/Utilities/RequestAnimFrame",
"Game/Server/NotificationCenter" "Game/Core/NotificationCenter"
], ],
function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) { function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, NotificationCenter) {
@ -14,15 +14,15 @@ function(Parent, PhysicsEngine, Settings, InputController, requestAnimFrame, Not
this.inputControllers = {}; this.inputControllers = {};
this.update(); //this.update();
this.updateWorld(); //this.updateWorld();
NotificationCenter.on('user/joined', this.userJoined, this); //NotificationCenter.on('user/joined', this.userJoined, this);
NotificationCenter.on('user/left', this.userLeft, this); //NotificationCenter.on('user/left', this.userLeft, this);
} }
GameController.prototype = Object.create(Parent.prototype); GameController.prototype = Object.create(Parent.prototype);
/*
GameController.prototype.update = function() { GameController.prototype.update = function() {
requestAnimFrame(this.update.bind(this)); 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); setTimeout(this.updateWorld.bind(this), Settings.WORLD_UPDATE_BROADCAST_INTERVAL);
} }
*/
return GameController; return GameController;
}); });

View file

@ -1,12 +1,13 @@
define([ define([
"Game/Core/User",
"Game/Core/Protocol/Helper", "Game/Core/Protocol/Helper",
"Game/Server/NotificationCenter" "Game/Core/NotificationCenter"
], ],
function(ProtocolHelper, NotificationCenter) { function(Parent, ProtocolHelper, NotificationCenter) {
function User(socketLink, coordinator) {
function User(id, coordinator) {
Parent.call(this, id);
this.id = socketLink.id; this.id = socketLink.id;
this.socketLink = socketLink; this.socketLink = socketLink;
this.coordinator = coordinator; this.coordinator = coordinator;
@ -15,19 +16,14 @@ function(ProtocolHelper, NotificationCenter) {
this.init(socketLink); this.init(socketLink);
} }
User.prototype = Object.create(Parent.prototype);
User.prototype.init = function(socketLink){ User.prototype.init = function(socketLink){
var self = this; var self = this;
socketLink.on('message', function(message){
self.onMessage(message);
});
socketLink.on('disconnect', function(){
self.onDisconnect();
});
} }
/*
User.prototype.setChannel = function(channel) { User.prototype.setChannel = function(channel) {
this.channel = channel; this.channel = channel;
} }
@ -38,44 +34,12 @@ function(ProtocolHelper, NotificationCenter) {
this.socketLink.send(message); 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() { User.prototype.toString = function() {
return "[User " + this.id + "]"; return "[User " + this.id + "]";
}; };
*/
return User; return User;
}); });

View file

@ -1,9 +1,12 @@
define([ define([
"Game/Server/User", "Lobby/User",
"Game/Server/Channel" "Game/Server/Channel",
"node-fork"
], ],
function(User, Channel) { function(User, Channel, nodeFork) {
var fork = nodeFork.fork;
function Coordinator() { function Coordinator() {
this.channels = {}; this.channels = {};
@ -16,16 +19,16 @@ function(User, Channel) {
} }
Coordinator.prototype.assignUserToLobby = function(user){ Coordinator.prototype.assignUserToLobby = function(user){
if(user.channel) { if(user.channelProcess) {
user.channel.releaseUser(user); //user.channel.releaseUser(user); -> generate message
} }
this.lobbyUsers[user.id] = user; this.lobbyUsers[user.id] = user;
} }
Coordinator.prototype.assignUserToChannel = function(user, channelName){ Coordinator.prototype.assignUserToChannel = function(user, channelName){
if(user.channel) { if(user.channelProcess) {
user.channel.releaseUser(user); //user.channel.releaseUser(user); -> generate message
} }
if(!Channel.validateName(channelName)){ if(!Channel.validateName(channelName)){
@ -35,20 +38,29 @@ function(User, Channel) {
var channel = this.channels[channelName]; var channel = this.channels[channelName];
if(!channel) { 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; this.channels[channelName] = channel;
} }
channel.addUser(user); //channel.addUser(user);
user.setChannel(channel); //user.setChannel(channel);
delete this.lobbyUsers[user.id]; delete this.lobbyUsers[user.id];
} }
Coordinator.prototype.removeUser = function(user){ Coordinator.prototype.removeUser = function(user){
delete this.lobbyUsers[user.id]; delete this.lobbyUsers[user.id];
if(user.channel) { if(user.channelProcess) {
user.channel.releaseUser(user); //user.channel.releaseUser(user); -> generate message
} }
} }

66
app/Lobby/User.js Executable file
View file

@ -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;
});

18
channel.js Executable file
View file

@ -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;
});

View file

@ -17,7 +17,8 @@
"dependencies": { "dependencies": {
"socket.io": ">= 0.9.6", "socket.io": ">= 0.9.6",
"node-static": ">= 0.6.0", "node-static": ">= 0.6.0",
"requirejs": ">= 2.0.2" "requirejs": ">= 2.0.2",
"node-fork": ">= 0.4.2"
}, },
"devDependencies": {}, "devDependencies": {},
"optionalDependencies": {}, "optionalDependencies": {},