2022-09-18 04:19:29 +00:00
|
|
|
from telegram import InlineKeyboardButton, InlineKeyboardMarkup, Update
|
2022-07-30 13:01:11 +00:00
|
|
|
from telegram.constants import ChatAction
|
2022-09-18 04:19:29 +00:00
|
|
|
from telegram.ext import CallbackContext, CommandHandler, MessageHandler, filters
|
2022-07-30 13:01:11 +00:00
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
from core.dependence.assets import AssetsCouldNotFound, AssetsService
|
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.search.models import WeaponEntry
|
|
|
|
from core.services.search.services import SearchServices
|
|
|
|
from core.services.template.services import TemplateService
|
|
|
|
from core.services.wiki.services import WikiService
|
2022-10-07 05:02:49 +00:00
|
|
|
from metadata.genshin import honey_id_to_game_id
|
2022-12-04 11:56:39 +00:00
|
|
|
from metadata.shortname import weaponToName, weapons as _weapons_data
|
2022-09-08 01:08:37 +00:00
|
|
|
from modules.wiki.weapon import Weapon
|
|
|
|
from utils.log import logger
|
2022-07-30 13:01:11 +00:00
|
|
|
|
|
|
|
|
2023-03-14 01:27:22 +00:00
|
|
|
class WeaponPlugin(Plugin):
|
2022-07-30 13:01:11 +00:00
|
|
|
"""武器查询"""
|
|
|
|
|
2022-08-28 14:37:31 +00:00
|
|
|
KEYBOARD = [
|
|
|
|
[InlineKeyboardButton(text="查看武器列表并查询", switch_inline_query_current_chat="查看武器列表并查询")]
|
2024-03-16 01:44:15 +00:00
|
|
|
]
|
2022-07-30 13:01:11 +00:00
|
|
|
|
2022-09-18 04:19:29 +00:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
template_service: TemplateService = None,
|
|
|
|
wiki_service: WikiService = None,
|
2022-10-07 05:02:49 +00:00
|
|
|
assets_service: AssetsService = None,
|
2022-12-04 11:56:39 +00:00
|
|
|
search_service: SearchServices = None,
|
2022-09-18 04:19:29 +00:00
|
|
|
):
|
2022-07-31 04:51:22 +00:00
|
|
|
self.wiki_service = wiki_service
|
|
|
|
self.template_service = template_service
|
2022-10-07 05:02:49 +00:00
|
|
|
self.assets_service = assets_service
|
2022-12-04 11:56:39 +00:00
|
|
|
self.search_service = search_service
|
2022-07-31 04:51:22 +00:00
|
|
|
|
2022-09-08 01:08:37 +00:00
|
|
|
@handler(CommandHandler, command="weapon", block=False)
|
|
|
|
@handler(MessageHandler, filters=filters.Regex("^武器查询(.*)"), block=False)
|
2022-07-31 04:51:22 +00:00
|
|
|
async def command_start(self, update: Update, context: CallbackContext) -> None:
|
2022-09-08 01:08:37 +00:00
|
|
|
message = update.effective_message
|
2023-03-14 01:27:22 +00:00
|
|
|
args = self.get_args(context)
|
2022-07-30 13:01:11 +00:00
|
|
|
if len(args) >= 1:
|
|
|
|
weapon_name = args[0]
|
|
|
|
else:
|
|
|
|
reply_message = await message.reply_text(
|
|
|
|
"请回复你要查询的武器", reply_markup=InlineKeyboardMarkup(self.KEYBOARD)
|
2024-03-16 01:44:15 +00:00
|
|
|
)
|
2022-07-30 13:01:11 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(message)
|
|
|
|
self.add_delete_message_job(reply_message)
|
2022-07-30 13:01:11 +00:00
|
|
|
return
|
|
|
|
weapon_name = weaponToName(weapon_name)
|
2024-03-10 12:40:26 +00:00
|
|
|
self.log_user(update, logger.info, "查询角色攻略命令请求 weapon_name[%s]", weapon_name)
|
2022-07-31 04:51:22 +00:00
|
|
|
weapons_list = await self.wiki_service.get_weapons_list()
|
2022-07-30 13:01:11 +00:00
|
|
|
for weapon in weapons_list:
|
2022-08-28 14:37:31 +00:00
|
|
|
if weapon.name == weapon_name:
|
2022-07-30 13:01:11 +00:00
|
|
|
weapon_data = weapon
|
|
|
|
break
|
|
|
|
else:
|
|
|
|
reply_message = await message.reply_text(
|
|
|
|
f"没有找到 {weapon_name}", reply_markup=InlineKeyboardMarkup(self.KEYBOARD)
|
2022-10-10 11:07:28 +00:00
|
|
|
)
|
2022-07-30 13:01:11 +00:00
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(message)
|
|
|
|
self.add_delete_message_job(reply_message)
|
2022-07-30 13:01:11 +00:00
|
|
|
return
|
|
|
|
await message.reply_chat_action(ChatAction.TYPING)
|
|
|
|
|
2022-08-28 14:37:31 +00:00
|
|
|
async def input_template_data(_weapon_data: Weapon):
|
|
|
|
if weapon.rarity > 2:
|
|
|
|
bonus = _weapon_data.stats[-1].bonus
|
|
|
|
if "%" in bonus:
|
|
|
|
bonus = str(round(float(bonus.rstrip("%")))) + "%"
|
|
|
|
else:
|
|
|
|
bonus = str(round(float(bonus)))
|
|
|
|
_template_data = {
|
|
|
|
"weapon_name": _weapon_data.name,
|
2023-04-07 13:07:59 +00:00
|
|
|
"weapon_rarity": _weapon_data.rarity,
|
2023-06-07 15:33:11 +00:00
|
|
|
"weapon_info_type_img": _weapon_data.weapon_type.name,
|
2022-08-28 14:37:31 +00:00
|
|
|
"progression_secondary_stat_value": bonus,
|
|
|
|
"progression_secondary_stat_name": _weapon_data.attribute.type.value,
|
2022-10-07 05:02:49 +00:00
|
|
|
"weapon_info_source_img": (
|
|
|
|
await self.assets_service.weapon(honey_id_to_game_id(_weapon_data.id, "weapon")).icon()
|
|
|
|
).as_uri(),
|
2022-08-28 14:37:31 +00:00
|
|
|
"weapon_info_max_level": _weapon_data.stats[-1].level,
|
|
|
|
"progression_base_atk": round(_weapon_data.stats[-1].ATK),
|
|
|
|
"weapon_info_source_list": [
|
2022-10-07 05:02:49 +00:00
|
|
|
(await self.assets_service.material(honey_id_to_game_id(mid, "material")).icon()).as_uri()
|
2022-08-28 14:37:31 +00:00
|
|
|
for mid in _weapon_data.ascension[-3:]
|
|
|
|
],
|
|
|
|
"special_ability_name": _weapon_data.affix.name,
|
|
|
|
"special_ability_info": _weapon_data.affix.description[0],
|
2023-04-07 13:07:59 +00:00
|
|
|
"weapon_description": _weapon_data.description,
|
2022-08-28 14:37:31 +00:00
|
|
|
}
|
|
|
|
else:
|
|
|
|
_template_data = {
|
|
|
|
"weapon_name": _weapon_data.name,
|
2023-04-07 13:07:59 +00:00
|
|
|
"weapon_rarity": _weapon_data.rarity,
|
2023-06-07 15:33:11 +00:00
|
|
|
"weapon_info_type_img": _weapon_data.weapon_type.name,
|
2022-08-28 14:37:31 +00:00
|
|
|
"progression_secondary_stat_value": " ",
|
|
|
|
"progression_secondary_stat_name": "无其它属性加成",
|
2022-10-07 05:02:49 +00:00
|
|
|
"weapon_info_source_img": (
|
|
|
|
await self.assets_service.weapon(honey_id_to_game_id(_weapon_data.id, "weapon")).icon()
|
|
|
|
).as_uri(),
|
2022-08-28 14:37:31 +00:00
|
|
|
"weapon_info_max_level": _weapon_data.stats[-1].level,
|
|
|
|
"progression_base_atk": round(_weapon_data.stats[-1].ATK),
|
|
|
|
"weapon_info_source_list": [
|
2022-10-07 05:02:49 +00:00
|
|
|
(await self.assets_service.material(honey_id_to_game_id(mid, "material")).icon()).as_uri()
|
2022-08-28 14:37:31 +00:00
|
|
|
for mid in _weapon_data.ascension[-3:]
|
|
|
|
],
|
|
|
|
"special_ability_name": "",
|
2023-04-07 13:07:59 +00:00
|
|
|
"special_ability_info": "",
|
|
|
|
"weapon_description": _weapon_data.description,
|
2022-08-28 14:37:31 +00:00
|
|
|
}
|
2022-07-30 13:01:11 +00:00
|
|
|
return _template_data
|
|
|
|
|
2022-11-19 08:08:35 +00:00
|
|
|
try:
|
|
|
|
template_data = await input_template_data(weapon_data)
|
|
|
|
except AssetsCouldNotFound as exc:
|
|
|
|
logger.warning("%s weapon_name[%s]", exc.message, weapon_name)
|
|
|
|
reply_message = await message.reply_text(f"数据库中没有找到 {weapon_name}")
|
|
|
|
if filters.ChatType.GROUPS.filter(reply_message):
|
2023-03-14 01:27:22 +00:00
|
|
|
self.add_delete_message_job(message)
|
|
|
|
self.add_delete_message_job(reply_message)
|
2022-11-19 08:08:35 +00:00
|
|
|
return
|
2022-07-31 04:51:22 +00:00
|
|
|
png_data = await self.template_service.render(
|
2023-05-09 11:01:45 +00:00
|
|
|
"genshin/weapon/weapon.jinja2", template_data, {"width": 540, "height": 540}, ttl=31 * 24 * 60 * 60
|
2022-07-30 13:01:11 +00:00
|
|
|
)
|
|
|
|
await message.reply_chat_action(ChatAction.UPLOAD_PHOTO)
|
2022-12-04 11:56:39 +00:00
|
|
|
reply_photo = await png_data.reply_photo(
|
2022-10-22 07:03:59 +00:00
|
|
|
message,
|
|
|
|
filename=f"{template_data['weapon_name']}.png",
|
|
|
|
allow_sending_without_reply=True,
|
2022-07-30 13:01:11 +00:00
|
|
|
)
|
2022-12-04 11:56:39 +00:00
|
|
|
if reply_photo.photo:
|
2023-02-22 06:18:45 +00:00
|
|
|
description = weapon_data.story
|
|
|
|
if description:
|
|
|
|
photo_file_id = reply_photo.photo[0].file_id
|
|
|
|
tags = _weapons_data.get(weapon_name)
|
|
|
|
entry = WeaponEntry(
|
|
|
|
key=f"plugin:weapon:{weapon_name}",
|
|
|
|
title=weapon_name,
|
|
|
|
description=description,
|
|
|
|
tags=tags,
|
|
|
|
photo_file_id=photo_file_id,
|
|
|
|
)
|
|
|
|
await self.search_service.add_entry(entry)
|