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

View file

@ -12,13 +12,30 @@ export class Rng {
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_low = (this.rand_low + this.rand_high) & 0xffffffff;
var n = (this.rand_high >>> 0) / 0xffffffff;
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 {
return {
rand_high: rng.rand_high,

View file

@ -27,35 +27,6 @@ export const logger = (...pre: Array<any>) => {
// get a unique id
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 {
if (typeof data === 'number') {
return data
@ -196,9 +167,6 @@ const hash = (str: string): number => {
export default {
hash,
uniqId,
randomInt,
choice,
shuffle,
encodeShape,
decodeShape,

View file

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

View file

@ -1,9 +1,9 @@
import GameCommon from './../common/GameCommon'
import Util from './../common/Util'
import { Rng } from '../common/Rng'
import { Rng } from './../common/Rng'
import GameLog from './GameLog'
import { createPuzzle } from './Puzzle'
import Protocol from '../common/Protocol'
import Protocol from './../common/Protocol'
import GameStorage from './GameStorage'
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 { logger } from '../common/Util.js'
import { DATA_DIR } from '../server/Dirs.js'
import { logger } from './../common/Util'
import { DATA_DIR } from './../server/Dirs'
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'
const log = logger('GameSocket.js')

View file

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

View file

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

View file

@ -1,6 +1,6 @@
import Util from '../common/Util'
import { Rng } from '../common/Rng'
import Images from './Images.js'
import Util from './../common/Util'
import { Rng } from './../common/Rng'
import Images from './Images'
interface PuzzleInfo {
width: number
@ -89,7 +89,7 @@ async function createPuzzle(
}
// then shuffle the positions
positions = Util.shuffle(rng, positions)
positions = rng.shuffle(positions)
tiles = tiles.map(tile => {
return Util.encodeTile({
@ -167,9 +167,9 @@ function determinePuzzleTileShapes(
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(rng, tabs),
right: coord.x === info.tilesX - 1 ? 0 : rng.choice(tabs),
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)

View file

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

View file

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