Skip to content

Commit

Permalink
fix: Fix missing character names in dmg lbs
Browse files Browse the repository at this point in the history
  • Loading branch information
seriaati committed Oct 11, 2024
1 parent 6da2c5b commit 09de07e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 38 deletions.
10 changes: 2 additions & 8 deletions hoyo_buddy/cogs/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from typing import TYPE_CHECKING, Any

import genshin # noqa: TCH002
from discord import ButtonStyle, Locale, TextStyle, ui
from discord import ButtonStyle, TextStyle, ui
from discord.ext import commands
from loguru import logger
from seria.utils import write_json
Expand Down Expand Up @@ -257,13 +257,7 @@ async def fill_lb_command(self, ctx: commands.Context) -> Any:
logger.info(f"Updating leaderboard data for {account}")
for lb_type in game_lb_types.get(account.game, ()):
try:
await cmd.update_lb_data(
translator=self.bot.translator,
pool=self.bot.pool,
lb_type=lb_type,
account=account,
locale=Locale.american_english,
)
await cmd.update_lb_data(pool=self.bot.pool, lb_type=lb_type, account=account)
except Exception as e:
self.bot.capture_exception(e)

Expand Down
30 changes: 7 additions & 23 deletions hoyo_buddy/commands/leaderboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from hoyo_buddy.exceptions import FeatureNotImplementedError, LeaderboardNotFoundError
from hoyo_buddy.hoyo.clients.ambr import AmbrAPIClient
from hoyo_buddy.icons import get_game_icon
from hoyo_buddy.l10n import EnumStr, LocaleStr, Translator
from hoyo_buddy.l10n import EnumStr, LocaleStr
from hoyo_buddy.ui.hoyo.leaderboard.others import LbPaginator
from hoyo_buddy.utils import ephemeral

Expand All @@ -17,7 +17,6 @@

import genshin
from asyncpg import Pool
from discord import Locale

from hoyo_buddy.types import Interaction

Expand Down Expand Up @@ -161,13 +160,7 @@ async def run(
account = account or await i.client.get_account(
i.user.id, self.get_games_by_lb_type(lb_type)
)
character_names = await self.update_lb_data(
translator=i.client.translator,
pool=i.client.pool,
lb_type=lb_type,
account=account,
locale=locale,
)
await self.update_lb_data(pool=i.client.pool, lb_type=lb_type, account=account)

lb_size = await self.get_lb_size(lb_type, account.game)
embed = (
Expand All @@ -178,6 +171,10 @@ async def run(

you = await Leaderboard.get_or_none(type=lb_type, game=account.game, uid=account.uid)

async with AmbrAPIClient(locale, i.client.translator) as api:
characters = await api.fetch_characters()
character_names = {char.id: char.name for char in characters}

view = LbPaginator(
embed,
you,
Expand All @@ -194,32 +191,21 @@ async def run(
await view.start(i)

async def update_lb_data(
self,
*,
translator: Translator,
pool: Pool,
lb_type: LeaderboardType,
account: HoyoAccount,
locale: Locale,
self, *, pool: Pool, lb_type: LeaderboardType, account: HoyoAccount
) -> dict[str, str] | None:
if lb_type in {LeaderboardType.ABYSS_DMG, LeaderboardType.THEATER_DMG}:
character = await self.fetch_character_by_lb_type(account, lb_type)

if character is None:
value = 0
extra_info = None
character_names = None
else:
value = character.value
extra_info = {"id": character.id, "icon": character.icon}

async with AmbrAPIClient(locale, translator) as api:
characters = await api.fetch_characters()
character_names = {char.id: char.name for char in characters}
else:
value = await self.fetch_value_by_lb_type(account, lb_type)
extra_info = None
character_names = None

if value > 0:
await Leaderboard.update_or_create(
Expand All @@ -231,5 +217,3 @@ async def update_lb_data(
extra_info=extra_info,
)
await update_lb_ranks(pool, game=account.game, type_=lb_type, order=LB_ORDERS[lb_type])

return character_names
11 changes: 4 additions & 7 deletions hoyo_buddy/ui/hoyo/leaderboard/others.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def __init__(
lb_size: int,
order: Literal["ASC", "DESC"],
process_value: Callable[[float], Any],
character_names: dict[str, str] | None,
character_names: dict[str, str],
game: Game,
lb_type: LeaderboardType,
author: User,
Expand All @@ -53,12 +53,9 @@ def __init__(
def get_lb_line(self, lb: Leaderboard) -> str:
value = self.process_value(lb.value)

if self.character_names:
id_ = lb.extra_info["id"]
name = self.character_names.get(str(id_), "???")
name = f" {name} "
else:
name = ""
id_ = lb.extra_info.get("id", "")
name = self.character_names.get(str(id_), "???")
name = f" {name} "

return f"{lb.rank}. {lb.username} ({blur_uid(lb.uid, arterisk='x')}) - {name}**{value}**"

Expand Down

0 comments on commit 09de07e

Please sign in to comment.