fixes #112 finished recorder

This commit is contained in:
Jeena 2015-03-15 21:06:44 +01:00
parent dfa71bc8e5
commit c88afc8b4c
11 changed files with 275 additions and 1765 deletions

View file

@ -9,6 +9,8 @@
function (GameController, Nc, User, ProtocolHelper, Options, Settings) {
"use strict";
function Channel (pipeToServer, options) {
var self = this;
@ -17,6 +19,7 @@
this.users = {};
this.pipeToServer = pipeToServer;
this.levelListIndex = -1;
this.gameController = null;
this.options = options = Options.merge(options, {
levelUids: Settings.CHANNEL_DEFAULT_LEVELS
@ -34,7 +37,7 @@
this.beginRound();
console.checkpoint('channel ' + this.name + ' created');
console.checkpoint("channel " + this.name + " created");
setTimeout(function() {
if(Object.keys(self.users).length < 1) {
@ -53,7 +56,7 @@
if(this.gameController) {
this.gameController.destroy();
delete this.gameController;
this.gameController = null;
}
var gameControllerOptions = {
@ -108,14 +111,14 @@
this.sendJoinSuccess(options);
this.users[options.id].sendControlCommand("beginRound", clientGameControllerOptions);
}
}
};
Channel.prototype.sendJoinSuccess = function(options) {
var user = new User(options.id, options);
var joinedUsers = [];
for(var userId in this.users) {
joinedUsers.push(this.users[userId].options)
joinedUsers.push(this.users[userId].options);
}
var levelUid = null;
@ -125,13 +128,13 @@
this.users[user.id] = user;
var options = {
options = {
user: user.options,
joinedUsers: joinedUsers,
levelUid: levelUid
};
//Nc.trigger('user/' + user.id + "/joinSuccess", options);
//Nc.trigger("user/" + user.id + "/joinSuccess", options);
user.sendControlCommand("joinSuccess", options);
Nc.trigger(Nc.ns.channel.events.user.joined, user);
@ -140,7 +143,6 @@
Channel.prototype.onReleaseUser = function (userId) {
var self = this;
var user = this.users[userId];
Nc.trigger(Nc.ns.channel.events.user.left, userId);
delete this.users[userId];
@ -158,7 +160,7 @@
}
}, Settings.CHANNEL_DESTRUCTION_TIME * 1000);
}
}
};
Channel.prototype.destroy = function() {
console.checkpoint("channel (" + this.name + ") destroyed");
@ -172,7 +174,7 @@
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) {
@ -180,13 +182,13 @@
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) {
@ -194,7 +196,7 @@
this.users[id].sendGameCommand(command, options);
}
}
}
};
return Channel;

View file

