]> git.aero2k.de Git - urlbot-v3.git/commitdiff
aggregate by option, show voters supporting the winning option master
authorThorsten <mail@aero2k.de>
Mon, 16 Sep 2024 20:35:31 +0000 (22:35 +0200)
committerThorsten <mail@aero2k.de>
Mon, 16 Sep 2024 20:35:31 +0000 (22:35 +0200)
src/distbot/plugins/votepoll.py

index 2ce8dbfe184867966f63142b8a8f6eedf6cc8b23..bab228f3eb9954dbf03fd50fe4de3d209842ec01 100644 (file)
@@ -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)