puzzle/server/Game.js

56 lines
1.4 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-11-12 19:19:02 +01:00
async function createGame(gameId, targetTiles, image) {
2020-11-17 22:34:15 +01:00
const game = {
2020-11-12 19:19:02 +01:00
puzzle: await createPuzzle(targetTiles, image),
players: {},
sockets: [],
evtInfos: {},
}
2020-11-17 22:34:15 +01:00
GameCommon.setGame(gameId, game)
2020-11-12 19:19:02 +01:00
}
2020-11-25 22:03:35 +01:00
function loadAllGames() {
const files = fs.readdirSync('./../data/')
for (const f of files) {
if (!f.match(/\.json$/)) {
continue
}
const gameId = f.replace(/\.json$/, '')
const contents = fs.readFileSync(`./../data/${f}`, 'utf-8')
const game = JSON.parse(contents)
GameCommon.setGame(gameId, {
puzzle: game.puzzle,
players: game.players,
sockets: [],
evtInfos: {},
})
}
}
function store(gameId) {
const game = GameCommon.get(gameId)
fs.writeFileSync('./../data/' + gameId + '.json', JSON.stringify({
puzzle: game.puzzle,
players: game.players,
}))
}
2020-11-12 19:19:02 +01:00
export default {
2020-11-25 22:03:35 +01:00
loadAllGames,
getAllGames: GameCommon.getAllGames,
store,
2020-11-12 19:19:02 +01:00
createGame,
2020-11-17 22:34:15 +01:00
exists: GameCommon.exists,
addPlayer: GameCommon.addPlayer,
2020-12-03 21:11:52 +01:00
playerExists: GameCommon.playerExists,
2020-11-17 22:34:15 +01:00
addSocket: GameCommon.addSocket,
2020-12-03 21:11:52 +01:00
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,
handleInput: GameCommon.handleInput,
2020-11-12 19:19:02 +01:00
}