-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroom-manager.js
200 lines (159 loc) · 5.64 KB
/
room-manager.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
var _ = require('lodash');
var Datastore = require('./datastore');
var Privacies = [ 'Public', 'Private' ];
var UserManager = require('./user-manager');
var UserClass = require('./user-class');
function RoomManagerClass() {
this.Rooms = {
Private: {},
Public: {}
};
}
RoomManagerClass.prototype.bindUsersWithRoom = function(room_id, privacy) {
var privacy = privacy.charAt(0).toUpperCase() + privacy.slice(1);
if (Privacies.indexOf(privacy) === -1) {
console.error('Invalid privacy name (%s)', privacy);
return null;
}
if (typeof this.Rooms[privacy][room_id] === 'undefined') {
console.log('room_id %s does not exist in %s privacy', room_id, privacy);
return ;
}
var User = {};
_.pluck(this.Rooms[privacy][room_id].Users, '_id').forEach(function(user_id) {
User = UserManager.Users[user_id];
if (typeof User === 'undefined') {
console.log('user_id %s unknown', user_id);
return ;
}
if (User.Socket.rooms.indexOf(room_id) === -1) {
console.log('--> User %s has been re binded to the room_id %s', User.pseudo, room_id);
User.Socket.join(room_id);
}else {
console.info('--> User %s was already binded to the room_id %s', User.pseudo, room_id);
}
});
};
RoomManagerClass.prototype.generateKeyFromUsers = function(A, B) {
if (!A instanceof UserClass || !B instanceof UserClass) {
console.log('One of the users is corrupted');
return null;
}
return [ A._id.toString(), B._id.toString() ].sort().join('-');
};
RoomManagerClass.prototype.generateKeyFromRoom = function(Room) {
if (!Room || Room.privacy !== 'private' || !Room.Users || Room.Users.length !== 2) {
console.log('Invalid private room: ', JSON.stringify(Room));
return null;
}
return [ Room.Users[0]._id.toString(), Room.Users[1]._id.toString() ].sort().join('-');
};
RoomManagerClass.prototype.findOrCreatePrivateRoom = function(A, B, callback) {
var room_key = this.generateKeyFromUsers(A, B);
if (!room_key) {
console.log('No key provided...');
return ;
}
var PrivateRoom = this.Rooms.Private[room_key];
if (PrivateRoom) {
console.log('-->Private room found !');
return callback(null, PrivateRoom, room_key);
}
console.log('-->No private room found...');
var Room = new Datastore.Models.Room({ privacy: 'private', name: 'private', Users: [ A, B ], key: room_key });
console.log('---->Private room %s created for them !', Room._id);
this.Rooms.Private[room_key] = Room;
callback(null, Room, room_key);
}
RoomManagerClass.prototype.disconnectUserFromChat = function(user_id) {
_.forEach(this.Rooms.Public, function(Room) {
Room.Users = Room.Users.filter(function(User) {
if (User._id.toString() !== user_id.toString()) {
return User;
}
});
});
};
RoomManagerClass.prototype.getRoomFromId = function(room_id, privacy) {
if (!privacy) {
console.error('No privacy provided !');
return ;
}
var privacy = privacy.charAt(0).toUpperCase() + privacy.slice(1);
if (Privacies.indexOf(privacy) === -1) {
console.error('Invalid privacy name (%s)', privacy);
return null;
}
return this.Rooms[privacy][room_id];
};
RoomManagerClass.prototype.getPrivateRoomFromUsers = function(Users) {
if (Users.length !== 2) {
console.log('A private room is composed by 2 users and not %s', Users.length);
return null;
}
var room_key = this.generateKeyFromUsers(Users[0], Users[1]);
return this.Rooms.Private[room_key];
}
RoomManagerClass.prototype.disconnectUserFromRoom = function(Leaver, Room) {
//var Room = this.Rooms[Room.privacy][Room._id];
Room.Users = Room.Users.filter(function(User) {
if (User._id.toString() === Leaver._id.toString()) {
var Socket = UserManager.getUserClass(User._id.toString()).Socket;
var room_id = Room._id.toString();
if (Room.privacy === 'private') {
room_id = this.generateKeyFromRoom(Room);
}
Socket.emit('user-leave-room', { room_id: room_id, leaver_id: Leaver._id, privacy: Room.privacy });
// If private, we keep the both users
if (Room.privacy !== 'private') {
Socket.leave(room_id);
return false;
}
}
return User;
}.bind(this));
};
RoomManagerClass.prototype.loadRooms = function(done) {
Datastore.Models.Room.find(function(err, Rooms) {
if (err) {
console.error('The rooms could not be loaded : ', err);
return done(err);
}
var nb_rooms = 0;
// TODO: Reset Users to {} (find a way in client side to send all opened rooms on an "init" stage)
Rooms.forEach(function(Room) {
var privacy = Room.privacy.charAt(0).toUpperCase() + Room.privacy.slice(1);
this.Rooms[privacy][Room.id] = Room;
nb_rooms++;
}.bind(this));
console.log(nb_rooms + ' Rooms model loaded !');
done();
}.bind(this));
};
RoomManagerClass.prototype.getRooms = function() {
return _.transform(this.Rooms.Public, function(Rooms, Room, room_id) {
Rooms[room_id] = { _id: Room._id.toString(), privacy: Room.privacy, name: Room.name, Users: Room.Users };
});
};
RoomManagerClass.prototype.getRoomFromUsers = function(Users, privacy) {
var privacy = privacy.charAt(0).toUpperCase() + privacy.slice(1);
var Room = _.find(this.Rooms[privacy], function(Room) {
console.log('Room = ', Room);
console.log('Users: ', Users);
console.log('_.pluck(Room.Users, _id) = ', _.pluck(Room.Users, '_id'));
console.log('_.pluck(Users, _id) = ', _.pluck(Users, '_id'));
return !_.difference(_.pluck(Room.Users, '_id'), _.pluck(Users, '_id'));
});
console.log('Room found ! ', Room);
};
RoomManagerClass.prototype.createRoom = function(RoomDatas, callback) {
var Room = new Datastore.Models.Room(RoomDatas);
Room.key = Room._id;
Room.save(function(err) {
if (typeof callback === 'function') {
callback(err);
}
});
};
var RoomManager = new RoomManagerClass();
module.exports = RoomManager;