remove util.js, common/Util.js

This commit is contained in:
Zutatensuppe 2020-11-12 19:25:42 +01:00
parent d592cef494
commit 3dc9edeee6
4 changed files with 9 additions and 13 deletions

View file

@ -1,5 +1,5 @@
import sizeOf from 'image-size'
import { choice, shuffle } from './util.js'
import Util from './../common/Util.js'
// cut size of each puzzle tile in the
// final resized version of the puzzle image
@ -69,7 +69,7 @@ async function createPuzzle(targetTiles, image) {
}
// then shuffle the positions
positions = shuffle(positions)
positions = Util.shuffle(positions)
tiles = tiles.map(tile => {
return {
@ -143,9 +143,9 @@ function determinePuzzleTileShapes(info) {
for (let i = 0; i < info.tiles; i++) {
shapes[i] = {
top: info.coords[i].y === 0 ? 0 : shapes[i - info.tiles_x].bottom * -1,
right: info.coords[i].x === info.tiles_x - 1 ? 0 : choice(tabs),
right: info.coords[i].x === info.tiles_x - 1 ? 0 : Util.choice(tabs),
left: info.coords[i].x === 0 ? 0 : shapes[i - 1].right * -1,
bottom: info.coords[i].y === info.tiles_y - 1 ? 0 : choice(tabs),
bottom: info.coords[i].y === info.tiles_y - 1 ? 0 : Util.choice(tabs),
}
}
return shapes

View file

@ -2,7 +2,7 @@ import WebSocketServer from './WebSocketServer.js'
import express from 'express'
import config from './config.js'
import { uniqId, choice } from './util.js'
import Util from './../common/Util.js'
import Game from './Game.js'
const EV_SERVER_STATE_CHANGED = 1
@ -50,7 +50,7 @@ app.use('/', (req, res, next) => {
html,body {margin: 0; overflow: hidden;}
html, body, #main { background: #222 }
</style></head><body>
<a href="/g/${uniqId()}">New game :P</a>
<a href="/g/${Util.uniqId()}">New game :P</a>
${Object.keys(games).map(k => {
return `<a href="/g/${k}">Game ${k}</a>`
})}
@ -81,7 +81,7 @@ wss.on('message', async ({socket, data}) => {
switch (type) {
case EV_CLIENT_INIT: {
if (!Game.exists(gameId)) {
await Game.createGame(gameId, TARGET_TILES, choice(IMAGES))
await Game.createGame(gameId, TARGET_TILES, Util.choice(IMAGES))
}
Game.addPlayer(gameId, playerId)
Game.addSocket(gameId, socket)

View file

@ -1,29 +0,0 @@
// 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 = (min, max) => Math.floor(Math.random() * (max - min + 1)) + min
// get one random item from the given array
export const choice = (array) => array[randomInt(0, array.length - 1)]
// return a shuffled (shallow) copy of the given array
export const shuffle = (array) => {
let arr = array.slice()
for (let i = 0; i <= arr.length - 2; i++)
{
const j = randomInt(i, arr.length -1);
const tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
return arr
}
export default {
uniqId,
randomInt,
choice,
shuffle,
}