Files
WPSBot/games/base.py
2025-10-28 13:00:35 +08:00

123 lines
2.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""游戏基类"""
import logging
from abc import ABC, abstractmethod
from core.database import get_db
logger = logging.getLogger(__name__)
class BaseGame(ABC):
"""游戏基类"""
def __init__(self):
"""初始化游戏"""
self.db = get_db()
@abstractmethod
async def handle(self, command: str, chat_id: int, user_id: int) -> str:
"""处理游戏指令
Args:
command: 完整指令
chat_id: 会话ID
user_id: 用户ID
Returns:
回复消息
"""
raise NotImplementedError
@abstractmethod
def get_help(self) -> str:
"""获取帮助信息
Returns:
帮助文本
"""
raise NotImplementedError
def get_help_message() -> str:
"""获取总体帮助信息"""
help_text = """## 🎮 WPS游戏机器人帮助
### 🎲 骰娘系统
- `.r XdY` - 掷骰子(如:.r 1d20
- `.r XdY+Z` - 带修正掷骰(如:.r 2d6+3
### ✊ 石头剪刀布
- `.rps 石头` - 出石头
- `.rps 剪刀` - 出剪刀
- `.rps 布` - 出布
- `.rps stats` - 查看战绩
### 🔮 运势占卜
- `.fortune` - 今日运势
- `.运势` - 今日运势
### 🔢 猜数字游戏
- `.guess start` - 开始游戏
- `.guess 数字` - 猜测数字
- `.guess stop` - 结束游戏
### 📝 问答游戏
- `.quiz` - 随机问题
- `.quiz 答案` - 回答问题
### 其他
- `.help` - 显示帮助
- `.stats` - 查看个人统计
---
💡 提示:@机器人 + 指令即可使用
"""
return help_text
def get_stats_message(user_id: int) -> str:
"""获取用户统计信息"""
db = get_db()
cursor = db.conn.cursor()
# 获取所有游戏统计
cursor.execute(
"SELECT game_type, wins, losses, draws, total_plays FROM game_stats WHERE user_id = ?",
(user_id,)
)
stats = cursor.fetchall()
if not stats:
return "📊 你还没有游戏记录哦~快来玩游戏吧!"
# 构建统计信息
text = "## 📊 你的游戏统计\n\n"
game_names = {
'rps': '✊ 石头剪刀布',
'guess': '🔢 猜数字',
'quiz': '📝 问答游戏'
}
for row in stats:
game_type = row[0]
wins = row[1]
losses = row[2]
draws = row[3]
total = row[4]
game_name = game_names.get(game_type, game_type)
win_rate = (wins / total * 100) if total > 0 else 0
text += f"### {game_name}\n"
text += f"- 总局数:{total}\n"
text += f"- 胜利:{wins}\n"
text += f"- 失败:{losses}\n"
if draws > 0:
text += f"- 平局:{draws}\n"
text += f"- 胜率:{win_rate:.1f}%\n\n"
return text