版本:未加入题库版本最后一版

This commit is contained in:
liuwei
2025-02-24 10:44:17 +08:00
parent 79fe3eb9d5
commit 427f9672f2
3 changed files with 80 additions and 9 deletions

View File

@@ -14,10 +14,12 @@ db_config = {
'cursorclass': pymysql.cursors.DictCursor
}
# 连接数据库
def get_db_connection():
return pymysql.connect(**db_config)
# 添加群聊
def add_group(group_id, player_id):
conn = get_db_connection()
@@ -35,6 +37,7 @@ def add_group(group_id, player_id):
cursor.close()
conn.close()
# 获取所有群聊ID
def get_group_ids():
conn = get_db_connection()
@@ -46,6 +49,7 @@ def get_group_ids():
cursor.close()
conn.close()
# 确保游戏启动(自动初始化群聊和玩家)
def ensure_game_started(group_id, player_id, player_name="未知玩家"):
conn = get_db_connection()
@@ -79,6 +83,7 @@ def ensure_game_started(group_id, player_id, player_name="未知玩家"):
cursor.close()
conn.close()
# 随机分配任务
def assign_random_task(group_id, player_id=None):
conn = get_db_connection()
@@ -147,6 +152,7 @@ def assign_random_task(group_id, player_id=None):
cursor.close()
conn.close()
# 提交答案并计分
def submit_answer(group_id, player_id, task_id, answer):
conn = get_db_connection()
@@ -277,6 +283,7 @@ def submit_answer(group_id, player_id, task_id, answer):
cursor.close()
conn.close()
# 显示排行榜
def show_rank(group_id, player_id):
conn = get_db_connection()
@@ -301,6 +308,7 @@ def show_rank(group_id, player_id):
cursor.close()
conn.close()
# 显示当前活跃任务
def show_active_tasks(group_id, player_id):
conn = get_db_connection()
@@ -331,6 +339,7 @@ def show_active_tasks(group_id, player_id):
cursor.close()
conn.close()
# 列举所有未完成任务
def list_uncompleted_tasks(group_id, player_id):
conn = get_db_connection()
@@ -361,6 +370,7 @@ def list_uncompleted_tasks(group_id, player_id):
cursor.close()
conn.close()
# 定时任务整点触发排除23:00-08:00
def run_random_task_assignment(group_id):
current_hour = datetime.now().hour
@@ -370,6 +380,7 @@ def run_random_task_assignment(group_id):
result = assign_random_task(group_id)
print(f"{datetime.now()} {result}")
# 处理群聊消息
def game_process_message(group_id, player_id, message, player_name="未知玩家"):
# 自动初始化群聊和玩家
@@ -436,15 +447,17 @@ def game_process_message(group_id, player_id, message, player_name="未知玩家
f"🎀 /r - 查看排行榜"
)
# 设置定时任务
def setup_schedule():
group_ids = get_group_ids()
for gid in group_ids:
run_random_task_assignment(group_id=gid)
# 主程序
if __name__ == "__main__":
# 测试用例
print(game_process_message("45317011307@chatroom", "Jyunere", "/t")) # 新用户获取任务
print(game_process_message("45317011307@chatroom", "Jyunere", "/a 18 罗马斗兽场")) # 提交答案
print(game_process_message("45317011307@chatroom", "Jyunere", "/r")) # 查看排行榜
print(game_process_message("45317011307@chatroom", "Jyunere", "/r")) # 查看排行榜