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

@ -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);
};