This commit is contained in:
Jeena 2014-03-01 23:11:36 +01:00
parent d83376d5c7
commit 810a74a28b
13 changed files with 445 additions and 281 deletions

View file

@ -9,7 +9,7 @@
function (GameController, Nc, User, ProtocolHelper, Options, Settings) {
function Channel (pipeToServer, name, options) {
function Channel (pipeToServer, options) {
var self = this;
@ -17,7 +17,7 @@
levelUids: Settings.DEFAULT_LEVELS
});
this.name = name;
this.name = options.channelName;
this.users = {};
this.pipeToServer = pipeToServer;
@ -34,11 +34,13 @@
Nc.on('broadcastGameCommand', this.broadcastGameCommand, this);
Nc.on('broadcastGameCommandExcept', this.broadcastGameCommandExcept, this);
console.checkpoint('channel ' + name + ' created');
}
console.checkpoint('channel ' + this.name + ' created');
Channel.validateName = function (name) {
return true;
setTimeout(function() {
if(Object.keys(self.users).length < 1) {
self.destroy();
}
}, Settings.CHANNEL_DESTRUCTION_TIME * 1000);
}
@ -81,11 +83,22 @@
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];
Nc.trigger('user/left', userId);
delete this.users[userId];
this.broadcastControlCommand("userLeft", userId);
// FIXME: if this was the last user terminate forked process
if(Object.keys(this.users).length < 1) {
this.destroy();
}
}
Channel.prototype.destroy = function() {
console.checkpoint("channel (" + this.name + ") destroyed");
this.pipeToServer.destroy();
};
// Sending commands

View file

@ -21,7 +21,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
Parent.call(this);
Nc.on('user/joined', this.onUserJoined, this);
Nc.on('user/left', this.onUserLeft, this); // FIXME: refactor this.userLeft -> this.onUserLeft, even in core and client
Nc.on('user/left', this.onUserLeft, this);
Nc.on('user/resetLevel', this.onResetLevel, this);
Nc.on('user/clientReady', this.onClientReady, this);
Nc.on('player/killed', this.onPlayerKilled, this);

View file

@ -17,10 +17,9 @@ function (Nc, Channel) {
process.on('message', function (message, handle) {
if(message.data.hasOwnProperty('CREATE')) {
self.channel = new Channel(this, message.data['CREATE']);
self.channel = new Channel(self, message.data.options);
} else if (message.data.hasOwnProperty('KILL')) {
self.channel.destroy();
process.exit(0);
} else {
self.onMessage(message);
}
@ -41,6 +40,11 @@ function (Nc, Channel) {
Nc.trigger(message.recipient + '/controlCommand', message);
}
PipeToServer.prototype.destroy = function() {
this.send('coordinator', {destroy:this.channel.name});
this.process.exit(0);
};
return PipeToServer;
});

View file

@ -20,7 +20,8 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
var self = this;
this.socketLink.on('message', function (message) {
if(Settings.NETWORK_LOG_INCOMING) {
var m = JSON.parse(message)
if(Settings.NETWORK_LOG_INCOMING && !m.gameCommand) {
console.log('INCOMING', message);
}
ProtocolHelper.applyCommand(message, self);
@ -35,8 +36,13 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
Networker.prototype.onConnect = function () {
console.log('connected.')
var channel = JSON.parse(localStorage["channel"]);
var player = JSON.parse(localStorage["player"]);
if(channel.name) {
this.sendCommand('join', channel.name);
var options = {
channelName: channel.name,
nickname: player.nickname
}
this.sendCommand('join', options);
} else {
window.location.href = "/";
}
@ -67,6 +73,11 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
this.initPing();
}
Networker.prototype.onJoinError = function(options) {
alert(options.message);
window.location.href = "/";
};
Networker.prototype.onLevelLoaded = function() {
for (var userId in this.users) {
this.gameController.createPlayer(this.users[userId]);
@ -116,8 +127,7 @@ function (ProtocolHelper, GameController, User, Nc, Settings, DomController) {
}
Networker.prototype.onUserLeft = function (userId) {
var user = this.users[userId];
this.gameController.onUserLeft(user);
this.gameController.onUserLeft(userId);
delete this.users[userId];
}

View file

@ -62,6 +62,7 @@ define(function() {
// NETWORKING
WORLD_UPDATE_BROADCAST_INTERVAL: 70,
CHANNEL_DESTRUCTION_TIME: 30,
NETWORK_LOG_INCOMING: false,
NETWORK_LOG_OUTGOING: false
}

View file

@ -79,14 +79,18 @@ function (PhysicsEngine, TiledLevel, Player, Nc) {
}
*/
GameController.prototype.onUserLeft = function (user) {
var player = this.players[user.id];
GameController.prototype.onUserLeft = function (userId) {
var player = this.players[userId];
if(!player) {
console.warn("User (", userId ,") left who has not joined");
return;
}
var i = this.gameObjects.animated.indexOf(player);
if(i>=0) this.gameObjects.animated.splice(i, 1);
player.destroy();
delete this.players[user.id];
delete this.players[userId];
}
GameController.prototype.createPlayer = function(user) {