puzzle/server/Game.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

2020-11-25 22:03:35 +01:00
import fs from 'fs'
2020-11-12 19:19:02 +01:00
import { createPuzzle } from './Puzzle.js'
2020-11-17 22:34:15 +01:00
import GameCommon from './../common/GameCommon.js'
2020-12-07 01:16:12 +01:00
import Util from './../common/Util.js'
2020-11-12 19:19:02 +01:00
const DATA_DIR = './../data'
2020-11-25 22:03:35 +01:00
function loadAllGames() {
const files = fs.readdirSync(DATA_DIR)
2020-11-25 22:03:35 +01:00
for (const f of files) {
if (!f.match(/\.json$/)) {
continue
}
const file = `${DATA_DIR}/${f}`
const contents = fs.readFileSync(file, 'utf-8')
2020-12-09 01:27:47 +01:00
let game
try {
game = JSON.parse(contents)
} catch {
console.log(`[ERR] unable to load game from file ${f}`);
}
2020-12-07 01:16:12 +01:00
if (typeof game.puzzle.data.started === 'undefined') {
game.puzzle.data.started = Math.round(fs.statSync(file).ctimeMs)
2020-12-07 01:16:12 +01:00
}
if (typeof game.puzzle.data.finished === 'undefined') {
let unfinished = game.puzzle.tiles.map(Util.decodeTile).find(t => t.owner !== -1)
game.puzzle.data.finished = unfinished ? 0 : Util.timestamp()
}
2020-12-05 19:45:34 +01:00
GameCommon.newGame({
id: game.id,
2020-11-25 22:03:35 +01:00
puzzle: game.puzzle,
players: game.players,
sockets: [],
2020-12-05 19:45:34 +01:00
evtInfos: {}
2020-11-25 22:03:35 +01:00
})
}
}
2020-12-13 00:11:42 +01:00
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() {
2020-12-05 19:45:34 +01:00
for (const game of GameCommon.getAllGames()) {
2020-12-13 00:11:42 +01:00
if (game.id in changedGames) {
fs.writeFileSync(`${DATA_DIR}/${game.id}.json`, JSON.stringify({
id: game.id,
puzzle: game.puzzle,
players: game.players,
}))
}
}
2020-11-25 22:03:35 +01:00
}
2020-11-12 19:19:02 +01:00
export default {
2020-11-25 22:03:35 +01:00
loadAllGames,
2020-12-13 00:11:42 +01:00
persistChangedGames,
2020-11-12 19:19:02 +01:00
createGame,
2020-12-13 00:11:42 +01:00
addPlayer,
addSocket,
handleInput,
2020-12-05 19:45:34 +01:00
getAllGames: GameCommon.getAllGames,
getActivePlayers: GameCommon.getActivePlayers,
getFinishedTileCount: GameCommon.getFinishedTileCount,
getImageUrl: GameCommon.getImageUrl,
getTileCount: GameCommon.getTileCount,
2020-11-17 22:34:15 +01:00
exists: GameCommon.exists,
2020-12-03 21:11:52 +01:00
playerExists: GameCommon.playerExists,
socketExists: GameCommon.socketExists,
2020-11-25 22:03:35 +01:00
removeSocket: GameCommon.removeSocket,
2020-11-17 22:34:15 +01:00
get: GameCommon.get,
getSockets: GameCommon.getSockets,
2020-12-07 02:38:07 +01:00
getStartTs: GameCommon.getStartTs,
getFinishTs: GameCommon.getFinishTs,
2020-11-12 19:19:02 +01:00
}