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,
|
|
|
|
|
addSocket: GameCommon.addSocket,
|
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
|
|
|
}
|