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

@ -59,6 +59,7 @@ async function createGameObject(gameId, targetTiles, image, ts) {
)
}
async function createGame(gameId, targetTiles, image, ts) {
GameLog.create(gameId)
GameLog.log(gameId, 'createGame', targetTiles, image, ts)
const seed = Util.hash(gameId + ' ' + ts)

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,
}

View file

@ -91,6 +91,7 @@ app.use('/', async (req, res, next) => {
const games = [
...Game.getAllGames().map(game => ({
id: game.id,
hasReplay: GameLog.exists(game.id),
started: Game.getStartTs(game.id),
finished: Game.getFinishTs(game.id),
tilesFinished: Game.getFinishedTileCount(game.id),