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