-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
190 lines (149 loc) · 6.01 KB
/
server.js
File metadata and controls
190 lines (149 loc) · 6.01 KB
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
// Require the necessary modules
const express = require('express');
const { v4: uuidv4 } = require('uuid');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);
const config = require('./config');
const possibleUserNames = [
"Lazy Lion", "Gritty Goon", "Pretty Penguin", "Jolly Jaguar", "Brave Bear", "Witty Walrus",
"Daring Deer", "Eager Elephant", "Zealous Zebra", "Calm Camel", "Silly Squirrel", "Curious Cat",
"Merry Monkey", "Rugged Rhino", "Quick Quokka", "Fancy Fox", "Happy Hippo", "Tiny Tiger", "Giddy Gorilla",
"Bouncy Bunny", "Adventurous Ant", "Playful Panda", "Keen Kangaroo", "Vibrant Vulture", "Noble Nightingale"
];
app.set('view engine', 'ejs');
app.use(express.static('public'));
// Define the login page route
app.get('/', (req, res) => {
res.render('login', { email: config.email, password: config.password });
});
// Define the index page route
app.get('/index', (req, res) => {
res.render('index');
});
app.get('/r', (req, res) => {
res.render('researcher', { activeRooms, secondActiveRooms });
});
const secondActiveRooms = {};
app.get('/:roomId/waitingRoom', (req, res) => {
const roomId = req.params.roomId;
secondActiveRooms[roomId] = {
users: []
};
res.render('second_phase', { roomId });
});
// Define the route that show a list of open waiting rooms not being used
app.get('/rw',(req,res)=>{
res.render('researcher_waiting',{secondActiveRooms})
});
// Active rooms data structure (you can choose your preferred data structure)
const activeRooms = {};
app.get('/create', (req, res) => {
const roomId = uuidv4();
activeRooms[roomId] = {
users: []
};
res.json({ roomId });
});
// Define the route to join an active room with a UUID
app.get('/:roomId', (req, res) => {
const roomId = req.params.roomId;
const userRole = req.query.userRole;
const userName = req.query.userName;
if (activeRooms.hasOwnProperty(roomId)) {
// Ensure the 'users' field exists before trying to access it.
if (activeRooms[roomId].hasOwnProperty('users')) {
const roomUsers = activeRooms[roomId].users; // Fetch roomUsers from activeRooms
res.render('room', { roomId, userRole, userName, roomUsers }); // Include roomUsers
} else {
// Handle this case appropriately; maybe render the room without users, or redirect.
res.render('room', { roomId, userRole, userName });
}
} else {
res.redirect('/');
}
});
// Retrieve all the users in a room
app.get('/room/:roomId/users', (req, res) => {
const roomId = req.params.roomId;
if (activeRooms[roomId]) {
res.json(activeRooms[roomId].users);
} else {
res.status(404).json({ message: 'Room not found' });
}
});
// Retrieve all the users in a room
app.get('/room/:roomId/criticalMoments', (req, res) => {
const roomId = req.params.roomId;
if (activeRooms[roomId]) {
res.json(activeRooms[roomId].users);
} else {
res.status(404).json({ message: 'Room not found' });
}
});
io.on('connection', socket => {
socket.on('join-room', (roomId, userId, userName, userRole) => {
console.log(`Client ${userId} is joining room: ${roomId}`);
try {
socket.join(roomId);
// Get the names of the users in the room
const userNamesInRoom = activeRooms[roomId].users.map(user => user.userName);
let availableUserNames = possibleUserNames.filter(name => !userNamesInRoom.includes(name));
// // Find an available name from possibleUserNames
let randomIndex = Math.floor(Math.random() * availableUserNames.length);
let randomName = availableUserNames[randomIndex];
if (!userName) {
userName = randomName;
}
// this does not account for all names being taken (but that is not happening)
if (activeRooms[roomId]) {
activeRooms[roomId].users.push({ userId, userName, userRole });
// Emit the updated user list to the newly joined user
io.to(roomId).emit('update-room-users');
// Check for researcher and emit status
const hasResearcher = (userRole === 'researcher');
io.to(roomId).emit('researcher_status_update', hasResearcher);
}
socket.to(roomId).emit('user-connected', userId, userName, userRole);
socket.on('disconnect', () => {
socket.to(roomId).emit('user-disconnected', userId);
//sends message that user disconnected
io.to(roomId).emit('chat-message', { userId, message: "user disconnected" });
// Emit a chat message to notify all users in the room
io.to(roomId).emit('chat-message', { userId: "system", message: 'a user has left the room' });
// Remove user from the list of users in the room
if (activeRooms[roomId]) {
const index = activeRooms[roomId].users.findIndex(u => u.userId === userId);
if (index !== -1) {
activeRooms[roomId].users.splice(index, 1);
}
io.to(roomId).emit('update-room-users');
// Re-check for researcher and emit status
const hasResearcher = activeRooms[roomId].users.some(user => user.userRole === 'Researcher');
io.to(roomId).emit('researcher_status_update', hasResearcher);
}
});
socket.on('chat-message', message => {
console.log("chat message sent: ", message);
io.to(roomId).emit('chat-message', { userId, message });
});
} catch (error) {
console.error('Error in join-room event:', error);
}
});
socket.on('join-second-room', (roomId, userId) => {
try {
socket.join(roomId);
socket.to(roomId).emit('user-connected', userId);
socket.on('disconnect', () => {
socket.to(roomId).emit('user-disconnected', userId);
});
} catch (error) {
console.error('Error in join-room event:', error);
}
});
});
// Start the server
server.listen(3000, () => {
console.log('Server started on port 3000');
});