-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
252 lines (221 loc) · 8.08 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
const express = require('express');
const app = express();
const http = require('http').createServer(app);
const io = require('socket.io')(http);
const bcrypt = require('bcrypt');
const crypto = require('crypto');
const { warn } = require('console');
const fs = require('fs').promises;
const path = require('path');
app.use(express.json());
app.use(express.static('public'));
const registeredUsers = new Map();
const buzzerPresses = [];
let buzzerPressed = false;
const adminPassword = process.env.ADMIN_PASSWORD || 'secret';
const stateFilePath = path.join(__dirname, 'appState.json');
function getTeamByName(teamName) {
return Array.from(registeredUsers.values()).find(team => team.teamName === teamName);
}
function checkAuth(req, res, next) {
const authHeader = req.headers.authorization;
if (!authHeader) {
return res.status(401).json({ error: 'No credentials sent!' });
}
const encodedCredentials = authHeader.split(' ')[1];
const decodedCredentials = Buffer.from(encodedCredentials, 'base64').toString('ascii');
const [username, password] = decodedCredentials.split(':');
if (username === 'admin' && bcrypt.compareSync(password, bcrypt.hashSync(adminPassword, 10))) {
next(); // Proceed to the next middleware or route handler
} else {
res.status(401).json({ error: 'Invalid credentials' });
}
}
async function saveState() {
const state = {
registeredUsers: Array.from(registeredUsers.entries()),
buzzerPresses,
buzzerPressed
};
await fs.writeFile(stateFilePath, JSON.stringify(state, null, 2));
}
async function loadState() {
try {
const data = await fs.readFile(stateFilePath, 'utf8');
const state = JSON.parse(data);
state.registeredUsers.forEach(([key, value]) => registeredUsers.set(key, value));
buzzerPresses.push(...state.buzzerPresses);
buzzerPressed = state.buzzerPressed;
console.log('State loaded successfully');
} catch (error) {
console.log('No saved state found or error loading state:', error.message);
}
}
app.get('/', (req, res) => {
res.sendFile(__dirname + '/public/user.html');
});
app.get('/main', (req, res) => {
res.sendFile(__dirname + '/public/main.html');
});
app.get('/admin', (req, res) => {
res.sendFile(__dirname + '/public/admin.html');
});
app.get('/history', (req, res) => {
res.json(buzzerPresses);
});
app.post('/register', (req, res) => {
const { teamName, socketId } = req.body;
const team = getTeamByName(teamName);
if (team) {
res.status(400).json({ error: 'Team name already registered' });
} else {
const pin = crypto.randomBytes(4).toString('hex');
registeredUsers.set(pin, { teamName, socketId, approved: false });
res.json({ success: true, pin });
io.emit('updateUsers', Array.from(registeredUsers.entries()));
saveState();
}
});
app.post('/reRegister', (req, res) => {
const { pin, socketId } = req.body;
const team = registeredUsers.get(pin);
if (team && team.approved) {
team.socketId = socketId;
const hasPressed = buzzerPresses.some(press => press.teamName === team.teamName);
res.json({ success: true, hasPressed, teamName: team.teamName, description: team.description });
} else {
res.status(401).json({ error: 'Invalid team name or PIN, or registration not approved' });
}
});
app.post('/changeTeamName', (req, res) => {
const { oldName, newName, pin } = req.body;
const team = registeredUsers.get(pin);
if (team && team.teamName === oldName) {
if (getTeamByName(newName)) {
res.status(400).json({ error: 'New team name already exists' });
} else {
team.pendingNameChange = newName;
res.json({ success: true, message: 'Name change request submitted for approval' });
}
} else {
res.status(401).json({ error: 'Invalid team name or PIN' });
}
});
app.post('/admin/login', checkAuth, (req, res) => {
const { password } = req.body;
if (bcrypt.compareSync(password, bcrypt.hashSync(adminPassword, 10))) {
res.json({ success: true });
} else {
res.status(401).json({ error: 'Invalid password' });
}
});
app.get('/admin/users', checkAuth, (req, res) => {
res.json(Array.from(registeredUsers.entries()));
});
app.post('/admin/approve', checkAuth, (req, res) => {
const { teamName, approve } = req.body;
const team = getTeamByName(teamName);
if (team) {
team.approved = approve;
res.json({ success: true });
io.emit('updateUsers', Array.from(registeredUsers.entries()));
if (approve && team.socketId) {
io.to(team.socketId).emit('registrationApproved', team);
}
saveState();
} else {
res.status(404).json({ error: 'Team not found' });
}
});
app.post('/admin/approveNameChange', (req, res) => {
const { oldName, newName, approve } = req.body;
const team = getTeamByName(oldName);
if (team && team.pendingNameChange === newName) {
if (approve) {
team.teamName = newName;
io.to(team.socketId).emit('nameChange', { oldName, newName });
}
delete team.pendingNameChange;
res.json({ success: true });
io.emit('updateUsers', Array.from(registeredUsers.entries()));
} else {
res.status(404).json({ error: 'Team or name change request not found' });
}
});
app.post('/admin/editUser', checkAuth, (req, res) => {
const { oldName, newName, description } = req.body;
const team = getTeamByName(oldName);
if (team) {
if (getTeamByName(newName) && newName !== oldName) {
res.status(400).json({ error: 'New team name already exists' });
} else {
team.teamName = newName;
team.description = description;
delete team.pendingNameChange;
io.to(team.socketId).emit('teamUpdated', team);
io.emit('updateUsers', Array.from(registeredUsers.entries()));
res.json({ success: true });
saveState();
}
} else {
res.status(404).json({ error: 'Team not found' });
}
});
app.post('/admin/removeUser', checkAuth, (req, res) => {
const { teamName } = req.body;
const pin = Array.from(registeredUsers.entries()).find(([k,v]) => v.teamName === teamName)[0];
if (pin) {
const team = registeredUsers.get(pin);
registeredUsers.delete(pin);
if (team.socketId) {
io.to(team.socketId).emit('registrationRemoved', team);
}
io.emit('updateUsers', Array.from(registeredUsers.entries()));
res.json({ success: true });
saveState();
} else {
res.status(404).json({ error: 'Team not found' });
}
});
app.post('/admin/reset', checkAuth, (req, res) => {
buzzerPressed = false;
buzzerPresses.length = 0;
io.emit('reset');
res.json({ success: true });
saveState();
});
io.on('connection', (socket) => {
socket.on('buzzerPress', (data) => {
const { pin } = data;
const team = registeredUsers.get(pin);
if (team && team.socketId === socket.id) {
const { teamName, description } = team;
const timestamp = new Date().toISOString();
buzzerPresses.push({ teamName, timestamp });
if (!buzzerPressed) {
buzzerPressed = true;
io.emit('buzzerPressed', { teamName, description, timestamp });
}
io.emit('updatePresses', buzzerPresses);
saveState();
}
});
socket.on('disconnect', () => {
for (const [pin, team] of registeredUsers.entries()) {
if (team.socketId === socket.id) {
if (!team.approved) {
registeredUsers.delete(pin);
}
team.socketId = null;
break;
}
}
saveState();
io.emit('updateUsers', Array.from(registeredUsers.entries()));
});
});
const port = process.env.PORT || 3000;
http.listen(port, () => {
console.log(`Server running on port ${port}`);
loadState();
});