add replay check in overview

This commit is contained in:
Zutatensuppe 2020-12-22 22:54:31 +01:00
parent 083fc0463c
commit 98c05304c5
4 changed files with 32 additions and 3 deletions

View file

@ -2,14 +2,36 @@ import fs from 'fs'
const DATA_DIR = './../data'
const filename = (gameId) => `${DATA_DIR}/log_${gameId}.log`
const create = (gameId) => {
const file = filename(gameId)
if (!fs.existsSync(file)) {
fs.appendFileSync(file, '')
}
}
const exists = (gameId) => {
const file = filename(gameId)
return fs.existsSync(file)
}
const log = (gameId, ...args) => {
const file = filename(gameId)
if (!fs.existsSync(file)) {
return
}
const str = JSON.stringify(args)
fs.appendFileSync(`${DATA_DIR}/log_${gameId}.log`, str + "\n")
fs.appendFileSync(file, str + "\n")
}
const get = (gameId) => {
const all = fs.readFileSync(`${DATA_DIR}/log_${gameId}.log`, 'utf-8')
return all.split("\n").filter(line => !!line).map((line) => {
const file = filename(gameId)
if (!fs.existsSync(file)) {
return []
}
const lines = fs.readFileSync(file, 'utf-8').split("\n")
return lines.filter(line => !!line).map((line) => {
try {
return JSON.parse(line)
} catch (e) {
@ -20,6 +42,8 @@ const get = (gameId) => {
}
export default {
create,
exists,
log,
get,
}