-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatabase.js
More file actions
56 lines (53 loc) · 1.19 KB
/
database.js
File metadata and controls
56 lines (53 loc) · 1.19 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
const userList = {}
const rooms = {}
const setRooms = (roomId, userId) => {
if (typeof rooms[roomId] == 'undefined') {
//new ROOM
rooms[roomId] = {open:true, users:[userId]}
} else {
if(rooms[roomId].open){
rooms[roomId].users.push(userId)
} else {
return false
}
}
}
const closeRoom = (roomId) =>{
console.log(rooms,roomId );
rooms[roomId].open = false
console.log(rooms,roomId );
}
const getOpenRooms = () => {
let roomList = []
for (const key in rooms) {
if (rooms.hasOwnProperty(key)) {
const room = rooms[key];
if(room.open) roomList.push(key)
}
}
return roomList
}
const getList = (roomId) => {
let list = {}
rooms[roomId].users.map(u => {
list[u] = userList[u]
})
return list
}
const removeUser = (roomId, userId) =>{
delete userList[userId]
rooms[roomId].users = rooms[roomId].users.filter(u=>u!=userId)
if(rooms[roomId].users.length==0){
delete rooms[roomId]
return false
}
return true
}
module.exports = {
userList,
getOpenRooms,
setRooms,
getList,
removeUser,
closeRoom
}