mirror of
https://github.com/A-Minos/nonebot-plugin-tetris-stats.git
synced 2026-03-05 05:36:54 +08:00
✨ 新增查分段指令 (#121)
* 忽略 .idea 文件夹 * 新增查分段指令 * 修复未知段位可能导致程序无限死循环的异常 * 我 keys 呢 * 🎨 整理 import * 🎨 规范引号 * 🐛 记得await * ⚡️ 一些优化 * 🐛 多个逗号 * 🚨 修正typing hint * 🐛 返回值是 tuple 哦 * 🐛 少个逗号 * 🐛 你得删前缀啊 * 🐛 怎么能用 is 呢 * 🐛 记得await * 试图匹配查询格式 * 💬 小改返回消息样式 * 🎨 修改变量名 * 🐛 修复查询大写问题 * 🐛 使用 get_db 获取数据库对象 --------- Co-authored-by: scdhh <wallfjjd@gmail.com>
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
import os
|
||||
from asyncio import gather
|
||||
from sqlite3 import Connection, connect
|
||||
@@ -39,10 +40,61 @@ class DataBase():
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS TOPBIND
|
||||
(QQ INTEGER NOT NULL,
|
||||
USER TEXT NOT NULL)''')
|
||||
cursor.execute('''CREATE TABLE IF NOT EXISTS IORANK
|
||||
(RANK VARCHAR(2) NOT NULL,
|
||||
TRENDING CHAR(1) NOT NULL,
|
||||
TRLINE FLOAT NOT NULL,
|
||||
PLAYERCOUNT INTEGER NOT NULL,
|
||||
AVGAPM FLOAT NOT NULL,
|
||||
AVGPPS FLOAT NOT NULL,
|
||||
ARGVS FLOAT NOT NULL,
|
||||
DATE TEXT NOT NULL)''')
|
||||
cls._db.commit()
|
||||
logger.info('数据库初始化完成')
|
||||
return cls._db
|
||||
|
||||
@classmethod
|
||||
async def query_rank_info_today(cls, rank: str) -> list | None:
|
||||
'''查询段位信息'''
|
||||
db = await cls._get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute('''SELECT TRENDING, TRLINE, PLAYERCOUNT, AVGAPM, AVGPPS, ARGVS, DATE
|
||||
FROM IORANK
|
||||
WHERE RANK = ? AND DATE = ?''', (rank, datetime.date.today()))
|
||||
result = cursor.fetchone()
|
||||
|
||||
if result is None:
|
||||
return None
|
||||
|
||||
return list(result)
|
||||
|
||||
@classmethod
|
||||
async def write_rank_info_today(cls, rank: str, trline: int, playercount: int, avgapm: float, avgpps: float, avgvs: float):
|
||||
'''写入段位信息'''
|
||||
|
||||
db = await cls._get_db()
|
||||
cursor = db.cursor()
|
||||
cursor.execute('''SELECT TRLINE
|
||||
FROM IORANK
|
||||
WHERE RANK = ? AND DATE = ?''', (rank, datetime.date.today()))
|
||||
|
||||
result = cursor.fetchone()
|
||||
|
||||
if result is None:
|
||||
trending = '?'
|
||||
else:
|
||||
if result[0] > trline:
|
||||
trending = '↑'
|
||||
else:
|
||||
trending = '↓'
|
||||
|
||||
cursor.execute('''INSERT INTO IORANK
|
||||
(RANK, TRENDING, TRLINE, PLAYERCOUNT, AVGAPM, AVGPPS, ARGVS, DATE)
|
||||
VALUES (?,?,?,?,?,?,?,?)''',
|
||||
(rank, trending, trline, playercount, avgapm, avgpps, avgvs, datetime.date.today()))
|
||||
|
||||
db.commit()
|
||||
|
||||
@classmethod
|
||||
async def _get_db(cls) -> Connection:
|
||||
'''获取数据库对象'''
|
||||
|
||||
@@ -53,6 +53,19 @@ async def handle_stats_query_message(message: str, game_type: str) -> tuple[str
|
||||
return await check_name(message, game_type)
|
||||
|
||||
|
||||
async def handle_rank_message(message: str) -> str:
|
||||
_cmd_aliases = ['io段位', 'iorank']
|
||||
# 剔除命令前缀
|
||||
for i in _cmd_aliases:
|
||||
if match(rf'(?i){i}', message):
|
||||
message = sub(rf'(?i){i}', '', message)
|
||||
message = message.strip()
|
||||
break
|
||||
else:
|
||||
raise ValueError('预期外行为, 请上报GitHub')
|
||||
return message
|
||||
|
||||
|
||||
async def check_name(name: str, game_type: str) -> tuple[str | None, tuple]:
|
||||
'''返回值为tuple[gameType, tuple[message, user]]'''
|
||||
if game_type == 'IO':
|
||||
|
||||
Reference in New Issue
Block a user