different game ids per game
This commit is contained in:
parent
3917c591b0
commit
d8f9f856e6
4 changed files with 39 additions and 15 deletions
|
|
@ -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(`
|
||||
<html><head><style>
|
||||
html,body {margin: 0; overflow: hidden;}
|
||||
html, body, #main { background: #222 }
|
||||
canvas {cursor: none;}
|
||||
</style></head><body>
|
||||
<script>window.GAME_ID = '${req.params.gid}'</script>
|
||||
<script>window.WS_ADDRESS = '${config.ws.connectstring}'</script>
|
||||
<script src="/index.js" type="module"></script>
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
})
|
||||
|
||||
app.use('/', (req, res, next) => {
|
||||
if (req.path === '/') {
|
||||
res.send(`
|
||||
<html><head><style>
|
||||
html,body {margin: 0; overflow: hidden;}
|
||||
html, body, #main { background: #222 }
|
||||
canvas {cursor: none;}
|
||||
</style></head><body>
|
||||
<script>window.WS_ADDRESS = '${config.ws.connectstring}'</script>
|
||||
<script src="index.js" type="module"></script>
|
||||
<a href="/g/${uniqId()}">New game :P</a>
|
||||
${Object.keys(games).map(k => {
|
||||
return `<a href="/g/${k}">Game ${k}</a>`
|
||||
})}
|
||||
</body>
|
||||
</html>
|
||||
`)
|
||||
|
|
@ -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}
|
||||
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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,
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue