Repository navigation
Expand file tree
/
Copy pathsession.js
More file actions
59 lines (50 loc) · 1.55 KB
/
Copy pathsession.js
File metadata and controls
59 lines (50 loc) · 1.55 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
const SESSION_CHARACTERS = "abcdefghijklmnopqrstuvwxyz0123456789";
const SESSION_CODE_LENGTH = 5;
var sessions = [];
var createSession = function(uuid) {
var sessionCode = "";
do {
for(var i = 0; i < SESSION_CODE_LENGTH; i++) {
sessionCode += SESSION_CHARACTERS.charAt(Math.floor(Math.random() * SESSION_CHARACTERS.length));
}
} while(sessionCode in sessions.map((session) => session.code));
var session = {
code: sessionCode,
members: [uuid],
preloaded: {},
ping: {},
queue: [],
paused: false,
timeReference: [0, 0],
lastEnd: 0,
nowPlaying: null
};
session.preloaded[uuid] = [];
sessions.push(session);
return session;
};
var disconnectMember = function(uuid) {
var session = getSessionByUUID(uuid);
if(session != null) {
session.members.splice(session.members.indexOf(uuid), 1);
if(session.members.length == 0) {
sessions.splice(sessions.indexOf(session), 1);
}
}
}
var hasSession = function(uuid) {
return sessions.some((session) => session.members.includes(uuid));
}
var getSessionByUUID = function(uuid) {
return sessions.find((session) => session.members.includes(uuid));
}
var getSessionByCode = function(code) {
return sessions.find((session) => session.code == code);
}
module.exports = {
createSession: createSession,
hasSession: hasSession,
getSessionByUUID: getSessionByUUID,
getSessionByCode: getSessionByCode,
disconnectMember: disconnectMember
};