Skip to content

Commit

Permalink
advent: display leaderboards as a colour image
Browse files Browse the repository at this point in the history
uses pillow to render leaderboards into an image with a style
very close to the website. inline display in the discord app
is a bit smaller than i would've liked but alas.

text format is kept alongside the image since it's probably nice
to have. maybe this should be behind an argument, but there are
already so many.

implementation has some weaknesses:
- unicode multi-character sequences and emoji aren't rendered
- relies very much on a monospace font for alignment

let me know if this is something you want.
  • Loading branch information
katrinafyi committed Nov 30, 2024
1 parent 8b82091 commit 99f8d51
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 33 deletions.
98 changes: 95 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ aiohttp = "^3.9"
aio-mc-rcon = "^3.2.0"
PyYAML = "^6.0"
mcstatus = "^11.1.0"
pillow = "^11.0.0"

[tool.poetry.scripts]
botdev = "dev.cli:main"
Expand Down
34 changes: 22 additions & 12 deletions uqcsbot/advent.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,13 @@
CACHE_TIME,
parse_leaderboard_column_string,
print_leaderboard,
render_leaderboard_to_image,
render_leaderboard_to_text,
)

# Leaderboard API URL with placeholders for year and code.
LEADERBOARD_URL = "https://adventofcode.com/{year}/leaderboard/private/view/{code}.json"
LEADERBOARD_VIEW_URL = "https://adventofcode.com/{year}/leaderboard/private/view/{code}"
LEADERBOARD_URL = LEADERBOARD_VIEW_URL + ".json"

# UQCS leaderboard ID.
UQCS_LEADERBOARD = 989288
Expand Down Expand Up @@ -519,28 +522,35 @@ async def leaderboard_command(
]
members.sort(key=sorting_functions_for_month[sortby])

view_url = LEADERBOARD_VIEW_URL.format(year=year, code=code)
message += f"\n{view_url}"

if not members:
await interaction.edit_original_response(
content="This leaderboard contains no people."
)
return

scoreboard_file = io.BytesIO(
bytes(
print_leaderboard(
parse_leaderboard_column_string(leaderboard_style, self.bot),
members,
day,
),
"utf-8",
)
leaderboard = print_leaderboard(
parse_leaderboard_column_string(leaderboard_style, self.bot),
members,
day,
)

scoreboard_text = render_leaderboard_to_text(leaderboard)
scoreboard_image = render_leaderboard_to_image(leaderboard)
basename = f"advent_{code}_{year}_{day}"

await interaction.edit_original_response(
content=message,
attachments=[
discord.File(
scoreboard_file,
filename=f"advent_{code}_{year}_{day}.txt",
io.BytesIO(scoreboard_text.encode('utf-8')),
filename=f"{basename}.txt",
),
discord.File(
io.BytesIO(scoreboard_image),
filename=f"{basename}.png",
)
],
)
Expand Down
Binary file added uqcsbot/static/NotoSansMono-Regular.ttf
Binary file not shown.
Loading

0 comments on commit 99f8d51

Please sign in to comment.