/
fusion.js
1085 lines (874 loc) · 42.2 KB
/
fusion.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var glicko2 = require("glicko2"),
ranking = new glicko2.Glicko2({
tau: 0.75,
rating: 1500,
rd: 200,
vol: 0.06
}),
pjson = require("./package.json"),
settings = require("./settings"),
db = require("./database"),
messageParse = /^!([^\ ]+)(?:\ +(.+[^\ ]))?\ *$/,
idParse = /^<@([0-9]+)>$/,
twoIdParse = /^<@([0-9]+)>\ <@([0-9]+)>$/,
forceMatchReportParse = /^<@([0-9]+)>\ <@([0-9]+)>\ (-?[0-9]+)\ (-?[0-9]+)$/,
reportParse = /^(-?[0-9]+)\ (-?[0-9]+)$/,
noPermissions = {
CREATE_INSTANT_INVITE: false,
ADD_REACTIONS: false,
READ_MESSAGES: false,
SEND_MESSAGES: false,
SEND_TTS_MESSAGES: false,
EMBED_LINKS: false,
ATTACH_FILES: false,
READ_MESSAGE_HISTORY: false,
MENTION_EVERYONE: false,
CONNECT: false,
SPEAK: false,
USE_VAD: false
},
textPermissions = {
CREATE_INSTANT_INVITE: true,
ADD_REACTIONS: true,
READ_MESSAGES: true,
SEND_MESSAGES: true,
SEND_TTS_MESSAGES: true,
EMBED_LINKS: true,
ATTACH_FILES: true,
READ_MESSAGE_HISTORY: true,
MENTION_EVERYONE: true
},
voicePermissions = {
CONNECT: true,
SPEAK: true,
USE_VAD: true
},
Fusion = {},
tmiCooldown = {},
discordCooldown = {},
tmi, discord, obsDiscord, generalChannel, resultsChannel, eventRole, seasonRole, event;
Fusion.start = (_tmi, _discord) => {
"use strict";
tmi = _tmi;
discord = _discord;
var startup = () => {
var readied = false,
tmiConnect = () => {
console.log("Connecting to IRC...");
tmi.connect().then(() => {
console.log("Connected. Startup complete.");
}).catch((err) => {
console.log("Error connecting, will retry.");
console.log(err);
});
},
discordConnect = () => {
console.log("Connecting to Discord...");
discord.login(settings.discord.token).catch((err) => {
if (err) {
console.log(err);
discord.destroy().then(discordConnect).catch(discordConnect);
}
console.log("Connected.");
});
};
console.log("Starting up...");
// Setup events.
tmi.on("disconnected", (message) => {
console.log("DISCONNECTED", message);
tmi.disconnect().then(() => {
tmiConnect();
}).catch(() => {
tmiConnect();
});
});
tmi.on("message", (channel, userstate, text, self) => {
if (!self && channel === "#roncli") {
Fusion.tmiMessage(userstate["display-name"], text);
}
});
discord.on("ready", () => {
console.log("Discord ready.");
obsDiscord = discord.guilds.find("name", "The Observatory");
generalChannel = obsDiscord.channels.find("name", "general");
resultsChannel = obsDiscord.channels.find("name", "match-results");
eventRole = obsDiscord.roles.find("name", "In Current Event");
seasonRole = obsDiscord.roles.find("name", "Season 1 Participant");
if (!readied) {
readied = true;
// Connect to IRC.
tmiConnect();
}
});
discord.on("message", (message) => {
var author;
if (message.guild && message.guild.name === obsDiscord.name) {
author = obsDiscord.members.get(message.author.id);
Fusion.discordMessage(author.displayName, author, message.channel, message.content);
} else if (!message.guild) {
Fusion.discordMessage(message.author.username, message.author, message.channel, message.content);
}
});
discordConnect();
};
startup();
};
Fusion.isAdmin = (user) => {
"use strict";
return user.user.username === settings.admin.username && user.user.discriminator === settings.admin.discriminator;
};
Fusion.getPlayers = () => new Promise((resolve, reject) => {
"use strict";
db.query("SELECT PlayerID, Name, DiscordID, Rating, RatingDeviation, Volatility from tblPlayer", {}).then((data) => {
resolve(data[0]);
}).catch((err) => {
console.log(err);
reject(err);
});
});
Fusion.resultsText = (match) => {
var player1 = obsDiscord.members.get(match.winner),
player2 = obsDiscord.members.get(match.players.find((p) => p !== match.winner)),
str = "```" + player1.displayName + " " + match.score[0] + ", " + player2.displayName + " " + match.score[1] + ", ";
if (event.joinable) {
str += event.players[match.home].home;
} else {
str += event.players[match.players[0]].home + " & " + event.players[match.players[1]].home;
}
str += "```";
if (match.comments) {
Object.keys(match.comments).forEach((id) => {
str += "\n" + obsDiscord.members.get(id).displayName + ": " + match.comments[id];
});
}
return str;
};
Fusion.tmiQueue = (message) => {
"use strict";
tmi.say("roncli", message);
};
Fusion.discordQueue = (message, channel) => new Promise((resolve, reject) => {
"use strict";
if (!channel) {
channel = generalChannel;
}
channel.sendMessage(message).then((message) => resolve(message));
});
Fusion.tmiMessage = (from, text) => {
"use strict";
var matches = messageParse.exec(text);
if (matches) {
if (Fusion.tmiMessages[matches[1]]) {
Fusion.tmiMessages[matches[1]].call(this, from, matches[2]);
}
}
};
Fusion.tmiMessages = {
version: (from, message) => {
"use strict";
if (message || tmiCooldown.version > new Date()) {
return;
}
Fusion.tmiQueue("FusionBot, DescentBot, whatever, I have an identity crisis. Written by roncli, Version " + pjson.version);
tmiCooldown.version = new Date(new Date().getTime() + 60000);
},
discord: (from, message) => {
"use strict";
if (message || tmiCooldown.discord > new Date()) {
return;
}
Fusion.tmiQueue("Interested in playing in the tournament? All skill levels are welcome! Join our Discord server at http://ronc.li/obs-discord!");
tmiCooldown.discord = new Date(new Date().getTime() + 60000);
},
website: (from, message) => {
"use strict";
if (message || tmiCooldown.website > new Date()) {
return;
}
Fusion.tmiQueue("Visit The Observatory on the web at http://roncli.com/gaming/the-observatory!");
tmiCooldown.discord = new Date(new Date().getTime() + 60000);
}
};
Fusion.discordMessage = (from, user, channel, text) => {
"use strict";
var matches = messageParse.exec(text);
if (matches) {
if (Fusion.discordMessages[matches[1]]) {
Fusion.discordMessages[matches[1]].call(this, from, user, channel, matches[2]);
}
}
};
Fusion.discordMessages = {
version: (from, user, channel, message) => {
"use strict";
if (message || discordCooldown.version > new Date()) {
return;
}
Fusion.discordQueue("FusionBot, or DescentBot, whatever, I have an identity crisis. Written by roncli, Version " + pjson.version, channel);
discordCooldown.version = new Date(new Date().getTime() + 60000);
},
join: (from, user, channel, message) => {
"use strict";
if (message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
if (!event.joinable) {
Fusion.discordQueue("Sorry, " + user + ", but this is not an event you can join.", channel);
return;
}
if (event.players[user.id] && !event.players[user.id].withdrawn) {
Fusion.discordQueue("Sorry, " + user + ", but you have already joined this event. You can use `!withdraw` to leave it.", channel);
return;
}
if (event.players[user.id]) {
delete event.players[user.id].withdrawn;
} else {
event.players[user.id] = {
home: undefined,
canHost: true
};
}
user.addRole(eventRole);
Fusion.discordQueue("You have been successfully added to the event. Please use the `!home` command to select a home level, for example, `!home Logic x2`. I assume you can host games, but if you cannot please issue the `!host` command to toggle this option.", user);
Fusion.discordQueue(obsDiscord.members.get(user.id).displayName + " has joined the tournament!", generalChannel);
},
withdraw: (from, user, channel, message) => {
"use strict";
if (message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
if (!event.joinable) {
Fusion.discordQueue("Sorry, " + user + ", but this is not an event you can withdraw from.", channel);
return;
}
if (!event.players[user.id]) {
Fusion.discordQueue("Sorry, " + user + ", but you have not yet entered this event. You can use `!join` to enter it.", channel);
return;
}
if (event.players[user.id].withdrawn) {
Fusion.discordQueue("Sorry, " + user + ", but you have have already withdrawn from this event. You can use `!join` to re-enter it.", channel);
return;
}
event.players[user.id].withdrawn = true;
user.removeRole(eventRole);
Fusion.discordQueue("You have been successfully withdrawn from the event. If you wish to return before the end of the event, you may use the `!join` command once again.", user);
Fusion.discordQueue(obsDiscord.members.get(user.id).displayName + " has withdrawn from the tournament!", generalChannel);
},
home: (from, user, channel, message) => {
"use strict";
if (!message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
if (!event.players[user.id]) {
if (event.joinable) {
Fusion.discordQueue("Sorry, " + user + ", but you first need to `!join` the tournament before selecting a home level.", channel);
return;
} else {
Fusion.discordQueue("Sorry, " + user + ", but you are not entered into this tournament.", channel);
return;
}
}
if (event.players[user.id].withdrawn) {
Fusion.discordQueue("Sorry, " + user + ", but you have withdrawn from the tournament.", channel);
return;
}
if (event.players[user.id].home && (event.round && event.round > 0 || event.matches && event.matches.length > 0)) {
Fusion.discordQueue("Sorry, " + user + ", but you cannot change your home level after the tournament has started.", channel);
return;
}
event.players[user.id].home = message;
Fusion.discordQueue("You have successfully set your home level to `" + message + "`. You may change it at any time before the tournament begins.", user);
Fusion.discordQueue(obsDiscord.members.get(user.id).displayName + " has set their home level to `" + message + "`.", generalChannel);
},
host: (from, user, channel, message) => {
"use strict";
if (message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
if (!event.players[user.id]) {
if (event.joinable) {
Fusion.discordQueue("Sorry, " + user + ", but you first need to `!join` the tournament before toggling your ability to host games.", channel);
return;
} else {
Fusion.discordQueue("Sorry, " + user + ", but you are not entered into this tournament.", channel);
return;
}
}
if (event.players[user.id].withdrawn) {
Fusion.discordQueue("Sorry, " + user + ", but you have withdrawn from the tournament.", channel);
return;
}
if (event.round && event.round > 0 || event.matches && event.matches.length > 0) {
Fusion.discordQueue("Sorry, " + user + ", but you cannot toggle your ability to host games after the tournament has started.", channel);
return;
}
event.players[user.id].canHost = !event.players[user.id].canHost;
Fusion.discordQueue("You have successfully toggled " + (event.players[user.id].canHost ? "on" : "off") + " your ability to host games. You may change it at any time before the tournament begins.", user);
Fusion.discordQueue(obsDiscord.members.get(user.id).displayName + " has set toggled " + (event.players[user.id].canHost ? "on" : "off") + " their ability to host games.", generalChannel);
},
report: (from, user, channel, message) => {
"use strict";
var matches, score1, score2, eventMatch, player2;
if (!message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
if (!event.joinable) {
Fusion.discordQueue("Sorry, " + user + ", but this is not an event that you can report games in.", channel);
return;
}
matches = reportParse.exec(message);
if (!matches) {
Fusion.discordQueue("Sorry, " + user + ", but you you must report the score in the following format: `!report 20 12`", channel);
return;
}
eventMatch = event.matches.filter((m) => !m.cancelled && m.players.indexOf(user.id) !== -1 && !m.winner)[0];
if (!eventMatch) {
Fusion.discordQueue("Sorry, " + user + ", but I cannot find a match available for you.", channel);
return;
}
score1 = +matches[1];
score2 = +matches[2];
// Allow reporting out of order.
if (score1 < score2) {
let temp = score1;
score1 = score2;
score2 = temp;
}
if (score1 < 20 || score1 === 20 && score1 - score2 < 2 || score1 > 20 && score1 - score2 !== 2) {
Fusion.discordQueue("Sorry, " + user + ", but that is an invalid score. Games must be played to 20, and you must win by 2 points.", channel);
return;
}
player2 = obsDiscord.members.get(eventMatch.players.filter((p) => p !== user.id)[0]);
eventMatch.reported = {
winner: player2.id,
score: [score1, score2]
};
Fusion.discordQueue("Game reported: " + player2.displayName + " " + score1 + ", " + obsDiscord.members.get(user.id).displayName + " " + score2 + ". " + player2 + ", please type `!confirm` to confirm the match. If there is an error, such as the wrong person reported the game, it can be reported again to correct it.", eventMatch.channel);
},
confirm: (from, user, channel, message) => {
"use strict";
var eventMatch;
if (message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
if (!event.joinable) {
Fusion.discordQueue("Sorry, " + user + ", but this is not an event that you can report games in.", channel);
return;
}
eventMatch = event.matches.filter((m) => !m.cancelled && m.players.indexOf(user.id) !== -1 && !m.winner)[0];
if (!eventMatch) {
Fusion.discordQueue("Sorry, " + user + ", but I cannot find a match available for you.", channel);
return;
}
if (!eventMatch.reported) {
Fusion.discordQueue("Sorry, " + user + ", but this match hasn't been reported yet. Make sure the loser reports the result of the game in the following format: '!report 20 12'", channel);
return;
}
if (!eventMatch.reported.winner === user.id) {
Fusion.discordQueue("Sorry, " + user + ", but you can't confirm your own reports!", channel);
return;
}
eventMatch.winner = eventMatch.reported.winner;
eventMatch.score = eventMatch.reported.score;
delete eventMatch.reported;
Fusion.discordQueue("This match has been reported as a win for " + obsDiscord.members.get(user.id).displayName + " by the score of " + eventMatch.score[0] + " to " + eventMatch.score[1] + ". If this is in error, please contact an admin. You may add a comment to this match using `!comment <your comment>` any time before your next match. This channel and the voice channel will close in 2 minutes.", eventMatch.channel);
setTimeout(() => {
var player1 = obsDiscord.members.get(user.id),
player2 = obsDiscord.members.get(eventMatch.players.filter((p) => p !== user.id)[0]);
eventMatch.channel.overwritePermissions(user, noPermissions);
eventMatch.channel.overwritePermissions(player2, noPermissions);
eventMatch.voice.delete();
delete eventMatch.channel;
delete eventMatch.voice;
player1.addRole(seasonRole);
player2.addRole(seasonRole);
Fusion.discordQueue(Fusion.resultsText(eventMatch), resultsChannel).then((message) => {
eventMatch.results = message;
});
}, 120000);
},
comment: (from, user, channel, message) => {
"use strict";
var eventMatches, eventMatch;
if (!message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
eventMatches = event.matches.filter((m) => !m.cancelled && m.players.indexOf(user.id) !== -1);
if (eventMatches.length === 0) {
Fusion.discordQueue("Sorry, " + user + ", but you have not played in any matches that can be commented on.", channel);
return;
}
eventMatch = eventMatches[eventMatches.length - 1];
if (!eventMatch.comments) {
eventMatch.comments = {};
}
eventMatch.comments[user.id] = message;
if (eventMatch.results) {
eventMatch.results.edit(Fusion.resultsText(eventMatch));
}
Fusion.discordQueue("Your match comment has been updated.", user);
},
openevent: (from, user, channel, message) => {
"use strict";
if (!Fusion.isAdmin(user) || message) {
return;
}
if (event) {
Fusion.discordQueue("Sorry, " + user + ", but you must `!endevent` the previous event first.", channel);
return;
}
event = {
joinable: true,
round: 0,
players: {},
matches: [],
byes: []
};
Fusion.discordQueue("Hey @everyone, a new tournament has been created. `!join` the tournament if you'd like to play!", generalChannel);
},
startevent: (from, user, channel, message) => {
"use strict";
if (!Fusion.isAdmin(user) || message) {
return;
}
if (event) {
Fusion.discordQueue("Sorry, " + user + ", but you must `!endevent` the previous event first.", channel);
return;
}
event = {
joinable: false,
players: {},
matches: []
};
},
addplayer: (from, user, channel, message) => {
"use strict";
var matches;
if (!Fusion.isAdmin(user) || !message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
// Get the user by ID.
matches = idParse.exec(message);
if (!matches) {
Fusion.discordQueue("Sorry, " + user + ", but you must mention the user to add them, try this command on a public channel instead.", channel);
return;
}
var addedUser = obsDiscord.members.get(matches[1]);
if (!addedUser) {
Fusion.discordQueue("Sorry, " + user + ", but that person is not part of this Discord server.", channel);
return;
}
if (event.players[addedUser.id]) {
Fusion.discordQueue("Sorry, " + user + ", but " + addedUser.displayName + " has already joined the event. You can use `!removeplayer` to remove them instead.", channel);
return;
}
event.players[addedUser.id] = {
home: undefined,
canHost: true
};
user.addRole(eventRole);
Fusion.discordQueue("You have been successfully added " + addedUser.displayName + " to the event.", user);
Fusion.discordQueue(obsDiscord.members.get(user.id).displayName + " has added you to the next event! Please use the `!home` command to select a home level, for example, `!home Logic x2`. I assume you can host games, but if you cannot please issue the `!host` command to toggle this option.", addedUser);
Fusion.discordQueue(addedUser.displayName + " has joined the tournament!", generalChannel);
},
removeplayer: (from, user, channel, message) => {
"use strict";
var matches;
if (!Fusion.isAdmin(user) || !message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
// Get the user by ID.
matches = idParse.exec(message);
if (!matches) {
Fusion.discordQueue("Sorry, " + user + ", but you must mention the user to remove them, try this command on a public channel instead.", channel);
return;
}
var removedUser = obsDiscord.members.get(matches[1]);
if (!removedUser) {
Fusion.discordQueue("Sorry, " + user + ", but that person is not part of this Discord server.", channel);
return;
}
if (!event.players[removedUser.id]) {
Fusion.discordQueue("Sorry, " + user + ", but " + removedUser.displayName + " is not part of the event. You can use `!removeplayer` to remove them instead.", channel);
return;
}
event.players[user.id].withdrawn = true;
user.removeRole(eventRole);
Fusion.discordQueue("You have been successfully removed " + removedUser.displayName + " from the event.", user);
Fusion.discordQueue(obsDiscord.members.get(user.id).displayName + " has removed you from the next event! If this is in error, please contact " + obsDiscord.members.get(user.id).displayName + ".", removedUser);
Fusion.discordQueue(removedUser.displayName + " has been removed from the tournament.", generalChannel);
},
generateround: (from, user, channel, message) => {
"use strict";
if (!Fusion.isAdmin(user) || message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
Fusion.getPlayers().then((ratedPlayers) => {
var eventPlayers = Object.getOwnPropertyNames(event.players).filter((id) => !event.players[id].withdrawn).map((id) => {
return {
id: id,
eventPlayer: event.players[id],
ratedPlayer: ratedPlayers.find((p) => p.DiscordID === id) || {
Name: obsDiscord.members.get(id).displayName,
DiscordID: id,
Rating: 1500,
RatingDeviation: 200,
Volatility: 0.06
},
points: event.matches.filter((m) => !m.cancelled && m.winner === id).length,
matches: event.matches.filter((m) => !m.cancelled && m.players.indexOf(id) !== -1).length
};
}).sort((a, b) => b.points - a.points || b.ratedPlayer.Rating - a.ratedPlayer.Rating || b.matches - a.matches || (Math.random() < 0.5 ? 1 : -1)),
matchPlayers = () => {
var remainingPlayers = eventPlayers.filter((p) => matches.filter((m) => m.indexOf(p.id) !== -1).length === 0),
firstPlayer = remainingPlayers[0],
potentialOpponents = remainingPlayers.filter(
(p) =>
// Potential opponents don't include the first player.
p.id !== firstPlayer.id &&
// Potential opponents cannot have played against the first player.
event.matches.filter((m) => !m.cancelled && m.players.indexOf(p.id) !== -1 && m.players.indexOf(firstPlayer.id) !== -1).length === 0 &&
// Potential opponents or the first player need to be able to host.
(firstPlayer.eventPlayer.canHost || p.eventPlayer.canHost)
);
// Attempt to assign a bye if necessary.
if (remainingPlayers.length === 1) {
if (event.byes.indexOf(firstPlayer.id) === -1) {
// We can assign the bye. We're done, return true.
event.byes.push(firstPlayer.id);
return true;
} else {
// We can't assign the bye, return false.
return false;
}
}
while (potentialOpponents.length > 0) {
// This allows us to get an opponent that's roughly in the middle in round 1, in the top 1/4 in round 2, the top 1/8 in round 3, etc, so as the tournament goes on we'll get what should be closer matches.
let index = Math.floor(potentialOpponents.length / Math.pow(2, event.round + 1));
// Add the match.
matches.push([firstPlayer.id, potentialOpponents[index].id]);
// If we had 2 or less remaining players at the start of this function, there's none left, so we're done! Return true.
if (remainingPlayers.length <= 2) {
return true;
}
// If we can match the remaining players, we're done! return true.
if (matchPlayers()) {
return true;
}
// If we get here, there was a problem with the previous pairing. Back the match out, remove the offending player, and continue the loop.
matches.pop();
remainingPlayers.splice(index, 1);
}
// If we get here, we can't do any pairings. Return false.
return false;
},
matches = [];
if (!matchPlayers()) {
Fusion.discordQueue("There was a problem matching players up. You'll have to match them up manually. Sorry about that!", user);
return;
}
// We have our matches! Setup rooms, pick home level, and announce the match.
event.round++;
Fusion.discordQueue("Round " + event.round + " starts now!", generalChannel);
Fusion.discordQueue("Round " + event.round + " results:", resultsChannel);
matches.forEach((match) => {
var player1 = obsDiscord.members.get(match[0]),
player2 = obsDiscord.members.get(match[1]),
fxs = [
obsDiscord.createChannel((player1.displayName + "-" + player2.displayName).toLowerCase().replace(/[^\-a-z0-9]/g, ""), "text"),
obsDiscord.createChannel(player1.displayName + "-" + player2.displayName, "voice")
];
Promise.all(fxs).then((channels) => {
var eventMatch = {
players: match,
channel: channels[0],
voice: channels[1]
};
// Select home level
match.sort((a, b) => {
event.matches.filter((m) => !m.cancelled && m.home === a).length - event.matches.filter((m) => !m.cancelled && m.home === b).length || (Math.random() < 0.5 ? 1 : -1);
});
eventMatch.home = match[0];
event.matches.push(eventMatch);
// Setup channels
eventMatch.channel.overwritePermissions(obsDiscord.roles.get(obsDiscord.id), noPermissions);
eventMatch.channel.overwritePermissions(player1, textPermissions);
eventMatch.channel.overwritePermissions(player2, textPermissions);
eventMatch.voice.overwritePermissions(obsDiscord.roles.get(obsDiscord.id), noPermissions);
eventMatch.voice.overwritePermissions(player1, voicePermissions);
eventMatch.voice.overwritePermissions(player2, voicePermissions);
// Announce match
Fusion.discordQueue(player1.displayName + " vs " + player2.displayName + " in " + event.players[eventMatch.home].home, generalChannel);
Fusion.discordQueue(player1 + " vs " + player2 + " in **" + event.players[eventMatch.home].home + "**", eventMatch.channel);
Fusion.discordQueue("A voice channel has been setup for you to use for this match!", eventMatch.channel);
Fusion.discordQueue("Please begin your match! Don't forget to open it up to at least 4 observers. Loser reports the match upon completion. Use the command `!report 20 12` to report the score.", eventMatch.channel);
});
});
}).catch((err) => {
console.log(err);
Fusion.discordQueue("There was a database problem generating the next round of matches! See the error log for details.", user);
});
},
creatematch: (from, user, channel, message) => {
"use strict";
var matches, player1, player2, fxs;
if (!Fusion.isAdmin(user) || !message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
matches = twoIdParse.exec(message);
if (!matches) {
Fusion.discordQueue("Sorry, " + user + ", but you must mention two users to match them up, try this command in a public channel instead.", channel);
return;
}
player1 = obsDiscord.members.get(matches[1]);
player2 = obsDiscord.members.get(matches[2]);
fxs = [
obsDiscord.createChannel((player1.displayName + "-" + player2.displayName).toLowerCase().replace(/[^\-a-z0-9]/g, ""), "text"),
obsDiscord.createChannel(player1.displayName + "-" + player2.displayName, "voice")
];
Promise.all(fxs).then((channels) => {
var eventMatch = {
players: [matches[1], matches[2]],
channel: channels[0],
voice: channels[1]
};
// Select home level
eventMatch.home = matches[1];
event.matches.push(eventMatch);
// Setup channels
eventMatch.channel.overwritePermissions(obsDiscord.roles.get(obsDiscord.id), noPermissions);
eventMatch.channel.overwritePermissions(player1, textPermissions);
eventMatch.channel.overwritePermissions(player2, textPermissions);
eventMatch.voice.overwritePermissions(obsDiscord.roles.get(obsDiscord.id), noPermissions);
eventMatch.voice.overwritePermissions(player1, voicePermissions);
eventMatch.voice.overwritePermissions(player2, voicePermissions);
// Announce match
Fusion.discordQueue(player1.displayName + " vs " + player2.displayName + ", first game in " + event.players[matches[1]].home + ", second game (and sudden death if necessary) in " + event.players[matches[2]].home, generalChannel);
Fusion.discordQueue(player1 + " vs " + player2 + " in **" + event.players[eventMatch.home].home + "**", eventMatch.channel);
Fusion.discordQueue("A voice channel has been setup for you to use for this match!", eventMatch.channel);
Fusion.discordQueue("Please begin your first game! Don't forget to open it up to at least 4 observers.", eventMatch.channel);
});
},
cancelmatch: (from, user, channel, message) => {
"use strict";
var matches, player1, player2, eventMatch;
if (!Fusion.isAdmin(user) || !message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
matches = forceMatchReportParse.exec(message);
if (!matches) {
Fusion.discordQueue("Sorry, " + user + ", but you must mention two users to force the report, followed by the score. Try this command in a public channel instead.", channel);
return;
}
player1 = obsDiscord.members.get(matches[1]);
player2 = obsDiscord.members.get(matches[2]);
eventMatch = event.matches.filter((m) => !m.cancelled && m.players.indexOf(matches[1]) !== -1 && m.players.indexOf(matches[2]) !== -1 && !m.winner)[0];
if (!eventMatch) {
Fusion.discordQueue("Sorry, " + user + ", but I cannot find a match between those two players.", channel);
return;
}
eventMatch.cancelled = true;
if (eventMatch.channel) {
Fusion.discordQueue("This match has been cancelled. This channel and the voice channel will close in 2 minutes.", eventMatch.channel);
setTimeout(() => {
eventMatch.channel.overwritePermissions(player1, noPermissions);
eventMatch.channel.overwritePermissions(player2, noPermissions);
eventMatch.voice.delete();
delete eventMatch.channel;
delete eventMatch.voice;
}, 120000);
}
},
forcereport: (from, user, channel, message) => {
"use strict";
var matches, player1, player2, score1, score2, eventMatch;
if (!Fusion.isAdmin(user) || !message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
matches = forceMatchReportParse.exec(message);
if (!matches) {
Fusion.discordQueue("Sorry, " + user + ", but you must mention two users to force the report, followed by the score. Try this command in a public channel instead.", channel);
return;
}
player1 = obsDiscord.members.get(matches[1]);
player2 = obsDiscord.members.get(matches[2]);
score1 = +matches[3];
score2 = +matches[4];
if (score1 - score2 < 2) {
Fusion.discordQueue("Sorry, " + user + ", but you must list the winner first, and the winner must win by 2.", channel);
return;
}
eventMatch = event.matches.filter((m) => !m.cancelled && m.players.indexOf(matches[1]) !== -1 && m.players.indexOf(matches[2]) !== -1 && !m.winner)[0];
if (!eventMatch) {
Fusion.discordQueue("Sorry, " + user + ", but I cannot find a match between those two players.", channel);
return;
}
eventMatch.winner = matches[1];
eventMatch.score = [score1, score2];
if (eventMatch.channel) {
Fusion.discordQueue("This match has been reported as a win for " + player1.displayName + " by the score of " + score1 + " to " + score2 + ". If this is in error, please contact " + user + ". You may add a comment to this match using `!comment <your comment>` any time before your next match. This channel and the voice channel will close in 2 minutes.", eventMatch.channel);
setTimeout(() => {
eventMatch.channel.overwritePermissions(player1, noPermissions);
eventMatch.channel.overwritePermissions(player2, noPermissions);
eventMatch.voice.delete();
delete eventMatch.channel;
delete eventMatch.voice;
player1.addRole(seasonRole);
player2.addRole(seasonRole);
Fusion.discordQueue(Fusion.resultsText(eventMatch), resultsChannel).then((message) => {
eventMatch.results = message;
});
}, 120000);
} else {
eventMatch.results.edit(Fusion.resultsText(eventMatch));
}
},
endevent: (from, user, channel, message) => {
"use strict";
if (!Fusion.isAdmin(user) || message) {
return;
}
if (!event) {
Fusion.discordQueue("Sorry, " + user + ", but there is no event currently running.", channel);
return;
}
// Get the players from the database.
Fusion.getPlayers().then((ratedPlayers) => {
var fxs;
// Add new ratings for players that haven't played yet.
Object.keys(event.players).forEach((id) => {
if (ratedPlayers.filter((p) => p.DiscordID === id).length === 0) {
ratedPlayers.push({
DiscordID: id,
Rating: 1500,
RatingDeviation: 200,
Volatility: 0.06
});