2023-07-19 05:52:30 +00:00
|
|
|
|
from typing import Optional, List, TYPE_CHECKING
|
2023-04-28 17:50:27 +00:00
|
|
|
|
|
2023-07-19 05:52:30 +00:00
|
|
|
|
from simnet.errors import BadRequest as SimnetBadRequest
|
2023-08-21 14:43:01 +00:00
|
|
|
|
from telegram import Update, Message
|
2023-04-28 17:50:27 +00:00
|
|
|
|
from telegram.constants import ChatAction
|
|
|
|
|
from telegram.ext import CallbackContext, filters
|
|
|
|
|
|
|
|
|
|
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
|
2023-10-19 10:58:51 +00:00
|
|
|
|
from plugins.tools.genshin import GenshinHelper
|
2023-04-28 17:50:27 +00:00
|
|
|
|
from utils.log import logger
|
2023-08-26 13:21:26 +00:00
|
|
|
|
from utils.uid import mask_number
|
2023-04-28 17:50:27 +00:00
|
|
|
|
|
2023-07-19 05:52:30 +00:00
|
|
|
|
if TYPE_CHECKING:
|
|
|
|
|
from simnet import StarRailClient
|
|
|
|
|
|
|
|
|
|
|
2023-04-28 17:50:27 +00:00
|
|
|
|
__all__ = ("PlayerStatsPlugins",)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class PlayerStatsPlugins(Plugin):
|
|
|
|
|
"""玩家统计查询"""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
|
self,
|
|
|
|
|
template: TemplateService,
|
|
|
|
|
helper: GenshinHelper,
|
|
|
|
|
):
|
|
|
|
|
self.template_service = template
|
|
|
|
|
self.helper = helper
|
|
|
|
|
|
2023-06-07 13:32:23 +00:00
|
|
|
|
async def get_uid(self, user_id: int, args: List[str], reply: Optional[Message]) -> int:
|
|
|
|
|
"""通过消息获取 uid,优先级:args > reply > self"""
|
|
|
|
|
uid, user_id_ = None, user_id
|
|
|
|
|
if args:
|
|
|
|
|
for i in args:
|
|
|
|
|
if i is not None:
|
|
|
|
|
if i.isdigit() and len(i) == 9:
|
|
|
|
|
uid = int(i)
|
|
|
|
|
if reply:
|
|
|
|
|
try:
|
|
|
|
|
user_id_ = reply.from_user.id
|
|
|
|
|
except AttributeError:
|
|
|
|
|
pass
|
|
|
|
|
if not uid:
|
|
|
|
|
player_info = await self.helper.players_service.get_player(user_id_)
|
|
|
|
|
if player_info is not None:
|
|
|
|
|
uid = player_info.player_id
|
|
|
|
|
if (not uid) and (user_id_ != user_id):
|
|
|
|
|
player_info = await self.helper.players_service.get_player(user_id)
|
|
|
|
|
if player_info is not None:
|
|
|
|
|
uid = player_info.player_id
|
|
|
|
|
return uid
|
|
|
|
|
|
2023-04-28 17:50:27 +00:00
|
|
|
|
@handler.command("stats", block=False)
|
|
|
|
|
@handler.message(filters.Regex("^玩家统计查询(.*)"), block=False)
|
|
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> Optional[int]:
|
|
|
|
|
user = update.effective_user
|
|
|
|
|
message = update.effective_message
|
|
|
|
|
logger.info("用户 %s[%s] 查询游戏用户命令请求", user.full_name, user.id)
|
|
|
|
|
try:
|
2023-06-08 15:26:46 +00:00
|
|
|
|
uid: int = await self.get_uid(user.id, context.args, message.reply_to_message)
|
2023-10-19 10:58:51 +00:00
|
|
|
|
async with self.helper.genshin_or_public(user.id, uid=uid) as client:
|
|
|
|
|
render_result = await self.render(client, uid)
|
2023-04-28 17:50:27 +00:00
|
|
|
|
except TooManyRequestPublicCookies:
|
|
|
|
|
await message.reply_text("用户查询次数过多 请稍后重试")
|
|
|
|
|
return
|
|
|
|
|
except AttributeError as exc:
|
|
|
|
|
logger.error("角色数据有误")
|
|
|
|
|
logger.exception(exc)
|
|
|
|
|
await message.reply_text("角色数据有误 估计是彦卿晕了")
|
|
|
|
|
return
|
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2023-07-19 05:52:30 +00:00
|
|
|
|
await render_result.reply_photo(message, filename=f"{user.id}.png", allow_sending_without_reply=True)
|
2023-04-28 17:50:27 +00:00
|
|
|
|
|
2023-07-19 05:52:30 +00:00
|
|
|
|
async def render(self, client: "StarRailClient", uid: Optional[int] = None) -> RenderResult:
|
2023-04-28 17:50:27 +00:00
|
|
|
|
if uid is None:
|
2023-07-19 05:52:30 +00:00
|
|
|
|
uid = client.player_id
|
2023-04-28 17:50:27 +00:00
|
|
|
|
|
|
|
|
|
user_info = await client.get_starrail_user(uid)
|
2023-06-07 13:32:23 +00:00
|
|
|
|
try:
|
|
|
|
|
rogue = await client.get_starrail_rogue(uid)
|
2023-07-19 05:52:30 +00:00
|
|
|
|
except SimnetBadRequest:
|
2023-06-07 13:32:23 +00:00
|
|
|
|
rogue = None
|
2023-04-28 17:50:27 +00:00
|
|
|
|
logger.debug(user_info)
|
|
|
|
|
|
|
|
|
|
# 因为需要替换线上图片地址为本地地址,先克隆数据,避免修改原数据
|
|
|
|
|
user_info = user_info.copy(deep=True)
|
|
|
|
|
|
|
|
|
|
data = {
|
2023-08-26 13:21:26 +00:00
|
|
|
|
"uid": mask_number(uid),
|
2023-06-07 13:32:23 +00:00
|
|
|
|
"info": user_info.info,
|
2023-04-28 17:50:27 +00:00
|
|
|
|
"stats": user_info.stats,
|
|
|
|
|
"stats_labels": [
|
|
|
|
|
("活跃天数", "active_days"),
|
|
|
|
|
("成就达成数", "achievement_num"),
|
|
|
|
|
("获取角色数", "avatar_num"),
|
|
|
|
|
("忘却之庭", "abyss_process"),
|
|
|
|
|
("战利品开启数", "chest_num"),
|
|
|
|
|
],
|
2023-06-07 13:32:23 +00:00
|
|
|
|
"rogue": rogue.basic_info if rogue else None,
|
|
|
|
|
"rogue_labels": [
|
|
|
|
|
("技能树已激活", "unlocked_skill_points"),
|
|
|
|
|
("已解锁奇物", "unlocked_miracle_num"),
|
|
|
|
|
("已解锁祝福", "unlocked_buff_num"),
|
|
|
|
|
],
|
2023-04-28 17:50:27 +00:00
|
|
|
|
"style": "xianzhou", # nosec
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return await self.template_service.render(
|
|
|
|
|
"starrail/stats/stats.html",
|
|
|
|
|
data,
|
|
|
|
|
{"width": 650, "height": 440},
|
|
|
|
|
full_page=True,
|
|
|
|
|
)
|