From 98c05304c51ef85d453c9c3b8bd59049427eb479 Mon Sep 17 00:00:00 2001 From: Zutatensuppe Date: Tue, 22 Dec 2020 22:54:31 +0100 Subject: [PATCH] add replay check in overview --- game/index.js | 3 +++ server/Game.js | 1 + server/GameLog.js | 30 +++++++++++++++++++++++++++--- server/index.js | 1 + 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/game/index.js b/game/index.js index f9ef63e..b30bfe1 100644 --- a/game/index.js +++ b/game/index.js @@ -60,6 +60,9 @@ export default { {{time(g.started, g.finished)}}
+ + ↪️ Watch replay + diff --git a/server/Game.js b/server/Game.js index 97b10b5..cb72ba5 100644 --- a/server/Game.js +++ b/server/Game.js @@ -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) diff --git a/server/GameLog.js b/server/GameLog.js index 300fdec..a3c2b19 100644 --- a/server/GameLog.js +++ b/server/GameLog.js @@ -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, } diff --git a/server/index.js b/server/index.js index 1775ea5..4d30143 100644 --- a/server/index.js +++ b/server/index.js @@ -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),