可收获多个地块

This commit is contained in:
2025-11-15 00:56:26 +08:00
parent a499cebfcd
commit 1716985d1a
3 changed files with 213 additions and 48 deletions

View File

@@ -22,15 +22,15 @@ class WPSGardenHarvest(WPSGardenBase):
return (
GuideEntry(
title="收获",
identifier="收获 <地块序号>",
description="从成熟地块采摘果实并发放额外奖励。",
identifier="收获 <地块序号> [地块序号...]",
description="从成熟地块采摘果实并发放额外奖励。支持一次收获多个地块。",
metadata={"别名": "harvest"},
icon="🧺",
details=[
{
"type": "steps",
"items": [
"输入正整数地块序号。",
"输入一个或多个正整数地块序号,用空格分隔",
"系统校验成熟状态,计算基础果实数量。",
"发放额外奖励:积分或额外物品会自动结算。",
],
@@ -43,7 +43,7 @@ class WPSGardenHarvest(WPSGardenBase):
return (
{
"title": "指令格式",
"description": "`收获 <地块序号>`,序号需为正整数。",
"description": "`收获 <地块序号> [地块序号...]`,支持一次收获多个地块,序号用空格分隔,序号需为正整数。",
},
{
"title": "收益构成",
@@ -59,56 +59,122 @@ class WPSGardenHarvest(WPSGardenBase):
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)
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])
# 解析多个地块序号
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)
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} 的额外积分",
# 收集结果
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
)
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)
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)