class GuildSetup(models.Model): guild = fields.OneToOneField(models.Guild, primary_key=True, on_delete=fields.CASCADE) main_series = fields.OneToOneField(TournamentSeries, null=True, blank=True, on_delete=fields.SET_NULL) allow_matches_in_dms = fields.BooleanField(default=False) use_rating = fields.BooleanField(default=True) show_rating = fields.BooleanField(default=True) verified = fields.BooleanField(default=False) # only verified guilds affect global ELO of players ingame_role = fields.RoleField(null=True, blank=True, on_delete=fields.SET_NULL) default_ruleset = fields.ForeignKey(Ruleset, null=True, blank=True, on_delete=fields.SET_NULL) player_1_blindpick_channel = fields.TextChannelField(null=True, blank=True, on_delete=fields.SET_NULL) player_2_blindpick_channel = fields.TextChannelField(null=True, blank=True, on_delete=fields.SET_NULL) NOT_FOUND_MESSAGE = "This server has not been set up yet, use the `to setup` command." @property def participant_role(self): return self.main_series.participant_role @property def organizer_role(self): return self.main_series.organizer_role @property def streamer_role(self): return self.main_series.streamer_role
class Message(DiscordModel): id = fields.BigIntegerField(primary_key=True) channel = fields.TextChannelField(db_index=True, on_delete=fields.CASCADE) author = fields.UserField(db_index=True, on_delete=fields.CASCADE) _discord_cls = discord.Message _discord_converter_cls = converter.MessageConverter @property def guild(self): return self.channel.guild @guild.setter def guild(self, value): if self.channel.is_dm: raise AttributeError("Cannot set guild of private message") self.channel.guild = value @classmethod def sync_from_discord_obj(cls, discord_obj, create_if_new=True): """Create a Hero object from a Discord object""" if not isinstance(discord_obj, cls._discord_cls): raise TypeError( f"discord_obj has to be a discord.{cls._discord_cls.__name__} " f"but a {type(discord_obj).__name__} was passed") channel, _ = TextChannel.sync_from_discord_obj( discord_obj.channel, create_if_new=create_if_new) if discord_obj.guild: user = discord_obj.author._user else: user = discord_obj.author author, _ = User.sync_from_discord_obj(user) if create_if_new: obj, created = cls.objects.get_or_create(id=discord_obj.id, channel=channel, author=author) else: obj = cls.objects.get(id=discord_obj.id) created = False # obj.channel._discord_obj = discord_obj.channel # obj.user._discord_obj = user obj._discord_obj = discord_obj return obj, not created async def fetch(self) -> discord.Message: # if not self.channel.is_fetched: # await self.channel.fetch() channel = await self.channel await channel.fetch() discord_message = await channel.fetch_message(self.id) self._discord_obj = discord_message return discord_message
class Message(DiscordModel): id = fields.BigIntegerField(primary_key=True) channel = fields.TextChannelField(db_index=True, on_delete=fields.CASCADE) author = fields.UserField(db_index=True, on_delete=fields.CASCADE) _discord_cls = discord.Message _discord_converter_cls = converter.MessageConverter @property def guild(self): return self.channel.guild @guild.setter def guild(self, value): self.channel.guild = value @classmethod def sync_from_discord_obj(cls, discord_obj): """Create a Hero object from a Discord object""" if not isinstance(discord_obj, cls._discord_cls): raise TypeError( f"discord_obj has to be a discord.{cls._discord_cls.__name__} " f"but a {type(discord_obj).__name__} was passed") channel, _ = TextChannel.sync_from_discord_obj(discord_obj.channel) author, _ = Member.sync_from_discord_obj(discord_obj.author) obj = cls(id=discord_obj.id, channel=channel, author=author) obj._discord_obj = discord_obj try: obj.load() existed_already = True except cls.DoesNotExist: existed_already = False return obj, existed_already async def fetch(self) -> discord.Message: if not self.channel.is_fetched: await self.channel.fetch() discord_message = self.channel.get_message(self.id) if discord_message is None: discord_message = await self.channel.fetch_message(self.id) self._discord_obj = discord_message return discord_message
class DoublesMatch(models.Model): id = fields.BigIntegerField(primary_key=True) channel = fields.TextChannelField(null=True, db_index=True, on_delete=fields.SET_NULL) guild = fields.GuildField(on_delete=fields.CASCADE) # if tournament is None, it's a matchmaking match tournament = fields.ForeignKey(Tournament, null=True, blank=True, on_delete=fields.CASCADE) in_dms = fields.BooleanField() team_1 = fields.ForeignKey(ParticipantTeam, on_delete=fields.CASCADE) team_2 = fields.ForeignKey(ParticipantTeam, on_delete=fields.CASCADE) team_1_score = fields.SmallIntegerField(default=0) team_2_score = fields.SmallIntegerField(default=0) current_game = fields.SmallIntegerField(default=1) last_game_won_by = fields.SmallIntegerField(null=True) wins_required = fields.SmallIntegerField(default=2) winner = fields.ForeignKey(ParticipantTeam, null=True, blank=True, on_delete=fields.CASCADE)
class Match(models.Model): class Meta: get_latest_by = 'started_at' id = fields.BigAutoField(primary_key=True) channel = fields.TextChannelField(null=True, blank=True, db_index=True, unique=True, on_delete=fields.SET_NULL) guild = fields.GuildField(on_delete=fields.CASCADE) voice_channel = fields.VoiceChannelField(null=True, blank=True, on_delete=fields.SET_NULL) # if tournament is None, it's a matchmaking match tournament = fields.ForeignKey(Tournament, null=True, blank=True, on_delete=fields.CASCADE) setup = fields.ForeignKey(MatchmakingSetup, null=True, blank=True, on_delete=fields.SET_NULL) management_message = fields.MessageField(null=True, blank=True, on_delete=fields.SET_NULL) ranked = fields.BooleanField() in_dms = fields.BooleanField() # if matchmaking match, looking is player_1, offering is player_2 player_1 = fields.UserField(db_index=True, on_delete=fields.CASCADE) player_1_rating = fields.IntegerField(null=True, blank=True) player_1_deviation = fields.IntegerField(null=True, blank=True) player_1_volatility = fields.FloatField(null=True, blank=True) player_1_global_rating = fields.IntegerField(null=True, blank=True) player_1_global_deviation = fields.IntegerField(null=True, blank=True) player_1_global_volatility = fields.FloatField(null=True, blank=True) player_1_score = fields.SmallIntegerField(default=0) player_2 = fields.UserField(db_index=True, on_delete=fields.CASCADE) player_2_rating = fields.IntegerField(null=True, blank=True) player_2_deviation = fields.IntegerField(null=True, blank=True) player_2_volatility = fields.FloatField(null=True, blank=True) player_2_global_rating = fields.IntegerField(null=True, blank=True) player_2_global_deviation = fields.IntegerField(null=True, blank=True) player_2_global_volatility = fields.FloatField(null=True, blank=True) player_2_score = fields.SmallIntegerField(default=0) current_game = fields.SmallIntegerField(default=1) wins_required = fields.SmallIntegerField(default=3) ruleset = fields.ForeignKey(Ruleset, null=True, blank=True, on_delete=fields.PROTECT) # if winner is None, match is active / ongoing # if winner is Purah, it was a friendly match winner = fields.UserField(null=True, blank=True, db_index=True, on_delete=fields.CASCADE) started_at = fields.DateTimeField(auto_now_add=True, db_index=True) ended_at = fields.DateTimeField(null=True, blank=True) spectating_message = fields.MessageField(null=True, blank=True, on_delete=fields.SET_NULL) match_end_message = fields.MessageField(null=True, blank=True, on_delete=fields.SET_NULL) @classmethod def ranked_matches_today_qs(cls, player_1, player_2, guild=None): one_day_ago = datetime.now() - timedelta(hours=18) # let's be generous if guild is None: qs = (cls.objects.filter(started_at__gt=one_day_ago, tournament=None, ranked=True, player_1=player_1, player_2=player_2) | cls.objects.filter(started_at__gt=one_day_ago, tournament=None, ranked=True, player_1=player_2, player_2=player_1)) else: qs = (cls.objects.filter(guild=guild, started_at__gt=one_day_ago, tournament=None, ranked=True, player_1=player_1, player_2=player_2) | cls.objects.filter(guild=guild, started_at__gt=one_day_ago, tournament=None, ranked=True, player_1=player_2, player_2=player_1)) return qs @async_using_db def get_match_participants(self): if self.tournament is None: return None, None member_1 = models.Member.objects.get(user=self.player_1, guild=self.guild) participant_1 = Participant.objects.get(pk=member_1) member_2 = models.Member.objects.get(user=self.player_2, guild=self.guild) participant_2 = Participant.objects.get(pk=member_2) return participant_1, participant_2
class Message(DiscordModel): channel = fields.TextChannelField(db_index=True, on_delete=fields.CASCADE) author = fields.UserField(db_index=True, on_delete=fields.CASCADE) guild = fields.GuildField(db_index=True, on_delete=fields.CASCADE)
class Tournament(models.Model): id = fields.BigIntegerField(primary_key=True) # Challonge ID key = fields.CharField(max_length=128, unique=True) name = fields.CharField(max_length=128) series = fields.ForeignKey(TournamentSeries, null=True, blank=True, db_index=True, on_delete=fields.SET_NULL) ranked = fields.BooleanField() signup_message = fields.MessageField(unique=True, db_index=True, null=True, on_delete=fields.SET_NULL) checkin_message = fields.MessageField(unique=True, db_index=True, null=True, blank=True, on_delete=fields.SET_NULL) # signup_emoji = fields.EmojiField(default=get_default_emoji, on_delete=fields.SET_DEFAULT) # checkin_emoji = fields.EmojiField(default=get_default_emoji, on_delete=fields.SET_DEFAULT) guild = fields.GuildField(db_index=True, on_delete=fields.CASCADE) announcements_channel = fields.TextChannelField(null=True, on_delete=fields.SET_NULL) talk_channel = fields.TextChannelField(null=True, blank=True, on_delete=fields.SET_NULL) participant_role = fields.RoleField(null=True, unique=True, on_delete=fields.SET_NULL) organizer_role = fields.RoleField(null=True, on_delete=fields.SET_NULL) streamer_role = fields.RoleField(null=True, blank=True, on_delete=fields.SET_NULL) doubles = fields.BooleanField(db_index=True) format = FormatField(default=Formats.double_elimination) allow_matches_in_dms = fields.BooleanField() # don't hard delete rulesets that have already been used # instead, the ruleset should be swapped out with the updated version # and only for upcoming and ongoing tournaments ruleset = fields.ForeignKey(Ruleset, on_delete=fields.PROTECT) start_time = fields.DateTimeField(db_index=True) delay_start = fields.SmallIntegerField(null=True, blank=True) # minutes start_task = fields.ForeignKey(ScheduledTask, null=True, on_delete=fields.SET_NULL) start_checkin_task = fields.ForeignKey(ScheduledTask, null=True, on_delete=fields.SET_NULL) check_reactions_task = fields.ForeignKey(ScheduledTask, null=True, on_delete=fields.SET_NULL) ended = fields.BooleanField(db_index=True, default=False) @property def full_challonge_url(self): return f"https://challonge.com/{self.key}" async def get_challonge_tournament(self): core = self._core extension_name = self._meta.app_label ssbu = core.get_controller(extension_name) return await ssbu.get_challonge_tournament(self.id) @classmethod async def convert(cls, ctx, argument): try: argument = int(argument) # argument is Challonge ID tournament = await cls.async_get(pk=argument) except ValueError: # argument is URL key tournament = await cls.async_get(key=argument) return tournament
class TournamentSeries(models.Model): class Meta: unique_together = (('name', 'guild'), ) key_prefix = fields.CharField(primary_key=True, max_length=128) guild = fields.GuildField(db_index=True, on_delete=fields.CASCADE) name = fields.CharField(max_length=128) next_iteration = fields.IntegerField(default=1) ranked = fields.BooleanField() admins = fields.ManyToManyField(Player, null=True, blank=True) participant_role = fields.RoleField( null=True, unique=True, on_delete=fields.SET_NULL ) # cancel tournament creation if not found on Discord organizer_role = fields.RoleField( null=True, on_delete=fields.SET_NULL ) # cancel tournament creation if not found on Discord streamer_role = fields.RoleField( null=True, blank=True, on_delete=fields.SET_NULL) # delete if not found on Discord # signup_emoji = fields.EmojiField(default=get_default_emoji, on_delete=fields.SET_DEFAULT) # checkin_emoji = fields.EmojiField(default=get_default_emoji, on_delete=fields.SET_DEFAULT) announcements_channel = fields.TextChannelField( null=True, on_delete=fields.SET_NULL) # if None, cancel tournament creation talk_channel = fields.TextChannelField(null=True, blank=True, on_delete=fields.SET_NULL) introduction = fields.TextField(max_length=2048) default_participants_limit = fields.IntegerField(default=512) last_start_time = fields.DateTimeField(null=True, blank=True) delay_start = fields.SmallIntegerField(null=True, blank=True) # minutes interval = IntervalField(null=True, blank=True) doubles = fields.BooleanField(db_index=True) format = FormatField(default=Formats.double_elimination) affects_elo = fields.BooleanField(default=True) allow_matches_in_dms = fields.BooleanField() ruleset = fields.ForeignKey(Ruleset, null=True, blank=True, on_delete=fields.SET_NULL) cancelled = fields.BooleanField(default=False) @property def next_start_time(self): self.last_start_time: datetime.datetime if self.last_start_time is None or self.interval is None: return None if self.interval == Intervals.MONTHLY: weekday = self.last_start_time.weekday() day_number = self.last_start_time.day @classmethod async def convert(cls, ctx, argument): try: tournament_series = await cls.async_get(pk=argument) except ObjectDoesNotExist: try: guild = await models.Guild.from_discord_obj(ctx.guild) tournament_series = await cls.async_get(guild=guild, name=argument) except ObjectDoesNotExist: raise BadArgument( f"{argument} does not seem to be a valid tournament series." ) return tournament_series