"""营地售出插件 - 处理战斗纪念品回收""" from __future__ import annotations from typing import Optional, Sequence from PWF.Convention.Runtime.Architecture import Architecture from PWF.Convention.Runtime.GlobalConfig import ConsoleFrontColor, ProjectConfig from Plugins.WPSAPI import GuideEntry from Plugins.WPSBackpackSystem import WPSBackpackSystem from Plugins.WPSConfigSystem import WPSConfigAPI from .combat_models import ADVENTURE_SOUVENIRS from .combat_plugin_base import WPSCombatBase logger: ProjectConfig = ProjectConfig() class WPSCombatCamp(WPSCombatBase): """营地售出指令插件""" def __init__(self) -> None: super().__init__() self._souvenir_by_id = ADVENTURE_SOUVENIRS self._souvenir_name_index = { meta[0].lower(): item_id for item_id, meta in self._souvenir_by_id.items() } def get_guide_subtitle(self) -> str: return "营地寄售区:纪念品快速变现" def collect_command_entries(self) -> Sequence[GuideEntry]: return ( GuideEntry( title="营地 售出", identifier="营地 售出 <纪念品> <数量>", description="将纪念品出售给系统换取积分。", metadata={"别名": "camp"}, icon="🏕️", details=[ {"type": "steps", "items": ["输入纪念品名称或 ID。", "校验背包数量并扣除。", "根据基础售价发放积分。"]}, ], ), ) def collect_guide_entries(self) -> Sequence[GuideEntry]: return ( { "title": "销售指令", "description": "`营地 售出 <纪念品> <数量>`,名称可用中文或物品ID。", }, { "title": "积分结算", "description": "根据纪念品基础售价乘以数量发放积分,并扣除背包库存。", }, ) def is_enable_plugin(self) -> bool: return True def wake_up(self) -> None: super().wake_up() logger.Log( "Info", f"{ConsoleFrontColor.GREEN}WPSCombatCamp 插件已加载{ConsoleFrontColor.RESET}", ) self.register_plugin("营地") self.register_plugin("camp") 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( self._help_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( self._help_message(), chat_id, user_id ) command = tokens[0].lower() if command in {"售出", "sell"}: return await self._handle_sell(tokens[1:], chat_id, user_id) return await self.send_markdown_message( self._help_message(), 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) resolved = self._resolve_souvenir(identifier) if not resolved: return await self.send_markdown_message( "❌ 未找到对应纪念品", chat_id, user_id ) item_id, name, price_per = resolved backpack: WPSBackpackSystem = Architecture.Get(WPSBackpackSystem) owned = 0 for item in backpack.get_user_items(user_id): if item.item_id == item_id: owned = item.quantity break if owned < quantity: return await self.send_markdown_message( "❌ 纪念品数量不足", chat_id, user_id ) backpack.set_item_quantity(user_id, item_id, owned - quantity) total_points = price_per * quantity config_api: WPSConfigAPI = Architecture.Get(WPSConfigAPI) new_points = await config_api.adjust_user_points( chat_id, user_id, total_points, reason=f"出售 {name}", ) message = ( "# 🛒 售出成功\n" f"- 纪念品:{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) def _resolve_souvenir( self, identifier: str ) -> Optional[tuple[str, str, int]]: key = identifier.strip() lowered = key.lower() if key in self._souvenir_by_id: item_id = key elif lowered in self._souvenir_by_id: item_id = lowered elif lowered in self._souvenir_name_index: item_id = self._souvenir_name_index[lowered] else: return None name, _tier, price, _desc = self._souvenir_by_id[item_id] return item_id, name, price def _help_message(self) -> str: return ( "# 🏕️ 营地指令\n" "- `营地 售出 <纪念品> <数量>`:将战斗纪念品出售给系统换取积分\n" "\n" "**示例:**\n" "- `营地 售出 战斗奖杯 1`\n" "- `camp sell combat_souvenir_medal 3`\n" ) __all__ = ["WPSCombatCamp"]