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