puzzle/server/Game.js

141 lines
3.7 KiB
JavaScript
Raw Normal View History

2020-11-25 22:03:35 +01:00
import fs from 'fs'
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-12-21 18:34:57 +01:00
import { Rng } from '../common/Rng.js'
2020-12-22 22:35:09 +01:00
import GameLog from './GameLog.js'
import { createPuzzle } from './Puzzle.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-12-21 18:34:57 +01:00
rng: {
type: game.rng ? game.rng.type : '_fake_',
obj: game.rng ? Rng.unserialize(game.rng.obj) : new Rng(),
},
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 = {}
2020-12-22 22:35:09 +01:00
async function createGameObject(gameId, targetTiles, image, ts) {
const seed = Util.hash(gameId + ' ' + ts)
const rng = new Rng(seed)
return GameCommon.__createGameObject(
gameId,
{
type: 'Rng',
obj: rng,
},
await createPuzzle(rng, targetTiles, image, ts),
{},
[],
{}
)
}
async function createGame(gameId, targetTiles, image, ts) {
GameLog.log(gameId, 'createGame', targetTiles, image, ts)
const seed = Util.hash(gameId + ' ' + ts)
const rng = new Rng(seed)
2020-12-13 00:11:42 +01:00
GameCommon.newGame({
id: gameId,
2020-12-21 18:34:57 +01:00
rng: {
type: 'Rng',
obj: rng,
},
2020-12-22 22:35:09 +01:00
puzzle: await createPuzzle(rng, targetTiles, image, ts),
2020-12-13 00:11:42 +01:00
players: {},
sockets: [],
evtInfos: {},
})
2020-12-22 22:35:09 +01:00
2020-12-13 00:11:42 +01:00
changedGames[gameId] = true
}
2020-12-22 22:35:09 +01:00
function addPlayer(gameId, playerId, ts) {
GameLog.log(gameId, 'addPlayer', playerId, ts)
GameCommon.addPlayer(gameId, playerId, ts)
2020-12-13 00:11:42 +01:00
changedGames[gameId] = true
}
function addSocket(gameId, socket) {
GameCommon.addSocket(gameId, socket)
changedGames[gameId] = true
}
2020-12-22 22:35:09 +01:00
function handleInput(gameId, playerId, input, ts) {
GameLog.log(gameId, 'handleInput', playerId, input, ts)
const ret = GameCommon.handleInput(gameId, playerId, input, ts)
2020-12-13 00:11:42 +01:00
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) {
2020-12-13 01:09:12 +01:00
delete changedGames[game.id]
2020-12-13 00:11:42 +01:00
fs.writeFileSync(`${DATA_DIR}/${game.id}.json`, JSON.stringify({
id: game.id,
2020-12-21 18:34:57 +01:00
rng: {
type: game.rng.type,
obj: Rng.serialize(game.rng.obj),
},
2020-12-13 00:11:42 +01:00
puzzle: game.puzzle,
players: game.players,
}))
2020-12-13 14:46:11 +01:00
console.info(`[INFO] persisted game ${game.id}`)
2020-12-13 00:11:42 +01:00
}
}
2020-11-25 22:03:35 +01:00
}
2020-11-12 19:19:02 +01:00
export default {
2020-12-22 22:35:09 +01:00
createGameObject,
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,
2020-12-21 02:29:14 +01:00
getRelevantPlayers: GameCommon.getRelevantPlayers,
2020-12-05 19:45:34 +01:00
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
}