forked from meghprkh/socket-buzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
admin.js
57 lines (54 loc) · 1.94 KB
/
admin.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
var jwt = require('jsonwebtoken');
var fs = require('fs')
var state = require('./state');
var scoring = require('./scoring');
const config = require('./config')
module.exports = function (app, io) {
app.post('/adminAuth', function (req, res) {
var password = req.body.password;
if (password == config.ADMIN_PASSWORD) {
var token = jwt.sign({}, config.PRIVATE_KEY, {
expiresIn: '5h',
subject: 'admin'
});
res.send(token)
} else res.status(401).send()
})
io.of('/admin').use(function(socket, next) {
var token = socket.request._query.token;
if (jwt.verify(token, config.PRIVATE_KEY, {subject: 'admin'}))
next();
else next(new Error("not authorized"))
});
io.of('/admin').on('connection', function(socket) {
socket.emit('queue', state.queue)
socket.on('correctAnswer', function () {
if (state.queue.length) {
var team = state.queue[0];
state.scores[team] = scoring.correctAnswer(state.scores[team])
}
state.queue = []
state.incorrectTeams = []
state.questionNumber++
fs.readFile('./questions/' + require('./getFileName')(state.questionNumber), 'utf8', function (err, data) {
if (err) throw err;
state.currentQuestion = data
state.questionBuzzable = require('./questionBuzzable')(state.questionNumber)
io.of('/public').emit('newQuestion', data)
io.of('/buzzer').emit('questionBuzzable', state.questionBuzzable)
});
socket.emit('queue', state.queue)
io.of('/public').emit('scoreboard', state.scores)
})
socket.on('incorrectAnswer', function () {
if (state.queue.length) {
var team = state.queue[0];
state.scores[team] = scoring.incorrectAnswer(state.scores[team])
state.incorrectTeams.push(team)
state.queue = state.queue.slice(1)
}
socket.emit('queue', state.queue)
io.of('/public').emit('scoreboard', state.scores)
})
})
}