"""Harvest 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 Plugins.WPSConfigSystem import WPSConfigAPI from Plugins.WPSFortuneSystem import WPSFortuneSystem from .garden_plugin_base import WPSGardenBase class WPSGardenHarvest(WPSGardenBase): def get_guide_subtitle(self) -> str: return "收获成熟作物并处理额外奖励" def collect_command_entries(self) -> Sequence[GuideEntry]: return ( GuideEntry( title="收获", identifier="收获 <地块序号>", description="从成熟地块采摘果实并发放额外奖励。", metadata={"别名": "harvest"}, icon="🧺", details=[ { "type": "steps", "items": [ "输入正整数地块序号。", "系统校验成熟状态,计算基础果实数量。", "发放额外奖励:积分或额外物品会自动结算。", ], } ], ), ) def collect_guide_entries(self) -> Sequence[GuideEntry]: return ( { "title": "指令格式", "description": "`收获 <地块序号>`,序号需为正整数。", }, { "title": "收益构成", "description": "基础果实直接入背包,额外奖励可能为积分或额外物品。", }, ) def wake_up(self) -> None: super().wake_up() self.register_plugin("harvest") 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 or not tokens[0].isdigit(): return await self.send_markdown_message("❌ 指令格式:`收获 <地块序号>`", chat_id, user_id) plot_index = int(tokens[0]) fortune: WPSFortuneSystem = Architecture.Get(WPSFortuneSystem) fortune_value = fortune.get_fortune_value(user_id) try: result = self.service().harvest( user_id=user_id, plot_index=plot_index, fortune_value=fortune_value, ) except ValueError as exc: return await self.send_markdown_message(f"❌ {exc}", chat_id, user_id) crop = result["crop"] base_qty = result["base_yield"] backpack: WPSBackpackSystem = Architecture.Get(WPSBackpackSystem) backpack.add_item(user_id, crop.fruit_id, base_qty) config_api: WPSConfigAPI = Architecture.Get(WPSConfigAPI) extra_lines = [] if result["extra"]: extra = result["extra"] if extra["type"] == "points": gained = int(extra["amount"]) if gained > 0: new_points = await config_api.adjust_user_points( chat_id, user_id, gained, reason=f"收获 {crop.display_name} 的额外积分", ) extra_lines.append(f"- 额外积分:+{gained}(当前积分 {new_points})") elif extra["type"] == "item": item_id = extra["item_id"] qty = int(extra["quantity"]) if qty > 0: backpack.add_item(user_id, item_id, qty) extra_lines.append(f"- 额外物品:{item_id} × {qty}") message_lines = [ "# ✅ 收获成功", f"- 地块:{plot_index}", f"- 作物:{crop.display_name}", f"- 基础果实:{crop.display_name}的果实 × {base_qty}", ] message_lines.extend(extra_lines) return await self.send_markdown_message("\n".join(message_lines), chat_id, user_id) __all__ = ["WPSGardenHarvest"]