quickfix for merged games xD

This commit is contained in:
Zutatensuppe 2020-11-08 17:11:32 +01:00
parent 2b4fe09d90
commit 8d5c6ac903

View file

@ -58,9 +58,11 @@ app.use('/', (req, res, next) => {
const wss = new WebSocketServer(config.ws); const wss = new WebSocketServer(config.ws);
const notify = (data) => { const notify = (data, sockets) => {
// TODO: throttle // TODO: throttle
wss.notifyAll(data) for (let socket of sockets) {
wss.notifyOne(data, socket)
}
console.log('notify', data) console.log('notify', data)
} }
@ -73,8 +75,14 @@ wss.on('message', async ({socket, data}) => {
switch (parsed.type) { switch (parsed.type) {
case 'init': { case 'init': {
// a new player (or previous player) joined // a new player (or previous player) joined
games[gid] = games[gid] || {puzzle: await createPuzzle(TARGET_TILES, choice(IMAGES)), players: {}} games[gid] = games[gid] || {
puzzle: await createPuzzle(TARGET_TILES, choice(IMAGES)),
players: {},
sockets: []
}
if (!games[gid].sockets.includes(socket)) {
games[gid].sockets.push(socket)
}
games[gid].players[uid] = {id: uid, x: 0, y: 0, down: false} games[gid].players[uid] = {id: uid, x: 0, y: 0, down: false}
wss.notifyOne({ wss.notifyOne({
@ -101,7 +109,11 @@ wss.on('message', async ({socket, data}) => {
} break; } break;
} }
} }
notify({type:'state_changed', origin: uid, changes: parsed.state.changes}) notify({
type:'state_changed',
origin: uid,
changes: parsed.state.changes,
}, games[gid].sockets)
} break; } break;
} }
} catch (e) { } catch (e) {