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

View file

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

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

View file

@ -1,58 +0,0 @@
define([
],
function() {
function NotificationCenter() {
this.topics = {};
this.subUid = -1;
}
NotificationCenter.prototype.trigger = function(topic) {
if (!this.topics[topic]) {
throw "No such topic " + topic + ". Could not trigger.";
}
var args = Array.prototype.slice.call(arguments, 1);
var subscribers = this.topics[topic];
var len = subscribers ? subscribers.length : 0;
while (len--) {
var subscriber = subscribers[len];
subscriber.func.apply(subscriber.context, args);
}
}
NotificationCenter.prototype.on = function(topic, func, context) {
if (!this.topics[topic]) {
this.topics[topic] = [];
}
var token = ( ++this.subUid ).toString();
this.topics[topic].push({
token: token,
func: func,
context: context
});
return token;
}
NotificationCenter.prototype.off = function(token) {
for(var m in this.topics) {
if (this.topics[m]) {
for(var i = 0, j = this.topics[m].length; i < j; i++) {
if (this.topics[m][i].token === token) {
this.topics[m].splice(i, 1);
return token;
}
}
}
}
}
return new NotificationCenter(); // making it singletone
});

View file

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