added automatic method calling

This commit is contained in:
logsol 2012-07-28 01:04:41 +02:00
parent b5b10ba0b9
commit ebb400a289

View file

@ -28,7 +28,7 @@ function(GameController, NotificationCenter) {
} }
// Messages look like: // Messages look like:
// {chanel: {setName: 'foo'}} // {channel: {setName: 'foo'}}
// {user: {jupm: null}, id: 12} // {user: {jupm: null}, id: 12}
Channel.prototype.onMessage = function(message) { Channel.prototype.onMessage = function(message) {
@ -37,12 +37,12 @@ function(GameController, NotificationCenter) {
switch(recipient) { switch(recipient) {
case 'user': case 'user':
this.users[message.id].onMessage(message.user); this.forward(this.users[message.id], message.user);
break; break;
case 'id': // Do nothing, it is needed by the user case 'id': // Do nothing, it is needed by the user
break; break;
case 'channel': case 'channel':
this.onCommand(message.channel); this.forward(this, message.channel);
break; break;
default: default:
throw 'unknown recipient'; throw 'unknown recipient';
@ -51,20 +51,21 @@ function(GameController, NotificationCenter) {
} }
}; };
Channel.prototype.onCommand = function(message) { Channel.prototype.forward = function(target, message) {
for(var command in message) { for(var command in message) {
switch(command) { if(typeof target[command] == 'function'){
target[command].call(target, message[command]);
case 'setName': } else {
this.name = message[command]; throw 'trying to call undefined function ' + target[command];
console.log("Chanel name set to '" + this.name + "'");
break;
default:
throw 'unknown command';
break;
} }
} }
}; };
Channel.prototype.setName = function(name) {
this.name = name;
console.log(' created channel ' + name);
}
/* /*
Channel.prototype.addUser = function(user){ Channel.prototype.addUser = function(user){
var userIds = Object.keys(this.users); var userIds = Object.keys(this.users);