@ -47,9 +47,9 @@ function(Parent, Nc, Parser, Settings) {
PlayerController.prototype.mePositionStateUpdate = function(update) {
if(!this.player.doll) {
console.warn("me state update, even though doll does not exist");
return;
if(!this.player.isSpawned()) {
// if someone still falls but is dead on the server already
return;
}
var difference = {

View file

@ -107,11 +107,11 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
var update = this.getWorldUpdateObject(false);
if(Object.getOwnPropertyNames(update).length > 0) {
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, 'worldUpdate', update);
Nc.trigger(Nc.ns.channel.to.client.gameCommand.broadcast, "worldUpdate", update);
}
this.worldUpdateTimeout = setTimeout(this.updateWorld.bind(this), Settings.NETWORK_UPDATE_INTERVAL);
}
};
GameController.prototype.getWorldUpdateObject = function(getSleeping) {
getSleeping = getSleeping || false;
@ -149,7 +149,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
var spawnedPlayers = [];
for(var id in this.players) {
var player = this.players[id];
if(player.isSpawned) {
if(player.isSpawned()) {
var options = {
id: id,
@ -169,7 +169,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
};
GameController.prototype.getRuntimeItems = function() {
var objects = []
var objects = [];
for (var i = 0; i < this.gameObjects.animated.length; i++) {
if(this.gameObjects.animated[i] instanceof RagDoll) {
@ -182,7 +182,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
options: object.options
});
}
};
}
return objects;
};
@ -195,7 +195,7 @@ function (Parent, PhysicsEngine, Settings, PlayerController, requestAnimFrame, N
worldUpdate: this.getWorldUpdateObject(true),
runtimeItems: this.getRuntimeItems(),
userId: userId
}
};
Nc.trigger(Nc.ns.channel.to.client.user.gameCommand.send + userId, "clientReadyResponse", options);

View file

@ -2,11 +2,10 @@ define([
"Lib/Utilities/NotificationCenter",
"Game/Channel/Channel",
"Game/Config/Settings",
'fs',
'util',
"fs"
],
function (Nc, Channel, Settings, fs, util) {
function (Nc, Channel, Settings, fs) {
"use strict";
@ -17,32 +16,43 @@ function (Nc, Channel, Settings, fs, util) {
Nc.on(Nc.ns.channel.to.server.controlCommand.send, this.send, this);
process.on('message', this.onProcessMessage.bind(this));
process.on("message", this.onProcessMessage.bind(this));
}
PipeToServer.prototype.onProcessMessage = function (message, handle) {
PipeToServer.prototype.onProcessMessage = function (message, handle) { // jshint unused:false
if(message.data.hasOwnProperty('CREATE')) {
if(message.data.hasOwnProperty("CREATE")) {
this.channel = new Channel(this, message.data.options);
message.data.options.playingFileName = "Quickstart-1425229312283.log";
message.data.options.playingFileName = Settings.CHANNEL_PLAY_RECORDING;
if(message.data.options.playingFileName) {
var self = this;
setTimeout(function() {
console.log(message.data.options.playingFileName)
console.log(message.data.options.playingFileName);
self.play(message.data.options.playingFileName);
}, 2000);
}, 2000); // giving channel time to set everything up
}
if(Settings.CHANNEL_RECORD_SESSION) {
this.recordingFileName = Settings.CHANNEL_RECORDING_PATH + this.channel.name + "-" + Date.now() + ".log";
this.recordingFileName = Settings.CHANNEL_RECORDING_PATH +
this.channel.name +
"-" +
(new Date()).toISOString() +
"-" +
message.data.options.levelUids.join("_") +
".log";
if(!fs.existsSync(Settings.CHANNEL_RECORDING_PATH)) {
fs.mkdirSync(Settings.CHANNEL_RECORDING_PATH);
}
setInterval(this.recordWorldUpdate.bind(this), 1000);
console.checkpoint("Started recording to: " + this.recordingFileName);
}
} else if (message.data.hasOwnProperty('KILL')) {
} else if (message.data.hasOwnProperty("KILL")) {
this.channel.destroy();
} else {
this.onMessage(message);
@ -50,7 +60,7 @@ function (Nc, Channel, Settings, fs, util) {
if(Settings.CHANNEL_RECORD_SESSION && this.channel && this.channel.name) {
var m = JSON.stringify(message);
var timestamp = Date.now();
var line = timestamp + m + "\n";
var line = "m" + timestamp + m + "\n";
fs.appendFile(this.recordingFileName, line, function (err) {
if (err) throw err;
@ -64,14 +74,14 @@ function (Nc, Channel, Settings, fs, util) {
var message = {
recipient: recipient,
data: data
}
};
this.process.send(message);
};
PipeToServer.prototype.onMessage = function (message) {
switch(message.recipient) {
case 'channel':
case "channel":
Nc.trigger(Nc.ns.channel.events.controlCommand.channel, message);
break;
default:
@ -79,40 +89,59 @@ function (Nc, Channel, Settings, fs, util) {
break;
}
}
};
PipeToServer.prototype.play = function(playingFileName) {
var self = this;
var data = fs.readFileSync(Settings.CHANNEL_RECORDING_PATH + playingFileName);
var lines = data.toString().split("\n");
var now = Date.now();
var start = 0;
for (var i = 0; i < lines.length; i++) {
// bind message variable
(function() {
var line = lines[i];
if(line.length > 0) {
var time = parseInt(line.substring(0, Date.now().toString().length), 10);
if(i == 0) {
var type = line.substring(0, 1);
var time = parseInt(line.substring(1, Date.now().toString().length + 1), 10);
if(i === 0) {
start = time;
}
time -= start;
var jsonString = line.substring(Date.now().toString().length);
var jsonString = line.substring(Date.now().toString().length + 1);
var message = JSON.parse(jsonString);
setTimeout(function() {
console.log(" ");
console.log(util.inspect(message, { showHidden: true, depth: 4 }));
self.onProcessMessage(message, null);
console.log(line);
if(type == "m") {
self.onProcessMessage(message, null);
} else if(type == "w") {
if(self.channel.gameController) {
self.channel.gameController.onWorldUpdate(message);
}
}
}, time);
}
})();
};
}
};
PipeToServer.prototype.recordWorldUpdate = function() {
if(this.channel.gameController) {
var update = this.channel.gameController.getWorldUpdateObject(true);
var worldUpdate = JSON.stringify(update);
var timestamp = Date.now();
var line = "w" + timestamp + worldUpdate + "\n";
fs.appendFile(this.recordingFileName, line, function (err) {
if (err) throw err;
});
}
};
PipeToServer.prototype.destroy = function() {
this.send('coordinator', {destroy:this.channel.name});
this.send("coordinator", {destroy:this.channel.name});
this.process.exit(0);
};