persist only changed games
This commit is contained in:
parent
1ac9be4307
commit
6676dcc933
2 changed files with 42 additions and 22 deletions
|
|
@ -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,8 +36,37 @@ 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()) {
|
||||
if (game.id in changedGames) {
|
||||
fs.writeFileSync(`${DATA_DIR}/${game.id}.json`, JSON.stringify({
|
||||
id: game.id,
|
||||
puzzle: game.puzzle,
|
||||
|
|
@ -55,25 +74,26 @@ function persistAll() {
|
|||
}))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue