mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 10:37:34 +00:00
fixed client removal, joining, etc.
This commit is contained in:
parent
2183e70bd8
commit
69fd43a900
6 changed files with 69 additions and 50 deletions
|
|
@ -21,8 +21,7 @@ function (ProtocolHelper, GameController) {
|
|||
});
|
||||
|
||||
this.socketLink.on('message', function (message) {
|
||||
//self.onMessage(message);
|
||||
console.log('Message from server: ', message);
|
||||
self.onMessage(message);
|
||||
});
|
||||
|
||||
this.socketLink.on('disconnect', function () {
|
||||
|
|
@ -49,11 +48,13 @@ function (ProtocolHelper, GameController) {
|
|||
|
||||
|
||||
Networker.prototype.onJoinSuccess = function (options) {
|
||||
this.gameController = new GameController();
|
||||
this.gameController.loadLevel("default.json");
|
||||
/*
|
||||
console.log("Joined " + options.channelName);
|
||||
//this.gameController = new GameController();
|
||||
//this.gameController.loadLevel("default.json");
|
||||
|
||||
|
||||
|
||||
console.log("Joined " + options.channelName);
|
||||
/*
|
||||
if (options.userIds && options.userIds.length > 0) {
|
||||
for(var i = 0; i < options.userIds.length; i++) {
|
||||
this.gameController.userJoined(options.userIds[i])
|
||||
|
|
@ -67,7 +68,7 @@ function (ProtocolHelper, GameController) {
|
|||
this.socketLink.send(message);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
Networker.prototype.onMessage = function (message) {
|
||||
var self = this;
|
||||
|
||||
|
|
@ -78,19 +79,21 @@ function (ProtocolHelper, GameController) {
|
|||
}
|
||||
|
||||
Networker.prototype.onUserJoined = function (userId) {
|
||||
this.gameController.userJoined(userId);
|
||||
//this.gameController.userJoined(userId);
|
||||
console.log("User " + userId + " joined");
|
||||
}
|
||||
|
||||
/*
|
||||
Networker.prototype.sendGameCommand = function (command, options) {
|
||||
this.sendCommand('gameCommand', ProtocolHelper.assemble(command, options));
|
||||
}
|
||||
|
||||
*/
|
||||
Networker.prototype.onUserLeft = function (userId) {
|
||||
this.gameController.userLeft(userId);
|
||||
//this.gameController.userLeft(userId);
|
||||
console.log("User " + userId + " left");
|
||||
}
|
||||
|
||||
Networker.prototype.processControlCommand = function (command, options) {
|
||||
|
||||
switch(command) {
|
||||
case 'joinSuccess':
|
||||
this.onJoinSuccess(options);
|
||||
|
|
@ -98,7 +101,7 @@ function (ProtocolHelper, GameController) {
|
|||
|
||||
case 'gameCommand':
|
||||
for(var gameCommand in options) {
|
||||
this.gameController.processGameCommand(gameCommand, options[gameCommand]);
|
||||
//this.gameController.processGameCommand(gameCommand, options[gameCommand]);
|
||||
}
|
||||
break;
|
||||
|
||||
|
|
@ -114,7 +117,7 @@ function (ProtocolHelper, GameController) {
|
|||
break;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
return Networker;
|
||||
|
||||
});
|
||||
|
|
@ -17,7 +17,12 @@ function (Parser) {
|
|||
}
|
||||
|
||||
Helper.runCommands = function (message, callback) {
|
||||
var commands = Parser.decode(message);
|
||||
var commands;
|
||||
if (typeof message == "string") {
|
||||
commands = Parser.decode(message);
|
||||
} else {
|
||||
commands = message;
|
||||
}
|
||||
|
||||
for(var command in commands) {
|
||||
callback(command, commands[command]);
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
define([
|
||||
"Game/Server/GameController",
|
||||
"Game/Core/NotificationCenter",
|
||||
"Game/Server/User"
|
||||
"Game/Server/User",
|
||||
"Game/Core/Protocol/Helper"
|
||||
],
|
||||
|
||||
function (GameController, NotificationCenter, User) {
|
||||
function (GameController, NotificationCenter, User, ProtocolHelper) {
|
||||
|
||||
function Channel (pipeToLobby, name) {
|
||||
|
||||
|
|
@ -37,19 +38,31 @@
|
|||
|
||||
switch(message.recipient) {
|
||||
case 'user':
|
||||
self.forward(self.users[message.id], message.data);
|
||||
console.log(message);
|
||||
var user = self.users[message.id];
|
||||
ProtocolHelper.runCommands(message.data, function (command, options) {
|
||||
user[command].call(user, options);
|
||||
});
|
||||
break;
|
||||
|
||||
case 'id': // Do nothing, it is needed by the user
|
||||
break;
|
||||
|
||||
case 'channel':
|
||||
self.forward(self, message.data);
|
||||
ProtocolHelper.runCommands(message.data, function (command, options) {
|
||||
self[command].call(self, options);
|
||||
});
|
||||
break;
|
||||
|
||||
default:
|
||||
throw 'unknown recipient';
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
NotificationCenter.on('channel/users/all', this.sendControlCommandToAllUsers, this);
|
||||
NotificationCenter.on('channel/users/all/except', this.sendControlCommandToAllUsersExcept, this);
|
||||
|
||||
console.checkpoint('channel ' + name + ' created');
|
||||
}
|
||||
|
||||
|
|
@ -57,49 +70,36 @@
|
|||
return true;
|
||||
}
|
||||
|
||||
Channel.prototype.forward = function (target, message) {
|
||||
for(var command in message) {
|
||||
if(typeof target[command] == 'function') {
|
||||
target[command].call(target, message[command]);
|
||||
} else {
|
||||
throw 'trying to call undefined function ' + target[command];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Channel.prototype.addUser = function (userId) {
|
||||
var user = new User(userId, this);
|
||||
this.users[user.id] = user;
|
||||
NotificationCenter.trigger('user/' + user.id + "/joinSuccess", {userId: user.id, channelName: this.name});
|
||||
NotificationCenter.trigger('user/joined', user);
|
||||
}
|
||||
/*
|
||||
Channel.prototype.send = function(recipient, message) {
|
||||
|
||||
this.pipeToLobby.send(recipient, message);
|
||||
}*/
|
||||
/*
|
||||
Channel.prototype.releaseUser = function (user) {
|
||||
this.gameController.userIdLeft(user.id);
|
||||
|
||||
this.sendCommandToAllUsersExcept("userLeft", user.id, user);
|
||||
Channel.prototype.releaseUser = function (userId) {
|
||||
var user = this.users[userId];
|
||||
//this.gameController.userIdLeft(user.id);
|
||||
|
||||
this.sendControlCommandToAllUsersExcept("userLeft", user.id, user);
|
||||
delete this.users[user.id];
|
||||
}
|
||||
|
||||
Channel.prototype.sendCommandToAllUsers = function (command, options) {
|
||||
Channel.prototype.sendControlCommandToAllUsers = function (command, options) {
|
||||
for(var id in this.users) {
|
||||
this.users[id].sendCommand(command, options);
|
||||
this.users[id].sendControlCommand(command, options);
|
||||
}
|
||||
}
|
||||
|
||||
Channel.prototype.sendCommandToAllUsersExcept = function (command, options, except_user) {
|
||||
Channel.prototype.sendControlCommandToAllUsersExcept = function (command, options, exceptUser) {
|
||||
for(var id in this.users) {
|
||||
if (id != except_user.id) {
|
||||
this.users[id].sendCommand(command, options);
|
||||
if (id != exceptUser.id) {
|
||||
this.users[id].sendControlCommand(command, options);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
Channel.prototype.processGameCommandFromUser = function (command, options, user) {
|
||||
this.gameController.progressGameCommandFromUser(command, options, user);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ function (NotificationCenter, Channel) {
|
|||
this.channel = null;
|
||||
this.process = process;
|
||||
|
||||
NotificationCenter.on('net/send', this.send, this);
|
||||
NotificationCenter.on('process/message', this.send, this);
|
||||
|
||||
process.on('message', function (message, handle) {
|
||||
|
||||
|
|
|
|||
|
|
@ -13,17 +13,28 @@ function(Parent, NotificationCenter, ProtocolHelper) {
|
|||
var self = this;
|
||||
|
||||
NotificationCenter.on('user/joined', function(user) {
|
||||
self.sendCommand("joined", true);
|
||||
if(user.id != self.id) {
|
||||
self.sendControlCommand("userJoined", user.id);
|
||||
}
|
||||
});
|
||||
|
||||
NotificationCenter.on('user/' + this.id + "/joinSuccess", function(options) {
|
||||
self.sendControlCommand("joinSuccess", options);
|
||||
});
|
||||
}
|
||||
|
||||
User.prototype = Object.create(Parent.prototype);
|
||||
|
||||
User.prototype.sendCommand = function(command, options) {
|
||||
User.prototype.sendControlCommand = function(command, options) {
|
||||
var recipient = "user/" + this.id;
|
||||
var data = ProtocolHelper.encodeCommand(command, options);
|
||||
|
||||
NotificationCenter.trigger("net/send", recipient, data);
|
||||
NotificationCenter.trigger("process/message", recipient, data);
|
||||
};
|
||||
|
||||
User.prototype.sendGameCommand = function(command, options) {
|
||||
var data = ProtocolHelper.encodeCommand(command, options);
|
||||
this.sendControlCommand("gameCommand", data);
|
||||
};
|
||||
|
||||
return User;
|
||||
|
|
|
|||
|
|
@ -53,7 +53,7 @@ function (User, Channel, PipeToChannel, NotificationCenter) {
|
|||
|
||||
Coordinator.prototype.removeUser = function (user) {
|
||||
|
||||
//user.channel.send('user/' + user.id + '/left');
|
||||
NotificationCenter.trigger('user/left', user);
|
||||
//NotificationCenter.off('channel/' + user.channel.channelName + '/user/' + user.id);
|
||||
|
||||
delete this.lobbyUsers[user.id];
|
||||
|
|
@ -82,7 +82,7 @@ function (User, Channel, PipeToChannel, NotificationCenter) {
|
|||
}, this);
|
||||
|
||||
NotificationCenter.on('user/left', function (user) {
|
||||
|
||||
channelPipe.send('channel', { releaseUser: user.id });
|
||||
}, this);
|
||||
|
||||
return channelPipe;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue