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 GameCommon from './../common/GameCommon.js'
|
||||||
import Util from './../common/Util.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'
|
const DATA_DIR = './../data'
|
||||||
|
|
||||||
function loadAllGames() {
|
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()) {
|
for (const game of GameCommon.getAllGames()) {
|
||||||
|
if (game.id in changedGames) {
|
||||||
fs.writeFileSync(`${DATA_DIR}/${game.id}.json`, JSON.stringify({
|
fs.writeFileSync(`${DATA_DIR}/${game.id}.json`, JSON.stringify({
|
||||||
id: game.id,
|
id: game.id,
|
||||||
puzzle: game.puzzle,
|
puzzle: game.puzzle,
|
||||||
|
|
@ -55,25 +74,26 @@ function persistAll() {
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
loadAllGames,
|
loadAllGames,
|
||||||
persistAll,
|
persistChangedGames,
|
||||||
createGame,
|
createGame,
|
||||||
|
addPlayer,
|
||||||
|
addSocket,
|
||||||
|
handleInput,
|
||||||
getAllGames: GameCommon.getAllGames,
|
getAllGames: GameCommon.getAllGames,
|
||||||
getActivePlayers: GameCommon.getActivePlayers,
|
getActivePlayers: GameCommon.getActivePlayers,
|
||||||
getFinishedTileCount: GameCommon.getFinishedTileCount,
|
getFinishedTileCount: GameCommon.getFinishedTileCount,
|
||||||
getImageUrl: GameCommon.getImageUrl,
|
getImageUrl: GameCommon.getImageUrl,
|
||||||
getTileCount: GameCommon.getTileCount,
|
getTileCount: GameCommon.getTileCount,
|
||||||
exists: GameCommon.exists,
|
exists: GameCommon.exists,
|
||||||
addPlayer: GameCommon.addPlayer,
|
|
||||||
playerExists: GameCommon.playerExists,
|
playerExists: GameCommon.playerExists,
|
||||||
addSocket: GameCommon.addSocket,
|
|
||||||
socketExists: GameCommon.socketExists,
|
socketExists: GameCommon.socketExists,
|
||||||
removeSocket: GameCommon.removeSocket,
|
removeSocket: GameCommon.removeSocket,
|
||||||
get: GameCommon.get,
|
get: GameCommon.get,
|
||||||
getSockets: GameCommon.getSockets,
|
getSockets: GameCommon.getSockets,
|
||||||
handleInput: GameCommon.handleInput,
|
|
||||||
getStartTs: GameCommon.getStartTs,
|
getStartTs: GameCommon.getStartTs,
|
||||||
getFinishTs: GameCommon.getFinishTs,
|
getFinishTs: GameCommon.getFinishTs,
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -181,7 +181,7 @@ wss.listen()
|
||||||
// persist games in fixed interval
|
// persist games in fixed interval
|
||||||
const persistInterval = setInterval(() => {
|
const persistInterval = setInterval(() => {
|
||||||
console.log('Persisting games...');
|
console.log('Persisting games...');
|
||||||
Game.persistAll()
|
Game.persistChangedGames()
|
||||||
}, config.persistence.interval)
|
}, config.persistence.interval)
|
||||||
|
|
||||||
const gracefulShutdown = (signal) => {
|
const gracefulShutdown = (signal) => {
|
||||||
|
|
@ -191,7 +191,7 @@ const gracefulShutdown = (signal) => {
|
||||||
clearInterval(persistInterval)
|
clearInterval(persistInterval)
|
||||||
|
|
||||||
console.log('persisting games...')
|
console.log('persisting games...')
|
||||||
Game.persistAll()
|
Game.persistChangedGames()
|
||||||
|
|
||||||
console.log('shutting down webserver...')
|
console.log('shutting down webserver...')
|
||||||
server.close()
|
server.close()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue