-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathuser-handler.js
54 lines (43 loc) · 1.22 KB
/
user-handler.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
function UserHandler () {
this.users = {};
}
UserHandler.prototype.addUser = function (nickname) {
var userId = this.generateUserId(),
userKeys = Object.keys(this.users),
self = this,
notTaken;
notTaken = userKeys.every(function (key) {
return nickname !== self.users[key].nickname
});
if (notTaken) {
this.users[userId] = {
nickname: nickname,
color: this.generateUserColor(),
lastMessage: null
}
return userId;
} else {
return false;
}
};
UserHandler.prototype.getUserById = function (userId) {
return this.users[userId] || null;
};
UserHandler.prototype.removeUser = function (userId) {
delete this.users[userId];
};
UserHandler.prototype.generateUserId = function () {
var id;
do {
id = "";
for (var i = 0; i < 10; i++) {
id += Math.floor(Math.random() * 16).toString(16);
}
} while (id in this.users);
return id;
};
UserHandler.prototype.generateUserColor = function () {
var allowedColors = ["#c0392b", "#16a085", "#27ae60", "#2980b9", "#2c3e50", "#8e44ad", "#d35400", "#f39c12", "#34495e", "#9b59b6", "#1abc9c"];
return allowedColors[Math.floor(Math.random() * allowedColors.length)];
};
exports.UserHandler = UserHandler;