-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoomManager.js
More file actions
84 lines (73 loc) · 1.75 KB
/
RoomManager.js
File metadata and controls
84 lines (73 loc) · 1.75 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
class RoomManager{
constructor(){
this.rooms = {}
this.socketMap = {}
}
add_room(room_name){
if(this.room_exists(room_name)){
return this.rooms[room_name]
}
this.rooms[room_name] = {}//empty json room
this.rooms[room_name]["users"] = []
this.rooms[room_name]["size"] = 0
this.rooms[room_name]["history"] = []
return this.rooms[room_name]
}
get_room(room_name){
return this.rooms[room_name]
}
get_history(room_name){
if(this.room_exists(room_name)){
return this.get_room(room_name)["history"]
}
return {}
}
room_exists(room_name){
return room_name in this.rooms
}
remove_user(user){
let room_name = this.socketMap[user]
if(typeof room_name !== 'undefined'){
let users = this.rooms[room_name]["users"]
for(var i = users.length - 1; i >= 0; i--) {
if(users[i] === user) {
users.splice(i, 1);
console.log('disconnect removed!')
}
}
}
}
add_user(room_name, user){
let room;
this.socketMap[user] = room_name
if(!this.room_exists(room_name)){
room = this.add_room(room_name)
}
else{
room = this.get_room(room_name);
}
room["users"].push(user)
console.log(room)
room["size"] += 1
}
push_stroke(room_name, stroke){
if(this.room_exists(room_name)){
this.get_history(room_name).push({"stroke": stroke})
}
}
get_host_socket(room_name){
console.log('sockets in room: ' + this.rooms[room_name]["users"])
return this.rooms[room_name]["users"][0]
}
}
class Singleton{
constructor(){
if (!Singleton.instance) {
Singleton.instance = new RoomManager();
}
}
getInstance(){
return Singleton.instance
}
}
module.exports = Singleton