reduce size of game json files

This commit is contained in:
Zutatensuppe 2020-12-05 19:45:34 +01:00
parent c6899a615b
commit b6a3cfd8ba
9 changed files with 380 additions and 149 deletions

View file

@ -3,13 +3,13 @@ import { createPuzzle } from './Puzzle.js'
import GameCommon from './../common/GameCommon.js'
async function createGame(gameId, targetTiles, image) {
const game = {
GameCommon.newGame({
id: gameId,
puzzle: await createPuzzle(targetTiles, image),
players: {},
sockets: [],
evtInfos: {},
}
GameCommon.setGame(gameId, game)
})
}
function loadAllGames() {
@ -21,20 +21,20 @@ function loadAllGames() {
const gameId = f.replace(/\.json$/, '')
const contents = fs.readFileSync(`./../data/${f}`, 'utf-8')
const game = JSON.parse(contents)
GameCommon.setGame(gameId, {
GameCommon.newGame({
id: gameId,
puzzle: game.puzzle,
players: game.players,
sockets: [],
evtInfos: {},
evtInfos: {}
})
}
}
function persistAll() {
const games = GameCommon.getAllGames()
for (const gameId of Object.keys(games)) {
const game = games[gameId]
fs.writeFileSync('./../data/' + gameId + '.json', JSON.stringify({
for (const game of GameCommon.getAllGames()) {
fs.writeFileSync('./../data/' + game.id + '.json', JSON.stringify({
id: game.id,
puzzle: game.puzzle,
players: game.players,
}))
@ -43,9 +43,13 @@ function persistAll() {
export default {
loadAllGames,
getAllGames: GameCommon.getAllGames,
persistAll,
createGame,
getAllGames: GameCommon.getAllGames,
getActivePlayers: GameCommon.getActivePlayers,
getFinishedTileCount: GameCommon.getFinishedTileCount,
getImageUrl: GameCommon.getImageUrl,
getTileCount: GameCommon.getTileCount,
exists: GameCommon.exists,
addPlayer: GameCommon.addPlayer,
playerExists: GameCommon.playerExists,

View file

@ -23,11 +23,12 @@ async function createPuzzle(targetTiles, image) {
let positions = new Array(info.tiles)
for (let tile of tiles) {
let coord = Util.coordByTileIdx(info, tile.idx)
positions[tile.idx] ={
// instead of info.tileSize, we use info.tileDrawSize
// to spread the tiles a bit
x: (info.coords[tile.idx].x) * (info.tileSize * 1.5),
y: (info.coords[tile.idx].y) * (info.tileSize * 1.5),
x: coord.x * info.tileSize * 1.5,
y: coord.y * info.tileSize * 1.5,
}
}
@ -73,7 +74,7 @@ async function createPuzzle(targetTiles, image) {
positions = Util.shuffle(positions)
tiles = tiles.map(tile => {
return {
return Util.encodeTile({
idx: tile.idx, // index of tile in the array
group: 0, // if grouped with other tiles
z: 0, // z index of the tile
@ -88,7 +89,7 @@ async function createPuzzle(targetTiles, image) {
// this position is the initial position only and is the
// value that changes when moving a tile
pos: positions[tile.idx],
}
})
})
// Complete puzzle object
@ -125,7 +126,6 @@ async function createPuzzle(targetTiles, image) {
tiles: info.tiles, // the final number of tiles in the puzzle
tilesX: info.tilesX, // number of tiles each row
tilesY: info.tilesY, // number of tiles each col
coords: info.coords, // map of tile index to its coordinates
// ( index => {x, y} )
// this is not the physical coordinate, but
// the tile_coordinate
@ -141,12 +141,13 @@ function determinePuzzleTileShapes(info) {
const shapes = new Array(info.tiles)
for (let i = 0; i < info.tiles; i++) {
shapes[i] = {
top: info.coords[i].y === 0 ? 0 : shapes[i - info.tilesX].bottom * -1,
right: info.coords[i].x === info.tilesX - 1 ? 0 : Util.choice(tabs),
left: info.coords[i].x === 0 ? 0 : shapes[i - 1].right * -1,
bottom: info.coords[i].y === info.tilesY - 1 ? 0 : Util.choice(tabs),
}
let coord = Util.coordByTileIdx(info, i)
shapes[i] = Util.encodeShape({
top: coord.y === 0 ? 0 : shapes[i - info.tilesX].bottom * -1,
right: coord.x === info.tilesX - 1 ? 0 : Util.choice(tabs),
left: coord.x === 0 ? 0 : shapes[i - 1].right * -1,
bottom: coord.y === info.tilesY - 1 ? 0 : Util.choice(tabs),
})
}
return shapes
}
@ -173,7 +174,6 @@ const determinePuzzleInfo = (w, h, targetTiles) => {
const tileSize = TILE_SIZE
const width = tilesX * tileSize
const height = tilesY * tileSize
const coords = buildCoords({ width, height, tileSize, tiles })
const tileMarginWidth = tileSize * .5;
const tileDrawSize = Math.round(tileSize + tileMarginWidth * 2)
@ -187,21 +187,9 @@ const determinePuzzleInfo = (w, h, targetTiles) => {
tiles,
tilesX,
tilesY,
coords,
}
}
const buildCoords = (puzzleInfo) => {
const wTiles = puzzleInfo.width / puzzleInfo.tileSize
const coords = new Array(puzzleInfo.tiles)
for (let i = 0; i < puzzleInfo.tiles; i++) {
const y = Math.floor(i / wTiles)
const x = i % wTiles
coords[i] = { x, y }
}
return coords
}
export {
createPuzzle,
}

View file

@ -76,9 +76,12 @@ app.use('/uploads/', express.static('./../data/uploads/'))
app.use('/', async (req, res, next) => {
if (req.path === '/') {
const games = [
...Object.keys(Game.getAllGames()).map(id => ({
id: id,
title: id,
...Game.getAllGames().map(game => ({
id: game.id,
tilesFinished: Game.getFinishedTileCount(game.id),
tilesTotal: Game.getTileCount(game.id),
players: Game.getActivePlayers(game.id).length,
imageUrl: Game.getImageUrl(game.id),
})),
]
@ -126,8 +129,10 @@ wss.on('message', async ({socket, data}) => {
const game = Game.get(gameId)
notify(
[Protocol.EV_SERVER_INIT, {
id: game.id,
puzzle: game.puzzle,
players: game.players,
sockets: [],
evtInfos: game.evtInfos,
}],
[socket]
@ -143,8 +148,10 @@ wss.on('message', async ({socket, data}) => {
const game = Game.get(gameId)
notify(
[Protocol.EV_SERVER_INIT, {
id: game.id,
puzzle: game.puzzle,
players: game.players,
sockets: [],
evtInfos: game.evtInfos,
}],
[socket]