Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Weigh team chosen for team game towards those with fewer points. #120

Merged
merged 3 commits into from
Apr 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading