mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
working towards usable new structure
This commit is contained in:
parent
3afc2fa66e
commit
815c63009f
15 changed files with 223 additions and 84 deletions
|
|
@ -1,9 +1,12 @@
|
|||
define([
|
||||
"Game/Server/User",
|
||||
"Game/Server/Channel"
|
||||
"Lobby/User",
|
||||
"Game/Server/Channel",
|
||||
"node-fork"
|
||||
],
|
||||
|
||||
function(User, Channel) {
|
||||
function(User, Channel, nodeFork) {
|
||||
|
||||
var fork = nodeFork.fork;
|
||||
|
||||
function Coordinator() {
|
||||
this.channels = {};
|
||||
|
|
@ -16,16 +19,16 @@ function(User, Channel) {
|
|||
}
|
||||
|
||||
Coordinator.prototype.assignUserToLobby = function(user){
|
||||
if(user.channel) {
|
||||
user.channel.releaseUser(user);
|
||||
if(user.channelProcess) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
this.lobbyUsers[user.id] = user;
|
||||
}
|
||||
|
||||
Coordinator.prototype.assignUserToChannel = function(user, channelName){
|
||||
|
||||
if(user.channel) {
|
||||
user.channel.releaseUser(user);
|
||||
if(user.channelProcess) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
|
||||
if(!Channel.validateName(channelName)){
|
||||
|
|
@ -35,20 +38,29 @@ function(User, Channel) {
|
|||
|
||||
var channel = this.channels[channelName];
|
||||
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;
|
||||
}
|
||||
|
||||
channel.addUser(user);
|
||||
user.setChannel(channel);
|
||||
//channel.addUser(user);
|
||||
//user.setChannel(channel);
|
||||
|
||||
delete this.lobbyUsers[user.id];
|
||||
}
|
||||
|
||||
Coordinator.prototype.removeUser = function(user){
|
||||
delete this.lobbyUsers[user.id];
|
||||
if(user.channel) {
|
||||
user.channel.releaseUser(user);
|
||||
if(user.channelProcess) {
|
||||
//user.channel.releaseUser(user); -> generate message
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
66
app/Lobby/User.js
Executable file
66
app/Lobby/User.js
Executable 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;
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue