Files
NewWPSBot/Plugins/WPSGardenSystem/garden_plugin_plant.py
2025-11-12 22:58:36 +08:00

111 lines
4.1 KiB
Python

"""Planting 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
class WPSGardenPlant(WPSGardenBase):
def get_guide_subtitle(self) -> str:
return "种植作物并分配地块"
def collect_command_entries(self) -> Sequence[GuideEntry]:
return (
GuideEntry(
title="种植",
identifier="种植 <种子> [地块序号]",
description="在指定地块种下一颗种子,默认选取下一个空地。",
metadata={"别名": "plant"},
icon="🌱",
details=[
{
"type": "steps",
"items": [
"确认背包中持有对应种子。",
"可选:指定地块序号(正整数),否则使用下一个空地。",
"系统校验库存并写入菜园数据库,生成成熟计时。",
],
},
"种植成功后立即扣除种子数量并返回成熟时间。",
],
),
)
def collect_guide_entries(self) -> Sequence[GuideEntry]:
return (
{
"title": "指令格式",
"description": "`种植 <种子ID|名称> [地块序号]`,默认选择下一个空地。",
},
{
"title": "库存校验",
"description": "会检查背包种子数量,不足时返回提示而不消耗资源。",
},
)
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"]