diff --git a/libs/discourse_helper.py b/libs/discourse_helper.py index 302ce56..f27dfec 100644 --- a/libs/discourse_helper.py +++ b/libs/discourse_helper.py @@ -24,15 +24,24 @@ def generate_post_winners(all_items: list) -> str: post += "

Raffle Results

\n\n" for item in all_items: - post += f"**{item['description']}**\n\n" + post += f"**{item['description']}**\n" + + post += "\nWinners:\n" for i, entrant in enumerate(item["sorted_winner_list"]): - post += str(i + 1) - post += ". " - post += entrant["username"] - post += " - " - post += entrant["user-item-dice-result"].hex()[:8] - post += "...\n" + if i < item["winners_count"]: + post += generate_entry(i + 1, entrant, True) + else: + continue + + if len(item["sorted_winner_list"]) >= item["winners_count"]: + post += '\n[details="Runner-ups"]\n' + + for i, entrant in enumerate(item["sorted_winner_list"]): + if i >= item["winners_count"]: + post += generate_entry(i + 1, entrant, False) + + post += "[/details]\n" post += "\n" @@ -56,6 +65,21 @@ def generate_post_data(all_items: list) -> str: return post +def generate_entry(idx: int, entrant: dict, tagged: bool) -> str: + output = "" + + output += str(idx) + output += ". " + if tagged: + output += "@" + output += entrant["username"] + output += " - " + output += entrant["user-item-dice-result"].hex()[:8] + output += "...\n" + + return output + + class DiscourseConnection: def __init__(self, url, discourse_api_key, api_username="system") -> None: self._discource_client = DiscourseClient( diff --git a/raffle.py b/raffle.py index 38e6e5a..c6ac3d5 100644 --- a/raffle.py +++ b/raffle.py @@ -8,10 +8,12 @@ import time from base64 import b64encode from gzip import compress +import re from libs.crypto_helper import get_dice_roll, get_hash, hash_xor from libs.discourse_helper import ( DiscourseConnection, + generate_entry, generate_post_data, generate_post_winners, ) @@ -25,6 +27,8 @@ warn = logger.warn err = logger.error +description_re = "^((\d+) [x*] )?\w+.+" + def parse_args(parser): parser.add_argument( @@ -36,8 +40,8 @@ def parse_args(parser): "dump-base64-picked-object", "post-data-to-topic", "post-winners-to-topic", - "print-data", - "print-winners", + "print-raw-data-post", + "print-raw-winners-post", ], help="What action to take.", ) @@ -167,6 +171,13 @@ def main(): for item in all_items: item["dice_roll_hash"] = get_dice_roll(item["close_time"]) + description_match = re.split(description_re, item["description"]) + + item["winners_count"] = 1 + + if description_match[2] is not None: + item["winners_count"] = int(description_match[2]) + for entrant in item["entrants"]: entrant["user-item-dice-result"] = hash_xor( entrant["user-item-hash"], item["dice_roll_hash"] @@ -198,25 +209,25 @@ def main(): for item in all_items: output += f"**{item['description']}**\n\n" - output += "Entrants: \n" + output += "Entrants:\n" for i, entrant in enumerate(item["entrants"]): - output += str(i + 1) - output += ". " - output += entrant["username"] - output += " - " - output += entrant["user-item-hash"].hex()[:8] - output += "...\n" + output += generate_entry(i + 1, entrant, False) - output += "\nWinning Order: \n" + output += "\nWinners:\n" for i, entrant in enumerate(item["sorted_winner_list"]): - output += str(i + 1) - output += ". " - output += entrant["username"] - output += " - " - output += entrant["user-item-dice-result"].hex()[:8] - output += "...\n" + if i < item["winners_count"]: + output += generate_entry(i + 1, entrant, True) + else: + continue + + if len(item["sorted_winner_list"]) >= item["winners_count"]: + output += "\nRunner-ups:\n" + + for i, entrant in enumerate(item["sorted_winner_list"]): + if i >= item["winners_count"]: + output += generate_entry(i + 1, entrant, False) output += "\n"