move random functions to rng class, fix some imports

This commit is contained in:
Zutatensuppe 2021-05-17 01:12:39 +02:00
parent 07c08019f5
commit 432e1b6668
16 changed files with 71 additions and 80 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -2,7 +2,7 @@
<head> <head>
<title>🧩 jigsaw.hyottoko.club</title> <title>🧩 jigsaw.hyottoko.club</title>
<script type="module" crossorigin src="/assets/index.4c0f0f73.js"></script> <script type="module" crossorigin src="/assets/index.2f18bf13.js"></script>
<link rel="modulepreload" href="/assets/vendor.00b608ff.js"> <link rel="modulepreload" href="/assets/vendor.00b608ff.js">
<link rel="stylesheet" href="/assets/index.421011a7.css"> <link rel="stylesheet" href="/assets/index.421011a7.css">
</head> </head>

View file

@ -21,6 +21,21 @@ class Rng {
var n = (this.rand_high >>> 0) / 0xffffffff; var n = (this.rand_high >>> 0) / 0xffffffff;
return (min + n * (max - min + 1)) | 0; return (min + n * (max - min + 1)) | 0;
} }
// get one random item from the given array
choice(array) {
return array[this.random(0, array.length - 1)];
}
// return a shuffled (shallow) copy of the given array
shuffle(array) {
const arr = array.slice();
for (let i = 0; i <= arr.length - 2; i++) {
const j = this.random(i, arr.length - 1);
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
return arr;
}
static serialize(rng) { static serialize(rng) {
return { return {
rand_high: rng.rand_high, rand_high: rng.rand_high,
@ -58,21 +73,6 @@ const logger = (...pre) => {
}; };
// get a unique id // get a unique id
const uniqId = () => Date.now().toString(36) + Math.random().toString(36).substring(2); const uniqId = () => Date.now().toString(36) + Math.random().toString(36).substring(2);
// get a random int between min and max (inclusive)
const randomInt = (rng, min, max) => rng.random(min, max);
// get one random item from the given array
const choice = (rng, array) => array[randomInt(rng, 0, array.length - 1)];
// return a shuffled (shallow) copy of the given array
const shuffle = (rng, array) => {
const arr = array.slice();
for (let i = 0; i <= arr.length - 2; i++) {
const j = randomInt(rng, i, arr.length - 1);
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
return arr;
};
function encodeShape(data) { function encodeShape(data) {
if (typeof data === 'number') { if (typeof data === 'number') {
return data; return data;
@ -202,9 +202,6 @@ const hash = (str) => {
var Util = { var Util = {
hash, hash,
uniqId, uniqId,
randomInt,
choice,
shuffle,
encodeShape, encodeShape,
decodeShape, decodeShape,
encodeTile, encodeTile,
@ -1345,7 +1342,7 @@ async function createPuzzle(rng, targetTiles, image, ts) {
} }
} }
// then shuffle the positions // then shuffle the positions
positions = Util.shuffle(rng, positions); positions = rng.shuffle(positions);
tiles = tiles.map(tile => { tiles = tiles.map(tile => {
return Util.encodeTile({ return Util.encodeTile({
idx: tile.idx, idx: tile.idx,
@ -1413,9 +1410,9 @@ function determinePuzzleTileShapes(rng, info) {
let coord = Util.coordByTileIdx(info, i); let coord = Util.coordByTileIdx(info, i);
shapes[i] = { shapes[i] = {
top: coord.y === 0 ? 0 : shapes[i - info.tilesX].bottom * -1, top: coord.y === 0 ? 0 : shapes[i - info.tilesX].bottom * -1,
right: coord.x === info.tilesX - 1 ? 0 : Util.choice(rng, tabs), right: coord.x === info.tilesX - 1 ? 0 : rng.choice(tabs),
left: coord.x === 0 ? 0 : shapes[i - 1].right * -1, left: coord.x === 0 ? 0 : shapes[i - 1].right * -1,
bottom: coord.y === info.tilesY - 1 ? 0 : Util.choice(rng, tabs), bottom: coord.y === info.tilesY - 1 ? 0 : rng.choice(tabs),
}; };
} }
return shapes.map(Util.encodeShape); return shapes.map(Util.encodeShape);

View file

@ -7,6 +7,19 @@ export default {
dir: 'build/server', dir: 'build/server',
format: 'es', format: 'es',
}, },
external: [
"express",
"multer",
"body-parser",
"v8",
"fs",
"ws",
"image-size",
"exif",
"sharp",
"url",
"path",
],
plugins: [typescript({ plugins: [typescript({
"tsconfig": "tsconfig.server.json" "tsconfig": "tsconfig.server.json"
})], })],

View file

@ -12,13 +12,30 @@ export class Rng {
this.rand_low = seed ^ 0x49616E42 this.rand_low = seed ^ 0x49616E42
} }
random (min: number, max: number) { random (min: number, max: number): number {
this.rand_high = ((this.rand_high << 16) + (this.rand_high >> 16) + this.rand_low) & 0xffffffff; this.rand_high = ((this.rand_high << 16) + (this.rand_high >> 16) + this.rand_low) & 0xffffffff;
this.rand_low = (this.rand_low + this.rand_high) & 0xffffffff; this.rand_low = (this.rand_low + this.rand_high) & 0xffffffff;
var n = (this.rand_high >>> 0) / 0xffffffff; var n = (this.rand_high >>> 0) / 0xffffffff;
return (min + n * (max-min+1))|0; return (min + n * (max-min+1))|0;
} }
// get one random item from the given array
choice<T> (array: Array<T>): T {
return array[this.random(0, array.length - 1)]
}
// return a shuffled (shallow) copy of the given array
shuffle<T> (array: Array<T>): Array<T> {
const arr = array.slice()
for (let i = 0; i <= arr.length - 2; i++) {
const j = this.random(i, arr.length -1)
const tmp = arr[i]
arr[i] = arr[j]
arr[j] = tmp
}
return arr
}
static serialize (rng: Rng): RngSerialized { static serialize (rng: Rng): RngSerialized {
return { return {
rand_high: rng.rand_high, rand_high: rng.rand_high,

View file

@ -27,35 +27,6 @@ export const logger = (...pre: Array<any>) => {
// get a unique id // get a unique id
export const uniqId = () => Date.now().toString(36) + Math.random().toString(36).substring(2) export const uniqId = () => Date.now().toString(36) + Math.random().toString(36).substring(2)
// get a random int between min and max (inclusive)
export const randomInt = (
rng: Rng,
min: number,
max: number,
) => rng.random(min, max)
// get one random item from the given array
export const choice = (
rng: Rng,
array: Array<any>
) => array[randomInt(rng, 0, array.length - 1)]
// return a shuffled (shallow) copy of the given array
export const shuffle = (
rng: Rng,
array: Array<any>
) => {
const arr = array.slice()
for (let i = 0; i <= arr.length - 2; i++)
{
const j = randomInt(rng, i, arr.length -1);
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
return arr
}
function encodeShape(data: any): number { function encodeShape(data: any): number {
if (typeof data === 'number') { if (typeof data === 'number') {
return data return data
@ -196,9 +167,6 @@ const hash = (str: string): number => {
export default { export default {
hash, hash,
uniqId, uniqId,
randomInt,
choice,
shuffle,
encodeShape, encodeShape,
decodeShape, decodeShape,

View file

@ -20,10 +20,10 @@ const explosionDividerFactor = 10
const nBombs = 1 const nBombs = 1
const percentChanceNewBomb = 5 const percentChanceNewBomb = 5
function color(rng: Rng) { function color(rng: Rng): string {
const r = Util.randomInt(rng, 0, 255) const r = rng.random(0, 255)
const g = Util.randomInt(rng, 0, 255) const g = rng.random(0, 255)
const b = Util.randomInt(rng, 0, 255) const b = rng.random(0, 255)
return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)' return 'rgba(' + r + ',' + g + ',' + b + ', 0.8)'
} }

View file

@ -1,9 +1,9 @@
import GameCommon from './../common/GameCommon' import GameCommon from './../common/GameCommon'
import Util from './../common/Util' import Util from './../common/Util'
import { Rng } from '../common/Rng' import { Rng } from './../common/Rng'
import GameLog from './GameLog' import GameLog from './GameLog'
import { createPuzzle } from './Puzzle' import { createPuzzle } from './Puzzle'
import Protocol from '../common/Protocol' import Protocol from './../common/Protocol'
import GameStorage from './GameStorage' import GameStorage from './GameStorage'
async function createGameObject(gameId: string, targetTiles: number, image: { file: string, url: string }, ts: number, scoreMode: number) { async function createGameObject(gameId: string, targetTiles: number, image: { file: string, url: string }, ts: number, scoreMode: number) {

View file

@ -1,6 +1,6 @@
import fs from 'fs' import fs from 'fs'
import { logger } from '../common/Util.js' import { logger } from './../common/Util'
import { DATA_DIR } from '../server/Dirs.js' import { DATA_DIR } from './../server/Dirs'
const log = logger('GameLog.js') const log = logger('GameLog.js')

View file

@ -1,4 +1,4 @@
import { logger } from '../common/Util.js' import { logger } from './../common/Util'
import WebSocket from 'ws' import WebSocket from 'ws'
const log = logger('GameSocket.js') const log = logger('GameSocket.js')

View file

@ -1,7 +1,7 @@
import fs from 'fs' import fs from 'fs'
import GameCommon from './../common/GameCommon' import GameCommon from './../common/GameCommon'
import Util, { logger } from './../common/Util' import Util, { logger } from './../common/Util'
import { Rng } from '../common/Rng' import { Rng } from './../common/Rng'
import { DATA_DIR } from './Dirs' import { DATA_DIR } from './Dirs'
import Time from './../common/Time' import Time from './../common/Time'

View file

@ -3,7 +3,7 @@ import fs from 'fs'
import exif from 'exif' import exif from 'exif'
import sharp from 'sharp' import sharp from 'sharp'
import {UPLOAD_DIR, UPLOAD_URL} from './Dirs.js' import {UPLOAD_DIR, UPLOAD_URL} from './Dirs'
const resizeImage = async (filename: string) => { const resizeImage = async (filename: string) => {
if (!filename.toLowerCase().match(/\.(jpe?g|webp|png)$/)) { if (!filename.toLowerCase().match(/\.(jpe?g|webp|png)$/)) {

View file

@ -1,6 +1,6 @@
import Util from '../common/Util' import Util from './../common/Util'
import { Rng } from '../common/Rng' import { Rng } from './../common/Rng'
import Images from './Images.js' import Images from './Images'
interface PuzzleInfo { interface PuzzleInfo {
width: number width: number
@ -89,7 +89,7 @@ async function createPuzzle(
} }
// then shuffle the positions // then shuffle the positions
positions = Util.shuffle(rng, positions) positions = rng.shuffle(positions)
tiles = tiles.map(tile => { tiles = tiles.map(tile => {
return Util.encodeTile({ return Util.encodeTile({
@ -167,9 +167,9 @@ function determinePuzzleTileShapes(
let coord = Util.coordByTileIdx(info, i) let coord = Util.coordByTileIdx(info, i)
shapes[i] = { shapes[i] = {
top: coord.y === 0 ? 0 : shapes[i - info.tilesX].bottom * -1, top: coord.y === 0 ? 0 : shapes[i - info.tilesX].bottom * -1,
right: coord.x === info.tilesX - 1 ? 0 : Util.choice(rng, tabs), right: coord.x === info.tilesX - 1 ? 0 : rng.choice(tabs),
left: coord.x === 0 ? 0 : shapes[i - 1].right * -1, left: coord.x === 0 ? 0 : shapes[i - 1].right * -1,
bottom: coord.y === info.tilesY - 1 ? 0 : Util.choice(rng, tabs), bottom: coord.y === info.tilesY - 1 ? 0 : rng.choice(tabs),
} }
} }
return shapes.map(Util.encodeShape) return shapes.map(Util.encodeShape)

View file

@ -1,5 +1,5 @@
import WebSocket from 'ws' import WebSocket from 'ws'
import { logger } from '../common/Util.js' import { logger } from './../common/Util'
const log = logger('WebSocketServer.js') const log = logger('WebSocketServer.js')

View file

@ -10,13 +10,9 @@ import v8 from 'v8'
import fs from 'fs' import fs from 'fs'
import GameLog from './GameLog' import GameLog from './GameLog'
import GameSockets from './GameSockets' import GameSockets from './GameSockets'
import Time from '../common/Time' import Time from './../common/Time'
import Images from './Images' import Images from './Images'
import { import { UPLOAD_DIR, UPLOAD_URL, PUBLIC_DIR } from './Dirs'
UPLOAD_DIR,
UPLOAD_URL,
PUBLIC_DIR,
} from './Dirs'
import GameCommon from '../common/GameCommon' import GameCommon from '../common/GameCommon'
import GameStorage from './GameStorage' import GameStorage from './GameStorage'