smaller logs

This commit is contained in:
Zutatensuppe 2021-06-05 23:02:04 +02:00
parent 849d39dac2
commit 60ae6e8a08
8 changed files with 97 additions and 164 deletions

View file

@ -590,6 +590,7 @@ export async function main(
return false
}
let GAME_TS = REPLAY.lastGameTs
const next = async () => {
if (REPLAY.logPointer + 1 >= REPLAY.log.length) {
await queryNextReplayBatch(gameId)
@ -614,18 +615,20 @@ export async function main(
}
const currLogEntry = REPLAY.log[REPLAY.logPointer]
const currTs: Timestamp = REPLAY.gameStartTs + currLogEntry[currLogEntry.length - 1]
const currTs: Timestamp = GAME_TS + currLogEntry[currLogEntry.length - 1]
const nextLogEntry = REPLAY.log[nextIdx]
const nextTs: Timestamp = REPLAY.gameStartTs + nextLogEntry[nextLogEntry.length - 1]
const diffToNext = nextLogEntry[nextLogEntry.length - 1]
const nextTs: Timestamp = currTs + diffToNext
if (nextTs > maxGameTs) {
// next log entry is too far into the future
if (REPLAY.skipNonActionPhases && (maxGameTs + 500 * Time.MS < nextTs)) {
const skipInterval = nextTs - currTs
maxGameTs += skipInterval
maxGameTs += diffToNext
}
break
}
GAME_TS = currTs
if (handleLogEntry(nextLogEntry, nextTs)) {
RERENDER = true
}

View file

@ -71,11 +71,10 @@ async function createGame(
function addPlayer(gameId: string, playerId: string, ts: Timestamp): void {
if (GameLog.shouldLog(GameCommon.getFinishTs(gameId), ts)) {
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
const diff = ts - GameCommon.getStartTs(gameId)
if (idx === -1) {
GameLog.log(gameId, Protocol.LOG_ADD_PLAYER, playerId, diff)
GameLog.log(gameId, Protocol.LOG_ADD_PLAYER, playerId, ts)
} else {
GameLog.log(gameId, Protocol.LOG_UPDATE_PLAYER, idx, diff)
GameLog.log(gameId, Protocol.LOG_UPDATE_PLAYER, idx, ts)
}
}
@ -91,8 +90,7 @@ function handleInput(
): Array<Change> {
if (GameLog.shouldLog(GameCommon.getFinishTs(gameId), ts)) {
const idx = GameCommon.getPlayerIndexById(gameId, playerId)
const diff = ts - GameCommon.getStartTs(gameId)
GameLog.log(gameId, Protocol.LOG_HANDLE_INPUT, idx, input, diff)
GameLog.log(gameId, Protocol.LOG_HANDLE_INPUT, idx, input, ts)
}
const ret = GameCommon.handleInput(gameId, playerId, input, ts)

View file

@ -23,8 +23,8 @@ const shouldLog = (finishTs: Timestamp, currentTs: Timestamp): boolean => {
return timeSinceGameEnd <= POST_GAME_LOG_DURATION
}
const filename = (gameId: string, offset: number) => `${DATA_DIR}/log_${gameId}-${offset}.log`
const idxname = (gameId: string) => `${DATA_DIR}/log_${gameId}.idx.log`
export const filename = (gameId: string, offset: number) => `${DATA_DIR}/log_${gameId}-${offset}.log`
export const idxname = (gameId: string) => `${DATA_DIR}/log_${gameId}.idx.log`
const create = (gameId: string): void => {
const idxfile = idxname(gameId)
@ -32,7 +32,9 @@ const create = (gameId: string): void => {
const logfile = filename(gameId, 0)
fs.appendFileSync(logfile, "")
fs.appendFileSync(idxfile, JSON.stringify({
gameId: gameId,
total: 0,
lastTs: 0,
currentFile: logfile,
perFile: LINES_PER_LOG_FILE,
}))
@ -44,15 +46,21 @@ const exists = (gameId: string): boolean => {
return fs.existsSync(idxfile)
}
const _log = (gameId: string, ...args: Array<any>): void => {
const _log = (gameId: string, type: number, ...args: Array<any>): void => {
const idxfile = idxname(gameId)
if (!fs.existsSync(idxfile)) {
return
}
const ts: Timestamp = args[args.length - 1]
const otherArgs: any[] = args.slice(0, -1)
const idx = JSON.parse(fs.readFileSync(idxfile, 'utf-8'))
idx.total++
fs.appendFileSync(idx.currentFile, JSON.stringify(args) + "\n")
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) {
@ -79,7 +87,7 @@ const get = (
const log = fs.readFileSync(file, 'utf-8').split("\n")
return log.filter(line => !!line).map(line => {
return JSON.parse(line)
return JSON.parse(`[${line}]`)
})
}