From c156a8ee224e79887d44eef316ab13443d29eb28 Mon Sep 17 00:00:00 2001 From: Thorsten Date: Mon, 16 Sep 2024 22:35:31 +0200 Subject: [PATCH] aggregate by option, show voters supporting the winning option --- src/distbot/plugins/votepoll.py | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) 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) -- 2.39.2