182 lines
7.3 KiB
Python
182 lines
7.3 KiB
Python
"""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()]
|
||
|
||
# 解析多个地块序号
|
||
plot_indices = []
|
||
for token in tokens:
|
||
if token.isdigit():
|
||
plot_index = int(token)
|
||
if plot_index > 0: # 确保是正整数
|
||
plot_indices.append(plot_index)
|
||
|
||
# 去重
|
||
plot_indices = list(set(plot_indices))
|
||
|
||
if not plot_indices:
|
||
return await self.send_markdown_message("❌ 指令格式:`收获 <地块序号> [地块序号...]`", chat_id, user_id)
|
||
|
||
fortune: WPSFortuneSystem = Architecture.Get(WPSFortuneSystem)
|
||
fortune_value = fortune.get_fortune_value(user_id)
|
||
backpack: WPSBackpackSystem = Architecture.Get(WPSBackpackSystem)
|
||
config_api: WPSConfigAPI = Architecture.Get(WPSConfigAPI)
|
||
|
||
# 收集结果
|
||
successful_harvests = []
|
||
failed_harvests = []
|
||
total_points_gained = 0
|
||
extra_items = {} # {item_id: quantity}
|
||
fruit_counts = {} # {fruit_id: quantity}
|
||
|
||
# 循环处理每个地块
|
||
for plot_index in plot_indices:
|
||
try:
|
||
result = self.service().harvest(
|
||
user_id=user_id,
|
||
plot_index=plot_index,
|
||
fortune_value=fortune_value,
|
||
)
|
||
crop = result["crop"]
|
||
base_qty = result["base_yield"]
|
||
|
||
# 添加到背包
|
||
backpack.add_item(user_id, crop.fruit_id, base_qty)
|
||
|
||
# 统计果实
|
||
fruit_counts[crop.fruit_id] = fruit_counts.get(crop.fruit_id, 0) + base_qty
|
||
|
||
# 处理额外奖励
|
||
if result["extra"]:
|
||
extra = result["extra"]
|
||
if extra["type"] == "points":
|
||
gained = int(extra["amount"])
|
||
if gained > 0:
|
||
total_points_gained += gained
|
||
elif extra["type"] == "item":
|
||
item_id = extra["item_id"]
|
||
qty = int(extra["quantity"])
|
||
if qty > 0:
|
||
extra_items[item_id] = extra_items.get(item_id, 0) + qty
|
||
backpack.add_item(user_id, item_id, qty)
|
||
|
||
successful_harvests.append((plot_index, crop, result))
|
||
except ValueError as exc:
|
||
failed_harvests.append((plot_index, str(exc)))
|
||
|
||
# 统一处理积分奖励
|
||
new_points = None
|
||
if total_points_gained > 0:
|
||
new_points = await config_api.adjust_user_points(
|
||
chat_id,
|
||
user_id,
|
||
total_points_gained,
|
||
reason=f"收获 {len(successful_harvests)} 个地块的额外积分",
|
||
)
|
||
|
||
# 生成汇总消息
|
||
message_lines = []
|
||
|
||
if successful_harvests:
|
||
message_lines.append("# ✅ 收获成功")
|
||
message_lines.append(f"- 成功收获:{len(successful_harvests)} 个地块")
|
||
|
||
# 列出成功的地块
|
||
for plot_index, crop, result in successful_harvests:
|
||
message_lines.append(f" - 地块 {plot_index}:{crop.display_name} × {result['base_yield']}")
|
||
|
||
# 汇总果实
|
||
if fruit_counts:
|
||
message_lines.append("\n- 获得果实:")
|
||
for fruit_id, qty in fruit_counts.items():
|
||
# 从成功收获中查找对应的作物显示名称
|
||
crop_for_fruit = next(
|
||
(c for _, c, _ in successful_harvests if c.fruit_id == fruit_id),
|
||
None
|
||
)
|
||
fruit_name = crop_for_fruit.display_name if crop_for_fruit else fruit_id
|
||
message_lines.append(f" - {fruit_name}的果实 × {qty}")
|
||
|
||
# 额外奖励
|
||
if total_points_gained > 0:
|
||
message_lines.append(f"\n- 额外积分:+{total_points_gained}(当前积分 {new_points})")
|
||
|
||
if extra_items:
|
||
message_lines.append("\n- 额外物品:")
|
||
for item_id, qty in extra_items.items():
|
||
message_lines.append(f" - {item_id} × {qty}")
|
||
|
||
if failed_harvests:
|
||
if message_lines:
|
||
message_lines.append("\n---\n")
|
||
message_lines.append("# ❌ 收获失败")
|
||
for plot_index, error_msg in failed_harvests:
|
||
message_lines.append(f"- 地块 {plot_index}:{error_msg}")
|
||
|
||
if not message_lines:
|
||
return await self.send_markdown_message("❌ 没有可收获的地块", chat_id, user_id)
|
||
|
||
return await self.send_markdown_message("\n".join(message_lines), chat_id, user_id)
|
||
|
||
|
||
__all__ = ["WPSGardenHarvest"]
|