Skip to content

Commit

Permalink
Merge pull request #120 from python-discord/games/uptime_command
Browse files Browse the repository at this point in the history
Weigh team chosen for team game towards those with fewer points.
  • Loading branch information
hedyhli authored Apr 2, 2024
2 parents ced66b9 + d9faed3 commit fb40b44
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion bot/exts/games.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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")
Expand Down

0 comments on commit fb40b44

Please # to comment.