133 lines
3.1 KiB
Python
133 lines
3.1 KiB
Python
"""游戏基类"""
|
||
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 答案` - 回答问题
|
||
|
||
### 🀄 成语接龙
|
||
- `.idiom start [成语]` - 开始游戏
|
||
- `.idiom [成语]` - 接龙
|
||
- `.idiom [成语] @某人` - 接龙并指定下一位
|
||
- `.idiom stop` - 结束游戏
|
||
- `.idiom status` - 查看状态
|
||
- `.idiom reject [词语]` - 拒绝词语加入黑名单(仅发起人)
|
||
- `.idiom blacklist` - 查看黑名单
|
||
|
||
### 其他
|
||
- `.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': '📝 问答游戏',
|
||
'idiom': '🀄 成语接龙'
|
||
}
|
||
|
||
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
|
||
|