2024-05-04 14:47:44 +00:00
|
|
|
from typing import Optional
|
2023-04-28 01:19:20 +00:00
|
|
|
|
2024-06-01 14:21:03 +00:00
|
|
|
from enkanetwork import Assets
|
|
|
|
|
2023-04-28 01:19:20 +00:00
|
|
|
from core.dependence.assets import AssetsService
|
|
|
|
from core.plugin import Plugin
|
|
|
|
from core.services.players.services import PlayerInfoService, PlayersService
|
|
|
|
from metadata.genshin import AVATAR_DATA
|
2023-04-28 05:21:27 +00:00
|
|
|
from utils.log import logger
|
2023-04-28 01:19:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PlayerInfoSystem(Plugin):
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
player_service: PlayersService = None,
|
|
|
|
assets_service: AssetsService = None,
|
|
|
|
player_info_service: PlayerInfoService = None,
|
|
|
|
) -> None:
|
|
|
|
self.assets_service = assets_service
|
|
|
|
self.player_info_service = player_info_service
|
|
|
|
self.player_service = player_service
|
|
|
|
|
2024-09-12 13:13:05 +00:00
|
|
|
async def get_player_info(self, player_id: int, user_id: Optional[int], user_name: str):
|
2024-05-04 14:47:44 +00:00
|
|
|
player = await self.player_service.get(user_id, player_id)
|
2023-04-28 01:19:20 +00:00
|
|
|
player_info = await self.player_info_service.get(player)
|
2024-05-04 14:47:44 +00:00
|
|
|
nickname = user_name
|
2023-04-28 01:19:20 +00:00
|
|
|
name_card: Optional[str] = None
|
|
|
|
avatar: Optional[str] = None
|
|
|
|
rarity: int = 5
|
2023-04-28 05:21:27 +00:00
|
|
|
try:
|
|
|
|
if player_info is not None:
|
|
|
|
if player_info.nickname is not None:
|
|
|
|
nickname = player_info.nickname
|
|
|
|
if player_info.name_card is not None:
|
|
|
|
name_card = (await self.assets_service.namecard(int(player_info.name_card)).navbar()).as_uri()
|
|
|
|
if player_info.hand_image is not None:
|
2024-06-01 14:21:03 +00:00
|
|
|
if player_info.hand_image > 10000000:
|
|
|
|
avatar = (await self.assets_service.avatar(player_info.hand_image).icon()).as_uri()
|
|
|
|
try:
|
|
|
|
rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(player_info.hand_image)]
|
|
|
|
except KeyError:
|
|
|
|
logger.warning("未找到角色 %s 的星级", player_info.hand_image)
|
|
|
|
else:
|
|
|
|
avatar = Assets.profile_picture(player_info.hand_image).url
|
|
|
|
rarity = 5
|
2023-04-28 05:21:27 +00:00
|
|
|
except Exception as exc: # pylint: disable=W0703
|
|
|
|
logger.error("卡片信息请求失败 %s", str(exc))
|
2023-04-28 06:42:19 +00:00
|
|
|
if name_card is None: # 默认
|
2024-04-12 15:09:37 +00:00
|
|
|
name_card = (await self.assets_service.namecard(0).navbar()).as_uri()
|
2024-05-04 14:47:44 +00:00
|
|
|
if avatar is None: # 默认
|
|
|
|
avatar = (await self.assets_service.avatar(0).icon()).as_uri()
|
2023-04-28 01:19:20 +00:00
|
|
|
return name_card, avatar, nickname, rarity
|
2023-04-28 05:21:27 +00:00
|
|
|
|
2024-03-10 12:40:26 +00:00
|
|
|
async def get_name_card(self, player_id: int, user_id: int):
|
|
|
|
player = await self.player_service.get(user_id, player_id)
|
2023-04-28 05:21:27 +00:00
|
|
|
player_info = await self.player_info_service.get(player)
|
|
|
|
name_card: Optional[str] = None
|
|
|
|
try:
|
2023-05-10 09:30:28 +00:00
|
|
|
if player_info is not None and player_info.name_card is not None:
|
|
|
|
name_card = (await self.assets_service.namecard(int(player_info.name_card)).navbar()).as_uri()
|
2023-04-28 05:21:27 +00:00
|
|
|
except Exception as exc: # pylint: disable=W0703
|
|
|
|
logger.error("卡片信息请求失败 %s", str(exc))
|
2023-04-28 06:42:19 +00:00
|
|
|
if name_card is None: # 默认
|
2024-04-12 15:09:37 +00:00
|
|
|
name_card = (await self.assets_service.namecard(0).navbar()).as_uri()
|
2023-04-28 05:21:27 +00:00
|
|
|
return name_card
|