-
Notifications
You must be signed in to change notification settings - Fork 8
/
users.js
117 lines (103 loc) · 2.23 KB
/
users.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
/**
* Users
* Phantom Ozonix - https://github.com/Ishaan0132/Phantom-Ozonix
*
* This file tracks information about users.
*
* @license MIT license
*/
'use strict';
const PRUNE_INTERVAL = 60 * 60 * 1000;
class User {
/**
* @param {string} name
* @param {string} id
*/
constructor(name, id) {
this.name = Tools.toName(name);
this.id = id;
/**@type {Map<Room, string>} */
this.rooms = new Map();
/**@type {Map<Room, {messages: Array<{time: number, message: string}>, points: number, lastAction: number}>} */
this.roomData = new Map();
/**@type {?Game} */
this.game = null;
}
/**
* @param {Room | string} room
* @param {string} targetRank
* @return {boolean}
*/
hasRank(room, targetRank) {
if (!Config.groups) return false;
let rank;
if (typeof room === 'string') {
rank = room;
} else {
rank = this.rooms.get(room);
}
if (!rank) return false;
return Config.groups[rank] >= Config.groups[targetRank];
}
/**
* @return {boolean}
*/
isDeveloper() {
return Config.developers && Config.developers.indexOf(this.id) !== -1;
}
/**
* @param {string} message
*/
say(message) {
message = Tools.normalizeMessage(message);
if (!message) return;
Client.send('|/pm ' + this.id + ', ' + message);
}
}
exports.User = User;
class Users {
constructor() {
/**@type {{[k: string]: User}} */
this.users = {};
this.self = this.add(Config.username);
this.pruneUsersInterval = setInterval(() => this.pruneUsers(), PRUNE_INTERVAL);
this.User = User;
}
/**
* @param {User | string} name
* @return {User}
*/
get(name) {
if (name instanceof User) return name;
return this.users[Tools.toId(name)];
}
/**
* @param {string} name
* @return {User}
*/
add(name) {
let id = Tools.toId(name);
let user = this.get(id);
if (!user) {
user = new User(name, id);
this.users[id] = user;
}
return user;
}
pruneUsers() {
let users = Object.keys(this.users);
users.splice(users.indexOf(this.self.id), 1);
for (let i = 0, len = users.length; i < len; i++) {
let user = this.users[users[i]];
if (!user.rooms.size) {
delete this.users[user.id];
}
}
}
destroyUsers() {
for (let i in this.users) {
delete this.users[i];
}
}
}
exports.Users = new Users();