diff --git a/server/util.js b/common/Util.js similarity index 99% rename from server/util.js rename to common/Util.js index 4d0293c..0d20aa1 100644 --- a/server/util.js +++ b/common/Util.js @@ -1,4 +1,3 @@ - // get a unique id export const uniqId = () => Date.now().toString(36) + Math.random().toString(36).substring(2) diff --git a/game/index.js b/game/index.js index 2c3c2ee..12a2418 100644 --- a/game/index.js +++ b/game/index.js @@ -6,6 +6,7 @@ import Graphics from './Graphics.js' import Debug from './Debug.js' import Communication from './Communication.js' import Geometry from './../common/Geometry.js' +import Util from './../common/Util.js' if (typeof GAME_ID === 'undefined') throw '[ GAME_ID not set ]' if (typeof WS_ADDRESS === 'undefined') throw '[ WS_ADDRESS not set ]' @@ -132,15 +133,11 @@ async function loadPuzzleBitmaps(puzzle) { return await createPuzzleTileBitmaps(bmpResized, puzzle.tiles, puzzle.info) } -function uniqId() { - return Date.now().toString(36) + Math.random().toString(36).substring(2) -} - function initme() { // return uniqId() let ID = localStorage.getItem("ID") if (!ID) { - ID = uniqId() + ID = Util.uniqId() localStorage.setItem("ID", ID) } return ID diff --git a/server/Puzzle.js b/server/Puzzle.js index d45ef09..ff8c9a2 100644 --- a/server/Puzzle.js +++ b/server/Puzzle.js @@ -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 diff --git a/server/index.js b/server/index.js index da9a4e4..b241553 100644 --- a/server/index.js +++ b/server/index.js @@ -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 } - New game :P + New game :P ${Object.keys(games).map(k => { return `Game ${k}` })} @@ -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)