-
Notifications
You must be signed in to change notification settings - Fork 1
/
debates.js
110 lines (91 loc) · 2.65 KB
/
debates.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
'use strict';
class Debates {
constructor(room) {
this.room = room;
this.users = {};
this.userList = [];
this.gameId = "game";
this.gameName = "Game";
this.allowJoins = false;
this.official = false;
this.state = null;
this.answerCommand = "standard";
this.allowRenames = true;
this.playerType = null;
}
onRename(oldId, newName) {
if (!this.allowRenames) return false;
if (!this.userList.includes(oldId)) return false;
let newId = toId(newName);
if (newId !== oldId) {
this.users[newId] = this.users[oldId];
delete this.users[oldId];
this.userList.splice(this.userList.indexOf(oldId), 1, newId);
}
this.users[newId].rename(newName);
if (this.currentPlayer && this.currentPlayer === oldId) this.currentPlayer = newId;
}
sendRoom(message) {
this.room.post(`${Users.get(Monitor.username).hasRank(this.room, "%") ? "/wall " : ""}${message}`);
}
onJoin(user) {
if (!this.allowJoins) return;
this.users[user.userid] = this.playerObject ? new this.playerObject(user, this) : new botGamePlayer(user, this);
this.userList.push(user.userid);
}
onLeave(user) {
delete this.users[user.userid];
this.userList.splice(this.userList.indexOf(user.userid), 1);
return true;
}
buildPlayerList() {
return {count: this.userList.length, players: this.userList.sort().map(u => this.users[u].name).join(", ")};
}
postPlayerList() {
this.list = this.buildPlayerList();
this.sendRoom(`Players (${this.list.count}): ${this.list.players}`);
}
onEnd() {
this.destroy();
}
destroy() {
if (this.timer) {
clearTimeout(this.timer);
this.timer = null;
}
if (this.autoStartTimer) {
clearTimeout(this.autoStartTimer);
this.autoStartTimer = null;
}
delete this.room.game;
}
runAutoStart(seconds) {
if (!('onStart' in this) || this.state !== 'signups') return; // the game does not start
// validate input
const int = parseInt(seconds);
if ((!int || int < 0) && seconds !== 'off') return false;
if (this.autoStartTimer) clearTimeout(this.autoStartTimer);
if (seconds === 'off') return this.sendRoom('The autostart timer has been turned off.');
this.autoStartTimer = setTimeout(() => {
this.onStart(); // we will assume that it will not try to start 2 games at the same time.
}, seconds * 1000);
this.sendRoom("The debate will automatically start in " + seconds + " seconds.");
}
}
class DebatePlayer {
constructor(user, game) {
this.name = user.name;
this.userid = user.userid;
this.user = user;
this.game = game;
}
rename(name) {
this.userid = toId(name);
this.user = Users.get(this.userid);
this.name = this.user.name;
}
}
module.exports = {
debate: Debates,
player: DebatePlayer,
};