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'
|
2020-11-17 21:46:56 +01:00
|
|
|
import Protocol from './../common/Protocol.js'
|
2020-11-12 19:25:42 +01:00
|
|
|
import Util from './../common/Util.js'
|
2020-11-12 19:19:02 +01:00
|
|
|
import Game from './Game.js'
|
|
|
|
|
|
2020-11-08 14:13:43 +01:00
|
|
|
// desired number of tiles
|
|
|
|
|
// actual calculated number can be higher
|
2020-11-12 19:19:02 +01:00
|
|
|
const TARGET_TILES = 1000
|
2020-11-08 14:13:43 +01:00
|
|
|
|
|
|
|
|
const IMAGES = [
|
|
|
|
|
'/example-images/ima_86ec3fa.jpeg',
|
|
|
|
|
'/example-images/bleu.png',
|
|
|
|
|
'/example-images/saechsische_schweiz.jpg',
|
|
|
|
|
'/example-images/132-2048x1365.jpg',
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
const games = {}
|
|
|
|
|
|
2020-11-07 12:21:38 +01:00
|
|
|
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/')
|
2020-11-08 14:49:34 +01:00
|
|
|
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>
|
|
|
|
|
`)
|
|
|
|
|
})
|
|
|
|
|
|
2020-11-12 19:19:02 +01:00
|
|
|
app.use('/common/', express.static('./../common/'))
|
2020-11-07 12:21:38 +01:00
|
|
|
app.use('/', (req, res, next) => {
|
|
|
|
|
if (req.path === '/') {
|
|
|
|
|
res.send(`
|
|
|
|
|
<html><head><style>
|
|
|
|
|
html,body {margin: 0; overflow: hidden;}
|
|
|
|
|
html, body, #main { background: #222 }
|
|
|
|
|
</style></head><body>
|
2020-11-12 19:25:42 +01:00
|
|
|
<a href="/g/${Util.uniqId()}">New game :P</a>
|
2020-11-08 14:49:34 +01:00
|
|
|
${Object.keys(games).map(k => {
|
|
|
|
|
return `<a href="/g/${k}">Game ${k}</a>`
|
|
|
|
|
})}
|
2020-11-07 12:21:38 +01:00
|
|
|
</body>
|
|
|
|
|
</html>
|
|
|
|
|
`)
|
|
|
|
|
} else {
|
|
|
|
|
statics(req, res, next)
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-11-07 11:35:29 +01:00
|
|
|
|
2020-11-07 12:21:38 +01:00
|
|
|
const wss = new WebSocketServer(config.ws);
|
2020-11-07 11:35:29 +01:00
|
|
|
|
2020-11-08 17:11:32 +01:00
|
|
|
const notify = (data, sockets) => {
|
2020-11-17 21:46:56 +01:00
|
|
|
// TODO: throttle?
|
2020-11-08 17:11:32 +01:00
|
|
|
for (let socket of sockets) {
|
|
|
|
|
wss.notifyOne(data, socket)
|
|
|
|
|
}
|
2020-11-07 11:35:29 +01:00
|
|
|
}
|
|
|
|
|
|
2020-11-08 14:13:43 +01:00
|
|
|
wss.on('message', async ({socket, data}) => {
|
2020-11-07 11:35:29 +01:00
|
|
|
try {
|
|
|
|
|
const proto = socket.protocol.split('|')
|
2020-11-17 21:46:56 +01:00
|
|
|
const clientId = proto[0]
|
2020-11-12 19:19:02 +01:00
|
|
|
const gameId = proto[1]
|
2020-11-17 21:46:56 +01:00
|
|
|
const msg = JSON.parse(data)
|
|
|
|
|
const msgType = msg[0]
|
|
|
|
|
switch (msgType) {
|
|
|
|
|
case Protocol.EV_CLIENT_INIT: {
|
2020-11-19 12:38:50 +01:00
|
|
|
const name = msg[1]
|
2020-11-12 19:19:02 +01:00
|
|
|
if (!Game.exists(gameId)) {
|
2020-11-12 19:25:42 +01:00
|
|
|
await Game.createGame(gameId, TARGET_TILES, Util.choice(IMAGES))
|
2020-11-08 17:11:32 +01:00
|
|
|
}
|
2020-11-19 12:38:50 +01:00
|
|
|
Game.addPlayer(gameId, clientId, name)
|
2020-11-12 19:19:02 +01:00
|
|
|
Game.addSocket(gameId, socket)
|
2020-11-07 16:33:28 +01:00
|
|
|
|
2020-11-17 21:46:56 +01:00
|
|
|
notify(
|
|
|
|
|
[Protocol.EV_SERVER_INIT, Game.get(gameId)],
|
|
|
|
|
[socket]
|
|
|
|
|
)
|
2020-11-07 11:35:29 +01:00
|
|
|
} break;
|
|
|
|
|
|
2020-11-17 21:46:56 +01:00
|
|
|
case Protocol.EV_CLIENT_EVENT: {
|
|
|
|
|
const clientSeq = msg[1]
|
|
|
|
|
const clientEvtData = msg[2]
|
|
|
|
|
const changes = Game.handleInput(gameId, clientId, clientEvtData)
|
|
|
|
|
notify(
|
|
|
|
|
[Protocol.EV_SERVER_EVENT, clientId, clientSeq, changes],
|
|
|
|
|
Game.getSockets(gameId)
|
|
|
|
|
)
|
2020-11-07 11:35:29 +01:00
|
|
|
} break;
|
|
|
|
|
}
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e)
|
|
|
|
|
}
|
|
|
|
|
})
|
2020-11-08 16:42:59 +01:00
|
|
|
|
|
|
|
|
app.listen(port, hostname, () => console.log(`server running on http://${hostname}:${port}`))
|
2020-11-07 11:35:29 +01:00
|
|
|
wss.listen()
|