diff --git a/.tasks/2025-11-15_2_trap-system.md b/.tasks/2025-11-15_2_trap-system.md index 933faac..60dc843 100644 --- a/.tasks/2025-11-15_2_trap-system.md +++ b/.tasks/2025-11-15_2_trap-system.md @@ -64,5 +64,14 @@ Yolo模式: Off - 阻碍因素:无 - 状态:未确认 +- 2025-11-15_17:11:26 + - 已修改: + - Plugins/WPSGardenSystem/garden_plugin_base.py: 修改 format_garden_overview 方法,添加 show_trap 参数控制是否显示陷阱信息 + - Plugins/WPSGardenSystem/garden_plugin_view.py: 添加 _resolve_target_user 和 _handle_view 方法,修改 callback 支持 "查看"/"view" 子指令,更新 collect_command_entries 和 collect_guide_entries + - 更改:实现了查看其他玩家菜园的功能,支持通过用户名或用户ID查看,且不显示陷阱信息以保护隐私 + - 原因:满足用户需求,允许查看任意玩家的菜园情况 + - 阻碍因素:无 + - 状态:未确认 + # 最终审查 diff --git a/Plugins/WPSGardenSystem/garden_plugin_base.py b/Plugins/WPSGardenSystem/garden_plugin_base.py index a007350..660ad96 100644 --- a/Plugins/WPSGardenSystem/garden_plugin_base.py +++ b/Plugins/WPSGardenSystem/garden_plugin_base.py @@ -508,7 +508,7 @@ class WPSGardenBase(WPSAPI): return crop return None - def format_garden_overview(self, user_id: int) -> str: + def format_garden_overview(self, user_id: int, show_trap: bool = True) -> str: service = self.service() plots = service.list_plots(user_id) config = service.config @@ -527,7 +527,7 @@ class WPSGardenBase(WPSAPI): remaining = plot["remaining_fruit"] theft_users = len(json.loads(plot["theft_users"])) if plot.get("theft_users") else 0 trap_info = "" - if plot.get("trap_item_id"): + if show_trap and plot.get("trap_item_id"): trap_durability = int(plot.get("trap_durability", 0)) from .garden_models import GARDEN_TRAPS_DICT trap = GARDEN_TRAPS_DICT.get(plot["trap_item_id"]) diff --git a/Plugins/WPSGardenSystem/garden_plugin_view.py b/Plugins/WPSGardenSystem/garden_plugin_view.py index b78eab1..e8c7e6a 100644 --- a/Plugins/WPSGardenSystem/garden_plugin_view.py +++ b/Plugins/WPSGardenSystem/garden_plugin_view.py @@ -35,6 +35,23 @@ class WPSGardenView(WPSGardenBase): } ], ), + GuideEntry( + title="菜园 查看", + identifier="菜园 查看 <用户名|用户ID>", + description="查看任意玩家的菜园情况(不显示陷阱信息)。", + metadata={"别名": "garden view"}, + icon="🔍", + details=[ + { + "type": "steps", + "items": [ + "输入目标用户名或用户ID。", + "系统显示该玩家的菜园状态和成熟作物信息。", + "无法看到陷阱信息,保护隐私。", + ], + } + ], + ), GuideEntry( title="菜园 售出", identifier="菜园 售出 <果实> <数量>", @@ -60,6 +77,10 @@ class WPSGardenView(WPSGardenBase): "title": "概览视图", "description": "默认输出地块编号、成熟状态、剩余果实与被偷记录。", }, + { + "title": "查看他人菜园", + "description": "`菜园 查看 <用户名|用户ID>`,查看任意玩家的菜园情况,不显示陷阱信息。", + }, { "title": "果实售出", "description": "`菜园 售出 <果实> <数量>`,自动结算积分并扣除背包库存。", @@ -77,6 +98,8 @@ class WPSGardenView(WPSGardenBase): return await self._send_overview(chat_id, user_id) tokens = [token.strip() for token in payload.split() if token.strip()] + if tokens and tokens[0] in {"查看", "view"}: + return await self._handle_view(tokens[1:], chat_id, user_id) if tokens and tokens[0] in {"售出", "sell"}: return await self._handle_sell(tokens[1:], chat_id, user_id) @@ -86,6 +109,74 @@ class WPSGardenView(WPSGardenBase): overview = self.format_garden_overview(user_id) return await self.send_markdown_message(overview, chat_id, user_id) + def _resolve_target_user(self, identifier: str) -> Optional[int]: + """解析目标用户名或用户ID + + Args: + identifier: 用户名或用户ID字符串 + + Returns: + 用户ID,如果解析失败则返回None + """ + text = identifier.strip() + if not text: + return None + + # 尝试通过用户名查找 + try: + config_api: WPSConfigAPI = Architecture.Get(WPSConfigAPI) + resolved_id = config_api.find_user_id_by_username(text) + if resolved_id is not None: + return resolved_id + except Exception: + pass + + # 尝试按用户ID解析 + if text.isdigit(): + return int(text) + + return None + + async def _handle_view(self, args: list[str], chat_id: int, user_id: int) -> Optional[str]: + """处理查看其他用户菜园的指令 + + Args: + args: 参数列表,应包含目标用户名或用户ID + chat_id: 会话ID + user_id: 当前用户ID + + Returns: + 消息或None + """ + if not args: + return await self.send_markdown_message( + "❌ 指令格式:`菜园 查看 <用户名|用户ID>`", + chat_id, + user_id, + ) + + target_identifier = " ".join(args) + target_user_id = self._resolve_target_user(target_identifier) + + if target_user_id is None: + return await self.send_markdown_message( + "❌ 未找到目标用户,请检查用户名或用户ID是否正确", + chat_id, + user_id, + ) + + # 获取目标用户名 + config_api: WPSConfigAPI = Architecture.Get(WPSConfigAPI) + target_username = config_api.get_user_name(target_user_id) + + # 格式化显示目标用户的菜园(不显示陷阱) + overview = self.format_garden_overview(target_user_id, show_trap=False) + + # 替换标题,显示目标用户信息 + overview_with_header = overview.replace("# 🌱 菜园概览", f"# 🌱 {target_username} 的菜园") + + return await self.send_markdown_message(overview_with_header, chat_id, user_id) + async def _handle_sell(self, args: list[str], chat_id: int, user_id: int) -> Optional[str]: if len(args) < 2: return await self.send_markdown_message(