mirror of
https://github.com/logsol/chuck.js.git
synced 2026-05-11 18:47:35 +00:00
first version of recorder
This commit is contained in:
parent
81f5990e0c
commit
5d70170ed9
4 changed files with 1725 additions and 17 deletions
|
|
@ -1,33 +1,63 @@
|
|||
define([
|
||||
"Lib/Utilities/NotificationCenter",
|
||||
"Game/Channel/Channel"
|
||||
"Game/Channel/Channel",
|
||||
"Game/Config/Settings",
|
||||
'fs'
|
||||
],
|
||||
|
||||
function (Nc, Channel) {
|
||||
function (Nc, Channel, Settings, fs) {
|
||||
|
||||
"use strict";
|
||||
|
||||
function PipeToServer (process) {
|
||||
|
||||
var self = this;
|
||||
|
||||
this.channel = null;
|
||||
this.process = process;
|
||||
this.recordingFileName = null;
|
||||
|
||||
Nc.on(Nc.ns.channel.to.server.controlCommand.send, this.send, this);
|
||||
|
||||
process.on('message', function (message, handle) {
|
||||
process.on('message', this.onProcessMessage.bind(this));
|
||||
}
|
||||
|
||||
if(message.data.hasOwnProperty('CREATE')) {
|
||||
self.channel = new Channel(self, message.data.options);
|
||||
} else if (message.data.hasOwnProperty('KILL')) {
|
||||
self.channel.destroy();
|
||||
} else {
|
||||
self.onMessage(message);
|
||||
PipeToServer.prototype.onProcessMessage = function (message, handle) {
|
||||
|
||||
if(message.data.hasOwnProperty('CREATE')) {
|
||||
this.channel = new Channel(this, message.data.options);
|
||||
|
||||
message.data.options.playingFileName = "Quickstart-1425229312283.log";
|
||||
|
||||
if(message.data.options.playingFileName) {
|
||||
var self = this;
|
||||
setTimeout(function() {
|
||||
console.log(message.data.options.playingFileName)
|
||||
self.play(message.data.options.playingFileName);
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
if(Settings.CHANNEL_RECORD_SESSION) {
|
||||
this.recordingFileName = Settings.CHANNEL_RECORDING_PATH + "/" + this.channel.name + "-" + Date.now() + ".log";
|
||||
if(!fs.existsSync(Settings.CHANNEL_RECORDING_PATH)) {
|
||||
fs.mkdirSync(Settings.CHANNEL_RECORDING_PATH);
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
}
|
||||
} else if (message.data.hasOwnProperty('KILL')) {
|
||||
this.channel.destroy();
|
||||
} else {
|
||||
this.onMessage(message);
|
||||
|
||||
if(Settings.CHANNEL_RECORD_SESSION && this.channel && this.channel.name) {
|
||||
var m = JSON.stringify(message);
|
||||
var timestamp = Date.now();
|
||||
var line = timestamp + m + "\n";
|
||||
|
||||
fs.appendFile(this.recordingFileName, line, function (err) {
|
||||
if (err) throw err;
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
PipeToServer.prototype.send = function (recipient, data) {
|
||||
var message = {
|
||||
|
|
@ -50,6 +80,34 @@ function (Nc, Channel) {
|
|||
|
||||
}
|
||||
|
||||
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) {
|
||||
start = time;
|
||||
}
|
||||
time -= start;
|
||||
|
||||
var jsonString = line.substring(Date.now().toString().length);
|
||||
var message = JSON.parse(jsonString);
|
||||
|
||||
setTimeout(function() {
|
||||
self.onProcessMessage(message, null);
|
||||
}, time);
|
||||
}
|
||||
})();
|
||||
};
|
||||
};
|
||||
|
||||
PipeToServer.prototype.destroy = function() {
|
||||
this.send('coordinator', {destroy:this.channel.name});
|
||||
this.process.exit(0);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue