mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
68 lines
No EOL
1.3 KiB
JavaScript
68 lines
No EOL
1.3 KiB
JavaScript
define(["Protocol/Helper"], function(ProtocolHelper) {
|
|
|
|
function User(socketLink, coordinator) {
|
|
|
|
this.id = socketLink.id;
|
|
this.socketLink = socketLink;
|
|
this.coordinator = coordinator;
|
|
this.channel = null;
|
|
|
|
this.init(socketLink);
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
User.prototype.sendCommand = function(command, options) {
|
|
var message = ProtocolHelper.encodeCommand(command, options);
|
|
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':
|
|
this.channel.processGameCommandFromUser(options, this);
|
|
break;
|
|
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
return User;
|
|
|
|
}); |