mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
refactored Server and Lobby
This commit is contained in:
parent
42475c9b38
commit
d83376d5c7
26 changed files with 29 additions and 29 deletions
122
app/Game/Channel/Channel.js
Executable file
122
app/Game/Channel/Channel.js
Executable file
|
|
@ -0,0 +1,122 @@
|
|||
define([
|
||||
"Game/Channel/GameController",
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Game/Channel/User",
|
||||
"Lib/Utilities/Protocol/Helper",
|
||||
"Lib/Utilities/Options",
|
||||
"Game/Config/Settings"
|
||||
],
|
||||
|
||||
function (GameController, Nc, User, ProtocolHelper, Options, Settings) {
|
||||
|
||||
function Channel (pipeToServer, name, options) {
|
||||
|
||||
var self = this;
|
||||
|
||||
this.options = options = Options.merge(options, {
|
||||
levelUids: Settings.DEFAULT_LEVELS
|
||||
});
|
||||
|
||||
this.name = name;
|
||||
this.users = {};
|
||||
|
||||
this.pipeToServer = pipeToServer;
|
||||
|
||||
this.gameController = new GameController(this);
|
||||
|
||||
Nc.on('channel/controlCommand', function (message) {
|
||||
ProtocolHelper.applyCommand(message.data, self);
|
||||
});
|
||||
|
||||
Nc.on('broadcastControlCommand', this.broadcastControlCommand, this);
|
||||
Nc.on('broadcastControlCommandExcept', this.broadcastControlCommandExcept, this);
|
||||
|
||||
Nc.on('broadcastGameCommand', this.broadcastGameCommand, this);
|
||||
Nc.on('broadcastGameCommandExcept', this.broadcastGameCommandExcept, this);
|
||||
|
||||
console.checkpoint('channel ' + name + ' created');
|
||||
}
|
||||
|
||||
Channel.validateName = function (name) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Channel command callbacks
|
||||
|
||||
Channel.prototype.onAddUser = function (userId) {
|
||||
var self = this;
|
||||
|
||||
if(!this.gameController.level || !this.gameController.level.isLoaded) {
|
||||
var token = Nc.on("game/level/loaded", function() {
|
||||
self.sendJoinSuccess(userId);
|
||||
Nc.off(token);
|
||||
});
|
||||
} else {
|
||||
self.sendJoinSuccess(userId);
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.sendJoinSuccess = function(userId) {
|
||||
var user = new User(userId, this);
|
||||
var joinedUsers = Object.keys(this.users);
|
||||
|
||||
var levelUid = null;
|
||||
if(this.gameController.level) {
|
||||
levelUid = this.gameController.level.uid;
|
||||
}
|
||||
|
||||
this.users[user.id] = user;
|
||||
|
||||
var options = {
|
||||
userId: user.id,
|
||||
channelName: this.name,
|
||||
joinedUsers: joinedUsers,
|
||||
levelUid: levelUid
|
||||
};
|
||||
|
||||
Nc.trigger('user/' + user.id + "/joinSuccess", options);
|
||||
Nc.trigger('user/joined', user);
|
||||
};
|
||||
|
||||
Channel.prototype.onReleaseUser = function (userId) {
|
||||
var user = this.users[userId];
|
||||
this.broadcastControlCommandExcept("userLeft", user.id, user);
|
||||
Nc.trigger('user/left', user);
|
||||
delete this.users[user.id];
|
||||
}
|
||||
|
||||
|
||||
// Sending commands
|
||||
|
||||
Channel.prototype.broadcastControlCommand = function (command, options) {
|
||||
for(var id in this.users) {
|
||||
this.users[id].sendControlCommand(command, options);
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.broadcastControlCommandExcept = function (command, options, exceptUser) {
|
||||
for(var id in this.users) {
|
||||
if (id != exceptUser.id) {
|
||||
this.users[id].sendControlCommand(command, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.broadcastGameCommand = function (command, options) {
|
||||
for(var id in this.users) {
|
||||
this.users[id].sendGameCommand(command, options);
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.broadcastGameCommandExcept = function (command, options, exceptUser) {
|
||||
for(var id in this.users) {
|
||||
if (id != exceptUser.id) {
|
||||
this.users[id].sendGameCommand(command, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Channel;
|
||||
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue