"""Planting plugin for garden system.""" from __future__ import annotations from typing import Optional from PWF.Convention.Runtime.Architecture import Architecture from Plugins.WPSBackpackSystem import WPSBackpackSystem from .garden_plugin_base import WPSGardenBase class WPSGardenPlant(WPSGardenBase): def wake_up(self) -> None: super().wake_up() self.register_plugin("plant") self.register_plugin("种植") 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 not tokens: return await self.send_markdown_message("❌ 指令格式:`种植 <种子> [地块序号]`", chat_id, user_id) plot_index: Optional[int] = None if len(tokens) >= 2 and tokens[-1].isdigit(): plot_index = int(tokens[-1]) identifier = " ".join(tokens[:-1]) else: identifier = " ".join(tokens) crop = self.resolve_seed_id(identifier) if crop is None: return await self.send_markdown_message("❌ 未找到对应种子", chat_id, user_id) backpack: WPSBackpackSystem = Architecture.Get(WPSBackpackSystem) owned = 0 for item in backpack.get_user_items(user_id): if item.item_id == crop.seed_id: owned = item.quantity break if owned <= 0: return await self.send_markdown_message("❌ 背包中没有该种子", chat_id, user_id) try: assigned_plot, mature_at_iso = self.service().plant( user_id=user_id, chat_id=chat_id, seed_id=crop.seed_id, plot_index=plot_index, register_callback=(self, "_clock_mark_mature"), ) except ValueError as exc: return await self.send_markdown_message(f"❌ {exc}", chat_id, user_id) backpack.set_item_quantity(user_id, crop.seed_id, owned - 1) maturity_label = self.service().format_display_time(mature_at_iso) message_body = ( "# 🌱 种植成功\n" f"- 地块:{assigned_plot}\n" f"- 作物:{crop.display_name}\n" f"- 预计成熟:{maturity_label}" ) return await self.send_markdown_message(message_body, chat_id, user_id) __all__ = ["WPSGardenPlant"]