Support Flip In Player Cards

This commit is contained in:
omg-xtao 2023-01-20 23:42:57 +08:00 committed by 洛水居室
parent b0576589b7
commit 28e0890390
No known key found for this signature in database
GPG Key ID: C9DE87DA724B88FC

View File

@ -1,3 +1,4 @@
import math
from typing import Any, List, Tuple, Union, Optional from typing import Any, List, Tuple, Union, Optional
import ujson import ujson
@ -121,15 +122,7 @@ class PlayerCards(Plugin, BasePlugin):
logger.info(f"用户 {user.full_name}[{user.id}] 角色卡片查询命令请求 || character_name[{character_name}] uid[{uid}]") logger.info(f"用户 {user.full_name}[{user.id}] 角色卡片查询命令请求 || character_name[{character_name}] uid[{uid}]")
else: else:
logger.info(f"用户 {user.full_name}[{user.id}] 角色卡片查询命令请求") logger.info(f"用户 {user.full_name}[{user.id}] 角色卡片查询命令请求")
buttons = [ buttons = self.gen_button(data, user.id, uid)
InlineKeyboardButton(
value.name,
callback_data=f"get_player_card|{user.id}|{uid}|{value.name}",
)
for value in data.characters
if value.name
]
buttons = [buttons[i : i + 4] for i in range(0, len(buttons), 4)]
if isinstance(self.temp_photo, str): if isinstance(self.temp_photo, str):
photo = self.temp_photo photo = self.temp_photo
else: else:
@ -170,7 +163,15 @@ class PlayerCards(Plugin, BasePlugin):
if user.id != user_id: if user.id != user_id:
await callback_query.answer(text="这不是你的按钮!\n" + config.notice.user_mismatch, show_alert=True) await callback_query.answer(text="这不是你的按钮!\n" + config.notice.user_mismatch, show_alert=True)
return return
logger.info(f"用户 {user.full_name}[{user.id}] 角色卡片查询命令请求 || character_name[{result}] uid[{uid}]") if result == "empty_data":
await callback_query.answer(text="此按钮不可用", show_alert=True)
return
page = 0
if result.isdigit():
page = int(result)
logger.info("用户 %s[%s] 角色卡片查询命令请求 || page[%s] uid[%s]", user.full_name, user.id, page, uid)
else:
logger.info("用户 %s[%s] 角色卡片查询命令请求 || character_name[%s] uid[%s]", user.full_name, user.id, result, uid)
data = await self._fetch_user(uid) data = await self._fetch_user(uid)
if isinstance(data, str): if isinstance(data, str):
await message.reply_text(data) await message.reply_text(data)
@ -179,6 +180,11 @@ class PlayerCards(Plugin, BasePlugin):
await message.delete() await message.delete()
await callback_query.answer("请先将角色加入到角色展柜并允许查看角色详情后再使用此功能,如果已经添加了角色,请等待角色数据更新后重试", show_alert=True) await callback_query.answer("请先将角色加入到角色展柜并允许查看角色详情后再使用此功能,如果已经添加了角色,请等待角色数据更新后重试", show_alert=True)
return return
if page:
buttons = self.gen_button(data, user.id, uid, page)
await message.edit_reply_markup(reply_markup=InlineKeyboardMarkup(buttons))
await callback_query.answer(f"已切换到第 {page}", show_alert=False)
return
for characters in data.characters: for characters in data.characters:
if characters.name == result: if characters.name == result:
break break
@ -192,6 +198,53 @@ class PlayerCards(Plugin, BasePlugin):
render_result.filename = f"player_card_{uid}_{result}.png" render_result.filename = f"player_card_{uid}_{result}.png"
await render_result.edit_media(message) await render_result.edit_media(message)
@staticmethod
def gen_button(
data: EnkaNetworkResponse,
user_id: Union[str, int],
uid: int,
page: int = 1,
) -> List[List[InlineKeyboardButton]]:
"""生成按钮"""
buttons = [
InlineKeyboardButton(
value.name,
callback_data=f"get_player_card|{user_id}|{uid}|{value.name}",
)
for value in data.characters
if value.name
]
all_buttons = [buttons[i : i + 4] for i in range(0, len(buttons), 4)]
send_buttons = all_buttons[(page - 1) * 3 : page * 3]
last_page = page - 1 if page > 1 else 0
all_page = math.ceil(len(all_buttons) / 3)
next_page = page + 1 if page < all_page and all_page > 1 else 0
last_button = []
if last_page:
last_button.append(
InlineKeyboardButton(
"<< 上一页",
callback_data=f"get_player_card|{user_id}|{uid}|{last_page}",
)
)
if last_page or next_page:
last_button.append(
InlineKeyboardButton(
f"{page}/{all_page}",
callback_data=f"get_player_card|{user_id}|{uid}|empty_data",
)
)
if next_page:
last_button.append(
InlineKeyboardButton(
"下一页 >>",
callback_data=f"get_player_card|{user_id}|{uid}|{next_page}",
)
)
if last_button:
send_buttons.append(last_button)
return send_buttons
class Artifact(BaseModel): class Artifact(BaseModel):
"""在 enka Equipments model 基础上扩展了圣遗物评分数据""" """在 enka Equipments model 基础上扩展了圣遗物评分数据"""