2020-12-22 22:35:09 +01:00
|
|
|
import fs from 'fs'
|
2021-05-29 11:44:55 +02:00
|
|
|
import readline from 'readline'
|
|
|
|
|
import stream from 'stream'
|
2021-05-31 20:05:41 +02:00
|
|
|
import Time from '../common/Time'
|
|
|
|
|
import { Timestamp } from '../common/Types'
|
2021-05-17 01:12:39 +02:00
|
|
|
import { logger } from './../common/Util'
|
|
|
|
|
import { DATA_DIR } from './../server/Dirs'
|
2021-04-13 20:18:41 +02:00
|
|
|
|
|
|
|
|
const log = logger('GameLog.js')
|
2020-12-22 22:35:09 +01:00
|
|
|
|
2021-05-31 20:05:41 +02:00
|
|
|
const POST_GAME_LOG_DURATION = 5 * Time.MIN
|
|
|
|
|
|
|
|
|
|
const shouldLog = (finishTs: Timestamp, currentTs: Timestamp): boolean => {
|
|
|
|
|
// when not finished yet, always log
|
|
|
|
|
if (!finishTs) {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// in finished games, log max POST_GAME_LOG_DURATION after
|
|
|
|
|
// the game finished, to record winning dance moves etc :P
|
|
|
|
|
const timeSinceGameEnd = currentTs - finishTs
|
|
|
|
|
return timeSinceGameEnd <= POST_GAME_LOG_DURATION
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-17 00:27:47 +02:00
|
|
|
const filename = (gameId: string) => `${DATA_DIR}/log_${gameId}.log`
|
2020-12-22 22:54:31 +01:00
|
|
|
|
2021-05-29 15:36:03 +02:00
|
|
|
const create = (gameId: string): void => {
|
2020-12-22 22:54:31 +01:00
|
|
|
const file = filename(gameId)
|
|
|
|
|
if (!fs.existsSync(file)) {
|
|
|
|
|
fs.appendFileSync(file, '')
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 15:36:03 +02:00
|
|
|
const exists = (gameId: string): boolean => {
|
2020-12-22 22:54:31 +01:00
|
|
|
const file = filename(gameId)
|
|
|
|
|
return fs.existsSync(file)
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-29 15:36:03 +02:00
|
|
|
const _log = (gameId: string, ...args: Array<any>): void => {
|
2020-12-22 22:54:31 +01:00
|
|
|
const file = filename(gameId)
|
|
|
|
|
if (!fs.existsSync(file)) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2020-12-22 22:35:09 +01:00
|
|
|
const str = JSON.stringify(args)
|
2020-12-22 22:54:31 +01:00
|
|
|
fs.appendFileSync(file, str + "\n")
|
2020-12-22 22:35:09 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-29 11:44:55 +02:00
|
|
|
const get = async (
|
|
|
|
|
gameId: string,
|
|
|
|
|
offset: number = 0,
|
|
|
|
|
size: number = 10000
|
|
|
|
|
): Promise<any[]> => {
|
2020-12-22 22:54:31 +01:00
|
|
|
const file = filename(gameId)
|
|
|
|
|
if (!fs.existsSync(file)) {
|
|
|
|
|
return []
|
|
|
|
|
}
|
2021-05-29 11:44:55 +02:00
|
|
|
return new Promise((resolve) => {
|
|
|
|
|
const instream = fs.createReadStream(file)
|
|
|
|
|
const outstream = new stream.Writable()
|
|
|
|
|
const rl = readline.createInterface(instream, outstream)
|
|
|
|
|
const lines: any[] = []
|
|
|
|
|
let i = -1
|
|
|
|
|
rl.on('line', (line) => {
|
|
|
|
|
if (!line) {
|
|
|
|
|
// skip empty
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
i++
|
|
|
|
|
if (offset > i) {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if (offset + size <= i) {
|
|
|
|
|
rl.close()
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
lines.push(JSON.parse(line))
|
|
|
|
|
})
|
|
|
|
|
rl.on('close', () => {
|
|
|
|
|
resolve(lines)
|
|
|
|
|
})
|
2020-12-22 22:35:09 +01:00
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export default {
|
2021-05-31 20:05:41 +02:00
|
|
|
shouldLog,
|
2020-12-22 22:54:31 +01:00
|
|
|
create,
|
|
|
|
|
exists,
|
2021-04-13 20:18:41 +02:00
|
|
|
log: _log,
|
2020-12-22 22:35:09 +01:00
|
|
|
get,
|
|
|
|
|
}
|