Files
NewWPSBot/Plugins/WPSGardenSystem/garden_plugin_harvest.py
2025-11-10 01:15:17 +08:00

79 lines
3.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""Harvest 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 Plugins.WPSConfigSystem import WPSConfigAPI
from Plugins.WPSFortuneSystem import WPSFortuneSystem
from .garden_plugin_base import WPSGardenBase
class WPSGardenHarvest(WPSGardenBase):
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"]