/
socketController.js
274 lines (259 loc) · 10.7 KB
/
socketController.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
"use strict";
// Socket Connections - All sockets connected will be stored in this connections array
const connections = [];
// This is the interface of connections (Just for our reference becos we love typescript so much)
// Connections: []Connection
// Connection {
// id: string;
// profileName: string;
// secretCode: string;
// ready: boolean
// userType: string;
// }
const rooms = [];
// room: Room
// Room {
// name: string;
// secretCode: string;
// readySessionEnabled: boolean;
// }
var availableCodeList = ['GROWTH','AMAZE','REFRESH','MAGIC','BOOZE','MASTER', 'HEROIC','HUSTLE','TEQUILA','POLISH','PARADOX','CANVAS','ISLAND','WONDER'];
var usedCodeList = [];
function findConnection(id){
return connections.filter(function(connection) {return connection.id === id;})[0];
}
function findRoom(secretCode){
return rooms.filter(function(room) {return room.secretCode === secretCode;})[0];
}
function findNumberOfRoomConnections(secretCode){
var roomConnections = connections.filter(function(connection) {return connection.secretCode === secretCode})
return roomConnections.length;
}
function findNumberOfReadyConnections(secretCode){
var readyConnections = connections.filter(function(connection) {return (connection.ready === true && connection.secretCode === secretCode)})
return readyConnections.length;
}
function resetAllStatusForRoomConnections(socket,secretCode){
var allRoomConnections = connections.filter(function(connection) {return connection.secretCode === secretCode;});
allRoomConnections.forEach(function(element){
// may not necessarily want to reset profile name in future
element.profileName = null;
element.secretCode = null;
element.ready = null;
element.userType = null;
// we need to get all sockets to leave
})
}
function resetReadyStatusForRoomConnections(socket,secretCode){
var allRoomConnections = connections.filter(function(connection) {return connection.secretCode === secretCode;});
allRoomConnections.forEach(function(element){
// may not necessarily want to reset profile name in future
element.ready = null;
})
}
function socketsLeaveRoom(secretCode){
var allRoomConnections = connections.filter(function(connection) {return connection.secretCode === secretCode;});
allRoomConnections.forEach(function(element){
element.socket.leave(secretCode)
})
}
function secretCodeGenerator(availableList, usedList) {
//take first element of availableCodeList array,
//add to end of usedCodeList
var secretCode = availableList.shift();
console.log('secret code is ' + secretCode);
usedList.push(secretCode);
return secretCode;
}
function returnBackSecretCode(availableList, usedList, secretCode){
var indexOfRemovedCode = usedList.indexOf(secretCode);
usedCodeList.splice(indexOfRemovedCode, 1);
availableList.push(secretCode);
}
// =================================== Socket Controller Functions =================================
function connect(socket){
//new connection, save the socket to them
// when a socket first connects, provide all these attributes so that you would not get error when you do a status reset
connections.push({
socket: socket,
id: socket.id,
profileName: null,
secretCode: null,
ready: null,
userType: null
});
console.log(`## New connection (${socket.id}). Total: ${connections.length}.`);
}
function disconnect(socket,io){
//find the connection and remove from the collection
let connection = findConnection(socket.id);
if (connection){
if (connection.userType === "instructor") {
let room = findRoom(connection.secretCode);
if (room.readySessionEnabled === true) {
instructorEndsReadySession(socket)
}
closeClass(socket,io);
}
if (connection.userType === "student") {
if (connection.ready === true) {
studentNotReady(socket, io)
}
studentLeaveClass(socket,io)
}
connections.splice(connections.indexOf(connection), 1);
console.log(`## Connection (${connection.id}) (${socket.id}) disconnected. Remaining: ${connections.length}.`);
}
socket.disconnect();
}
// =================================== Instructor ===========================
function submitClassName(socket, className){
// this connection guy does nothing yet (just put here in case)
var room = {
name: className,
// pass in secretcodefunction
secretCode: secretCodeGenerator(availableCodeList,usedCodeList)
}
socket.join(room.secretCode)
rooms.push(room);
let connection = findConnection(socket.id);
if (connection){
connection.secretCode = room.secretCode;
connection.userType = "instructor";
socket.emit('createSecretCode', room)
}
}
function closeClass(socket,io){
let connection = findConnection(socket.id);
if (connection) {
let room = findRoom(connection.secretCode);
if (room){
console.log(`## Room (${room.name}) closed. Remaining: ${rooms.length}.`);
rooms.splice(rooms.indexOf(room), 1);
returnBackSecretCode(availableCodeList,usedCodeList,room.secretCode);
// you want to find all connections in the room with a ready status and set them all to false
resetAllStatusForRoomConnections(socket,room.secretCode)
// reset room connection back to 0 for everyone when room is closed so that when you start a new room, the room starts with 0 connection
io.to(room.secretCode).emit("updateNumberOfRoomConnections", (findNumberOfRoomConnections(room.secretCode)));
// this socketsLeaveRoom function must come after the io because if it comes first, we would not be able to emit to all sockets in that room anymore
socketsLeaveRoom(room.secretCode)
socket.broadcast.to(room.secretCode).emit('studentsCloseClass')
}
}
}
function instructorCallReady(socket){
// use the classroomService to emit instructorReady() to the server that we are starting are u ready
// use the server to emit startStudentReady() to all those in the room (excluding instructor) - use broadcast
// and in the service (listen for an emit)
// in the emit (reroute the student into student ready)
let connection = findConnection(socket.id);
if (connection) {
let room = findRoom(connection.secretCode);
if (room) {
room.readySessionEnabled = true;
socket.broadcast.to(room.secretCode).emit('startStudentReady')
}
}
}
function instructorEndsReadySession(socket){
let connection = findConnection(socket.id);
if (connection){
let room = findRoom(connection.secretCode);
if (room) {
// you want to find all connections in the room with a ready status and set them all to false
room.readySessionEnabled = false;
resetReadyStatusForRoomConnections(socket,room.secretCode)
socket.broadcast.to(room.secretCode).emit('studentsEndReadySession')
}
}
}
// =================================== Student ===========================
function submitProfileName(socket, profileName){
let connection = findConnection(socket.id);
if (connection){
connection.profileName = profileName;
}
}
function submitSecretCode(socket, secretCode, io){
// this connection guy does nothing yet (just put here in case)
let room = findRoom(secretCode);
if (room) {
let connection = findConnection(socket.id);
if (connection){
connection.secretCode = secretCode;
connection.userType = "student"
socket.join(room.secretCode)
// emit something back so that we can route him to the next page
// =>> added this shit in when we were sleepy
socket.emit('secretCodeExist', {secretCodeExist: true, room: room})
// connections should be number of sockets who are joined to the room - 1 (instructor)
io.to(room.secretCode).emit("updateNumberOfRoomConnections", (findNumberOfRoomConnections(secretCode)-1));
}
} else {
// emit him some message so that client knows that the secret code is wrong
socket.emit('secretCodeExist', false)
}
}
function studentLeaveClass(socket,io){
let connection = findConnection(socket.id);
if (connection){
var secretCodeOfPreviousRoom = connection.secretCode
connection.secretCode = null;
if (connection.ready){
connection.ready = null;
}
io.to(secretCodeOfPreviousRoom).emit("updateNumberOfRoomConnections", (findNumberOfRoomConnections(secretCodeOfPreviousRoom)-1))
socket.leave(connection.secretCode)
}
}
function studentReady(socket,io){
let connection = findConnection(socket.id);
if (connection){
// the difference between studentReady and studentNotReady is setting connection.ready true and false
connection.ready = true;
// end of difference
let room = findRoom(connection.secretCode);
if (room) {
// this socket emit is for the individual student's ready toggle button
socket.emit("RecievedYourLovelyReadyResponse")
// this socket broadcast is for everyone in the room so that everyone knows the number of students who are ready
io.to(room.secretCode).emit("updateStudentReady", findNumberOfReadyConnections(connection.secretCode))
// on the client side, it is waiting for updatestudentready
// it will save a property for the number of studentready
// display it on both the instructor and student dashboard
// so we need to use the service reference hack so that both student and instructor ready component will be able to access the updated property
// 1)Once i receive the emit, i need to save that argument that is passed to me under a property object
// 2)I need to create a getProperty in the service (just above the constructor)
// 3)In my two components, i need to make a getPropertyRequest
}
}
}
function studentNotReady(socket, io){
let connection = findConnection(socket.id);
if (connection){
connection.ready = false;
let room = findRoom(connection.secretCode);
if (room) {
// this socket emit is for the individual student's ready toggle button
socket.emit("RecievedYourLovelyNotReadyResponse")
console.log("server received lovely not ready response")
// this socket broadcast is for everyone in the room so that everyone knows the number of students who are ready
io.to(room.secretCode).emit("updateStudentReady", findNumberOfReadyConnections(connection.secretCode))
console.log("studentNotReady completed run")
}
}
}
// Functions we are exporting to (server.js)
module.exports = {
connect: connect,
disconnect: disconnect,
submitClassName: submitClassName,
closeClass: closeClass,
instructorEndsReadySession: instructorEndsReadySession,
submitProfileName: submitProfileName,
submitSecretCode: submitSecretCode,
studentLeaveClass: studentLeaveClass,
instructorCallReady: instructorCallReady,
studentReady: studentReady,
studentNotReady: studentNotReady
}