2022-10-21 15:52:59 +00:00
|
|
|
|
import asyncio
|
2023-04-28 01:29:16 +00:00
|
|
|
|
from typing import List, Optional, Sequence, TYPE_CHECKING
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2022-11-17 08:19:01 +00:00
|
|
|
|
from genshin import Client, GenshinException, InvalidCookies
|
2022-10-16 10:11:04 +00:00
|
|
|
|
from genshin.models import CalculatorCharacterDetails, CalculatorTalent, Character
|
2023-04-28 01:29:16 +00:00
|
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup
|
2022-10-16 10:11:04 +00:00
|
|
|
|
from telegram.constants import ChatAction, ParseMode
|
2023-04-28 01:29:16 +00:00
|
|
|
|
from telegram.ext import filters
|
2022-11-28 08:38:49 +00:00
|
|
|
|
from telegram.helpers import create_deep_linked_url
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
from core.dependence.assets import AssetsService
|
2022-10-16 10:11:04 +00:00
|
|
|
|
from core.plugin import Plugin, handler
|
2023-03-14 01:27:22 +00:00
|
|
|
|
from core.services.cookies import CookiesService
|
|
|
|
|
from core.services.players import PlayersService
|
|
|
|
|
from core.services.players.services import PlayerInfoService
|
|
|
|
|
from core.services.template.models import FileType
|
|
|
|
|
from core.services.template.services import TemplateService
|
2023-04-28 01:29:16 +00:00
|
|
|
|
from metadata.genshin import AVATAR_DATA
|
2022-10-16 10:11:04 +00:00
|
|
|
|
from modules.wiki.base import Model
|
2023-03-14 01:27:22 +00:00
|
|
|
|
from plugins.tools.genshin import CookiesNotFoundError, GenshinHelper, PlayerNotFoundError, CharacterDetails
|
2022-10-16 10:11:04 +00:00
|
|
|
|
from utils.log import logger
|
2023-04-28 01:29:16 +00:00
|
|
|
|
|
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from telegram.ext import ContextTypes
|
|
|
|
|
from telegram import Update, User
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
|
|
|
|
|
2023-01-05 07:06:59 +00:00
|
|
|
|
class SkillData(Model):
|
|
|
|
|
"""天赋数据"""
|
|
|
|
|
|
|
|
|
|
skill: CalculatorTalent
|
|
|
|
|
buffed: bool = False
|
|
|
|
|
"""是否得到了命座加成"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class AvatarData(Model):
|
|
|
|
|
avatar: Character
|
|
|
|
|
detail: CalculatorCharacterDetails
|
|
|
|
|
icon: str
|
|
|
|
|
weapon: Optional[str]
|
|
|
|
|
skills: List[SkillData]
|
|
|
|
|
|
|
|
|
|
def sum_of_skills(self) -> int:
|
|
|
|
|
total_level = 0
|
2023-03-14 01:27:22 +00:00
|
|
|
|
for skill_data in self.skills:
|
|
|
|
|
total_level += skill_data.skill.level
|
2023-01-05 07:06:59 +00:00
|
|
|
|
return total_level
|
|
|
|
|
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
class AvatarListPlugin(Plugin):
|
2023-04-28 01:29:16 +00:00
|
|
|
|
"""练度统计"""
|
|
|
|
|
|
2022-10-16 10:11:04 +00:00
|
|
|
|
def __init__(
|
2022-12-27 13:56:12 +00:00
|
|
|
|
self,
|
2023-03-14 01:27:22 +00:00
|
|
|
|
player_service: PlayersService = None,
|
2022-12-27 13:56:12 +00:00
|
|
|
|
cookies_service: CookiesService = None,
|
|
|
|
|
assets_service: AssetsService = None,
|
|
|
|
|
template_service: TemplateService = None,
|
2023-03-14 01:27:22 +00:00
|
|
|
|
helper: GenshinHelper = None,
|
|
|
|
|
character_details: CharacterDetails = None,
|
|
|
|
|
player_info_service: PlayerInfoService = None,
|
2022-10-16 10:11:04 +00:00
|
|
|
|
) -> None:
|
|
|
|
|
self.cookies_service = cookies_service
|
|
|
|
|
self.assets_service = assets_service
|
|
|
|
|
self.template_service = template_service
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.helper = helper
|
|
|
|
|
self.character_details = character_details
|
|
|
|
|
self.player_service = player_service
|
|
|
|
|
self.player_info_service = player_info_service
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2023-04-28 01:29:16 +00:00
|
|
|
|
async def get_user_client(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE") -> Optional[Client]:
|
2023-03-14 01:27:22 +00:00
|
|
|
|
message = update.effective_message
|
|
|
|
|
user = update.effective_user
|
2022-10-16 10:11:04 +00:00
|
|
|
|
try:
|
2023-03-14 01:27:22 +00:00
|
|
|
|
return await self.helper.get_genshin_client(user.id)
|
|
|
|
|
except PlayerNotFoundError: # 若未找到账号
|
2022-11-28 08:38:49 +00:00
|
|
|
|
buttons = [[InlineKeyboardButton("点我绑定账号", url=create_deep_linked_url(context.bot.username, "set_cookie"))]]
|
2022-10-16 10:11:04 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2022-10-22 13:54:04 +00:00
|
|
|
|
reply_message = await message.reply_text(
|
2022-10-16 10:11:04 +00:00
|
|
|
|
"未查询到您所绑定的账号信息,请先私聊派蒙绑定账号", reply_markup=InlineKeyboardMarkup(buttons)
|
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
|
|
|
|
self.add_delete_message_job(message, delay=30)
|
2022-10-16 10:11:04 +00:00
|
|
|
|
else:
|
2022-10-22 13:54:04 +00:00
|
|
|
|
await message.reply_text("未查询到您所绑定的账号信息,请先绑定账号", reply_markup=InlineKeyboardMarkup(buttons))
|
2022-10-16 10:11:04 +00:00
|
|
|
|
except CookiesNotFoundError:
|
2022-11-28 08:38:49 +00:00
|
|
|
|
buttons = [[InlineKeyboardButton("点我绑定账号", url=create_deep_linked_url(context.bot.username, "set_cookie"))]]
|
2022-10-16 10:11:04 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2023-03-14 01:27:22 +00:00
|
|
|
|
reply_message = await message.reply_text(
|
2022-10-16 10:11:04 +00:00
|
|
|
|
"此功能需要绑定<code>cookie</code>后使用,请先私聊派蒙绑定账号",
|
|
|
|
|
reply_markup=InlineKeyboardMarkup(buttons),
|
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
|
|
|
|
self.add_delete_message_job(message, delay=30)
|
2022-10-16 10:11:04 +00:00
|
|
|
|
else:
|
2022-10-22 13:54:04 +00:00
|
|
|
|
await message.reply_text(
|
|
|
|
|
"此功能需要绑定<code>cookie</code>后使用,请先私聊派蒙进行绑定",
|
|
|
|
|
parse_mode=ParseMode.HTML,
|
|
|
|
|
reply_markup=InlineKeyboardMarkup(buttons),
|
|
|
|
|
)
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2022-10-21 15:52:59 +00:00
|
|
|
|
async def get_avatar_data(self, character: Character, client: Client) -> Optional["AvatarData"]:
|
2023-03-14 01:27:22 +00:00
|
|
|
|
detail = await self.character_details.get_character_details(client, character)
|
|
|
|
|
if detail is None:
|
2022-10-21 15:52:59 +00:00
|
|
|
|
return None
|
|
|
|
|
if character.id == 10000005: # 针对男草主
|
|
|
|
|
talents = []
|
|
|
|
|
for talent in detail.talents:
|
|
|
|
|
if "普通攻击" in talent.name:
|
|
|
|
|
talent.Config.allow_mutation = True
|
|
|
|
|
# noinspection Pydantic
|
|
|
|
|
talent.group_id = 1131
|
|
|
|
|
if talent.type in ["attack", "skill", "burst"]:
|
|
|
|
|
talents.append(talent)
|
|
|
|
|
else:
|
|
|
|
|
talents = [t for t in detail.talents if t.type in ["attack", "skill", "burst"]]
|
|
|
|
|
buffed_talents = []
|
|
|
|
|
for constellation in filter(lambda x: x.pos in [3, 5], character.constellations[: character.constellation]):
|
|
|
|
|
if result := list(
|
|
|
|
|
filter(lambda x: all([x.name in constellation.effect]), talents) # pylint: disable=W0640
|
|
|
|
|
):
|
|
|
|
|
buffed_talents.append(result[0].type)
|
|
|
|
|
return AvatarData(
|
|
|
|
|
avatar=character,
|
|
|
|
|
detail=detail,
|
|
|
|
|
icon=(await self.assets_service.avatar(character.id).side()).as_uri(),
|
|
|
|
|
weapon=(
|
|
|
|
|
await self.assets_service.weapon(character.weapon.id).__getattr__(
|
|
|
|
|
"icon" if character.weapon.ascension < 2 else "awaken"
|
|
|
|
|
)()
|
|
|
|
|
).as_uri(),
|
|
|
|
|
skills=[
|
|
|
|
|
SkillData(skill=s, buffed=s.type in buffed_talents)
|
|
|
|
|
for s in sorted(talents, key=lambda x: ["attack", "skill", "burst"].index(x.type))
|
|
|
|
|
],
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
async def get_avatars_data(
|
|
|
|
|
self, characters: Sequence[Character], client: Client, max_length: int = None
|
|
|
|
|
) -> List["AvatarData"]:
|
2023-01-05 07:06:59 +00:00
|
|
|
|
async def _task(c):
|
|
|
|
|
return await self.get_avatar_data(c, client)
|
2022-10-21 15:52:59 +00:00
|
|
|
|
|
2023-01-05 07:06:59 +00:00
|
|
|
|
task_results = await asyncio.gather(*[_task(character) for character in characters])
|
2022-10-21 15:52:59 +00:00
|
|
|
|
|
2023-01-05 16:04:59 +00:00
|
|
|
|
return sorted(
|
|
|
|
|
list(filter(lambda x: x, task_results)),
|
|
|
|
|
key=lambda x: (
|
|
|
|
|
x.avatar.level,
|
|
|
|
|
x.avatar.rarity,
|
|
|
|
|
x.sum_of_skills(),
|
|
|
|
|
x.avatar.constellation,
|
|
|
|
|
# TODO 如果加入武器排序条件,需要把武器转化为图片url的处理后置
|
|
|
|
|
# x.weapon.level,
|
|
|
|
|
# x.weapon.rarity,
|
|
|
|
|
# x.weapon.refinement,
|
|
|
|
|
x.avatar.friendship,
|
|
|
|
|
),
|
|
|
|
|
reverse=True,
|
|
|
|
|
)[:max_length]
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2023-04-28 01:29:16 +00:00
|
|
|
|
async def get_final_data(self, player_id: int, user: "User"):
|
2023-03-14 01:27:22 +00:00
|
|
|
|
player = await self.player_service.get(user.id, player_id)
|
|
|
|
|
player_info = await self.player_info_service.get(player)
|
|
|
|
|
nickname = user.full_name
|
|
|
|
|
name_card: Optional[str] = None
|
|
|
|
|
avatar: Optional[str] = None
|
|
|
|
|
rarity: int = 5
|
|
|
|
|
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:
|
2023-04-28 01:29:16 +00:00
|
|
|
|
name_card = (await self.assets_service.namecard(int(player_info.name_card)).navbar()).as_uri()
|
2023-03-14 01:27:22 +00:00
|
|
|
|
if player_info.hand_image is not None:
|
|
|
|
|
avatar = (await self.assets_service.avatar(player_info.hand_image).icon()).as_uri()
|
|
|
|
|
rarity = {k: v["rank"] for k, v in AVATAR_DATA.items()}[str(player_info.hand_image)]
|
2023-04-28 01:29:16 +00:00
|
|
|
|
if name_card is not None: # 默认
|
|
|
|
|
name_card = (await self.assets_service.namecard(210001).navbar()).as_uri()
|
2022-12-25 13:26:08 +00:00
|
|
|
|
return name_card, avatar, nickname, rarity
|
2022-10-17 06:27:37 +00:00
|
|
|
|
|
2022-10-18 06:47:40 +00:00
|
|
|
|
@handler.command("avatars", filters.Regex(r"^/avatars\s*(?:(\d+)|(all))?$"), block=False)
|
|
|
|
|
@handler.message(filters.Regex(r"^(全部)?练度统计$"), block=False)
|
2023-04-28 01:29:16 +00:00
|
|
|
|
async def avatar_list(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE"):
|
2022-10-16 10:11:04 +00:00
|
|
|
|
user = update.effective_user
|
|
|
|
|
message = update.effective_message
|
|
|
|
|
|
2022-10-17 06:27:37 +00:00
|
|
|
|
args = [i.lower() for i in context.match.groups() if i]
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2022-10-17 06:27:37 +00:00
|
|
|
|
all_avatars = any(["all" in args, "全部" in args]) # 是否发送全部角色
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2022-12-23 13:35:26 +00:00
|
|
|
|
logger.info("用户 %s[%s] [bold]练度统计[/bold]: all=%s", user.full_name, user.id, all_avatars, extra={"markup": True})
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
client = await self.get_user_client(update, context)
|
2022-10-16 10:11:04 +00:00
|
|
|
|
if not client:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
notice = await message.reply_text("派蒙需要收集整理数据,还请耐心等待哦~")
|
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
|
|
2022-10-16 14:00:28 +00:00
|
|
|
|
try:
|
2022-12-23 13:35:26 +00:00
|
|
|
|
characters = await client.get_genshin_characters(client.uid)
|
2022-10-16 14:00:28 +00:00
|
|
|
|
avatar_datas: List[AvatarData] = await self.get_avatars_data(
|
|
|
|
|
characters, client, None if all_avatars else 20
|
|
|
|
|
)
|
2022-12-23 13:35:26 +00:00
|
|
|
|
except InvalidCookies as exc:
|
2022-11-17 08:19:01 +00:00
|
|
|
|
await notice.delete()
|
2022-12-23 13:35:26 +00:00
|
|
|
|
await client.get_genshin_user(client.uid)
|
|
|
|
|
logger.warning("用户 %s[%s] 无法请求角色数数据 API返回信息为 [%s]%s", user.full_name, user.id, exc.retcode, exc.original)
|
2023-01-07 07:27:00 +00:00
|
|
|
|
reply_message = await message.reply_text("出错了呜呜呜 ~ 当前访问令牌无法请求角色数数据,请尝试重新获取Cookie。")
|
2022-12-23 13:35:26 +00:00
|
|
|
|
if filters.ChatType.GROUPS.filter(message):
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(reply_message, delay=30)
|
|
|
|
|
self.add_delete_message_job(message, delay=30)
|
2022-12-23 13:35:26 +00:00
|
|
|
|
return
|
2022-10-16 14:00:28 +00:00
|
|
|
|
except GenshinException as e:
|
2023-01-07 07:27:00 +00:00
|
|
|
|
await notice.delete()
|
2022-10-16 14:00:28 +00:00
|
|
|
|
if e.retcode == -502002:
|
2022-12-23 13:35:26 +00:00
|
|
|
|
reply_message = await message.reply_html("请先在米游社中使用一次<b>养成计算器</b>后再使用此功能~")
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(reply_message, delay=20)
|
2022-10-16 14:00:28 +00:00
|
|
|
|
return
|
|
|
|
|
raise e
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
2023-04-28 01:29:16 +00:00
|
|
|
|
name_card, avatar, nickname, rarity = await self.get_final_data(client.uid, user)
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
|
|
|
|
render_data = {
|
|
|
|
|
"uid": client.uid, # 玩家uid
|
|
|
|
|
"nickname": nickname, # 玩家昵称
|
|
|
|
|
"avatar": avatar, # 玩家头像
|
|
|
|
|
"rarity": rarity, # 玩家头像对应的角色星级
|
2022-12-25 13:26:08 +00:00
|
|
|
|
"namecard": name_card, # 玩家名片
|
2022-10-16 10:11:04 +00:00
|
|
|
|
"avatar_datas": avatar_datas, # 角色数据
|
|
|
|
|
"has_more": len(characters) != len(avatar_datas), # 是否显示了全部角色
|
|
|
|
|
}
|
|
|
|
|
|
2022-10-22 13:54:04 +00:00
|
|
|
|
as_document = all_avatars and len(characters) > 20
|
2022-10-22 07:03:59 +00:00
|
|
|
|
|
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_DOCUMENT if as_document else ChatAction.UPLOAD_PHOTO)
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
|
|
|
|
image = await self.template_service.render(
|
|
|
|
|
"genshin/avatar_list/main.html",
|
|
|
|
|
render_data,
|
|
|
|
|
viewport={"width": 1040, "height": 500},
|
|
|
|
|
full_page=True,
|
|
|
|
|
query_selector=".container",
|
2022-10-22 07:03:59 +00:00
|
|
|
|
file_type=FileType.DOCUMENT if as_document else FileType.PHOTO,
|
|
|
|
|
ttl=30 * 24 * 60 * 60,
|
2022-10-16 10:11:04 +00:00
|
|
|
|
)
|
2023-03-14 01:27:22 +00:00
|
|
|
|
self.add_delete_message_job(notice, delay=5)
|
2022-10-22 07:03:59 +00:00
|
|
|
|
if as_document:
|
|
|
|
|
await image.reply_document(message, filename="练度统计.png")
|
2022-10-16 10:11:04 +00:00
|
|
|
|
else:
|
2022-10-22 07:03:59 +00:00
|
|
|
|
await image.reply_photo(message)
|
2022-10-16 10:11:04 +00:00
|
|
|
|
|
|
|
|
|
logger.info(
|
2022-12-23 13:35:26 +00:00
|
|
|
|
"用户 %s[%s] [bold]练度统计[/bold]发送%s成功",
|
|
|
|
|
user.full_name,
|
|
|
|
|
user.id,
|
|
|
|
|
"文件" if all_avatars else "图片",
|
2022-10-16 10:11:04 +00:00
|
|
|
|
extra={"markup": True},
|
|
|
|
|
)
|