From: Thorsten Date: Mon, 16 Sep 2024 20:35:31 +0000 (+0200) Subject: aggregate by option, show voters supporting the winning option X-Git-Url: https://git.aero2k.de/?a=commitdiff_plain;p=urlbot-v3.git aggregate by option, show voters supporting the winning option --- diff --git a/src/distbot/plugins/votepoll.py b/src/distbot/plugins/votepoll.py index 2ce8dbf..bab228f 100644 --- a/src/distbot/plugins/votepoll.py +++ b/src/distbot/plugins/votepoll.py @@ -2,6 +2,7 @@ import hashlib import json import logging import time +from collections import Counter, defaultdict from dataclasses import dataclass from datetime import datetime @@ -95,17 +96,27 @@ class Poll: def status_report(self): # Calculate winner and format result message - winner = "It's a tie!" if self.votes: - max_votes = max(self.votes.values()) - winners = [option for option, votes in self.votes.items() if votes == max_votes] + option_counts = {} + for voter, option in self.votes.items(): + if option not in option_counts: + option_counts[option] = 0 + option_counts[option] += 1 + + winners = {} + max_count = max(option_counts.values()) + for option, count in option_counts.items(): + if count == max_count: + winners[option] = [voter for voter, chosen_option in self.votes.items() if + chosen_option == option] else: - winners = [] + winners = {} - if len(winners) == 1: - winner = f"**Winner:** {winners[0]}" - elif len(winners) > 1: - winner = f"**Winners:** {', '.join(winners)}" + if winners: + w = "Winner" if len(winners) == 1 else "Winners" + winner = f"**{w}:** " + ", ".join([f"{o} ({', '.join(v)})" for (o, v) in winners.items()]) + else: + winner = "No winner..." status_message = f"**Poll status:**\n" status_message += self.option_listing(with_votes=True)