2022-10-29 04:11:26 +00:00
|
|
|
import random
|
2024-08-29 14:50:53 +00:00
|
|
|
from pathlib import Path
|
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-08-29 14:50:53 +00:00
|
|
|
from utils.const import RESOURCE_DIR
|
|
|
|
from utils.error import UrlResourcesNotFoundError
|
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
|
|
|
|
from simnet.models.genshin.chronicle.stats import GenshinUserStats
|
|
|
|
from simnet import GenshinClient
|
|
|
|
|
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
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
def __init__(self, template: TemplateService, helper: GenshinHelper):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.template_service = template
|
|
|
|
self.helper = helper
|
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-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-06-12 11:59:16 +00:00
|
|
|
render_result = await self.render(client, client.player_id)
|
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
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
async def render(self, client: "GenshinClient", uid: Optional[int] = None) -> RenderResult:
|
2022-10-08 13:35:46 +00:00
|
|
|
if uid is None:
|
2023-07-18 09:29:31 +00:00
|
|
|
uid = client.player_id
|
2022-10-08 12:47:55 +00:00
|
|
|
|
2022-10-08 13:35:46 +00:00
|
|
|
user_info = await client.get_genshin_user(uid)
|
2022-10-08 12:47:55 +00:00
|
|
|
|
|
|
|
# 因为需要替换线上图片地址为本地地址,先克隆数据,避免修改原数据
|
|
|
|
user_info = user_info.copy(deep=True)
|
|
|
|
|
|
|
|
data = {
|
2023-08-26 10:19:00 +00:00
|
|
|
"uid": mask_number(uid),
|
2022-10-08 12:47:55 +00:00
|
|
|
"info": user_info.info,
|
|
|
|
"stats": user_info.stats,
|
|
|
|
"explorations": user_info.explorations,
|
2024-04-28 03:24:01 +00:00
|
|
|
"skip_explor": [10],
|
2022-10-08 12:47:55 +00:00
|
|
|
"teapot": user_info.teapot,
|
|
|
|
"stats_labels": [
|
|
|
|
("活跃天数", "days_active"),
|
|
|
|
("成就达成数", "achievements"),
|
|
|
|
("获取角色数", "characters"),
|
2024-08-29 14:50:53 +00:00
|
|
|
("满好感角色数", "full_fetter_avatar_num"),
|
2022-10-08 12:47:55 +00:00
|
|
|
("深境螺旋", "spiral_abyss"),
|
|
|
|
("解锁传送点", "unlocked_waypoints"),
|
|
|
|
("解锁秘境", "unlocked_domains"),
|
|
|
|
("奇馈宝箱数", "remarkable_chests"),
|
|
|
|
("华丽宝箱数", "luxurious_chests"),
|
|
|
|
("珍贵宝箱数", "precious_chests"),
|
|
|
|
("精致宝箱数", "exquisite_chests"),
|
|
|
|
("普通宝箱数", "common_chests"),
|
|
|
|
("风神瞳", "anemoculi"),
|
|
|
|
("岩神瞳", "geoculi"),
|
|
|
|
("雷神瞳", "electroculi"),
|
|
|
|
("草神瞳", "dendroculi"),
|
2023-08-21 02:05:08 +00:00
|
|
|
("水神瞳", "hydroculi"),
|
2024-08-29 14:50:53 +00:00
|
|
|
("火神瞳", "pyroculi"),
|
2022-10-08 12:47:55 +00:00
|
|
|
],
|
2022-10-29 04:11:26 +00:00
|
|
|
"style": random.choice(["mondstadt", "liyue"]), # nosec
|
2022-10-08 12:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
await self.cache_images(user_info)
|
|
|
|
|
|
|
|
return await self.template_service.render(
|
2023-05-09 11:01:45 +00:00
|
|
|
"genshin/stats/stats.jinja2",
|
2022-10-08 12:47:55 +00:00
|
|
|
data,
|
|
|
|
{"width": 650, "height": 800},
|
|
|
|
full_page=True,
|
|
|
|
)
|
|
|
|
|
2024-08-29 14:50:53 +00:00
|
|
|
async def _download_resource(self, url: str) -> str:
|
|
|
|
try:
|
|
|
|
return await self.download_resource(url)
|
2024-09-04 14:00:16 +00:00
|
|
|
except Exception:
|
2024-08-29 14:50:53 +00:00
|
|
|
path = Path(url)
|
|
|
|
file_path = RESOURCE_DIR / "img" / "city" / path.name
|
|
|
|
if file_path.exists():
|
|
|
|
return file_path.as_uri()
|
|
|
|
logger.warning("缓存地区图片资源失败 %s", url)
|
|
|
|
|
2023-07-18 09:29:31 +00:00
|
|
|
async def cache_images(self, data: "GenshinUserStats") -> None:
|
2022-10-08 12:47:55 +00:00
|
|
|
"""缓存所有图片到本地"""
|
|
|
|
# TODO: 并发下载所有资源
|
|
|
|
|
|
|
|
# 探索地区
|
|
|
|
for item in data.explorations:
|
2024-08-29 14:50:53 +00:00
|
|
|
item.icon = await self._download_resource(item.icon)
|
|
|
|
item.cover = await self._download_resource(item.cover)
|
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
|
|
|
|
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, client.player_id)
|
|
|
|
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 get_inline_use_data(self) -> List[Optional[IInlineUseData]]:
|
|
|
|
return [
|
|
|
|
IInlineUseData(
|
|
|
|
text="玩家统计",
|
|
|
|
hash="stats",
|
|
|
|
callback=self.stats_use_by_inline,
|
|
|
|
player=True,
|
|
|
|
),
|
|
|
|
]
|