积分系统
This commit is contained in:
248
games/gift.py
Normal file
248
games/gift.py
Normal file
@@ -0,0 +1,248 @@
|
||||
"""积分赠送系统游戏模块"""
|
||||
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 ['stats', '统计']:
|
||||
return self._get_gift_stats(user_id)
|
||||
|
||||
# 发送记录
|
||||
elif args in ['sent', '发送', '送出']:
|
||||
return self._get_gift_records_sent(user_id)
|
||||
|
||||
# 接收记录
|
||||
elif args in ['received', '接收', '收到']:
|
||||
return self._get_gift_records_received(user_id)
|
||||
|
||||
# 赠送帮助
|
||||
elif 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_id> <points> [message]
|
||||
parts = args.split(maxsplit=2)
|
||||
|
||||
if len(parts) < 2:
|
||||
return "❌ 指令格式错误!\n\n正确格式:`.gift <用户ID> <积分数量> [附赠消息]`\n\n示例:\n`.gift 123 50 生日快乐`\n`.gift 456 100`"
|
||||
|
||||
try:
|
||||
receiver_id = int(parts[0])
|
||||
points = int(parts[1])
|
||||
message = parts[2] if len(parts) > 2 else None
|
||||
except ValueError:
|
||||
return "❌ 用户ID和积分数量必须是数字!"
|
||||
|
||||
# 验证参数
|
||||
if points <= 0:
|
||||
return "❌ 赠送积分数量必须大于0!"
|
||||
|
||||
if points > 1000:
|
||||
return "❌ 单次赠送积分不能超过1000!"
|
||||
|
||||
if sender_id == receiver_id:
|
||||
return "❌ 不能赠送积分给自己!"
|
||||
|
||||
# 执行赠送
|
||||
if self.db.send_gift(sender_id, receiver_id, points, message):
|
||||
# 获取更新后的积分信息
|
||||
sender_points = self.db.get_user_points(sender_id)
|
||||
receiver_points = self.db.get_user_points(receiver_id)
|
||||
|
||||
text = f"## 🎁 积分赠送成功!\n\n"
|
||||
text += f"**赠送者**:用户{sender_id}\n\n"
|
||||
text += f"**接收者**:用户{receiver_id}\n\n"
|
||||
text += f"**赠送积分**:{points} 分\n\n"
|
||||
|
||||
if message:
|
||||
text += f"**附赠消息**:{message}\n\n"
|
||||
|
||||
text += f"**赠送者剩余积分**:{sender_points['available_points']} 分\n\n"
|
||||
text += f"**接收者当前积分**:{receiver_points['available_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 = record.get('receiver_name', f"用户{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 = record.get('sender_name', f"用户{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 123 50 生日快乐\n"
|
||||
text += f".gift 456 100 感谢你的帮助\n"
|
||||
text += f".gift 789 200\n"
|
||||
text += f".gift stats\n"
|
||||
text += f"```\n\n"
|
||||
|
||||
text += f"### 说明\n"
|
||||
text += f"- 所有赠送都有完整记录\n"
|
||||
text += f"- 赠送和接收都会在积分记录中显示\n"
|
||||
text += f"- 赠送是单向的,无需对方确认\n"
|
||||
|
||||
return text
|
||||
|
||||
def get_help(self) -> str:
|
||||
"""获取帮助信息"""
|
||||
return self._get_gift_help()
|
||||
Reference in New Issue
Block a user