MibooGram/plugins/zzz/stats.py

136 lines
5.3 KiB
Python
Raw Normal View History

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
from telegram.ext import filters
2022-07-31 05:16:38 +00:00
from core.config import config
from core.plugin import Plugin, handler
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
from plugins.tools.genshin import GenshinHelper
2024-07-12 13:54:54 +00:00
from plugins.tools.player_info import PlayerInfoSystem
from utils.log import logger
from utils.uid import mask_number
2022-07-31 05:16:38 +00:00
if TYPE_CHECKING:
from telegram import Update
from telegram.ext import ContextTypes
from simnet import ZZZClient
__all__ = ("PlayerStatsPlugins",)
2022-07-31 05:16:38 +00:00
class PlayerStatsPlugins(Plugin):
"""玩家统计查询"""
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,
):
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
@handler.command("stats", player=True, block=False)
@handler.message(filters.Regex("^玩家统计查询(.*)"), player=True, block=False)
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)
nickname = self.get_real_user_name(update)
uid, offset = self.get_real_uid_or_offset(update)
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:
async with self.helper.genshin_or_public(user_id, uid=uid, offset=offset) as client:
if not client.public:
await client.get_record_cards()
render_result = await self.render(client, nickname)
except TooManyRequestPublicCookies:
await message.reply_text("用户查询次数过多 请稍后重试")
return
2022-07-31 05:16:38 +00:00
except AttributeError as exc:
logger.error("角色数据有误")
logger.exception(exc)
await message.reply_text(f"角色数据有误 估计是{config.notice.bot_name}晕了")
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)
await render_result.reply_photo(message, filename=f"{client.player_id}.png")
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)
data = {
"uid": mask_number(uid),
"stats": user_info.stats,
2024-07-12 13:54:54 +00:00
"nickname": player_info.nickname,
"level": player_info.level,
"stats_labels": [
("活跃天数", "active_days"),
("获取角色数", "avatar_num"),
("绳网声望", "world_level_name"),
("式舆防卫战防线", "cur_period_zone_layer_count"),
("获得邦布数", "buddy_num"),
],
"style": "main", # nosec
}
await self.add_theme_data(data, uid)
return await self.template_service.render(
"zzz/stats/stats.html",
data,
{"width": 650, "height": 400},
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
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()
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)
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
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,
),
]