From d8f9f856e6d3eed2fdb2c6688e272a32642496c7 Mon Sep 17 00:00:00 2001 From: Zutatensuppe Date: Sun, 8 Nov 2020 14:49:34 +0100 Subject: [PATCH] different game ids per game --- game/index.js | 11 +++++++---- server/index.js | 26 +++++++++++++++++++++----- server/puzzle.js | 7 ++++--- server/util.js | 10 +++++++--- 4 files changed, 39 insertions(+), 15 deletions(-) diff --git a/game/index.js b/game/index.js index ef5dee5..dffcf3e 100644 --- a/game/index.js +++ b/game/index.js @@ -7,6 +7,7 @@ import Camera from './Camera.js' import EventAdapter from './EventAdapter.js' import WsClient from './WsClient.js' +if (!GAME_ID) throw '[ GAME_ID not set ]' if (!WS_ADDRESS) throw '[ WS_ADDRESS not set ]' function createCanvas(width = 0, height = 0) { @@ -207,6 +208,7 @@ function imageToBitmap(img) { } async function loadImageToBitmap(imagePath) { + console.log(imagePath) const img = new Image() await new Promise((resolve) => { img.onload = resolve @@ -421,11 +423,11 @@ function setupNetwork(me) { async function main () { // todo: maybe put in protocols, same as `me()` - let gameId = 'asdfbla' // uniqId() + let gameId = GAME_ID // uniqId() let me = initme() - let cursorGrab = await loadImageToBitmap('./grab.png') - let cursorHand = await loadImageToBitmap('./hand.png') + let cursorGrab = await loadImageToBitmap('/grab.png') + let cursorHand = await loadImageToBitmap('/hand.png') let conn = setupNetwork(me + '|' + gameId) conn.send(JSON.stringify({ type: 'init' })) @@ -926,7 +928,8 @@ async function main () { // if it improves performance: // 1. background // 2. tiles - // 3. players + // 3. (moving tiles) + // 4. (players) // (currently, if a player moves, everthing needs to be // rerendered at that position manually, maybe it is faster // when using layers) diff --git a/server/index.js b/server/index.js index f41b108..e233014 100644 --- a/server/index.js +++ b/server/index.js @@ -3,6 +3,7 @@ import WebSocketServer from './WebSocketServer.js' import express from 'express' import { createPuzzle } from './puzzle.js' import config from './config.js' +import { uniqId, choice } from './util.js' // desired number of tiles // actual calculated number can be higher @@ -14,7 +15,6 @@ const IMAGES = [ '/example-images/saechsische_schweiz.jpg', '/example-images/132-2048x1365.jpg', ] -const IMAGE_URL = IMAGES[0] const games = {} @@ -22,16 +22,32 @@ const port = config.http.port const hostname = config.http.hostname const app = express() const statics = express.static('./../game/') +app.use('/g/:gid', (req, res, next) => { + res.send(` + + + + + + + `) +}) + app.use('/', (req, res, next) => { if (req.path === '/') { res.send(` - - + New game :P + ${Object.keys(games).map(k => { + return `Game ${k}` + })} `) @@ -58,7 +74,7 @@ wss.on('message', async ({socket, data}) => { switch (parsed.type) { case 'init': { // a new player (or previous player) joined - games[gid] = games[gid] || {puzzle: await createPuzzle(TARGET_TILES, IMAGE_URL), players: {}} + games[gid] = games[gid] || {puzzle: await createPuzzle(TARGET_TILES, choice(IMAGES)), players: {}} games[gid].players[uid] = {id: uid, x: 0, y: 0, down: false} diff --git a/server/puzzle.js b/server/puzzle.js index 1cfbdc5..5ad7b7c 100644 --- a/server/puzzle.js +++ b/server/puzzle.js @@ -6,10 +6,11 @@ import { choice } from './util.js' const TILE_SIZE = 64 async function createPuzzle(targetTiles, image) { - const imgFile = './../game' + image - const imgUrl = './' + image + const imgPath = './../game' + image + const imgUrl = image + // load bitmap, to determine the original size of the image - let dim = sizeOf(imgFile) + let dim = sizeOf(imgPath) // determine puzzle information from the bitmap let info = determinePuzzleInfo(dim.width, dim.height, targetTiles) diff --git a/server/util.js b/server/util.js index bf46cd3..4d0293c 100644 --- a/server/util.js +++ b/server/util.js @@ -1,4 +1,7 @@ +// 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 @@ -19,7 +22,8 @@ export const shuffle = (array) => { } export default { - randomInt, - choice, - shuffle, + uniqId, + randomInt, + choice, + shuffle, }