diff --git a/bot/exts/games.py b/bot/exts/games.py index 6900d4e..35880c8 100644 --- a/bot/exts/games.py +++ b/bot/exts/games.py @@ -146,7 +146,7 @@ async def on_message(self, msg: discord.Message) -> None: await self.set_reaction_time("team") self.team_game_message_id = msg.id - self.chosen_team = random.choice(list(Team)) + self.chosen_team = await self.weighted_random_team() logger.info(f"Starting game in {msg.channel.name} for team {self.chosen_team}") await msg.add_reaction(self.chosen_team.value.emoji) @@ -235,6 +235,19 @@ def get_team(self, member: discord.Member) -> Team | None: return team return None + async def weighted_random_team(self) -> Team: + """Randomly select the next chosen team weighted by current team points.""" + scores = await self.points.to_dict() + teams: list[str] = list(scores.keys()) + inverse_points = [1 / (points or 1) for points in scores.values()] + total_inverse_weights = sum(inverse_points) + weights = [w / total_inverse_weights for w in inverse_points] + + logger.debug(f"{scores = }, {weights = }") + + team_selection = random.choices(teams, weights=weights, k=1)[0] + return Team[team_selection.upper()] + async def set_reaction_time(self, reaction_type: GameType) -> None: """Set the time after which a team reaction can be added.""" reaction_min = await self.game_settings.get("reaction_min")