-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
353 lines (281 loc) · 11.4 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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
const { read } = require("fs");
const Express = require("express")();
const Http = require("http").Server(Express);
const Socketio = require("socket.io")(Http, {
cors: {
origin: "http://localhost:4200",
methods: ["GET", "POST"]
}
});
// {teacher: int, theme:[int], question:string, ans1:string, ans2:string, ans3:string, correct:string, unit:string, resol:string, date:string, img:string}
var exercises = [];
loadExercises();
var connections = [];
var users = [];
var rooms = ["None"];
var chat = {};
var rooms_idx = {};
var rooms_started = {};
var last_room = null;
var rooms_settings = {};
var rooms_users = {"None":{}}; // {room_name : [socketids]}
var room_timer = {};
var room_points = {};
var rooms_ended = [];
function removeRoomVariables(room_id) {
console.log("destroying room");
console.log(room_id);
// handles the end of the game
rooms_started[room_id] = { state: false, counter: 0 };
rooms.splice(rooms.indexOf(room_id), 1);
Socketio.emit("loadRooms", rooms);
delete rooms_idx[room_id];
delete rooms_settings[room_id];
delete rooms_users[room_id];
delete chat[room_id];
rooms_ended.splice(rooms_ended.indexOf(room_id), 1);
console.log("Successfully destroyed room: " + room_id);
}
Socketio.on("connection", socket => {
rooms_users["None"][socket.id] = false;
// When a new socket connects its pushed to the connections array
connections.push(socket.id);
Socketio.emit("socket_id", socket.id);
// sends all the socket connections
Socketio.emit("connections", connections);
// load rooms when connects
console.log(rooms);
Socketio.emit("loadRooms", rooms);
// when a socket disconnects its removed from the connections array
socket.on("disconnect", () => {
connections.splice(connections.indexOf(socket.id), 1);
console.log(connections.toString());
users.splice(users[socket.id],1);
Socketio.emit("connections", connections);
socket.leave(last_room);
delete rooms_users["None"][socket.id];
var room_id = null;
for (var room in rooms_users) {
if (Object.keys(rooms_users[room])!=null) {
if (Object.keys(rooms_users[room]).includes(socket.id)) {
room_id=room;
}
}
}
p_ready = 0;
for(var key in rooms_users[room_id]) {
if(rooms_users[room_id][key] == true) {
p_ready++;
}
}
if (p_ready >0) {
p_ready--;
}
Socketio.to(room_id).emit("players_ready", p_ready);
if (rooms_users[room_id]!=null) {
total_players=(Object.keys(rooms_users[room_id]).length);
Socketio.to(room_id).emit("totalPlayers",total_players);
} else {
Socketio.to(room_id).emit("totalPlayers", 0);
}
});
// Send random question to clients in room
socket.on("client_get_question", (room_id) => {
for(let i = 0; i < rooms.length; i++) {
Socketio.to(rooms[i]).emit("server_get_question", exercises[rooms[i]]);
}
});
// store chat messages
socket.on("send_message", (data, room_id) => {
socket.join(room_id);
chat[room_id].push({'src': users[socket.id] , 'data': data});
Socketio.to(room_id).emit("chat", chat[room_id]);
console.log(chat);
});
// adds the new room to the respective data structures on the server and tells all clients to load the list of rooms
socket.on("createRoom", (socket_id, data) => {
loadExercises();
var tmp = rooms.length;
rooms.push(data.name);
chat[rooms[rooms.length - 1]] = [];
var idx = Math.floor(Math.random()* exercises.length);
rooms_idx[rooms[rooms.length - 1]] = idx;
rooms_started[rooms[rooms.length - 1]] = {state: false, counter: 10};
rooms_settings[rooms[rooms.length - 1]] = {numExercises : data.numExercises, exercisesLeft : data.numExercises};
Socketio.emit("loadRooms", rooms);
});
//load chat messages when enters
socket.on("change_room", (room_id, last_room_tmp) => {
socket.leave(last_room_tmp);
if(last_room_tmp != "") {
delete rooms_users[last_room_tmp][socket.id];
}
socket.join(room_id);
Socketio.to(socket.id).emit("chat", chat[room_id]);
Socketio.to(socket.id).emit("question_change_room", exercises[rooms_idx[room_id]]);
rooms.forEach( room_name => {
if(Socketio.sockets.adapter.rooms.get(room_name) != null) {
var tmp_array_users = Array.from(Socketio.sockets.adapter.rooms.get(room_name));
var tmp_dicts_users = {};
for(index in tmp_array_users) {
tmp_dicts_users[tmp_array_users[index]] = false;
}
console.log(tmp_dicts_users);
rooms_users[room_name] = tmp_dicts_users;
}
});
var l_ready = 0;
for(var key in rooms_users[last_room_tmp]) {
if(rooms_users[last_room_tmp][key] == true) {
l_ready++;
}
}
Socketio.to(last_room_tmp).emit("players_ready",l_ready);
if (rooms_users[room_id]!=null) {
players=(Object.keys(rooms_users[room_id]).length);
Socketio.to(room_id).emit("totalPlayers",players);
}
if (rooms_users[last_room_tmp]!=null) {
players=(Object.keys(rooms_users[last_room_tmp]).length);
Socketio.to(last_room_tmp).emit("totalPlayers",players);
}
var p_ready = 0;
for(var key in rooms_users[room_id]) {
if(rooms_users[room_id][key] == true) {
p_ready++;
}
}
console.log("rooms users");
console.log(rooms_users);
console.log("last room");
console.log(last_room_tmp);
if(Object.keys(rooms_users).includes(last_room_tmp)) {
console.log(Object.keys(rooms_users[last_room_tmp]));
if(Object.keys(rooms_users[last_room_tmp]).length == 0) {
removeRoomVariables(last_room_tmp);
}
}
// // change socket id room
// if(Object.keys(rooms_users).includes(last_room_tmp)) {
// console.log(Object.keys(rooms_users[last_room_tmp]));
// console.log(last_room_tmp);
// console.log(rooms_users);
// // room destroyed when all clients exit the room
// console.log(Object.keys(rooms_users[last_room_tmp]).length);
// if(Object.keys(rooms_users[last_room_tmp]).length == 0) {
// removeRoomVariables(last_room_tmp);
// }
// } else {
// console.log("ainda nao");
// }
Socketio.to(room_id).emit("players_ready",p_ready);
});
//send total players in room
socket.on("client_get_totalPlayers", (room_id) => {
players=(Object.keys(rooms_users[room_id]).length);
Socketio.to(room_id).emit("totalPlayers",players);
});
//send players ready in room
socket.on("client_get_players_ready", (room_id) => {
p_ready = 0;
for(var key in rooms_users[room_id]) {
if(rooms_users[room_id][key] == true) {
p_ready++;
}
}
Socketio.to(room_id).emit("players_ready", p_ready);
});
//playerNotReady
socket.on("playerNotReady", (room_id) => {
rooms_users[room_id][socket.id] = false;
var p_ready = 0;
for(var key in rooms_users[room_id]) {
if(rooms_users[room_id][key] == true) {
p_ready++;
}
}
Socketio.to(room_id).emit("players_ready",p_ready);
});
// starts the game
socket.on("playerReady", (room_id) => {
rooms_users[room_id][socket.id] = true;
var p_ready = 0;
for(var key in rooms_users[room_id]) {
if(rooms_users[room_id][key] == true) {
p_ready++;
}
}
Socketio.to(room_id).emit("players_ready",p_ready);
//count elemnts in dict
if(p_ready == Object.keys(rooms_users[room_id]).length) {
Socketio.to(socket.id).emit("question_change_room", exercises[rooms_idx[room_id]]);
rooms_started[room_id]["state"] = true;
tmp_roomTimer = setInterval(function() {
checkExercisesLeft(room_id);
rooms_started[room_id]["counter"] = rooms_started[room_id]["counter"] - 1;
console.log(rooms_started[room_id]["counter"]);
if(rooms_started[room_id]["counter"] >= 0) {
Socketio.to(room_id).emit("counter", rooms_started[room_id]["counter"]);
} else if(rooms_started[room_id]["counter"] == -1) {
Socketio.to(room_id).emit("show_result", true);
rooms_settings[room_id]["exercisesLeft"]--;
console.log(rooms_settings);
}
else if(rooms_started[room_id]["counter"] == -3) {
Socketio.to(room_id).emit("show_result", false);
// generate another random question for each room
rooms.forEach(element => {
rooms_idx[element] = Math.floor(Math.random() * exercises.length);
});
console.log(rooms_idx);
// send the new question for each room
rooms.forEach(element => {
Socketio.to(element).emit("server_get_question", exercises[rooms_idx[element]]);
});
rooms_started[room_id]["counter"] = 11;
}
}, 1000);
room_timer[room_id] = tmp_roomTimer;
Socketio.to(room_id).emit("game_started", true);
}
});
//tells the name of the user that connected
socket.on("nname",username => {
users[socket.id]=username;
});
// sends the total points on that room associated to each username
socket.on("client_total_points", (socket_id, room_id, correct_answers) => {
room_points[room_id][users[socket_id]] = correct_answers;
if(Object.keys(room_points[room_id]).length == Object.keys(rooms_users[room_id]).length) {
Socketio.to(room_id).emit("game_results", room_points[room_id]);
}
});
});
Http.listen(3000, () => {
console.log("Listening at port 3000!");
});
// checks if there is exercises left on that room.
// if there aren't the game stop on that room.
function checkExercisesLeft(room_id) {
if(rooms_settings[room_id]["exercisesLeft"] <= 0) {
clearInterval(room_timer[room_id]);
rooms_ended.push(room_id);
room_points[room_id] = {};
Socketio.to(room_id).emit("game_over");
}
}
// gets exercises from the rest api
function loadExercises() {
const http = require("http");
http.get("http://127.0.0.1:8000/exercise/api/exercises", (resp) => {
let data = "";
resp.on("data", (chunk) => {
data += chunk;
});
resp.on("end", () => {
for(let i = 0; i < JSON.parse(data).length; i++) {
exercises.push(JSON.parse(data)[i]);
}
});
});
}