Skip to content

Commit

Permalink
[BattleRoyale] better formatting for leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
japandotorg committed Jul 19, 2024
1 parent d923d54 commit a98a1f9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
16 changes: 9 additions & 7 deletions battleroyale/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
from .constants import SWORDS
from .converters import EmojiConverter
from .game import Game
from .utils import _cooldown, _get_attachments, exceptions, guild_roughly_chunked
from .utils import _cooldown, _get_attachments, exceptions, guild_roughly_chunked, truncate
from .views import JoinGameView

log: logging.Logger = logging.getLogger("red.seina.battleroyale")
Expand Down Expand Up @@ -533,18 +533,20 @@ async def _leaderboard(
leaderboard = leaderboard[:10]
table = pt.PrettyTable()
table.title = "Battle Royale Leaderboard"
table.field_names = ["#", "User", "Wins", "Games", "Kills", "Deaths"]
table.field_names = ["#", "Games / Wins / Kills / Deaths", "User"]
for index, (user_id, user_data) in enumerate(leaderboard, start=1):
if (user := ctx.bot.get_user(int(user_id))) is None:
continue
table.add_row(
row=[
index,
user.display_name,
user_data["wins"],
user_data["games"],
user_data["kills"],
user_data["deaths"],
"{} / {} / {} / {}".format(
user_data["games"],
user_data["wins"],
user_data["kills"],
user_data["deaths"],
),
truncate(user.display_name, max=10),
]
)
description = box(table.get_string(), lang="sml")
Expand Down
7 changes: 7 additions & 0 deletions battleroyale/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,3 +98,10 @@ def _cooldown(ctx: commands.Context) -> Optional[commands.Cooldown]:

def guild_roughly_chunked(guild: discord.Guild) -> bool:
return len(guild.members) / guild.member_count > 0.9


def truncate(text: str, *, max: int) -> str:
if len(text) <= max:
return text
truncated: str = text[: max - 3]
return truncated + "..."

0 comments on commit a98a1f9

Please sign in to comment.