264 lines
9.6 KiB
Python
264 lines
9.6 KiB
Python
"""积分赠送系统游戏模块"""
|
||
import logging
|
||
from datetime import datetime
|
||
from games.base import BaseGame
|
||
from utils.parser import CommandParser
|
||
from core.database import get_db
|
||
|
||
logger = logging.getLogger(__name__)
|
||
|
||
|
||
class GiftGame(BaseGame):
|
||
"""积分赠送系统游戏"""
|
||
|
||
def __init__(self):
|
||
"""初始化游戏"""
|
||
super().__init__()
|
||
self.db = get_db()
|
||
|
||
async def handle(self, command: str, chat_id: int, user_id: int) -> str:
|
||
"""处理积分赠送相关指令
|
||
|
||
Args:
|
||
command: 指令,如 ".gift 123 50 生日快乐", ".gift sent", ".gift received"
|
||
chat_id: 会话ID
|
||
user_id: 用户ID
|
||
|
||
Returns:
|
||
回复消息
|
||
"""
|
||
try:
|
||
# 提取参数
|
||
_, args = CommandParser.extract_command_args(command)
|
||
args = args.strip()
|
||
|
||
|
||
# 赠送帮助
|
||
if args in ['help', '帮助']:
|
||
return self._get_gift_help()
|
||
|
||
# 默认:执行赠送
|
||
else:
|
||
return self._process_gift_command(args, user_id)
|
||
|
||
except Exception as e:
|
||
logger.error(f"处理积分赠送指令错误: {e}", exc_info=True)
|
||
return f"❌ 处理指令出错: {str(e)}"
|
||
|
||
def _process_gift_command(self, args: str, sender_id: int) -> str:
|
||
"""处理赠送指令
|
||
|
||
Args:
|
||
args: 指令参数
|
||
sender_id: 发送者ID
|
||
|
||
Returns:
|
||
处理结果消息
|
||
"""
|
||
# 解析参数:.gift <receiver_identifier> <points> [message]
|
||
parts = args.split(maxsplit=2)
|
||
|
||
if len(parts) < 2:
|
||
return "❌ 指令格式错误!\n\n正确格式:`.gift <用户名或ID> <积分数量> [附赠消息]`\n\n示例:\n`.gift 张三 50 生日快乐`\n`.gift 123 50`\n`.gift 456 100`"
|
||
|
||
# 解析积分数量
|
||
try:
|
||
points = int(parts[1])
|
||
message = parts[2] if len(parts) > 2 else None
|
||
except ValueError:
|
||
return "❌ 积分数量必须是数字!"
|
||
|
||
# 检查第一部分是用户名还是ID
|
||
receiver_input = parts[0]
|
||
|
||
if receiver_input.isdigit():
|
||
# 是数字,作为用户ID处理
|
||
receiver_id = int(receiver_input)
|
||
else:
|
||
# 是用户名,通过数据库查找
|
||
user = self.db.get_user_by_name(receiver_input)
|
||
if not user:
|
||
return f"❌ 未找到用户: {receiver_input}\n\n请确认用户名是否正确,或使用用户ID。"
|
||
receiver_id = user['user_id']
|
||
|
||
# 获取接收者名称用于显示
|
||
receiver_name = self.db.get_user_display_name(receiver_id)
|
||
|
||
# 获取发送者名称用于显示
|
||
sender_name = self.db.get_user_display_name(sender_id)
|
||
|
||
# 验证参数
|
||
if points <= 0:
|
||
return "❌ 赠送积分数量必须大于0!"
|
||
|
||
if sender_id == receiver_id:
|
||
return "❌ 不能赠送积分给自己!"
|
||
|
||
# 检查赠送者积分是否足够
|
||
sender_points = self.db.get_user_points(sender_id)
|
||
if sender_points['points'] < points:
|
||
return f"❌ 积分不足!需要 {points} 积分,当前可用 {sender_points['points']} 积分"
|
||
|
||
# 执行赠送(消费赠送者积分,增加接收者积分)
|
||
if (self.db.consume_points(sender_id, points, "gift_send", f"赠送积分给用户{receiver_id}") and
|
||
self.db.add_points(receiver_id, points, "gift_receive", f"收到用户{sender_id}的积分赠送")):
|
||
|
||
# 获取更新后的积分信息
|
||
sender_points_after = self.db.get_user_points(sender_id)
|
||
receiver_points_after = self.db.get_user_points(receiver_id)
|
||
|
||
text = f"## 🎁 积分赠送成功!\n\n"
|
||
text += f"**赠送者**:{sender_name}\n\n"
|
||
text += f"**接收者**:{receiver_name}\n\n"
|
||
text += f"**赠送积分**:{points} 分\n\n"
|
||
|
||
if message:
|
||
text += f"**附赠消息**:{message}\n\n"
|
||
|
||
text += f"**赠送者剩余积分**:{sender_points_after['points']} 分\n\n"
|
||
text += f"**接收者当前积分**:{receiver_points_after['points']} 分\n\n"
|
||
text += "---\n\n"
|
||
text += "💝 感谢您的慷慨赠送!"
|
||
|
||
return text
|
||
else:
|
||
return "❌ 赠送失败!请检查积分是否足够或用户ID是否正确。"
|
||
|
||
def _get_gift_stats(self, user_id: int) -> str:
|
||
"""获取赠送统计信息
|
||
|
||
Args:
|
||
user_id: 用户ID
|
||
|
||
Returns:
|
||
统计信息消息
|
||
"""
|
||
stats = self.db.get_gift_stats(user_id)
|
||
|
||
if stats['sent_count'] == 0 and stats['received_count'] == 0:
|
||
return "📊 你还没有任何积分赠送记录哦~"
|
||
|
||
text = f"## 🎁 积分赠送统计\n\n"
|
||
text += f"**发送统计**:\n"
|
||
text += f"- 赠送次数:{stats['sent_count']} 次\n"
|
||
text += f"- 赠送积分:{stats['total_sent']} 分\n\n"
|
||
|
||
text += f"**接收统计**:\n"
|
||
text += f"- 接收次数:{stats['received_count']} 次\n"
|
||
text += f"- 接收积分:{stats['total_received']} 分\n\n"
|
||
|
||
net_gift = stats['net_gift']
|
||
if net_gift > 0:
|
||
text += f"**净收益**:+{net_gift} 分 🎉\n\n"
|
||
elif net_gift < 0:
|
||
text += f"**净收益**:{net_gift} 分 😅\n\n"
|
||
else:
|
||
text += f"**净收益**:0 分 ⚖️\n\n"
|
||
|
||
text += "---\n\n"
|
||
text += "💡 提示:使用 `.gift sent` 查看发送记录,`.gift received` 查看接收记录"
|
||
|
||
return text
|
||
|
||
def _get_gift_records_sent(self, user_id: int, limit: int = 10) -> str:
|
||
"""获取发送的赠送记录
|
||
|
||
Args:
|
||
user_id: 用户ID
|
||
limit: 限制数量
|
||
|
||
Returns:
|
||
记录信息消息
|
||
"""
|
||
records = self.db.get_gift_records_sent(user_id, limit)
|
||
|
||
if not records:
|
||
return "📝 暂无发送记录"
|
||
|
||
text = f"## 🎁 发送记录(最近 {len(records)} 条)\n\n"
|
||
|
||
for record in records:
|
||
timestamp = datetime.fromtimestamp(record['created_at']).strftime('%m-%d %H:%M')
|
||
receiver_name = self.db.get_user_display_name(record['receiver_id'])
|
||
points = record['points']
|
||
message = record.get('message', '')
|
||
|
||
text += f"**{timestamp}** 赠送 {points} 分给 {receiver_name}\n"
|
||
if message:
|
||
text += f" 💬 {message}\n"
|
||
text += "\n"
|
||
|
||
return text
|
||
|
||
def _get_gift_records_received(self, user_id: int, limit: int = 10) -> str:
|
||
"""获取接收的赠送记录
|
||
|
||
Args:
|
||
user_id: 用户ID
|
||
limit: 限制数量
|
||
|
||
Returns:
|
||
记录信息消息
|
||
"""
|
||
records = self.db.get_gift_records_received(user_id, limit)
|
||
|
||
if not records:
|
||
return "📝 暂无接收记录"
|
||
|
||
text = f"## 🎁 接收记录(最近 {len(records)} 条)\n\n"
|
||
|
||
for record in records:
|
||
timestamp = datetime.fromtimestamp(record['created_at']).strftime('%m-%d %H:%M')
|
||
sender_name = self.db.get_user_display_name(record['sender_id'])
|
||
points = record['points']
|
||
message = record.get('message', '')
|
||
|
||
text += f"**{timestamp}** 收到 {sender_name} 的 {points} 分\n"
|
||
if message:
|
||
text += f" 💬 {message}\n"
|
||
text += "\n"
|
||
|
||
return text
|
||
|
||
def _get_gift_help(self) -> str:
|
||
"""获取赠送帮助信息
|
||
|
||
Returns:
|
||
帮助信息消息
|
||
"""
|
||
text = f"## 🎁 积分赠送系统\n\n"
|
||
text += f"### 基础用法\n"
|
||
text += f"- `.gift <用户名或ID> <积分数量> [附赠消息]` - 赠送积分\n"
|
||
text += f"- `.gift stats` - 查看赠送统计\n"
|
||
text += f"- `.gift sent` - 查看发送记录\n"
|
||
text += f"- `.gift received` - 查看接收记录\n"
|
||
text += f"- `.gift help` - 查看帮助\n\n"
|
||
|
||
text += f"### 赠送规则\n"
|
||
text += f"- **积分限制**:单次最多赠送1000积分\n"
|
||
text += f"- **自赠限制**:不能赠送给自己\n"
|
||
text += f"- **积分检查**:必须有足够积分才能赠送\n"
|
||
text += f"- **附赠消息**:可选,最多100字符\n\n"
|
||
|
||
text += f"### 示例\n"
|
||
text += f"```\n"
|
||
text += f".gift 张三 50 生日快乐\n"
|
||
text += f".gift 123 50 (使用用户ID)\n"
|
||
text += f".gift 李四 100 感谢你的帮助\n"
|
||
text += f".gift 王五 200\n"
|
||
text += f".gift stats\n"
|
||
text += f"```\n\n"
|
||
|
||
text += f"### 说明\n"
|
||
text += f"- 支持使用用户名或用户ID进行赠送\n"
|
||
text += f"- 使用用户名需要先通过 `.register` 注册名称\n"
|
||
text += f"- 所有赠送都有完整记录\n"
|
||
text += f"- 赠送和接收都会在积分记录中显示\n"
|
||
text += f"- 赠送是单向的,无需对方确认\n"
|
||
|
||
return text
|
||
|
||
def get_help(self) -> str:
|
||
"""获取帮助信息"""
|
||
return self._get_gift_help()
|