Skip to content

Commit be4509f

Browse files
authored
Merge pull request #14 from turkmenkaan/kaan
Bug fixes for rooms
2 parents ceefa88 + 3c34a7f commit be4509f

11 files changed

+1108
-71
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
.env
2-
node_modules/*
2+
node_modules/*
3+
service-account-file.json

index.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const app = require('express')();
22
const http = require('http').createServer(app);
33
const io = require('socket.io')(http);
44
const mongoose = require('mongoose');
5+
const admin = require('firebase-admin');
56
const { v4: uuidv4 } = require('uuid');
67

78
const Question = require('./model');
@@ -10,17 +11,20 @@ require('dotenv').config();
1011

1112
const port = process.env.PORT || 3000;
1213

13-
const rooms = {};
14-
let waitingUsers = [];
15-
1614
/**
1715
* {
18-
* socket : room
16+
* socket : room (object)
1917
* ...
2018
* }
2119
*/
2220
const connectedUsers = {};
2321

22+
/**
23+
* roomId: room (object)
24+
*/
25+
const rooms = {};
26+
let waitingUsers = [];
27+
2428
mongoose.connect(process.env.DB_URI, {
2529
useNewUrlParser: true,
2630
useUnifiedTopology: true,
@@ -64,10 +68,18 @@ io.on('connection', (socket) => {
6468
console.log('[CONNECTED]');
6569
connectedUsers[socket] = null;
6670

67-
// TODO: Oyun sırasında bir kişi disconnect ederse handle'la
6871
socket.on('disconnect', () => {
6972
console.log('[DISCONNECTED]');
7073

74+
// If the user is in a room
75+
if (connectedUsers[socket]) {
76+
const activeRoom = connectedUsers[socket];
77+
console.log("[DISCONNECTED] In a room");
78+
activeRoom.endGame(socket);
79+
connectedUsers.delete(socket);
80+
rooms = rooms.filter(room => !Object.is(room, activeRoom))
81+
}
82+
7183
// Remove the user from the waiting list
7284
// Time complexity O(n)
7385
waitingUsers = waitingUsers.filter(user => !(Object.is(socket, user.socket)));
@@ -85,6 +97,7 @@ io.on('connection', (socket) => {
8597
socket.on("JOIN ROOM", (object) => {
8698
console.log(`[JOIN ROOM] ${object.username} is looking for room`);
8799

100+
88101
// If there is another user waiting for a game
89102
// put them in the same room and start the game
90103
if (waitingUsers.length > 0) {
@@ -107,9 +120,11 @@ io.on('connection', (socket) => {
107120
users.set(waitingUsers[0].socket, {
108121
username: waitingUsers[0].username
109122
});
123+
124+
// console.log(users);
125+
// console.log(`${JSON.stringify(users)}`);
110126

111-
console.log(`${JSON.stringify(users)}`);
112-
127+
113128
/*
114129
{
115130
'socket' : { username: 'kaan' }
@@ -131,7 +146,8 @@ io.on('connection', (socket) => {
131146
username: object.username,
132147
socket,
133148
});
134-
}
149+
}
150+
135151
});
136152

137153
socket.on("READY", (object) => {
@@ -160,7 +176,7 @@ io.on('connection', (socket) => {
160176
// TODO: Oyun bitince oyunu rooms'dan sil
161177
// TODO: LEAVE GAME mesajını handle'la
162178
socket.on("LEAVE GAME", (object) => {
163-
179+
console.log("[LEAVE GAME]")
164180
});
165181
})
166182

messages.txt

+11-13
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
JOIN ROOM
33
{
44
'username' : 'sahircan',
5+
'languages' : {
6+
'from' : 'en',
7+
'to' : 'tr'
8+
},
9+
'category' : 'fruits',
510
}
611

712
START GAME - Server to user.
@@ -39,22 +44,13 @@ ANSWER - User to Server. User 1's answer
3944

4045
END QUESTION - Server to room.
4146
{
42-
'answers' : [
43-
{
44-
'user' : 'kaan',
45-
'answer' : null,
46-
},
47-
{
48-
'user' : 'sahircan',
49-
'answer' : 'avokado'
50-
}
51-
],
52-
'correct-answer' : 'merhaba'
47+
'correct-answer' : 'merhaba',
48+
'timeout' : true
5349
}
5450

5551
QUESTION
5652

57-
ENDGAME
53+
END GAME
5854
{
5955
'scoreboard' : [
6056
{
@@ -67,4 +63,6 @@ ENDGAME
6763
}
6864
],
6965
'winner' : 'kaan'
70-
}
66+
}
67+
68+
LEAVE GAME

0 commit comments

Comments
 (0)