-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathTournament.js
181 lines (168 loc) · 6.59 KB
/
Tournament.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
let formats = {};
/*
* Data options
* - forcepublic: true for forcing public tour games
* - scouting: true to ban scouting
* - modjoin: true to disallow modjoin
* - scrappie: true to have the bot send .official on tour creation, to denote it as an official tour for Scrappie
* - autostart: autostart in minutes
* - autodq: autodq in minutes
* - forcetimer: true to force timers in the tour
*/
class Tournament {
constructor(room, data) {
if (!data) data = {};
this.room = room;
this.started = false;
this.players = {};
this.official = data.official || data.scrappie;
this.blt = data.blt;
this.chill = data.chill;
this.notifications = {};
this.rules = {
bans: [],
unbans: [],
addrules: [],
remrules: [],
};
this.name = false;
this.format = false;
this.officialname = data.officialname;
if (data.late) {
this.started = true;
return;
}
if (Config.tours[room.id] && !(data.autostart || data.autodq || data.scouting)) {
let t = Config.tours[room.id];
this.room.send(`/tour autostart ${t[0]}`);
this.room.send(`/tour autodq ${t[1]}`);
if (t[2]) this.room.send("/tour scouting disallow");
}
// Various data things
if (data.scrappie) room.send(".official");
if (data.monopoke) room.send("/tour autostart 7");
if (data.forcepublic) room.send("/tour forcepublic on");
if (data.scouting) room.send("/tour scouting disallow");
if (data.modjoin) room.send("/tour modjoin off");
if (data.autostart) room.send(`/tour autostart ${data.autostart}`);
if (data.autodq) room.send(`/tour autodq ${data.autodq}`);
if (data.forcetimer) room.send(`/tour forcetimer on`);
if (this.chill) room.send("/modchat +");
this.startCheckTimer = false;
this.autostart = false;
}
updateName(name) {
this.name = name;
this.buildNotifs();
}
buildNotifs(end = false) {
for (let roomid in this.notifications) {
let notifroom = Rooms[roomid];
let id = this.notifications[roomid];
let msg = `<div class="infobox"><a href="/${this.room.id}" class="ilink">`;
msg += `<b>${this.name || this.format}</b>${this.name && this.name != this.format ? " (" + this.format + ")" : ""} tournament`;
msg += ` in <b>${this.room.name}</b>`;
if (end) msg += ` (ended)`;
else if (this.started) msg += ` (started)`;
msg += `</a></div>`
notifroom.send(`/changeuhtml ${id}, ${msg}`)
}
}
start() {
this.started = true;
this.buildNotifs();
}
checkstart() {
if (!this.autostart) return;
if (Date.now() < this.autostart) return;
if (this.started) return;
if (Object.keys(this.players).length >= 2) this.room.send("/tour start");
else if (Users.self.can(this.room, "*")) {
this.room.send("Not enough players... Ending tournament.");
this.room.send("/tour end");
this.room.endTour();
}
}
setAutostart(val) {
this.startCheckTimer = false;
this.autostart = false;
if (!val) return;
let now = Date.now();
this.autostart = now + val + 60 * 2 * 1000;
}
buildRules() {
if (
this.rules.bans.length === 0 &&
this.rules.unbans.length === 0 &&
this.rules.addrules.length === 0 &&
this.rules.remrules.length === 0
)
return "";
let ret = "/tour rules ";
console.log(ret);
for (let i of this.rules.bans) {
ret += "-" + i + ", ";
}
for (let i of this.rules.unbans) {
ret += "+" + i + ", ";
}
for (let i of this.rules.addrules) {
console.log(ret, i);
ret += i + ", ";
}
for (let i of this.rules.remrules) {
ret += "!" + i + ", ";
}
console.log(ret);
return ret.substring(0, ret.length - 2);
}
end(data) {
if (data) {
let dt = JSON.parse(data);
if (this.blt) {
let rn = dt.bracketData.rootNode;
let first = rn.team;
let second = rn.children[0].team === first ? rn.children[1].team : rn.children[0].team;
let third = [
rn.children[0].children && rn.children[0].children[0] ? rn.children[0].children[0].team : false,
rn.children[1].children && rn.children[0].children[0] ? rn.children[1].children[0].team : false,
rn.children[0].children && rn.children[0].children[1] ? rn.children[0].children[1].team : false,
rn.children[1].children && rn.children[0].children[1] ? rn.children[1].children[1].team : false,
];
let thirds = [];
for (let i of third) {
if (!i) continue;
if (i !== first && i !== second) thirds.push(i);
}
let ret = `Congratulations to the ${
formats[BLT.last + "monotype"]
} BLT qualifier tournament winners. ${first} won first place, ${second} won second place`;
if (thirds.length) {
ret += `, and ${thirds[0]}${thirds[1] ? " and " + thirds[1] : ""} won third place!`;
}
this.room.send(ret);
BLT.addpoints(first, second, thirds);
} else if (this.room.id === "anythinggoes" && this.official) {
let rn = dt.bracketData.rootNode;
let first = rn.team;
let second = rn.children[0].team === first ? rn.children[1].team : rn.children[0].team;
this.room.send(
`/wall Congratulations to ${first} for winning the tournament, and to ${second} for being runner-up! Please put your smogon username in chat to claim your points.`
);
}
if (dt.format && formats[dt.format]) this.name = formats[dt.format];
else this.name = dt.format ? dt.format : this.name;
}
if (this.chill) this.room.send("/modchat ac");
this.buildNotifs(true);
}
}
Tournament.prototype.toString = function () {
if (this.name) return this.name;
if (this.format) return this.format === "unknown" ? false : this.format;
return false;
};
module.exports = {
Tournament: Tournament,
formats: formats,
};