-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
82 lines (68 loc) · 1.92 KB
/
app.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
const express = require('express');
const http = require('http');
const socketIO = require('socket.io');
const app = express();
const server = http.createServer(app);
const io = socketIO(server);
app.set('view engine', 'ejs');
app.use(express.static('public'));
app.get('/', (req, res) => {
res.render('index', {users: users});
});
app.get('/admin', (req, res) => {
res.render('admin');
});
app.get('/about', (req, res) => {
res.render('about');
});
let currentTask = '';
let estimatesHidden = true;
let adminSocketId = null;
const users = {};
io.on('connection', (socket) => {
socket.on('post-task', (task) => {
estimatesHidden = true;
currentTask = task;
socket.broadcast.emit('new-task', task);
});
socket.on('admin-identify', () => {
currentTask = '';
adminSocketId = socket.id;
});
socket.on('user-identify', (user) => {
users[socket.id] = user;
io.emit('load-cards', users);
if(currentTask !== ''){
socket.broadcast.emit('new-task', currentTask);
}
});
socket.on('submit-estimate', (estimate) => {
if (users[socket.id]) {
users[socket.id].estimate = estimate;
io.emit('new-estimate', users[socket.id]);
}
});
socket.on('reveal-estimates', () => {
if (socket.id === adminSocketId) {
estimatesHidden = false;
io.emit('reveal-estimates', users);
currentTask = '';
}
});
socket.on('clear-estimates', () => {
for (let user in users) {
users[user].estimate = null;
}
io.emit('load-cards', users);
});
socket.on('disconnect', () => {
if (socket.id === adminSocketId) {
adminSocketId = null;
} else {
delete users[socket.id];
}
});
});
server.listen(3000, () => {
console.log('Server is running on http://localhost:3000');
});