This repository has been archived by the owner on Jun 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
167 lines (138 loc) · 4.48 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
var express = require('express')
var bodyParser = require('body-parser')
var fs = require('fs')
var WebSocket = require('ws')
var game = require('./game')
var http = require('http')
var https= require('https');
var app = express()
var answers = new Map()
var answerPerClient = new Map()
var clientsPerAnswer = new Map()
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static('public'))
app.use('/jquery', express.static(__dirname + '/node_modules/jquery/dist/'))
app.use('/bootstrap', express.static(__dirname + '/node_modules/bootstrap/dist/'))
app.use('/mustache', express.static(__dirname + '/node_modules/mustache/'))
app.use('/handlebars', express.static(__dirname + '/node_modules/handlebars/'))
const wss = new WebSocket.Server( {port:8999} );
wss.on('connection', function(ws) {
// send the question initially if game started
if(game.question() != null)
ws.send(JSON.stringify(game.question()))
ws.on('message', function(msg) {
const answer = JSON.parse(msg);
console.log("received: %s", (JSON.stringify(answer)));
handleAnswer(answer);
})
})
function broadcast() {
wss.clients.forEach(function(client) {
if(client !== wss && client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(game.question()));
}
})
}
function needsLogin(req, res, next) {
const auth = { login: 'admin', password: 'secret' }
const b64auth = (req.headers.authorization || '').split(' ')[1] || ''
const [login, password] = new Buffer(b64auth, 'base64').toString().split(':')
if(!login || !password || login !== auth.login || password !== auth.password) {
res.set('WWW-Authenticate', 'Basic realm="quiz"')
res.status(401).send('Forbidden').end()
} else {
next();
}
}
app.use('/admin/*', needsLogin, function(req, res, next) {
//--- pass control to next handler
next()
})
app.get('/admin/next', function(req, res) {
const n = game.next()
res.json(n)
broadcast()
})
app.get('/admin/prev', function(req, res) {
const p = game.prev()
res.json(p)
broadcast()
})
app.get('/admin/current', function(req, res) {
const p = game.question()
res.json(p)
})
app.post('/answer', function(req, res) {
handleAnswer(req.body);
res.end()
})
app.get('/admin/stats/:index', function(req, res) {
const index = req.params.index
question = game.index(index)
//--- assert each choice is at least once in the list
var merged = question.choices;
if( answers.get(question.id) != null )
merged = merged.concat(answers.get(question.id))
var histogram = merged
.reduce(function(accumulator,value) {
accumulator[value] = (value in accumulator ? accumulator[value] + 1 : 1);
return accumulator
}, {});
Object.keys(histogram).map(function(k) { return --histogram[k] })
//---
//--- map clients per answer to Object
var o = Object.create(null);
var clients = clientsPerAnswer.get(question.id);
if(clients != null) clients.forEach((v,k) => o[k] = v);
res.json({
question: question,
histogram: histogram,
users: o
})
})
function handleAnswer(answer) {
// validate
if(answer.id !== game.question().id) {
console.log("received invalid answer %s for question %s", answer, game.question());
return
}
// special handling if client is set
if(Object.keys(answer).includes('client') ) {
var answerMap
if(answerPerClient.has(answer.client)) {
answerMap = answerPerClient.get(answer.client)
} else {
answerMap = new Map()
answerPerClient.set(answer.client, answerMap);
}
answerMap.set(answer.id, answer.choice)
//--- add client per answer
if(clientsPerAnswer.has(answer.id)) {
answerMap = clientsPerAnswer.get(answer.id)
} else {
answerMap = new Map()
}
if(answerMap.has(answer.choice)) {
answerMap.get(answer.choice).push(answer.client)
} else {
answerMap.set(answer.choice, [answer.client])
}
clientsPerAnswer.set(answer.id, answerMap)
}
// if id already exists - push back
if(answers.has(answer.id)) {
var choices = answers.get(answer.id);
choices.push(answer.choice);
} else {
answers.set(answer.id, [answer.choice])
}
console.log((answers))
}
//var privateKey = fs.readFileSync('ssl/server.key', 'utf8')
//var certificate = fs.readFileSync('ssl/server.cert', 'utf8')
//var credentials = {key: privateKey, cert: certificate};
var httpServer = http.createServer(app)
//var httpsServer = https.createServer(credentials, app)
httpServer.listen(8080);
//httpsServer.listen(8443);