新增菜园系统
This commit is contained in:
86
Plugins/WPSGardenSystem/garden_plugin_steal.py
Normal file
86
Plugins/WPSGardenSystem/garden_plugin_steal.py
Normal file
@@ -0,0 +1,86 @@
|
||||
"""Steal plugin for garden system."""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Optional
|
||||
|
||||
from PWF.Convention.Runtime.Architecture import Architecture
|
||||
|
||||
from PWF.CoreModules.database import get_db
|
||||
from Plugins.WPSBackpackSystem import WPSBackpackSystem
|
||||
|
||||
from .garden_plugin_base import WPSGardenBase
|
||||
|
||||
|
||||
class WPSGardenSteal(WPSGardenBase):
|
||||
def wake_up(self) -> None:
|
||||
super().wake_up()
|
||||
self.register_plugin("steal")
|
||||
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 len(tokens) < 2:
|
||||
return await self.send_markdown_message("❌ 指令格式:`偷取 <用户> <地块序号>`", chat_id, user_id)
|
||||
target_identifier = tokens[0]
|
||||
if not tokens[1].isdigit():
|
||||
return await self.send_markdown_message("❌ 指令格式:`偷取 <用户> <地块序号>`", chat_id, user_id)
|
||||
plot_index = int(tokens[1])
|
||||
|
||||
owner_id = self._resolve_user_identifier(target_identifier)
|
||||
if owner_id is None:
|
||||
return await self.send_markdown_message("❌ 未找到目标用户", chat_id, user_id)
|
||||
if owner_id == user_id:
|
||||
return await self.send_markdown_message("❌ 不能偷取自己的菜园", chat_id, user_id)
|
||||
|
||||
try:
|
||||
result = self.service().steal(
|
||||
thief_id=user_id,
|
||||
owner_id=owner_id,
|
||||
plot_index=plot_index,
|
||||
)
|
||||
except ValueError as exc:
|
||||
return await self.send_markdown_message(f"❌ {exc}", chat_id, user_id)
|
||||
|
||||
crop = result["crop"]
|
||||
backpack: WPSBackpackSystem = Architecture.Get(WPSBackpackSystem)
|
||||
backpack.add_item(user_id, crop.fruit_id, result["stolen_quantity"])
|
||||
|
||||
remaining = result["remaining"]
|
||||
message = (
|
||||
"# 🕵️ 偷取成功\n"
|
||||
f"- 目标:{crop.display_name}\n"
|
||||
f"- 获得:{crop.display_name}的果实 × {result['stolen_quantity']}\n"
|
||||
f"- 目标剩余果实:{remaining}"
|
||||
)
|
||||
await self.send_markdown_message(message, chat_id, user_id)
|
||||
|
||||
owner_chat = result.get("chat_id")
|
||||
if owner_chat:
|
||||
owner_message = (
|
||||
"# ⚠️ 菜园警报\n"
|
||||
f"- 你的 {crop.display_name} 被偷走了 1 个果实\n"
|
||||
f"- 当前剩余果实:{remaining}"
|
||||
)
|
||||
await self.send_markdown_message(owner_message, owner_chat, owner_id)
|
||||
return None
|
||||
|
||||
def _resolve_user_identifier(self, identifier: str) -> Optional[int]:
|
||||
text = identifier.strip()
|
||||
if text.isdigit():
|
||||
return int(text)
|
||||
cursor = get_db().conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT user_id FROM user_info WHERE username = ? COLLATE NOCASE",
|
||||
(text,),
|
||||
)
|
||||
row = cursor.fetchone()
|
||||
if row:
|
||||
return int(row["user_id"])
|
||||
return None
|
||||
|
||||
|
||||
__all__ = ["WPSGardenSteal"]
|
||||
Reference in New Issue
Block a user