forked from bufferapp/buzzer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
112 lines (94 loc) · 2.77 KB
/
index.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
const http = require('http')
const express = require('express')
const socketio = require('socket.io')
const basicAuth = require('express-basic-auth');
const dotenv = require('dotenv').config();
const app = express();
const server = http.Server(app);
const io = socketio(server);
const title = 'Remote Quiz'
let data = {
users: new Set(),
buzzes: new Set(),
}
function filterTeam(users, team) {
let result = users.filter(function (user) {
return user.team === team;
});
console.log(team)
console.log(result)
return result;
}
getData = () => ({
users: [...data.users],
buzzes: [...data.buzzes].map(b => {
const [name, team] = b.split('-')
return { name, team }
})
})
app.use(express.static('public'))
app.set('view engine', 'pug')
app.get('/', (req, res) => res.render('index', Object.assign({ title }, getData())))
app.get('/host', basicAuth({
users: { admin: process.env.ADMIN_BUZZER_PASSWORD },
challenge: true // <--- needed to actually show the login dialog!
}), (req, res) => res.render('host', Object.assign({ title }, getData())))
function logUsers() {
console.log("Current players : ")
console.log(JSON.stringify([...data.users]))
}
function UpdateUsersView() {
io.emit('active', data.users.size)
io.emit('usersInTeam', { usersInMayo: filterTeam([...data.users], 'Mayo'), usersInKetchup: filterTeam([...data.users], 'Ketchup') })
}
io.on('connection', (socket) => {
var userId;
socket.on('join', (user) => {
console.log(JSON.stringify(user) + " a rejoint !")
userId = user.id
data.users.add(user)
UpdateUsersView()
io.emit('checkBuzzerAvailable', [...data.buzzes].find(u => u === (user.name + '-' + user.team)))
logUsers()
})
socket.on('buzz', (user) => {
data.buzzes.add(`${user.name}-${user.team}`)
io.emit('buzzes', [...data.buzzes])
console.log(`${user.name} a buzzé !`)
})
socket.on('clear', () => {
data.buzzes = new Set()
io.emit('buzzes', [...data.buzzes])
io.emit('disableBuzzer')
console.log(`Clear buzzes`)
})
socket.on('updateUsersView', () => {
UpdateUsersView()
})
// Custom event when the player change team
socket.on('changeTeam', function (userSent) {
console.log(`${userId} disconnect ! `);
data.users.forEach(function (user) {
if (user.id === userSent.id) {
data.users.delete(user);
}
});
UpdateUsersView()
logUsers()
});
// Default event for refresh
socket.on('disconnect', function () {
console.log(`${userId} disconnect ! `);
data.users.forEach(function (user) {
if (user.id === userId) {
data.users.delete(user);
}
});
UpdateUsersView()
logUsers()
});
})
const PORT = process.env.PORT || 3000;
server.listen(PORT, () => {
console.log(`Our app is running on port ${PORT}`);
});