Skip to content

Commit

Permalink
feat: improved winner results (#18)
Browse files Browse the repository at this point in the history
- also typo fix from modes rename
- winners get tagged
- runner-ups get hidden on discourse

Closes #8 #16
  • Loading branch information
TyIsI authored Jul 17, 2024
1 parent f756123 commit 922546a
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 23 deletions.
38 changes: 31 additions & 7 deletions libs/discourse_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,24 @@ def generate_post_winners(all_items: list) -> str:
post += "<h1>Raffle Results</h1>\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"

Expand All @@ -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(
Expand Down
43 changes: 27 additions & 16 deletions raffle.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -25,6 +27,8 @@
warn = logger.warn
err = logger.error

description_re = "^((\d+) [x*] )?\w+.+"


def parse_args(parser):
parser.add_argument(
Expand All @@ -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.",
)
Expand Down Expand Up @@ -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"]
Expand Down Expand Up @@ -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"

Expand Down

0 comments on commit 922546a

Please sign in to comment.