refactored Server and Lobby

This commit is contained in:
Jeena 2014-03-01 14:07:03 +01:00
parent 42475c9b38
commit d83376d5c7
26 changed files with 29 additions and 29 deletions

52
app/Server/Api.js Normal file
View file

@ -0,0 +1,52 @@
define([
"Lib/Utilities/NotificationCenter",
"Lib/Utilities/Protocol/Helper"
],
function (Nc, ProtocolHelper) {
function Api(coordinator) {
this.coordinator = coordinator;
this.isError = false;
this.output = null;
}
Api.prototype.handleCall = function(queryParameters) {
var command;
try {
var message = JSON.parse(queryParameters);
command = message.command;
} catch(e) {
console.error(e)
}
var output = null;
switch(command) {
case "getChannels":
output = this.coordinator.getChannels();
break;
default:
this.isError = true;
output = "Command not found";
break;
}
this.output = output;
}
Api.prototype.getOutput = function() {
var output = {};
var key = this.isError ? "error" : "success";
output[key] = this.output;
return JSON.stringify(output);
};
Api.prototype.getContentType = function() {
return "application/json";
};
return Api;
});

107
app/Server/Coordinator.js Executable file
View file

@ -0,0 +1,107 @@
define([
"Server/User",
"Game/Channel/Channel",
"Server/PipeToChannel",
"Lib/Utilities/NotificationCenter"
],
function (User, Channel, PipeToChannel, Nc) {
function Coordinator () {
this.channelPipes = {};
this.lobbyUsers = {};
console.checkpoint('create Coordinator');
}
Coordinator.prototype.createUser = function (socketLink) {
var user = new User(socketLink, this);
console.checkpoint('creating user');
this.assignUserToLobby(user);
}
Coordinator.prototype.assignUserToLobby = function (user) {
if(user.channelPipe) {
//user.channel.releaseUser(user); -> generate message
}
this.lobbyUsers[user.id] = user;
console.checkpoint('assign user to lobby');
}
Coordinator.prototype.assignUserToChannel = function (user, channelName) {
if(user.channelPipe) {
//user.channel.releaseUser(user); -> generate message
}
if(!Channel.validateName(channelName)) {
//TODO send validation error
return false;
}
var channelPipe = this.channelPipes[channelName];
if(!channelPipe) {
this.createPipe(channelName);
}
//channel.addUser(user);
//user.setChannel(channel);
Nc.trigger('user/joined', user);
delete this.lobbyUsers[user.id];
}
Coordinator.prototype.removeUser = function (user) {
Nc.trigger('user/left', user);
//NotificationCenter.off('channel/' + user.channel.channelName + '/user/' + user.id);
delete this.lobbyUsers[user.id];
}
Coordinator.prototype.createPipe = function(channelName) {
var channelPipe = new PipeToChannel(channelName);
this.channelPipes[channelName] = channelPipe;
Nc.on('channel/' + channelName + '/message', function (data) {
channelPipe.send('channel', data);
}, this);
// sending info to user
Nc.on('user/joined', function (user) {
/*
Nc.on('channel/' + channelName + '/user/' + user.id, function (recipient, data) {
channelPipe.send(recipient, data);
}, this);
*/
channelPipe.send('channel', { addUser: user.id });
}, this);
Nc.on('user/left', function (user) {
channelPipe.send('channel', { releaseUser: user.id });
}, this);
Nc.on('user/controlCommand', function (userId, data) {
channelPipe.sendToUser(userId, data);
}, this);
return channelPipe;
};
Coordinator.prototype.getChannels = function(options) {
var list = [];
for (var channelName in this.channelPipes) {
list.push({
name: channelName
});
}
return list;
};
return Coordinator;
});

55
app/Server/PipeToChannel.js Executable file
View file

@ -0,0 +1,55 @@
define([
"Lib/Utilities/NotificationCenter",
"child_process"
],
function (Nc, childProcess) {
var fork = childProcess.fork;
function PipeToChannel (channelName) {
this.channelPipe = null;
try {
this.channelPipe = fork('channel.js');
} catch (err) {
throw 'Failed to fork channel! (' + err + ')';
}
console.checkpoint('creating channel process for ' + channelName);
this.send('channel/' + channelName, { CREATE: channelName });
this.channelPipe.on('message', this.onMessage.bind(this));
var self = this;
}
// While creating user
PipeToChannel.prototype.send = function (recipient, data) {
var message = {
recipient: recipient,
data: data
}
this.channelPipe.send(message);
}
// If user already created
PipeToChannel.prototype.sendToUser = function (id, data) {
var message = {
recipient: "user/" + id,
data: data
}
this.channelPipe.send(message);
}
PipeToChannel.prototype.onMessage = function (message) {
Nc.trigger(message.recipient + '/message', message.data);
}
return PipeToChannel;
});

60
app/Server/User.js Executable file
View file

@ -0,0 +1,60 @@
define([
"Game/Core/User",
"Lib/Utilities/Protocol/Helper",
"Lib/Utilities/NotificationCenter"
],
function (Parent, ProtocolHelper, Nc) {
function User (socketLink, coordinator) {
Parent.call(this, socketLink.id);
this.coordinator = coordinator;
this.channelProcess = null;
this.socketLink = socketLink;
socketLink.on('message', this.onMessage.bind(this));
socketLink.on('disconnect', this.onDisconnect.bind(this));
Nc.on("user/" + this.socketLink.id + "/message", this.socketLink.send, this.socketLink);
}
User.prototype = Object.create(Parent.prototype);
// Socket callbacks
User.prototype.onMessage = function (message) {
ProtocolHelper.applyCommand(message, this);
}
User.prototype.onDisconnect = function () {
this.coordinator.removeUser(this);
}
// User command callbacks
// Remember: control commands are coordinator relevant commands
User.prototype.onJoin = function(options) {
this.coordinator.assignUserToChannel(this, options);
};
User.prototype.onLeave = function(options) {
this.coordinator.assignUserToLobby(this);
};
User.prototype.onGameCommand = function(options) {
// repacking for transport via pipe
var message = ProtocolHelper.encodeCommand("gameCommand", options);
Nc.trigger("user/controlCommand", this.id, message);
};
User.prototype.onPing = function(timestamp) {
var message = ProtocolHelper.encodeCommand("pong", timestamp);
Nc.trigger("user/" + this.socketLink.id + "/message", message);
};
return User;
});