新增菜园系统

This commit is contained in:
2025-11-10 01:15:17 +08:00
parent 22d2271bac
commit cdb3433b8a
14 changed files with 1230 additions and 15 deletions

View File

@@ -0,0 +1,89 @@
"""Garden overview and selling plugin."""
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 .garden_plugin_base import WPSGardenBase
class WPSGardenView(WPSGardenBase):
def wake_up(self) -> None:
super().wake_up()
self.register_plugin("garden")
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_overview(chat_id, user_id)
tokens = [token.strip() for token in payload.split() if token.strip()]
if tokens and tokens[0] in {"售出", "sell"}:
return await self._handle_sell(tokens[1:], chat_id, user_id)
return await self._send_overview(chat_id, user_id)
async def _send_overview(self, chat_id: int, user_id: int) -> Optional[str]:
overview = self.format_garden_overview(user_id)
return await self.send_markdown_message(overview, chat_id, user_id)
async def _handle_sell(self, args: list[str], chat_id: int, user_id: int) -> Optional[str]:
if len(args) < 2:
return await self.send_markdown_message(
"❌ 指令格式:`菜园 售出 <果实> <数量>`",
chat_id,
user_id,
)
identifier = args[0]
try:
quantity = int(args[1])
except ValueError:
return await self.send_markdown_message("❌ 数量必须是整数", chat_id, user_id)
if quantity <= 0:
return await self.send_markdown_message("❌ 数量必须大于0", chat_id, user_id)
crop = self.resolve_fruit_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.fruit_id:
owned = item.quantity
break
if owned < quantity:
return await self.send_markdown_message("❌ 果实数量不足", chat_id, user_id)
total_points, price_per = self.service().sell_fruit(
user_id=user_id,
fruit_id=crop.fruit_id,
quantity=quantity,
)
backpack.set_item_quantity(user_id, crop.fruit_id, owned - quantity)
config_api: WPSConfigAPI = Architecture.Get(WPSConfigAPI)
new_points = await config_api.adjust_user_points(
chat_id,
user_id,
total_points,
reason=f"出售 {crop.display_name} 的果实",
)
message = (
"# 🛒 售出成功\n"
f"- 果实:{crop.display_name} × {quantity}\n"
f"- 单价:{price_per}\n"
f"- 总计:{total_points}\n"
f"- 当前积分:{new_points}"
)
return await self.send_markdown_message(message, chat_id, user_id)
__all__ = ["WPSGardenView"]