Files
NewWPSBot/Plugins/WPSCombatSystem/combat_plugin_equipment.py
2025-11-10 14:59:07 +08:00

160 lines
5.2 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""装备管理插件 - 装备和卸下物品"""
from __future__ import annotations
from typing import Optional
from PWF.Convention.Runtime.GlobalConfig import ConsoleFrontColor, ProjectConfig
from .combat_plugin_base import WPSCombatBase
logger: ProjectConfig = ProjectConfig()
class WPSCombatEquipment(WPSCombatBase):
"""装备管理插件"""
def is_enable_plugin(self) -> bool:
return True
def wake_up(self) -> None:
super().wake_up()
logger.Log(
"Info",
f"{ConsoleFrontColor.GREEN}WPSCombatEquipment 插件已加载{ConsoleFrontColor.RESET}"
)
self.register_plugin("装备")
self.register_plugin("equip")
self.register_plugin("卸下")
self.register_plugin("unequip")
async def callback(self, message: str, chat_id: int, user_id: int) -> Optional[str]:
"""
处理装备相关命令
命令格式:
- 装备 <物品名>
- 卸下 <槽位>
"""
message = self.parse_message_after_at(message).strip()
if not message:
return await self.send_markdown_message(
self._help_message(),
chat_id,
user_id
)
tokens = message.split(maxsplit=1)
if not tokens:
return await self.send_markdown_message(
self._help_message(),
chat_id,
user_id
)
# 判断是装备还是卸下
# 注意由于命令已经通过register_plugin匹配这里tokens[0]可能为空
# 实际命令词已经被消费了
# 如果消息为空,说明只输入了命令词
if not tokens or len(tokens) == 0:
return await self.send_markdown_message(
self._help_message(),
chat_id,
user_id
)
# tokens[0] 应该是物品名或槽位
target = tokens[0] if len(tokens) > 0 else ""
# 通过检查self被哪个命令触发来判断操作
# 但这里我们无法直接知道,所以通过参数判断
# 如果是槽位名称weapon/helmet等则是卸下操作
# 否则尝试装备
slot_names = ["weapon", "helmet", "armor", "boots", "accessory",
"武器", "头盔", "护甲", "鞋子", "饰品"]
# 简化:检查是否包含"卸下"或"unequip"
# 由于命令注册了这些词,我们可以假设如果达到这里,就是对应的操作
# 实际上,由于我们注册了"装备"和"卸下"两个命令,
# message已经去掉了命令词只剩参数
# 所以我们需要重新解析原始消息来判断
# 更简单的方法检查target是否是有效槽位
slot_map = {
"weapon": "weapon", "武器": "weapon",
"helmet": "helmet", "头盔": "helmet",
"armor": "armor", "护甲": "armor",
"boots": "boots", "鞋子": "boots",
"accessory": "accessory", "饰品": "accessory",
}
if target.lower() in slot_map:
# 卸下操作
return await self._handle_unequip(chat_id, user_id, slot_map[target.lower()])
else:
# 装备操作
return await self._handle_equip(chat_id, user_id, target)
async def _handle_equip(self, chat_id: int, user_id: int, item_name: str) -> Optional[str]:
"""处理装备命令"""
if not item_name:
return await self.send_markdown_message(
"❌ 请指定要装备的物品名称\n用法:`装备 <物品名>`",
chat_id,
user_id
)
service = self.service()
# 尝试通过名称查找装备item_id
from .combat_models import EQUIPMENT_REGISTRY
item_id = None
for eq_id, eq_def in EQUIPMENT_REGISTRY.items():
if eq_def.name.lower() == item_name.lower() or eq_id.lower() == item_name.lower():
item_id = eq_id
break
if not item_id:
return await self.send_markdown_message(
f"❌ 未找到装备:{item_name}",
chat_id,
user_id
)
success, msg = service.equip_item(user_id, item_id)
return await self.send_markdown_message(msg, chat_id, user_id)
async def _handle_unequip(self, chat_id: int, user_id: int, slot: str) -> Optional[str]:
"""处理卸下命令"""
service = self.service()
success, msg = service.unequip_item(user_id, slot)
return await self.send_markdown_message(msg, chat_id, user_id)
def _help_message(self) -> str:
"""帮助信息"""
return """# ⚔️ 装备管理
**命令格式:**
- `装备 <物品名>`:装备指定物品
- `卸下 <槽位>`:卸下指定槽位的装备
**槽位名称:**
- weapon/武器
- helmet/头盔
- armor/护甲
- boots/鞋子
- accessory/饰品
**示例:**
- `装备 木剑`
- `卸下 weapon`
"""
__all__ = ["WPSCombatEquipment"]