This commit is contained in:
Zutatensuppe 2021-06-09 09:52:58 +02:00
parent accd38eb02
commit 4b10fbc01b
2 changed files with 33 additions and 36 deletions

View file

@ -1276,13 +1276,11 @@ const idxname = (gameId) => `${DATA_DIR}/log_${gameId}.idx.log`;
const create = (gameId, ts) => {
const idxfile = idxname(gameId);
if (!fs.existsSync(idxfile)) {
const logfile = filename(gameId, 0);
fs.appendFileSync(logfile, "");
fs.appendFileSync(idxfile, JSON.stringify({
gameId: gameId,
total: 0,
lastTs: ts,
currentFile: logfile,
currentFile: '',
perFile: LINES_PER_LOG_FILE,
}));
}
@ -1296,21 +1294,21 @@ const _log = (gameId, type, ...args) => {
if (!fs.existsSync(idxfile)) {
return;
}
const ts = args[args.length - 1];
const otherArgs = args.slice(0, -1);
const idx = JSON.parse(fs.readFileSync(idxfile, 'utf-8'));
idx.total++;
const diff = ts - idx.lastTs;
idx.lastTs = ts;
const line = JSON.stringify([type, ...otherArgs, diff]).slice(1, -1);
fs.appendFileSync(idx.currentFile, line + "\n");
// prepare next log file
if (idx.total % idx.perFile === 0) {
const logfile = filename(gameId, idx.total);
fs.appendFileSync(logfile, "");
idx.currentFile = logfile;
const idxObj = JSON.parse(fs.readFileSync(idxfile, 'utf-8'));
if (idxObj.total % idxObj.perFile === 0) {
idxObj.currentFile = filename(gameId, idxObj.total);
}
fs.writeFileSync(idxfile, JSON.stringify(idx));
const tsIdx = type === Protocol.LOG_HEADER ? 3 : (args.length - 1);
const ts = args[tsIdx];
if (type !== Protocol.LOG_HEADER) {
// for everything but header save the diff to last log entry
args[tsIdx] = ts - idxObj.lastTs;
}
const line = JSON.stringify([type, ...args]).slice(1, -1);
fs.appendFileSync(idxObj.currentFile, line + "\n");
idxObj.total++;
idxObj.lastTs = ts;
fs.writeFileSync(idxfile, JSON.stringify(idxObj));
};
const get = (gameId, offset = 0) => {
const idxfile = idxname(gameId);