rng class..

This commit is contained in:
Zutatensuppe 2020-12-21 18:34:57 +01:00
parent 3ff375dbb4
commit cbc2b88f47
12 changed files with 4762 additions and 25 deletions

View file

@ -2,6 +2,7 @@ import fs from 'fs'
import { createPuzzle } from './Puzzle.js'
import GameCommon from './../common/GameCommon.js'
import Util from './../common/Util.js'
import { Rng } from '../common/Rng.js'
const DATA_DIR = './../data'
@ -28,6 +29,10 @@ function loadAllGames() {
}
GameCommon.newGame({
id: game.id,
rng: {
type: game.rng ? game.rng.type : '_fake_',
obj: game.rng ? Rng.unserialize(game.rng.obj) : new Rng(),
},
puzzle: game.puzzle,
players: game.players,
sockets: [],
@ -38,9 +43,14 @@ function loadAllGames() {
const changedGames = {}
async function createGame(gameId, targetTiles, image) {
const rng = new Rng(gameId);
GameCommon.newGame({
id: gameId,
puzzle: await createPuzzle(targetTiles, image),
rng: {
type: 'Rng',
obj: rng,
},
puzzle: await createPuzzle(rng, targetTiles, image),
players: {},
sockets: [],
evtInfos: {},
@ -70,6 +80,10 @@ function persistChangedGames() {
delete changedGames[game.id]
fs.writeFileSync(`${DATA_DIR}/${game.id}.json`, JSON.stringify({
id: game.id,
rng: {
type: game.rng.type,
obj: Rng.serialize(game.rng.obj),
},
puzzle: game.puzzle,
players: game.players,
}))

View file

@ -1,6 +1,7 @@
import sizeOf from 'image-size'
import Util from './../common/Util.js'
import exif from 'exif'
import { Rng } from '../common/Rng.js'
// cut size of each puzzle tile in the
// final resized version of the puzzle image
@ -34,7 +35,11 @@ async function getExifOrientation(imagePath) {
})
}
async function createPuzzle(targetTiles, image) {
async function createPuzzle(
/** @type Rng */ rng,
targetTiles,
image
) {
const imagePath = image.file
const imageUrl = image.url
@ -48,7 +53,7 @@ async function createPuzzle(targetTiles, image) {
for (let i = 0; i < tiles.length; i++) {
tiles[i] = { idx: i }
}
const shapes = determinePuzzleTileShapes(info)
const shapes = determinePuzzleTileShapes(rng, info)
let positions = new Array(info.tiles)
for (let tile of tiles) {
@ -100,7 +105,7 @@ async function createPuzzle(targetTiles, image) {
}
// then shuffle the positions
positions = Util.shuffle(positions)
positions = Util.shuffle(rng, positions)
tiles = tiles.map(tile => {
return Util.encodeTile({
@ -167,7 +172,10 @@ async function createPuzzle(targetTiles, image) {
}
}
function determinePuzzleTileShapes(info) {
function determinePuzzleTileShapes(
/** @type Rng */ rng,
info
) {
const tabs = [-1, 1]
const shapes = new Array(info.tiles)
@ -175,9 +183,9 @@ function determinePuzzleTileShapes(info) {
let coord = Util.coordByTileIdx(info, i)
shapes[i] = {
top: coord.y === 0 ? 0 : shapes[i - info.tilesX].bottom * -1,
right: coord.x === info.tilesX - 1 ? 0 : Util.choice(tabs),
right: coord.x === info.tilesX - 1 ? 0 : Util.choice(rng, tabs),
left: coord.x === 0 ? 0 : shapes[i - 1].right * -1,
bottom: coord.y === info.tilesY - 1 ? 0 : Util.choice(tabs),
bottom: coord.y === info.tilesY - 1 ? 0 : Util.choice(rng, tabs),
}
}
return shapes.map(Util.encodeShape)

View file

@ -10,6 +10,7 @@ import Game from './Game.js'
import twing from 'twing'
import bodyParser from 'body-parser'
import v8 from 'v8'
import { Rng } from '../common/Rng.js'
const allImages = () => [
...fs.readdirSync('./../data/uploads/').map(f => ({
@ -130,9 +131,14 @@ wss.on('message', async ({socket, data}) => {
Game.addPlayer(gameId, clientId)
Game.addSocket(gameId, socket)
const game = Game.get(gameId)
console.log(gameId, game)
notify(
[Protocol.EV_SERVER_INIT, {
id: game.id,
rng: {
type: game.rng.type,
obj: Rng.serialize(game.rng.obj),
},
puzzle: game.puzzle,
players: game.players,
sockets: [],