修复装备系统的bug
This commit is contained in:
@@ -13,24 +13,14 @@ from .combat_plugin_base import WPSCombatBase
|
||||
logger: ProjectConfig = ProjectConfig()
|
||||
|
||||
|
||||
class WPSCombatEquipment(WPSCombatBase):
|
||||
"""装备管理插件"""
|
||||
class WPSCombatUnequipment(WPSCombatBase):
|
||||
"""卸下战斗装备插件"""
|
||||
|
||||
def get_guide_subtitle(self) -> str:
|
||||
return "穿戴与卸下战斗装备"
|
||||
return "卸下战斗装备"
|
||||
|
||||
def collect_command_entries(self) -> Sequence[GuideEntry]:
|
||||
return (
|
||||
GuideEntry(
|
||||
title="装备",
|
||||
identifier="装备 [物品名|ID]",
|
||||
description="穿戴指定装备,或不带参数时查看当前装备。",
|
||||
metadata={"别名": "equip"},
|
||||
icon="🛡️",
|
||||
details=[
|
||||
{"type": "list", "items": ["名称与 ID 均可匹配装备库条目。", "若未携带该装备会提示不足。"]},
|
||||
],
|
||||
),
|
||||
GuideEntry(
|
||||
title="卸下",
|
||||
identifier="卸下 <槽位>",
|
||||
@@ -59,10 +49,8 @@ class WPSCombatEquipment(WPSCombatBase):
|
||||
super().wake_up()
|
||||
logger.Log(
|
||||
"Info",
|
||||
f"{ConsoleFrontColor.GREEN}WPSCombatEquipment 插件已加载{ConsoleFrontColor.RESET}"
|
||||
f"{ConsoleFrontColor.GREEN}WPSCombatUnequipment 插件已加载{ConsoleFrontColor.RESET}"
|
||||
)
|
||||
self.register_plugin("装备")
|
||||
self.register_plugin("equip")
|
||||
self.register_plugin("卸下")
|
||||
self.register_plugin("unequip")
|
||||
|
||||
@@ -83,10 +71,6 @@ class WPSCombatEquipment(WPSCombatBase):
|
||||
if not tokens:
|
||||
return await self._send_equipment_overview(chat_id, user_id)
|
||||
|
||||
# 判断是装备还是卸下
|
||||
# 注意:由于命令已经通过register_plugin匹配,这里tokens[0]可能为空
|
||||
# 实际命令词已经被消费了
|
||||
|
||||
# 如果消息为空,说明只输入了命令词
|
||||
if not tokens or len(tokens) == 0:
|
||||
return await self.send_markdown_message(
|
||||
@@ -107,19 +91,6 @@ class WPSCombatEquipment(WPSCombatBase):
|
||||
user_id
|
||||
)
|
||||
|
||||
# 通过检查self被哪个命令触发来判断操作
|
||||
# 但这里我们无法直接知道,所以通过参数判断
|
||||
# 如果是槽位名称(weapon/helmet等),则是卸下操作
|
||||
# 否则尝试装备
|
||||
|
||||
# 简化:检查是否包含"卸下"或"unequip"
|
||||
# 由于命令注册了这些词,我们可以假设如果达到这里,就是对应的操作
|
||||
|
||||
# 实际上,由于我们注册了"装备"和"卸下"两个命令,
|
||||
# message已经去掉了命令词,只剩参数
|
||||
# 所以我们需要重新解析原始消息来判断
|
||||
|
||||
# 更简单的方法:检查target是否是有效槽位
|
||||
slot_map = {
|
||||
"weapon": "weapon", "武器": "weapon",
|
||||
"helmet": "helmet", "头盔": "helmet",
|
||||
@@ -132,39 +103,12 @@ class WPSCombatEquipment(WPSCombatBase):
|
||||
# 卸下操作
|
||||
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用法:`装备 <物品名>`",
|
||||
self._help_message(),
|
||||
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()
|
||||
@@ -226,5 +170,174 @@ class WPSCombatEquipment(WPSCombatBase):
|
||||
|
||||
return await self.send_markdown_message("\n".join(lines), chat_id, user_id)
|
||||
|
||||
class WPSCombatEquipment(WPSCombatBase):
|
||||
"""装备管理插件"""
|
||||
|
||||
__all__ = ["WPSCombatEquipment"]
|
||||
def get_guide_subtitle(self) -> str:
|
||||
return "穿戴战斗装备"
|
||||
|
||||
def collect_command_entries(self) -> Sequence[GuideEntry]:
|
||||
return (
|
||||
GuideEntry(
|
||||
title="装备",
|
||||
identifier="装备 [物品名|ID]",
|
||||
description="穿戴指定装备,或不带参数时查看当前装备。",
|
||||
metadata={"别名": "equip"},
|
||||
icon="🛡️",
|
||||
details=[
|
||||
{"type": "list", "items": ["名称与 ID 均可匹配装备库条目。", "若未携带该装备会提示不足。"]},
|
||||
],
|
||||
),
|
||||
)
|
||||
|
||||
def collect_guide_entries(self) -> Sequence[GuideEntry]:
|
||||
return (
|
||||
{
|
||||
"title": "槽位说明",
|
||||
"description": "支持 weapon/helmet/armor/boots/accessory 五个槽位。",
|
||||
},
|
||||
{
|
||||
"title": "属性加成",
|
||||
"description": "装备属性直接影响战斗与冒险计算,可在 `战斗属性` 指令中查看综合加成。",
|
||||
},
|
||||
)
|
||||
|
||||
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")
|
||||
|
||||
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_equipment_overview(chat_id, user_id)
|
||||
|
||||
tokens = message.split(maxsplit=1)
|
||||
if not tokens:
|
||||
return await self._send_equipment_overview(chat_id, user_id)
|
||||
|
||||
# 如果消息为空,说明只输入了命令词
|
||||
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 ""
|
||||
if not target:
|
||||
return await self._send_equipment_overview(chat_id, user_id)
|
||||
|
||||
if target.lower() in ["帮助", "help"]:
|
||||
return await self.send_markdown_message(
|
||||
self._help_message(),
|
||||
chat_id,
|
||||
user_id
|
||||
)
|
||||
|
||||
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)
|
||||
|
||||
def _help_message(self) -> str:
|
||||
"""帮助信息"""
|
||||
return """# ⚔️ 装备管理
|
||||
**命令格式:**
|
||||
- `装备`:查看当前装备
|
||||
- `装备 <物品名>`:装备指定物品
|
||||
- `卸下 <槽位>`:卸下指定槽位的装备
|
||||
|
||||
**槽位名称:**
|
||||
- weapon/武器
|
||||
- helmet/头盔
|
||||
- armor/护甲
|
||||
- boots/鞋子
|
||||
- accessory/饰品
|
||||
|
||||
**示例:**
|
||||
- `装备 木剑`
|
||||
- `卸下 weapon`
|
||||
"""
|
||||
|
||||
|
||||
async def _send_equipment_overview(self, chat_id: int, user_id: int) -> Optional[str]:
|
||||
"""展示当前装备概览"""
|
||||
service = self.service()
|
||||
equipped = service.get_equipped_items(user_id)
|
||||
stats = service.calculate_player_stats(user_id)
|
||||
|
||||
slot_order = [
|
||||
("weapon", "武器"),
|
||||
("helmet", "头盔"),
|
||||
("armor", "护甲"),
|
||||
("boots", "鞋子"),
|
||||
("accessory", "饰品"),
|
||||
]
|
||||
|
||||
lines = [
|
||||
"# 🛡️ 当前装备情况",
|
||||
f"- 装备强度:`{stats.equipment_strength:.1f}`",
|
||||
"",
|
||||
"**装备栏:**",
|
||||
]
|
||||
|
||||
for slot_key, slot_name in slot_order:
|
||||
eq_def = equipped.get(slot_key) if equipped else None
|
||||
if eq_def:
|
||||
attrs = ", ".join([f"{attr}+{value}" for attr, value in eq_def.attributes.items()]) or "无属性加成"
|
||||
tier_label = eq_def.tier.to_markdown_label(eq_def.tier.display_name)
|
||||
lines.append(f"- {slot_name}:{tier_label} {eq_def.name}({attrs})")
|
||||
else:
|
||||
lines.append(f"- {slot_name}:`未装备`")
|
||||
|
||||
lines.append("\n> 使用 `装备 <物品名>` 可以直接穿戴,`卸下 <槽位>` 可卸下装备")
|
||||
|
||||
return await self.send_markdown_message("\n".join(lines), chat_id, user_id)
|
||||
|
||||
|
||||
|
||||
__all__ = ["WPSCombatEquipment", "WPSCombatUnequipment"]
|
||||
|
||||
Reference in New Issue
Block a user