forked from thomasgonda3/QueueBot
-
Notifications
You must be signed in to change notification settings - Fork 3
/
mogi_objects.py
888 lines (771 loc) · 34.5 KB
/
mogi_objects.py
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
from __future__ import annotations
import traceback
import common
from contextlib import suppress
from common import flatten
import random
import discord
from datetime import datetime, timezone, timedelta
from discord.ui import View
from typing import List, Tuple, Callable, Set
import host_fcs
class PrepFailure(Exception):
pass
class WrongChannelType(TypeError, PrepFailure):
pass
class RoleFailure(PrepFailure):
pass
class RoleAddFailure(RoleFailure):
pass
class RoleNotFound(RoleFailure):
pass
class NoFreeChannels(PrepFailure):
pass
def average(list_: List[int | float]) -> float:
return sum(list_) / len(list_)
class Mogi:
ALGORITHM_STATUS_INSUFFICIENT_PLAYERS = 1
ALGORITHM_STATUS_2_OR_MORE_ROOMS = 2
ALGORITHM_STATUS_SUCCESS_FOUND = 3
ALGORITHM_STATUS_SUCCESS_EMPTY = 4
ALGORITHM_STATUS_SUCCESS_SOME_INVALID = 5
def __init__(
self,
sq_id: int,
max_players_per_team: int,
players_per_room: int,
mogi_channel: discord.TextChannel,
is_automated=False,
start_time=None,
display_time=None,
additional_extension_minutes=0,
format=None):
self.started = False
self.gathering = False
self.making_rooms_run = False
self.making_rooms_run_time = None
self.sq_id = sq_id
self.max_player_per_team = max_players_per_team
self.players_per_room = players_per_room
self.mogi_channel = mogi_channel
self.teams: List[Team] = []
self.rooms: List[Room] = []
self.is_automated = is_automated
self.start_time = start_time if is_automated else None
self.display_time = display_time if is_automated else None
self.additional_extension = timedelta(
minutes=additional_extension_minutes)
self.format = format
self.has_checked_auto_extend = False
@property
def num_players(self):
"""Returns the total number of players in teams where all players have confirmed"""
return sum(len(t.players) for t in self.teams if t.all_registered())
@property
def num_teams(self):
"""Returns the total number of teams where all players have confirmed"""
return self.count_registered()
@property
def max_possible_rooms(self) -> int:
"""Returns the maximum possible number of rooms based on teams where all players have confirmed. Depending on range cutoffs or
the specifics of the algorithm used to make the actual rooms, this number could be higher than the final number of rooms."""
return self.num_players // self.players_per_room
@staticmethod
def _minimize_range(
players: List[Player],
num_players: int) -> List[Player] | None:
"""Returns a collection of players (the number of players in the collection is the given num_players parameter) whose has the smallest
mmr spread. If the number of players in the given list is smaller than the request num_players collection size, or the num_players is less than 2 (doesn't make sense), None is returned."""
# The number of players we were given is less than the collection size
# we are supposed to return, so return None
if len(players) < num_players:
return None
if num_players <= 1:
return None
# Sort the players so we easily know the player with the lowest rating
# and highest rating in any given collection
sorted_players = sorted(players)
# In the beginning, the best found collection of players is the first
# 12
best_collection = sorted_players[0:num_players]
cur_min = best_collection[-1].adjusted_mmr - \
best_collection[0].adjusted_mmr
# Find the collection of players with the least rating spread
for lowest_player_index, highest_player in enumerate(
sorted_players[num_players:], 1):
lowest_player = sorted_players[lowest_player_index]
cur_range = highest_player.adjusted_mmr - lowest_player.adjusted_mmr
if cur_range < cur_min:
cur_min = cur_range
best_collection = sorted_players[lowest_player_index:
lowest_player_index + num_players]
return best_collection
def _all_room_final_list_algorithm(self, valid_players_check: Callable[[
List[Player]], bool]) -> Tuple[List[List[Player]], int]:
if self.max_possible_rooms == 0:
return [], Mogi.ALGORITHM_STATUS_INSUFFICIENT_PLAYERS
all_confirmed_players = self.players_on_confirmed_teams()
first_late_player_index = (
self.num_players // self.players_per_room) * self.players_per_room
on_time_players = sorted(
all_confirmed_players[:first_late_player_index], reverse=True)
late_players = all_confirmed_players[first_late_player_index:]
player_rooms: List[List[Player]] = list(
common.divide_chunks(on_time_players, self.players_per_room))
any_invalid = False
for pr_index, player_room in enumerate(player_rooms, 0):
for lp_index in range(len(late_players) + 1):
current_late_check_players = late_players[0:lp_index]
best_collection = Mogi._minimize_range(
player_room + current_late_check_players, self.players_per_room)
if valid_players_check(best_collection):
best_collection.sort(reverse=True)
# Get all the players who were swapped out of the room and
# put them at the front of the late player list
swapped_out_players = [
p for p in player_room if p not in best_collection]
swapped_in_players = [
p for p in best_collection if p not in player_room]
late_players = swapped_out_players + \
list(
filter(lambda p: p not in swapped_in_players, late_players))
player_rooms[pr_index] = best_collection
break
else:
# After checking all late players, no room player list with a
# valid range could be found for this room
any_invalid = True
return player_rooms, (
Mogi.ALGORITHM_STATUS_SUCCESS_SOME_INVALID if any_invalid else Mogi.ALGORITHM_STATUS_SUCCESS_FOUND)
def _one_room_final_list_algorithm(self, valid_players_check: Callable[[
List[Player]], bool]) -> Tuple[List[Player], int]:
if self.max_possible_rooms == 0:
return [], Mogi.ALGORITHM_STATUS_INSUFFICIENT_PLAYERS
if self.max_possible_rooms > 1:
confirmed_players = self.players_on_confirmed_teams()
return sorted(confirmed_players[0:self.players_per_room *
self.max_possible_rooms], reverse=True), Mogi.ALGORITHM_STATUS_2_OR_MORE_ROOMS
# At this point, we can only make one possible room, so our algorithm
# will be used
confirmed_players = self.players_on_confirmed_teams()
cur_check_list = list(confirmed_players[0:self.players_per_room])
late_players = list(confirmed_players[self.players_per_room:])
while True:
best_collection = Mogi._minimize_range(
cur_check_list, self.players_per_room)
if valid_players_check(best_collection):
return sorted(
best_collection, reverse=True), Mogi.ALGORITHM_STATUS_SUCCESS_FOUND
if len(late_players) == 0:
break
cur_check_list.append(late_players.pop(0))
# Even after checking the late players, we did not find
return best_collection, Mogi.ALGORITHM_STATUS_SUCCESS_EMPTY
def _mk8dx_generate_final_list(self) -> List[Player]:
confirmed_players = self.players_on_confirmed_teams()
if self.max_possible_rooms == 0:
return confirmed_players
return sorted(
confirmed_players[0:self.players_per_room * self.max_possible_rooms], reverse=True)
def _mkw_generate_final_list(self, valid_players_check: Callable[[
List[Player]], bool]) -> List[Player]:
result, status = self._all_room_final_list_algorithm(
valid_players_check)
if status is Mogi.ALGORITHM_STATUS_INSUFFICIENT_PLAYERS:
return self.players_on_confirmed_teams()
# I do not want to formally prove that sorting here is correct
return sorted(common.flatten(result), reverse=True)
def generate_proposed_list(self,
valid_players_check: Callable[[List[Player]],
bool] = None) -> List[Player]:
"""Algorithm that generates a proposed list of players that will play. This algorithm may differ between
MK8DX and MKW. The algorithm is allowed to propose any list of players it wants to. Among several possibilities,
this allows the algorithm to change the order of the players in the returned list, add or remove players,
and more.
The algorithm may or may not enforce a hard check of the valid players. That is up to the implemented
algorithm."""
if common.SERVER is common.Server.MK8DX:
return self._mk8dx_generate_final_list()
elif common.SERVER is common.Server.MKW:
return self._mkw_generate_final_list(valid_players_check)
else:
raise ValueError(f"Unknown server in config: {common.Server}")
def _count_cancelled(self,
player_list: List[Player],
valid_players_check: Callable[[List[Player]],
bool] = None) -> int:
"""Using the provided valid players check function and player list, returns the number of rooms that would be cancelled."""
if valid_players_check is None:
return 0
return sum(
1 for room_players in common.divide_chunks(
player_list,
self.players_per_room) if not valid_players_check(room_players))
def _any_cancelled(self,
player_list: List[Player],
valid_players_check: Callable[[List[Player]],
bool] = None) -> bool:
return self._count_cancelled(player_list, valid_players_check) > 0
def any_room_cancelled(self, valid_players_check: Callable[[
List[Player]], bool] = None) -> bool:
"""Using the provided valid players check function, returns if any of the rooms will be cancelled."""
if valid_players_check is None:
return False
proposed_list = self.generate_proposed_list(valid_players_check)
return self._any_cancelled(proposed_list, valid_players_check)
def check_player(self, member):
for team in self.teams:
if team.has_player(member):
return team
return None
def count_registered(self) -> int:
"""Returns the number of teams that are registered"""
return sum(1 for team in self.teams if team.all_registered())
def confirmed_teams(self) -> List["Team"]:
return [team for team in self.teams if team.all_registered()]
def players_on_confirmed_teams(self) -> List[Player]:
return flatten([team.players for team in self.confirmed_teams()])
def all_room_channel_ids(self) -> Set[int]:
return {
r.channel.id for r in self.rooms if r is not None and r.channel is not None}
def channel_id_in_rooms(self, channel_id: int):
return channel_id in self.all_room_channel_ids()
def get_room_from_channel_id(self, channel_id: int):
for room in self.rooms:
if room is None or room.channel is None:
continue
if room.channel.id == channel_id:
return room
return None
async def populate_host_fcs(self):
all_hosts = {str(plr.member.id): plr for plr in filter(
lambda p: p.host, self.players_on_confirmed_teams())}
hosts = await host_fcs.get_hosts(all_hosts)
for host_discord_id, host_fc in hosts.items():
player: Player = all_hosts.get(host_discord_id)
if player is not None:
player.host_fc = host_fc
async def assign_roles(self, guild: discord.Guild):
for room in self.rooms:
try:
to_skip = None if room.room_role is None else {
room.room_role.id}
await room.assign_roles(guild=guild, role_skip=to_skip)
except RoleAddFailure:
pass
class Room:
def __init__(
self,
teams,
room_num: int,
channel: discord.Thread | discord.TextChannel,
tier_info):
self.teams: List["Team"] = teams
self.room_num = room_num
self.channel = channel
self.view = None
self.finished = False
self.host_list: List["Player"] = []
self.subs: List[int] = []
self.tier_info = tier_info
self.room_role = None
@property
def tier(self) -> str:
if common.SERVER is common.Server.MK8DX:
return get_tier_mk8dx(round(self.avg_mmr) - 500)
if common.SERVER is common.Server.MKW:
return str(get_tier_mkw(self.avg_mmr))
@property
def tier_collection(self) -> str:
if common.SERVER is common.Server.MK8DX:
return self.tier
if common.SERVER is common.Server.MKW:
tier_number = self.tier
if not common.is_int(tier_number):
return "HT"
tier_number = int(tier_number)
if tier_number <= 2:
return "LT"
if tier_number >= 5:
return "HT"
return "MT"
@property
def mmr_high(self) -> int:
if self.teams is None:
return None
return max(self.players).adjusted_mmr
@property
def mmr_low(self) -> int:
if self.teams is None:
return None
return min(self.players).adjusted_mmr
@property
def avg_mmr(self) -> int:
if self.teams is None:
return 0
return int(average([p.mmr for p in self.players]))
@property
def players(self) -> List[Player]:
if self.teams is None:
return []
return flatten([t.players for t in self.teams])
def get_player_list(self):
return [player.member.id for team in self.teams for player in team.players]
def create_host_list(self):
all_hosts = list(filter(lambda p: p.host, self.players))
random.shuffle(all_hosts)
self.host_list.clear()
self.host_list.extend(all_hosts)
def get_host_str(self) -> str:
if len(self.host_list) == 0:
return ""
host_strs = []
for i, player in enumerate(self.host_list, 1):
host_strs.append(f"{i}. {player.member.display_name}")
# First player on the list should be bold
if i == 1:
host_strs[0] = f"**{host_strs[0]}**"
result = f"Host: {', '.join(host_strs)}"
if common.SERVER is common.Server.MKW and self.host_list[0].host_fc is not None:
result += f"\n**Host ({self.host_list[0].member.display_name}) Friend Code: {self.host_list[0].host_fc}**"
return result
def check_player(self, member):
if self.teams is None:
return None
for team in self.teams:
if team.has_player(member):
return team
return None
async def assign_member_room_role(self, member: discord.Member):
if self.room_role is None:
raise RoleNotFound(f"Could not find role for tier {self.tier}")
try:
await member.add_roles(self.room_role)
except BaseException:
raise RoleAddFailure(
f"Failed to add role {self.room_role.name} to {member}\n")
async def assign_roles(self, guild: discord.Guild, role_skip=None):
role_add_fail_text = ""
role_skip = set() if role_skip is None else set(role_skip)
for player in self.players:
updated_member = guild.get_member(player.member.id)
if updated_member is not None:
member_role_ids = {r.id for r in updated_member.roles}
if len(role_skip.intersection(member_role_ids)) > 0:
continue
try:
await self.assign_member_room_role(player.member)
except RoleAddFailure:
role_add_fail_text += f"Failed to add role {self.room_role.name} to {player.member}\n"
print(traceback.format_exc())
if role_add_fail_text != "":
await self.channel.send(role_add_fail_text)
raise RoleAddFailure(role_add_fail_text)
async def prepare_room_channel(self, guild: discord.Guild, all_events: List[Mogi | None]):
if common.CONFIG["USE_THREADS"]:
return
# Find the available tier channels for the tier (collection)
tier_collection = self.tier_collection
tier_data = common.CONFIG["TIER_CHANNELS"][tier_collection]
free_tier_channel_ids = list(tier_data["channel_ids"])
for event in all_events:
if event is None:
continue
for channel_id in event.all_room_channel_ids():
with suppress(ValueError):
free_tier_channel_ids.remove(channel_id)
# If there are no available tier channels, raise an exception
if len(free_tier_channel_ids) == 0:
raise NoFreeChannels(
f"No free channels for tier {tier_collection}")
# Get the discord.GuildChannel of the first available channel id
found_channel = guild.get_channel(free_tier_channel_ids[0])
if not isinstance(found_channel, discord.TextChannel):
raise WrongChannelType(
f"For tier {tier_collection}, channel id {channel_id} is of type {type(found_channel)}, expected discord.TextChannel")
self.channel = found_channel
# Assign the role for the tier collection to all players in the room so
# they can see the tier
tier_role_id = tier_data["tier_role_id"]
self.room_role = guild.get_role(tier_role_id)
if self.room_role is None:
raise RoleNotFound(
f"Could not find role for role id {tier_role_id} for tier {tier_collection}")
await self.assign_roles(guild=guild, role_skip=tier_data["role_ids_can_see_already"] + [tier_role_id])
class Team:
def __init__(self, players: List["Player"]):
self.players = players
def get_mentions(self):
"""Return a string where all players on the team are discord @'d"""
return " ".join([p.member.mention for p in self.players])
@property
def avg_mmr(self):
return average([p.mmr for p in self.players])
def all_registered(self):
"""Returns if all players on the team are registered"""
return all(player.confirmed for player in self.players)
def has_player(self, member):
return any(player.member.id == member.id for player in self.players)
def get_player(self, member: discord.Member) -> Player | None:
for player in self.players:
if player.member.id == member.id:
return player
return None
def num_confirmed(self):
"""Returns the number of confirmed players in the team"""
return sum(1 for player in self.players if player.confirmed)
def get_unconfirmed(self):
"""Returns a list of players on the team who have not confirmed yet."""
return [player for player in self.players if not player.confirmed]
def __lt__(self, other):
return self.avg_mmr < other.avg_mmr
def __gt__(self, other):
return other < self
# def __eq__(self, other):
# if self.avg_mmr == other.avg_mmr:
# return True
# return False
def __str__(self):
return ", ".join([p.lounge_name for p in self.players])
class Player:
def __init__(
self,
member: discord.Member,
lounge_name: str,
mmr: int,
confirmed=False,
host=False):
self.member = member
self.lounge_name = lounge_name
self.mmr = mmr
self.confirmed = confirmed
self.score = 0
self.host = host
self.host_fc = None
@property
def adjusted_mmr(self):
minimum_mmr = common.CONFIG["MATCHMAKING_BOTTOM_MMR"]
maximum_mmr = common.CONFIG["MATCHMAKING_TOP_MMR"]
if minimum_mmr is None or maximum_mmr is None:
return self.mmr
if self.mmr < minimum_mmr:
return minimum_mmr
if self.mmr > maximum_mmr:
return maximum_mmr
return self.mmr
@property
def is_matchmaking_mmr_adjusted(self):
return self.mmr != self.adjusted_mmr
@property
def mention(self):
"""String that, when sent in a Discord message, will mention and ping the player."""
if self.member is None:
return "<@!1>"
return self.member.mention
def __repr__(self):
return f"{__class__.__name__}(member={self.member}, lounge_name={self.lounge_name}, mmr={self.mmr}, confirmed={self.confirmed})"
def __lt__(self, other: Player):
return self.mmr < other.mmr
class VoteView(View):
def __init__(
self,
players,
thread,
mogi: Mogi,
room: Room,
penalty_time: int,
room_start_msg_link):
super().__init__()
self.players: List[Player] = players
self.room_channel: discord.Thread | discord.TextChannel = thread
self.mogi = mogi
self.room = room
self.header_text = ""
self.message = None
self.room_start_msg_link = room_start_msg_link
self.teams_text = ""
self.found_winner = False
self.penalty_time = penalty_time
self.votes = {"FFA": [],
"2v2": [],
"3v3": [],
"4v4": [],
"6v6": []
}
async def create_message(self):
if self.mogi.format:
await self.room_channel.send(
content=f"The format **{self.mogi.format}** has been selected!",
view=self
)
if self.mogi.format == "FFA":
await self.make_teams(1, "FFA")
elif self.mogi.format == "2v2":
await self.make_teams(2, "2v2")
elif self.mogi.format == "3v3":
await self.make_teams(3, "3v3")
elif self.mogi.format == "4v4":
await self.make_teams(4, "4v4")
elif self.mogi.format == "6v6":
await self.make_teams(6, "6v6")
else:
self.add_button("FFA", self.general_vote_callback)
self.add_button("2v2", self.general_vote_callback)
self.add_button("3v3", self.general_vote_callback)
self.add_button("4v4", self.general_vote_callback)
if common.SERVER is common.Server.MKW:
self.add_button("6v6", self.general_vote_callback)
self.message = await self.room_channel.send(
view=self
)
async def make_teams(self, players_per_team: int, vote_str: str):
if common.SERVER is common.Server.MKW:
self.mogi.making_rooms_run_time = datetime.now(timezone.utc)
if vote_str == "6v6":
players_per_team = 1
elif common.SERVER is common.Server.MK8DX:
self.mogi.making_rooms_run_time = self.mogi.start_time + \
timedelta(minutes=5)
random.shuffle(self.players)
room = self.mogi.get_room_from_channel_id(self.room_channel.id)
msg = "ㅤ\n"
if not self.mogi.format:
msg = f"""**Poll Ended!**
1) FFA - {len(self.votes['FFA'])}
2) 2v2 - {len(self.votes['2v2'])}
3) 3v3 - {len(self.votes['3v3'])}
4) 4v4 - {len(self.votes['4v4'])}
"""
if common.SERVER is common.Server.MKW:
msg += f"5) 6v6 - {len(self.votes['6v6'])}\n"
msg += f"Winner: {vote_str}\n\n"
self.header_text = ""
tier_text = "Tier " if common.SERVER is common.Server.MK8DX else "T"
self.header_text += f"**Room {room.room_num} MMR: {room.avg_mmr} - {tier_text}{room.tier}** "
msg += self.header_text + "\n"
teams = []
teams_per_room = 12 // players_per_team
for j in range(teams_per_room):
team = Team(
self.players[j * players_per_team:(j + 1) * players_per_team])
teams.append(team)
teams.sort(key=lambda team: team.avg_mmr, reverse=True)
scoreboard_text = []
for j in range(teams_per_room):
team_text = f"`{j + 1}.` "
team_names = ", ".join([discord.utils.escape_markdown(
p.lounge_name) for p in teams[j].players])
raw_team_names = ", ".join(
[p.lounge_name for p in teams[j].players])
scoreboard_text.append(raw_team_names)
team_text += team_names
team_text += f" ({int(teams[j].avg_mmr)} MMR)\n"
msg += team_text
self.teams_text += team_text
if common.SERVER is common.Server.MK8DX:
msg += f"\nTable: `/scoreboard`\n"
msg += f"RandomBot Scoreboard: `/scoreboard {teams_per_room} {', '.join(scoreboard_text)}`\n"
elif common.SERVER is common.Server.MKW and vote_str == "6v6":
captain_1 = teams[0].players[0]
captain_2 = teams[1].players[0]
msg += f"\nCaptain #1: **{captain_1.lounge_name}**\n"
msg += f"Captain #2: **{captain_2.lounge_name}**\n"
msg += f"""Draft instructions:
1. **{captain_1.lounge_name}** picks 1 player
2. **{captain_2.lounge_name}** picks 2 players
3. **{captain_1.lounge_name}** picks 2 players
4. **{captain_2.lounge_name}** picks 2 players
5. **{captain_1.lounge_name}** picks 2 players
6. **{captain_2.lounge_name}** picks 1 players\n"""
penalty_time = self.mogi.making_rooms_run_time + \
timedelta(minutes=self.penalty_time)
room_open_time = self.mogi.making_rooms_run_time
potential_host_str = self.room.get_host_str()
if potential_host_str == "":
if common.SERVER is common.Server.MK8DX:
msg += f"\nRoom open at :{room_open_time.minute:02}, penalty at :{penalty_time.minute:02}. Good luck!"
elif common.SERVER.MKW:
msg += f"\nPenalty is {self.penalty_time} minutes after the room opens. Good luck!"
else:
if common.SERVER is common.Server.MK8DX:
msg += f"\n{potential_host_str}"
msg += f"\nRoom open at :{room_open_time.minute:02}, penalty at :{penalty_time.minute:02}. Good luck!"
else:
cur_time = datetime.now(timezone.utc)
mkw_room_open_time = cur_time + timedelta(minutes=1)
pen_time = mkw_room_open_time + \
timedelta(minutes=self.penalty_time)
msg += f"\nRoom open at :{mkw_room_open_time.minute:02}, penalty at :{pen_time.minute:02}. Good luck!"
room.teams = teams
self.found_winner = True
try:
await self.room_channel.send(msg)
if common.CONFIG["USE_THREADS"]:
new_thread_name = self.room_channel.name + \
f" - {tier_text}{room.tier}"
await self.room_channel.edit(name=new_thread_name)
except discord.HTTPException as e:
print(
f"HTTP error while ending voting for room {self.room.room_num} in function end_voting, skipping to next room.",
flush=True)
async def find_winner(self):
if not self.found_winner:
most_votes = len(max(self.votes.values(), key=len))
winners = []
if len(self.votes["FFA"]) == most_votes:
winners.append((1, "FFA"))
if len(self.votes["2v2"]) == most_votes:
winners.append((2, "2v2"))
if len(self.votes["3v3"]) == most_votes:
winners.append((3, "3v3"))
if len(self.votes["4v4"]) == most_votes:
winners.append((4, "4v4"))
if common.SERVER is common.Server.MKW and len(
self.votes["6v6"]) == most_votes:
winners.append((6, "6v6"))
winner = random.choice(winners)
for curr_button in self.children:
curr_button.disabled = True
if self.message:
try:
await self.message.edit(view=self)
except discord.errors.NotFound:
print(
f"Failed to edit message for interaction: {self.message.id}. Message is no longer valid.", flush=True)
await self.make_teams(*winner)
def add_button(self, label, callback):
button = discord.ui.Button(label=f"{label}: 0", custom_id=label)
button.callback = callback
self.add_item(button)
async def general_vote_callback(self, interaction: discord.Interaction):
if not self.found_winner:
if interaction.user.id not in [p.member.id for p in self.players]:
await interaction.response.send_message("You are not a player in this event.", ephemeral=True)
return
vote = interaction.data['custom_id']
players_per_team = 1
if vote != "FFA":
players_per_team = int(vote[0])
original_vote = None
for vote_option, voter_ids in self.votes.items():
if interaction.user.id in voter_ids:
original_vote = vote_option
voter_ids.remove(interaction.user.id)
if original_vote != vote: # They changed their vote or are a new voter
self.votes[vote].append(interaction.user.id)
if len(self.votes[vote]) == 6:
self.found_winner = True # This fixes a race condition
await self.make_teams(players_per_team, vote)
for curr_button in self.children:
curr_button.label = f"{curr_button.custom_id}: {len(self.votes[curr_button.custom_id])}"
if self.found_winner:
curr_button.disabled = True
try:
await interaction.response.edit_message(view=self)
except discord.errors.NotFound:
print(
f"Failed to edit message for interaction: {interaction.id}. Interaction is no longer valid.", flush=True)
class JoinView(View):
def __init__(self,
room: Room,
get_rating_from_discord_id,
sub_range_mmr_allowance,
bottom_room_num,
is_restricted: Callable[[discord.User | discord.Member],
bool] | None = None):
super().__init__(timeout=1200)
self.room = room
self.get_rating_from_discord_id = get_rating_from_discord_id
self.sub_range_mmr_allowance = sub_range_mmr_allowance
self.bottom_room_num = bottom_room_num
self.is_restricted = is_restricted
@discord.ui.button(label="Join Room")
async def button_callback(self, interaction: discord.Interaction, button):
if self.is_restricted is not None and self.is_restricted(
interaction.user):
await interaction.response.send_message(
"Players with the muted or restricted role cannot use the sub button.", ephemeral=True)
return
if interaction.user.id in self.room.get_player_list() + self.room.subs:
await interaction.response.send_message(
"You are already in this room.", ephemeral=True)
return
try:
user_mmr = self.get_rating_from_discord_id(interaction.user.id)
if isinstance(user_mmr, int):
user_mmr = Player(None, "", user_mmr).adjusted_mmr
except BaseException:
await interaction.response.send_message(
"MMR lookup for player has failed, please try again.", ephemeral=True)
return
mmr_high = 999999 if self.room.room_num == 1 else self.room.mmr_high
mmr_low = -999999 if self.room.room_num == self.bottom_room_num else self.room.mmr_low
if isinstance(user_mmr, int) and mmr_high + \
self.sub_range_mmr_allowance > user_mmr > mmr_low - self.sub_range_mmr_allowance:
self.room.subs.append(interaction.user.id)
button.disabled = True
await interaction.response.edit_message(view=self)
if not common.CONFIG["USE_THREADS"]:
try:
await self.room.assign_member_room_role(interaction.user)
except RoleFailure as e:
self.room.channel.send(str(e))
mention = interaction.user.mention
await self.room.channel.send(f"{mention} has joined the room.")
else:
await interaction.response.send_message(
"You do not meet room requirements", ephemeral=True)
def get_tier_mkw(mmr: int):
if mmr > 10999:
return '8'
if mmr > 9499:
return '7'
if mmr > 7999:
return '6'
if mmr > 6499:
return '5'
if mmr > 4999:
return '4'
if mmr > 3499:
return '3'
if mmr > 1999:
return '2'
if mmr > 499:
return '1'
else:
return '0'
#for tier in tier_info:
#if (tier["minimum_mmr"] is None or mmr >= tier["minimum_mmr"]) and (
#tier["maximum_mmr"] is None or mmr <= tier["maximum_mmr"]):
#return tier["ladder_order"]
def get_tier_mk8dx(mmr: int):
if mmr > 14000:
return 'X'
if mmr > 13000:
return 'S'
if mmr > 12000:
return 'A'
if mmr > 11000:
return 'AB'
if mmr > 10000:
return 'B'
if mmr > 9000:
return 'BC'
if mmr > 8000:
return 'C'
if mmr > 7000:
return 'CD'
if mmr > 6000:
return 'D'
if mmr > 5000:
return 'DE'
if mmr > 4000:
return 'E'
if mmr > 3000:
return 'EF'
if mmr > 2000:
return 'F'
if mmr > 1000:
return 'FG'
else:
return 'G'