diff --git a/server/Game.js b/server/Game.js index a25d582..18da51d 100644 --- a/server/Game.js +++ b/server/Game.js @@ -3,16 +3,6 @@ import { createPuzzle } from './Puzzle.js' import GameCommon from './../common/GameCommon.js' import Util from './../common/Util.js' -async function createGame(gameId, targetTiles, image) { - GameCommon.newGame({ - id: gameId, - puzzle: await createPuzzle(targetTiles, image), - players: {}, - sockets: [], - evtInfos: {}, - }) -} - const DATA_DIR = './../data' function loadAllGames() { @@ -46,34 +36,64 @@ function loadAllGames() { } } -function persistAll() { +const changedGames = {} +async function createGame(gameId, targetTiles, image) { + GameCommon.newGame({ + id: gameId, + puzzle: await createPuzzle(targetTiles, image), + players: {}, + sockets: [], + evtInfos: {}, + }) + changedGames[gameId] = true +} + +function addPlayer(gameId, playerId) { + GameCommon.addPlayer(gameId, playerId) + changedGames[gameId] = true +} + +function addSocket(gameId, socket) { + GameCommon.addSocket(gameId, socket) + changedGames[gameId] = true +} + +function handleInput(gameId, playerId, input) { + const ret = GameCommon.handleInput(gameId, playerId, input) + changedGames[gameId] = true + return ret +} + +function persistChangedGames() { for (const game of GameCommon.getAllGames()) { - fs.writeFileSync(`${DATA_DIR}/${game.id}.json`, JSON.stringify({ - id: game.id, - puzzle: game.puzzle, - players: game.players, - })) + if (game.id in changedGames) { + fs.writeFileSync(`${DATA_DIR}/${game.id}.json`, JSON.stringify({ + id: game.id, + puzzle: game.puzzle, + players: game.players, + })) + } } } export default { loadAllGames, - persistAll, + persistChangedGames, createGame, + addPlayer, + addSocket, + handleInput, getAllGames: GameCommon.getAllGames, getActivePlayers: GameCommon.getActivePlayers, getFinishedTileCount: GameCommon.getFinishedTileCount, getImageUrl: GameCommon.getImageUrl, getTileCount: GameCommon.getTileCount, exists: GameCommon.exists, - addPlayer: GameCommon.addPlayer, playerExists: GameCommon.playerExists, - addSocket: GameCommon.addSocket, socketExists: GameCommon.socketExists, removeSocket: GameCommon.removeSocket, get: GameCommon.get, getSockets: GameCommon.getSockets, - handleInput: GameCommon.handleInput, getStartTs: GameCommon.getStartTs, getFinishTs: GameCommon.getFinishTs, } diff --git a/server/index.js b/server/index.js index 8b315ab..4c9240c 100644 --- a/server/index.js +++ b/server/index.js @@ -181,7 +181,7 @@ wss.listen() // persist games in fixed interval const persistInterval = setInterval(() => { console.log('Persisting games...'); - Game.persistAll() + Game.persistChangedGames() }, config.persistence.interval) const gracefulShutdown = (signal) => { @@ -191,7 +191,7 @@ const gracefulShutdown = (signal) => { clearInterval(persistInterval) console.log('persisting games...') - Game.persistAll() + Game.persistChangedGames() console.log('shutting down webserver...') server.close()