2020-11-07 11:35:29 +01:00
|
|
|
import WebSocketServer from './WebSocketServer.js'
|
|
|
|
|
|
|
|
|
|
import express from 'express'
|
|
|
|
|
|
2020-11-07 12:21:38 +01:00
|
|
|
import config from './config.js'
|
|
|
|
|
|
|
|
|
|
const port = config.http.port
|
|
|
|
|
const hostname = config.http.hostname
|
2020-11-07 11:35:29 +01:00
|
|
|
const app = express()
|
2020-11-07 12:21:38 +01:00
|
|
|
const statics = express.static('./../game/')
|
|
|
|
|
app.use('/', (req, res, next) => {
|
|
|
|
|
if (req.path === '/') {
|
|
|
|
|
res.send(`
|
|
|
|
|
<html><head><style>
|
|
|
|
|
html,body {margin: 0; overflow: hidden;}
|
|
|
|
|
html, body, #main { background: #222 }
|
2020-11-07 17:49:42 +01:00
|
|
|
canvas {cursor: none;}
|
2020-11-07 12:21:38 +01:00
|
|
|
</style></head><body>
|
|
|
|
|
<script>window.WS_ADDRESS = '${config.ws.connectstring}'</script>
|
|
|
|
|
<script src="index.js" type="module"></script>
|
|
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
`)
|
|
|
|
|
} else {
|
|
|
|
|
statics(req, res, next)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
app.listen(port, hostname, () => console.log(`server running on http://${hostname}:${port}`))
|
2020-11-07 11:35:29 +01:00
|
|
|
|
|
|
|
|
const games = {}
|
|
|
|
|
|
2020-11-07 12:21:38 +01:00
|
|
|
const wss = new WebSocketServer(config.ws);
|
2020-11-07 11:35:29 +01:00
|
|
|
|
|
|
|
|
const notify = (data) => {
|
|
|
|
|
// TODO: throttle
|
|
|
|
|
wss.notifyAll(data)
|
|
|
|
|
console.log('notify', data)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
wss.on('message', ({socket, data}) => {
|
|
|
|
|
try {
|
|
|
|
|
const proto = socket.protocol.split('|')
|
|
|
|
|
const uid = proto[0]
|
|
|
|
|
const gid = proto[1]
|
|
|
|
|
const parsed = JSON.parse(data)
|
|
|
|
|
switch (parsed.type) {
|
|
|
|
|
case 'init': {
|
|
|
|
|
// a new player (or previous player) joined
|
2020-11-07 16:33:28 +01:00
|
|
|
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)
|
2020-11-07 11:35:29 +01:00
|
|
|
} break;
|
|
|
|
|
|
2020-11-07 16:33:28 +01:00
|
|
|
// new puzzle was created and sent to us
|
2020-11-07 11:35:29 +01:00
|
|
|
case 'init_puzzle': {
|
2020-11-07 16:33:28 +01:00
|
|
|
games[gid].puzzle = parsed.puzzle
|
2020-11-07 11:35:29 +01:00
|
|
|
} break;
|
|
|
|
|
|
2020-11-07 16:33:28 +01:00
|
|
|
// somebody has changed the state
|
2020-11-07 11:35:29 +01:00
|
|
|
case 'state': {
|
2020-11-07 16:33:28 +01:00
|
|
|
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})
|
2020-11-07 11:35:29 +01:00
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
wss.listen()
|