persist games in fixed intervals and on shutdown

This commit is contained in:
Zutatensuppe 2020-12-04 00:04:47 +01:00
parent 5160008c89
commit 996822b046
5 changed files with 88 additions and 36 deletions

View file

@ -30,18 +30,21 @@ function loadAllGames() {
}
}
function store(gameId) {
const game = GameCommon.get(gameId)
fs.writeFileSync('./../data/' + gameId + '.json', JSON.stringify({
puzzle: game.puzzle,
players: game.players,
}))
function persistAll() {
const games = GameCommon.getAllGames()
for (const gameId of Object.keys(games)) {
const game = games[gameId]
fs.writeFileSync('./../data/' + gameId + '.json', JSON.stringify({
puzzle: game.puzzle,
players: game.players,
}))
}
}
export default {
loadAllGames,
getAllGames: GameCommon.getAllGames,
store,
persistAll,
createGame,
exists: GameCommon.exists,
addPlayer: GameCommon.addPlayer,