This repository has been archived by the owner on May 17, 2020. It is now read-only.
forked from Slayer95/Pokemon-Showdown-Bot
-
Notifications
You must be signed in to change notification settings - Fork 64
/
rooms.js
98 lines (87 loc) · 2.37 KB
/
rooms.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
/**
* This is where joined rooms are stored.
*
* On startup, the Pokemon Showdown Bot joins the configured rooms here,
* and tracks their userlists. Room command and modding settings are
* loaded on room join, if present.
*/
var Rooms = Object.create(null);
var rooms = Rooms.rooms = new Map();
Rooms.join = function () {
for (let i = 0; i < Config.rooms.length; i++) {
let room = toId(Config.rooms[i]);
if (room === 'lobby' && Config.serverid === 'showdown') continue;
send('|/join ' + room);
}
for (let i = 0; i < Config.privaterooms.length; i++) {
let room = toId(Config.privaterooms[i]);
if (room === 'lobby' && Config.serverid === 'showdown') continue;
send('|/join ' + room);
}
};
class Room {
constructor(roomid, type) {
this.id = roomid;
this.isPrivate = type;
this.users = new Map();
}
onUserlist(users) {
if (users === '0') return false; // no users in room
users = users.split(',');
for (let i = 1; i < users.length; i++) {
let username = users[i];
let group = username.charAt(0);
let user = Users.get(username);
if (!user) user = Users.add(username, this.id);
user.rooms.set(this.id, group);
this.users.set(user.id, group);
}
}
onJoin(username, group) {
var user = Users.get(username);
if (!user) user = Users.add(username);
this.users.set(user.id, group);
user.rooms.set(this.id, group);
return user;
}
onRename(username, oldid) {
var user = Users.get(oldid);
var group = username.charAt(0);
this.users.delete(oldid);
if (!user) { // already changed nick
user = Users.get(username);
} else if (username.substr(1) !== user.name) { // changing nick
user = user.rename(username);
}
this.users.set(user.id, group);
user.rooms.set(this.id, group);
return user;
}
onLeave(username) {
var user = Users.get(username);
this.users.delete(user.id);
user.rooms.delete(this.id);
if (user.rooms.size) return user;
user.destroy();
return null;
}
destroy() {
this.users.forEach(function (group, userid) {
var user = Users.get(userid);
user.rooms.delete(this.id);
if (!user.rooms.size) user.destroy();
});
rooms.delete(this.id);
}
}
var getRoom = Rooms.get = function (name) {
return rooms.get(name);
};
Rooms.add = function (roomid, type) {
var room = getRoom(roomid);
if (room) return room;
room = new Room(roomid, type);
rooms.set(roomid, room);
return room;
};
module.exports = Rooms;