-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
298 lines (257 loc) · 8.75 KB
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const express = require('express')
const bodyParser = require('body-parser')
const crypto = require('crypto')
const mongoose = require('mongoose')
const http = require('http')
const generateWordId = require('faster-word-id')
const WebSocket = require('ws')
const Game = require('./models/Game')
const { getMarkup } = require('./frontend')
const { pick, getKeyFromPlayer } = require('./util')
const { renderGameJson } = require('./game')
const { makeManager } = require('./manager')
mongoose.connect(process.env.MONGODB_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
useCreateIndex: true,
useFindAndModify: false,
autoReconnect: true,
}).then(() => console.log('MongoDB connected'))
.catch((err) => console.error('Failed to connect MongoDB', err))
const app = express()
const manager = makeManager()
app.use('/static', express.static('frontend/dist'))
app.use('/', express.static('public'))
app.use(bodyParser.urlencoded({extended: true}))
app.use(bodyParser.json())
function makeWebError(status, message) {
const err = new Error(message)
err.code = status
return err
}
/* API endpoints */
app.post('/api/games/:id', async (req, res) => {
const gameId = req.params.id
const adminModifiable = pick(req.body, [
'questions', 'playerRegex', 'playerRegexMessage',
'buttonLeftTitle', 'buttonMiddleTitle', 'buttonRightTitle',
'backgroundColorLeft', 'backgroundColorMiddle', 'backgroundColorRight',
'radius',
'openQuestionId',
])
const game = await Game.findOneAndUpdate({
gameId,
adminCode: req.query.adminCode,
}, adminModifiable, { upsert: false, new: true })
// Could also be adminCode mis-match.
if (!game) { throw makeWebError(404, 'Not found') }
res.json(renderGameJson(game))
if ('openQuestionId' in req.body) {
// update game state.
manager.broadcastUpdate(gameId, renderGameJson(game))
}
})
app.post('/api/games/:id/choose', async (req, res) => {
const gameId = req.params.id
const choice = req.body.choice
const player = req.body.player
const questionId = req.body.questionId
if (!questionId || !player) { throw makeWebError(400, 'Missing questionId or player') }
const patch = {}
if (choice === null) {
patch['$pull'] = {
[`responses.${questionId}.a`]: player,
[`responses.${questionId}.b`]: player,
}
} else if (choice === 'a') {
patch['$pull'] = { [`responses.${questionId}.b`]: player }
patch['$addToSet'] = { [`responses.${questionId}.a`]: player }
} else if (choice === 'b') {
patch['$pull'] = { [`responses.${questionId}.a`]: player }
patch['$addToSet'] = { [`responses.${questionId}.b`]: player }
} else {
return res.status(400).json({ error: 'Invalid choice' })
}
const game = await Game.findOneAndUpdate({ gameId, openQuestionId: questionId },
patch, { upsert: false, new: true })
// Could also be that the question ID does not match the open one.
if (!game) { throw makeWebError(404, 'Not found') }
const gameJson = renderGameJson(game)
res.json(gameJson)
manager.broadcastUpdate(gameId, gameJson)
})
app.get('/api/games/:id', async (req, res) => {
const gameId = req.params.id
const game = await Game.findOne({ gameId })
if (!game) { throw makeWebError(404, 'Not found') }
const gameJson = renderGameJson(game)
if (req.query.show_online && req.query.show_online == 'true') {
gameJson.online = (manager.getPlayers(gameId) || [])
.filter((c) => !['admin', 'present'].includes(c.player))
.map((c) => pick(c, ['player', 'name', 'lastMessageTime', 'clientId']))
}
res.json(gameJson)
})
/* UI endpoints */
app.post('/new_game', async (req, res) => {
const gameId = req.body.gameId
if (!gameId) { return res.redirect('/') }
const adminCode = crypto.randomUUID().replaceAll('-', '')
try {
await Game.create({ gameId, adminCode })
} catch (err) {
if (err.code === 11000) {
console.warn('Creating game hit dupe gameId with:', gameId)
return res.redirect('/?state=errorDupeGameId')
} else {
console.error('Failed to create game', err)
return res.redirect('/?state=errorUnexpected')
}
}
res.redirect('/' + gameId + '/manage-' + adminCode)
})
app.post('/:id/join', async (req, res) => {
const gameId = req.params.id
const game = await Game.findOne({ gameId })
if (!game) { return res.redirect('/') }
const player = req.body.player
const name = req.body.name
const uriParams = 'player=' + encodeURIComponent(player) + '&name=' + encodeURIComponent(name)
if (!player) {
return res.redirect('/' + gameId + '?state=errorPlayer&' + uriParams)
}
if (game.playerRegex && !player.match(game.playerRegex)) {
return res.redirect('/' + gameId + '?state=errorPlayerRegex&' + uriParams)
}
if (!name) {
return res.redirect('/' + gameId + '?state=errorPlayerName&' + uriParams)
}
const newGame = await Game.findOneAndUpdate({ gameId }, {
$addToSet: { players: player },
$set: {
[`playerInfo.${getKeyFromPlayer(player)}.player`]: player,
[`playerInfo.${getKeyFromPlayer(player)}.name`]: name,
},
}, { new: true, upsert: true })
res.redirect('/' + gameId + '/play?' + uriParams)
})
app.get('/:id/manage-:adminCode', async (req, res) => {
const game = await Game.findOne({ gameId: req.params.id, adminCode: req.params.adminCode })
if (!game) { return res.redirect('/' + req.params.id) }
const markup = getMarkup({
gameId: game.gameId,
adminCode: game.adminCode,
page: 'manage',
testInfo: generateWordId(),
})
res.end(markup)
})
app.get('/:id/data.json', async (req, res) => {
const game = await Game.findOne({ gameId: req.params.id })
if (!game) { res.redirect('/') }
res.set('content-type', 'application/json')
res.end(JSON.stringify(renderGameJson(game), null, 2))
})
app.get('/:id/present', async (req, res) => {
const game = await Game.findOne({ gameId: req.params.id })
if (!game) { return res.redirect('/') }
const markup = getMarkup({
...req.query,
page: 'present',
...renderGameJson(game),
})
res.end(markup)
})
app.get('/:id/play', async (req, res) => {
const player = req.query.player
const name = req.query.name
if (!player || !name) { return res.redirect('/' + req.params.id) }
const game = await Game.findOne({ gameId: req.params.id })
if (!game) { return res.redirect('/') }
if (!game.players.includes(player)) { return res.redirect('/' + req.params.id )}
const markup = getMarkup({
page: 'play',
player,
name,
...renderGameJson(game),
})
res.end(markup)
})
app.get('/:id/:adminCode', (req, res) => {
res.redirect(`/${req.params.id}/manage-${req.params.adminCode}`)
})
app.get('/:id', async (req, res) => {
const game = await Game.findOne({ gameId: req.params.id })
if (!game) { return res.redirect('/') }
const markup = getMarkup({
page: 'lobby',
...renderGameJson(game),
})
res.end(markup)
})
app.get('/', (req, res) => {
const markup = getMarkup({
...req.query,
page: 'index',
newGameId: generateWordId(),
})
res.end(markup)
})
app.use(function (err, req, res, next) {
console.log('handling error')
if (!err.code || err.code >= 500) {
console.error('Unrecognized error!', err.stack)
res.status(500).json({ error: 'Internal server error' })
} else {
res.status(err.code).json({ error: err.message })
}
})
const server = http.createServer(app);
const wss = new WebSocket.Server({
clientTracking: true,
noServer: true,
});
wss.on('connection', function connection(ws) {
ws.on('close', async function(message) {
try {
manager.removePlayer(ws.gameId, ws.clientId)
const game = await Game.findOne({ gameId: ws.gameId })
manager.broadcastUpdate(ws.gameId, renderGameJson(game))
} catch (err) {
console.error('Failed to handle close', ws.gameId, ws.player, err)
}
})
ws.on('message', async function incoming(message) {
console.log('received: %s', message);
let msg
try {
msg = JSON.parse(message)
} catch(err) {
console.error('Failed to parse message', message);
}
if (msg.event === 'join') {
const { gameId, player, name } = msg
const game = await Game.findOne({ gameId })
if (!game) {
// Probably unexpected
return ws.send({ event: 'error', error: 'Game not found' })
}
manager.addPlayer(gameId, player, name, Date.now(), ws)
manager.broadcastUpdate(gameId, renderGameJson(game))
}
else if (msg.event === 'ping') {
console.log('got ping', ws.player)
}
else {
console.warn('Unrecognized event', msg)
}
})
})
server.on('upgrade', function (request, socket, head) {
wss.handleUpgrade(request, socket, head, function (ws) {
wss.emit('connection', ws, request);
})
})
const listener = server.listen(process.env.PORT || 3000, () => {
console.log(`Listening on ${listener.address().port}`)
})