-
Notifications
You must be signed in to change notification settings - Fork 12
/
rooms.ts
137 lines (121 loc) · 3.51 KB
/
rooms.ts
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
export class Room {
roomid: string;
title: string;
roomType: string;
/** userid -> name */
users: Map<string, string> = new Map();
auth: Map<string, string> = new Map();
game: RoomGame | null = null;
mafiaTracker: MafiaTracker | null = null;
iso: MafiaISO | null = null;
mafiaCooldown: MafiaCooldown | null = null;
mafiaUGO: MafiaUGO | null = null;
// fixme
scavengerHunt: import('./plugins/scavs').ScavengerHunt | null = null;
pendingScavengerHunt: import('./plugins/scavs').ScavengerHunt | null = null;
constructor(roomid: string, roomType: string) {
this.roomid = roomid;
this.title = roomid;
this.roomType = roomType;
// auth is strictly roomauth, gets updated on /roomauth and on (pro/de)mote messages
sendMessage('', `/cmd roominfo ${this.roomid}`); // need to use roomauth1 because it gives the roomname
}
send(message: string) {
sendMessage(this.roomid, message);
}
destroy() {
debug(`DEINIT ROOM: ${this.roomid}`);
Rooms.rooms.delete(this.roomid);
}
setTitle(newTitle: string) {
this.title = newTitle;
}
updateUserlist(users: string) {
const userList = users.split(',').slice(1);
for (const user of userList) {
this.userJoin(user);
}
}
get userList() {
return [...this.users.keys()];
}
get userCount() {
return this.userList.length;
}
userLeave(name: string) {
const [, username] = Tools.splitUser(name);
const userid = toId(username);
const user = this.users.get(userid);
if (!user) return debug(`User '${userid}' trying to leave a room '${this.roomid}' when they're not in it`);
//this.auth.delete(userid);
this.users.delete(userid);
}
userJoin(name: string) {
const [, username] = Tools.splitUser(name);
const userid = toId(username);
this.users.set(userid, username);
//this.auth.set(userid, group);
}
userRename(from: string, to: string) {
const [, oldName] = Tools.splitUser(from);
const [, newName] = Tools.splitUser(to);
const oldId = toId(oldName);
const newId = toId(newName);
if (oldId === newId) {
this.users.set(newId, newName);
//this.auth.set(newId, newGroup);
return;
}
this.users.delete(oldId);
//this.auth.delete(oldId);
this.users.set(newId, newName);
//this.auth.set(newId, newGroup);
debug(`User rename in '${this.roomid}': '${from}' => '${to}'`);
if (this.game && this.game.onRename) this.game.onRename(oldId, newName);
}
getAuth(userid: string) {
return this.auth.get(userid) || ' ';
}
}
function getRoom(roomid: string) {
if (!roomid) return null;
if (typeof roomid === 'object') return roomid;
return Rooms.rooms.get(roomid.startsWith('groupchat') ? roomid : toId(roomid)) || null;
}
function addRoom(roomid: string, roomType: string) {
let room = Rooms(roomid);
if (room) {
if (room.roomType !== roomType) {
debug(`Recreating room '${room.roomid}@${room.roomType}' as '${roomid}@${roomType}'`);
} else {
return room;
}
}
room = new Room(roomid, roomType);
Rooms.rooms.set(roomid, room);
debug(`INIT ROOM: ${roomid}`);
return room;
}
/**
* Returns the roomid where the bot has the bot rank and where the target is in
* Returns a valid roomid or null
*/
function canPMInfobox(user: string) {
const nick = toId(Config.nick);
user = toId(user);
for (const room of [...Rooms.rooms.values()]) {
if (room.getAuth(nick) === '*') {
if (room.users.has(user)) return room.roomid;
}
}
return null;
}
import { RoomGame, RoomGamePlayer } from './room-game.js';
export let Rooms = Object.assign(getRoom, {
Room,
rooms: new Map<string, Room>(),
addRoom,
canPMInfobox,
RoomGame,
RoomGamePlayer,
});