"""Place trap plugin for garden system.""" from __future__ import annotations from typing import Optional, Sequence from PWF.Convention.Runtime.Architecture import Architecture from Plugins.WPSAPI import GuideEntry from Plugins.WPSBackpackSystem import WPSBackpackSystem from .garden_plugin_base import WPSGardenBase from .garden_models import GARDEN_TRAPS_DICT class WPSGardenPlaceTrap(WPSGardenBase): def get_guide_subtitle(self) -> str: return "在地块上放置防护陷阱" def collect_command_entries(self) -> Sequence[GuideEntry]: return ( GuideEntry( title="放置陷阱", identifier="放置陷阱 <地块序号> <陷阱物品>", description="在地块上放置防护陷阱,当偷盗者触发时会受到惩罚。", metadata={"别名": "place_trap"}, icon="🪤", details=[ { "type": "steps", "items": [ "输入地块序号和陷阱物品名称或ID。", "系统检查地块是否存在且背包中是否有该陷阱。", "成功放置后陷阱会在下次偷盗时生效。", ], }, "陷阱有不同的触发概率、罚金和禁止时长,等级越高效果越强。", ], ), ) def collect_guide_entries(self) -> Sequence[GuideEntry]: return ( { "title": "指令格式", "description": "`放置陷阱 <地块序号> <陷阱物品>`。", }, { "title": "陷阱效果", "description": "不同等级的陷阱具有不同的触发概率、罚金和禁止时长。", }, ) def wake_up(self) -> None: super().wake_up() self.register_plugin("放置陷阱") self.register_plugin("place_trap") def _resolve_trap_id(self, keyword: str) -> Optional[str]: """解析陷阱物品ID""" key = keyword.strip().lower() for trap_item_id, trap in GARDEN_TRAPS_DICT.items(): if trap_item_id.lower() == key: return trap_item_id if trap.display_name.lower() == key: return trap_item_id if trap.display_name.lower().replace("陷阱", "").replace("守护", "").replace("结界", "").replace("网", "") == key: return trap_item_id return None async def callback(self, message: str, chat_id: int, user_id: int) -> Optional[str]: payload = self.parse_message_after_at(message).strip() if not payload: return await self.send_markdown_message( "❌ 指令格式:`放置陷阱 <地块序号> <陷阱物品>`", chat_id, user_id ) tokens = [token.strip() for token in payload.split() if token.strip()] if len(tokens) < 2: return await self.send_markdown_message( "❌ 指令格式:`放置陷阱 <地块序号> <陷阱物品>`", chat_id, user_id ) if not tokens[0].isdigit(): return await self.send_markdown_message( "❌ 指令格式:`放置陷阱 <地块序号> <陷阱物品>`", chat_id, user_id ) plot_index = int(tokens[0]) trap_identifier = " ".join(tokens[1:]) trap_item_id = self._resolve_trap_id(trap_identifier) if not trap_item_id: return await self.send_markdown_message( "❌ 未找到该陷阱物品,可用陷阱:\n" + "\n".join([f"- {trap.display_name} ({trap_item_id})" for trap_item_id, trap in GARDEN_TRAPS_DICT.items()]), chat_id, user_id ) # 检查背包中是否有该陷阱 backpack: WPSBackpackSystem = Architecture.Get(WPSBackpackSystem) user_items = backpack.get_user_items(user_id) owned_quantity = 0 for item in user_items: if item.item_id == trap_item_id: owned_quantity = item.quantity break if owned_quantity <= 0: trap = GARDEN_TRAPS_DICT[trap_item_id] return await self.send_markdown_message( f"❌ 背包中没有 {trap.display_name},需要先合成获取", chat_id, user_id ) try: self.service().place_trap( user_id=user_id, plot_index=plot_index, trap_item_id=trap_item_id, ) # 消耗陷阱物品 backpack.set_item_quantity(user_id, trap_item_id, owned_quantity - 1) trap = GARDEN_TRAPS_DICT[trap_item_id] return await self.send_markdown_message( f"✅ 已在地块 {plot_index} 上放置 {trap.display_name}\n" f"- 触发概率:{trap.trigger_rate * 100:.0f}%\n" f"- 罚金:{trap.fine_points} 分\n" f"- 禁止时长:{trap.ban_hours} 小时\n" f"- 耐久度:{trap.durability} 次", chat_id, user_id ) except ValueError as exc: return await self.send_markdown_message(f"❌ {exc}", chat_id, user_id) __all__ = ["WPSGardenPlaceTrap"]