some improvements :D

This commit is contained in:
Zutatensuppe 2020-11-07 16:33:28 +01:00
parent 426c8578e5
commit 61837a155f
3 changed files with 180 additions and 176 deletions

View file

@ -26,8 +26,6 @@ app.use('/', (req, res, next) => {
})
app.listen(port, hostname, () => console.log(`server running on http://${hostname}:${port}`))
const players = {}
const games = {}
const wss = new WebSocketServer(config.ws);
@ -47,42 +45,38 @@ wss.on('message', ({socket, data}) => {
switch (parsed.type) {
case 'init': {
// a new player (or previous player) joined
players[uid] = players[uid] || {}
players[uid].id = uid
players[uid].tiles = players[uid].tiles || 0
players[uid].m_x = players[uid].x || null
players[uid].m_y = players[uid].y || null
players[uid].m_d = false
console.log('init', players)
const puzzle = games[gid] ? games[gid].puzzle : null
console.log('init', games[gid])
wss.notifyOne({type: 'init', puzzle: puzzle}, socket)
games[gid] = games[gid] || {puzzle: null, players: {}}
games[gid].players[uid] = parsed.player
wss.notifyOne({
type: 'init',
puzzle: games[gid].puzzle,
players: games[gid].players,
}, socket)
} break;
// new puzzle was created and sent to us
case 'init_puzzle': {
games[gid] = {
puzzle: parsed.puzzle,
}
games[gid].puzzle = parsed.puzzle
} break;
// somebody has changed the state
case 'state': {
players[uid].m_x = parsed.state.m_x
players[uid].m_y = parsed.state.m_y
players[uid].m_d = parsed.state.m_d
} break;
case 'change_tile': {
let idx = parsed.idx
let z = parsed.z
let finished = parsed.finished
let pos = { x: parsed.pos.x, y: parsed.pos.y }
let group = parsed.group
// games[gid].puzzle.tiles[idx] = games[gid].puzzle.tiles[idx] || {}
games[gid].puzzle.tiles[idx].pos = pos
games[gid].puzzle.tiles[idx].z = z
games[gid].puzzle.tiles[idx].finished = finished
games[gid].puzzle.tiles[idx].group = group
notify({type:'tile_changed', origin: uid, idx, pos, z, finished, group})
for (let change of parsed.state.changes) {
switch (change.type) {
case 'change_player': {
games[gid].players[uid] = change.player
} break;
case 'change_tile': {
games[gid].puzzle.tiles[change.tile.idx] = change.tile
} break;
case 'change_data': {
games[gid].puzzle.data = change.data
} break;
}
}
notify({type:'state_changed', origin: uid, changes: parsed.state.changes})
} break;
}
} catch (e) {