2024-06-19 08:29:47 +00:00
|
|
|
from typing import Optional, TYPE_CHECKING, List
|
2022-07-31 05:16:38 +00:00
|
|
|
from telegram.constants import ChatAction
|
2023-07-18 09:29:31 +00:00
|
|
|
from telegram.ext import filters
|
2022-07-31 05:16:38 +00:00
|
|
|
|
2024-06-07 15:22:30 +00:00
|
|
|
from core.config import config
|
2022-09-08 01:08:37 +00:00
|
|
|
from core.plugin import Plugin, handler
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.services.cookies.error import TooManyRequestPublicCookies
|
|
|
|
from core.services.template.models import RenderResult
|
|
|
|
from core.services.template.services import TemplateService
|
2024-06-19 08:29:47 +00:00
|
|
|
from gram_core.plugin.methods.inline_use_data import IInlineUseData
|
2023-10-19 10:58:00 +00:00
|
|
|
from plugins.tools.genshin import GenshinHelper
|
2024-07-12 13:54:54 +00:00
|
|
|
from plugins.tools.player_info import PlayerInfoSystem
|
2022-09-08 01:08:37 +00:00
|
|
|
from utils.log import logger
|
2023-08-26 10:19:00 +00:00
|
|
|
from utils.uid import mask_number
|
2022-07-31 05:16:38 +00:00
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
if TYPE_CHECKING:
|
|
|
|
from telegram import Update
|
|
|
|
from telegram.ext import ContextTypes
|
2024-07-04 11:09:48 +00:00
|
|
|
from simnet import ZZZClient
|
2023-07-18 09:29:31 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
__all__ = ("PlayerStatsPlugins",)
|
2022-07-31 05:16:38 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
|
|
|
|
class PlayerStatsPlugins(Plugin):
|
2022-09-08 01:08:37 +00:00
|
|
|
"""玩家统计查询"""
|
2022-07-31 05:16:38 +00:00
|
|
|
|
2024-07-12 13:54:54 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
template: TemplateService,
|
|
|
|
helper: GenshinHelper,
|
|
|
|
player_info: PlayerInfoSystem,
|
|
|
|
):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.template_service = template
|
|
|
|
self.helper = helper
|
2024-07-12 13:54:54 +00:00
|
|
|
self.player_info = player_info
|
2022-07-31 05:16:38 +00:00
|
|
|
|
2024-03-25 12:48:19 +00:00
|
|
|
@handler.command("stats", player=True, block=False)
|
|
|
|
@handler.message(filters.Regex("^玩家统计查询(.*)"), player=True, block=False)
|
2024-06-12 11:59:16 +00:00
|
|
|
async def command_start(self, update: "Update", _: "ContextTypes.DEFAULT_TYPE") -> Optional[int]:
|
2024-03-10 12:40:26 +00:00
|
|
|
user_id = await self.get_real_user_id(update)
|
2024-07-04 11:09:48 +00:00
|
|
|
nickname = self.get_real_user_name(update)
|
2024-06-12 11:59:16 +00:00
|
|
|
uid, offset = self.get_real_uid_or_offset(update)
|
2022-09-08 01:08:37 +00:00
|
|
|
message = update.effective_message
|
2024-03-10 12:40:26 +00:00
|
|
|
self.log_user(update, logger.info, "查询游戏用户命令请求")
|
2022-07-31 05:16:38 +00:00
|
|
|
try:
|
2024-06-12 11:59:16 +00:00
|
|
|
async with self.helper.genshin_or_public(user_id, uid=uid, offset=offset) as client:
|
2023-10-19 10:58:00 +00:00
|
|
|
if not client.public:
|
2023-08-22 02:18:57 +00:00
|
|
|
await client.get_record_cards()
|
2024-07-04 11:09:48 +00:00
|
|
|
render_result = await self.render(client, nickname)
|
2022-10-09 05:45:50 +00:00
|
|
|
except TooManyRequestPublicCookies:
|
|
|
|
await message.reply_text("用户查询次数过多 请稍后重试")
|
|
|
|
return
|
2022-07-31 05:16:38 +00:00
|
|
|
except AttributeError as exc:
|
2022-10-05 16:08:13 +00:00
|
|
|
logger.error("角色数据有误")
|
|
|
|
logger.exception(exc)
|
2024-06-07 15:22:30 +00:00
|
|
|
await message.reply_text(f"角色数据有误 估计是{config.notice.bot_name}晕了")
|
2022-10-09 05:45:50 +00:00
|
|
|
return
|
2024-04-12 14:34:22 +00:00
|
|
|
except ValueError as exc:
|
|
|
|
logger.warning("获取 uid 发生错误! 错误信息为 %s", str(exc))
|
|
|
|
await message.reply_text("输入错误")
|
|
|
|
return
|
2022-07-31 05:16:38 +00:00
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2024-06-10 16:58:39 +00:00
|
|
|
await render_result.reply_photo(message, filename=f"{client.player_id}.png")
|
2022-10-08 12:47:55 +00:00
|
|
|
|
2024-07-04 11:09:48 +00:00
|
|
|
async def render(self, client: "ZZZClient", nickname: str) -> RenderResult:
|
|
|
|
uid = client.player_id
|
|
|
|
user_info = await client.get_zzz_user(uid)
|
2024-07-12 13:54:54 +00:00
|
|
|
player_info = await self.player_info.get_player_info(uid, nickname)
|
2022-10-08 12:47:55 +00:00
|
|
|
|
|
|
|
data = {
|
2023-08-26 10:19:00 +00:00
|
|
|
"uid": mask_number(uid),
|
2022-10-08 12:47:55 +00:00
|
|
|
"stats": user_info.stats,
|
2024-07-12 13:54:54 +00:00
|
|
|
"nickname": player_info.nickname,
|
|
|
|
"level": player_info.level,
|
2022-10-08 12:47:55 +00:00
|
|
|
"stats_labels": [
|
2024-07-04 11:09:48 +00:00
|
|
|
("活跃天数", "active_days"),
|
|
|
|
("获取角色数", "avatar_num"),
|
|
|
|
("绳网声望", "world_level_name"),
|
|
|
|
("式舆防卫战防线", "cur_period_zone_layer_count"),
|
|
|
|
("获得邦布数", "buddy_num"),
|
2022-10-08 12:47:55 +00:00
|
|
|
],
|
2024-07-05 14:10:36 +00:00
|
|
|
"style": "main", # nosec
|
2022-10-08 12:47:55 +00:00
|
|
|
}
|
2024-07-05 14:10:36 +00:00
|
|
|
await self.add_theme_data(data, uid)
|
2022-10-08 12:47:55 +00:00
|
|
|
return await self.template_service.render(
|
2024-07-05 14:10:36 +00:00
|
|
|
"zzz/stats/stats.html",
|
2022-10-08 12:47:55 +00:00
|
|
|
data,
|
2024-07-04 11:09:48 +00:00
|
|
|
{"width": 650, "height": 400},
|
2022-10-08 12:47:55 +00:00
|
|
|
full_page=True,
|
|
|
|
)
|
|
|
|
|
2024-06-19 08:29:47 +00:00
|
|
|
async def stats_use_by_inline(self, update: "Update", context: "ContextTypes.DEFAULT_TYPE"):
|
|
|
|
callback_query = update.callback_query
|
|
|
|
user = update.effective_user
|
2024-07-04 11:09:48 +00:00
|
|
|
nickname = self.get_real_user_name(update)
|
2024-06-19 08:29:47 +00:00
|
|
|
user_id = user.id
|
|
|
|
uid = IInlineUseData.get_uid_from_context(context)
|
|
|
|
|
|
|
|
self.log_user(update, logger.info, "查询游戏用户命令请求")
|
|
|
|
notice = None
|
|
|
|
try:
|
|
|
|
async with self.helper.genshin_or_public(user_id, uid=uid) as client:
|
|
|
|
if not client.public:
|
|
|
|
await client.get_record_cards()
|
2024-07-04 11:09:48 +00:00
|
|
|
render_result = await self.render(client, nickname)
|
2024-06-19 08:29:47 +00:00
|
|
|
except TooManyRequestPublicCookies:
|
|
|
|
notice = "用户查询次数过多 请稍后重试"
|
|
|
|
except AttributeError as exc:
|
|
|
|
logger.error("角色数据有误")
|
|
|
|
logger.exception(exc)
|
|
|
|
notice = f"角色数据有误 估计是{config.notice.bot_name}晕了"
|
|
|
|
except ValueError as exc:
|
|
|
|
logger.warning("获取 uid 发生错误! 错误信息为 %s", str(exc))
|
|
|
|
notice = "UID 内部错误"
|
|
|
|
|
|
|
|
if notice:
|
|
|
|
await callback_query.answer(notice, show_alert=True)
|
|
|
|
return
|
|
|
|
await render_result.edit_inline_media(callback_query)
|
|
|
|
|
2024-07-05 14:10:36 +00:00
|
|
|
async def add_theme_data(self, data, player_id: int):
|
2024-07-12 13:54:54 +00:00
|
|
|
theme_info = await self.player_info.get_theme_info(player_id)
|
|
|
|
data["avatar"] = theme_info.avatar
|
|
|
|
data["background"] = theme_info.name_card
|
2024-07-05 14:10:36 +00:00
|
|
|
return data
|
|
|
|
|
2024-06-19 08:29:47 +00:00
|
|
|
async def get_inline_use_data(self) -> List[Optional[IInlineUseData]]:
|
|
|
|
return [
|
|
|
|
IInlineUseData(
|
|
|
|
text="玩家统计",
|
|
|
|
hash="stats",
|
|
|
|
callback=self.stats_use_by_inline,
|
|
|
|
player=True,
|
|
|
|
),
|
|
|
|
]
|