From fd1676b9082c370e4acc3f7530cf65ec267a397e Mon Sep 17 00:00:00 2001 From: liuwei Date: Tue, 18 Mar 2025 10:23:43 +0800 Subject: [PATCH] =?UTF-8?q?feature:=20=E6=95=B0=E6=8D=AE=E5=BA=93=E8=BF=9E?= =?UTF-8?q?=E6=8E=A5=E4=B8=8ESQL=E9=9B=86=E4=B8=AD=E7=AE=A1=E7=90=86?= =?UTF-8?q?=EF=BC=8C=E6=8F=90=E9=AB=98=E4=BB=A3=E7=A0=81=E5=8F=AF=E8=AF=BB?= =?UTF-8?q?=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- game_task/game_task_encyclopedia.py | 226 +++++++++++----------------- message_report/write_db.py | 120 ++++++--------- message_sign/main.py | 139 +++++------------ robot.py | 16 +- 4 files changed, 184 insertions(+), 317 deletions(-) diff --git a/game_task/game_task_encyclopedia.py b/game_task/game_task_encyclopedia.py index e4fda89..9dabd16 100644 --- a/game_task/game_task_encyclopedia.py +++ b/game_task/game_task_encyclopedia.py @@ -1,79 +1,51 @@ import random from datetime import datetime -import pymysql +from typing import Dict, List, Optional from game_task.game_chatgpt_qa import game_question_json, game_answer_json +from db.connection import DBConnectionManager +from db.encyclopedia import EncyclopediaDB -# 数据库连接配置 -db_config = { - 'host': '192.168.2.32', - 'user': 'root', - 'password': 'lw123456', - 'database': 'message_archive', - 'charset': 'utf8mb4', - 'cursorclass': pymysql.cursors.DictCursor -} - - -# 连接数据库 -def get_db_connection(): - return pymysql.connect(**db_config) +# 获取数据库连接管理器的单例 +db_manager = DBConnectionManager() +encyclopedia_db = EncyclopediaDB(db_manager) # 添加群聊 def add_group(group_id, player_id): - conn = get_db_connection() - cursor = conn.cursor() try: - cursor.execute( - "INSERT INTO t_encyclopedia_groups (group_id) VALUES (%s)", - (group_id,) - ) - conn.commit() - message = f"🎉 群 {group_id} 已就位,准备开燥!" + result = encyclopedia_db.add_group(group_id) + if result: + message = f"🎉 群 {group_id} 已就位,准备开燥!" + else: + message = f"🌟 群 {group_id} 早就蓄势待发啦!" return {"message": message, "player_id": player_id} - except pymysql.err.IntegrityError: + except Exception as e: + print(f"添加群聊出错: {e}") message = f"🌟 群 {group_id} 早就蓄势待发啦!" return {"message": message, "player_id": player_id} - finally: - cursor.close() - conn.close() # 获取所有群聊ID def get_group_ids(): - conn = get_db_connection() - cursor = conn.cursor() try: - cursor.execute("SELECT group_id FROM t_encyclopedia_groups") - return [row['group_id'] for row in cursor.fetchall()] - finally: - cursor.close() - conn.close() + return encyclopedia_db.get_all_groups() + except Exception as e: + print(f"获取群聊ID出错: {e}") + return [] # 确保游戏启动(自动初始化群聊和玩家) def ensure_game_started(group_id, player_id, player_name="未知玩家"): - conn = get_db_connection() - cursor = conn.cursor() try: # 检查并添加群聊 - cursor.execute("SELECT group_id FROM t_encyclopedia_groups WHERE group_id = %s", (group_id,)) - if not cursor.fetchone(): + if not encyclopedia_db.check_group_exists(group_id): add_group(group_id, player_id) # 检查并添加玩家 - cursor.execute( - "SELECT player_name FROM t_encyclopedia_players WHERE group_id = %s AND player_id = %s", - (group_id, player_id) - ) - existing_player = cursor.fetchone() - if not existing_player: - cursor.execute( - "INSERT INTO t_encyclopedia_players (player_id, group_id, player_name) VALUES (%s, %s, %s)", - (player_id, group_id, player_name) - ) - conn.commit() + player = encyclopedia_db.get_player(player_id, group_id) + if not player: + encyclopedia_db.add_player(player_id, group_id, player_name) message = ( f"🎉 哇塞,{player_name} 你来啦!\n" f"🌟 群 {group_id} 瞬间燃爆!\n" @@ -81,18 +53,17 @@ def ensure_game_started(group_id, player_id, player_name="未知玩家"): ) return {"message": message, "player_id": player_id} return {"message": None, "player_id": player_id} - finally: - cursor.close() - conn.close() + except Exception as e: + print(f"确保游戏启动出错: {e}") + return {"message": None, "player_id": player_id} # 随机分配任务 def assign_random_task(group_id, player_id=None): - conn = get_db_connection() - cursor = conn.cursor() try: - cursor.execute("SELECT player_id, player_name FROM t_encyclopedia_players WHERE group_id = %s", (group_id,)) - players = cursor.fetchall() + # 获取群内所有玩家 + players = encyclopedia_db.get_all_players_in_group(group_id) + if not players: message = ( f"😔 哎呀,群 {group_id} 静悄悄\n" @@ -122,17 +93,14 @@ def assign_random_task(group_id, player_id=None): score = int(task["score"]) description = task.get("description", "") - cursor.execute( - "INSERT INTO t_encyclopedia_active_tasks (group_id, question, answer, score, description, holder_id) VALUES (%s, %s, %s, %s, %s, %s)", - (group_id, question, answer, score, description, holder_id) + # 创建活跃任务 + active_task_id = encyclopedia_db.create_active_task( + group_id, question, answer, score, description, holder_id ) - conn.commit() - cursor.execute( - "SELECT active_task_id FROM t_encyclopedia_active_tasks WHERE group_id = %s AND question = %s AND holder_id = %s ORDER BY assigned_at DESC LIMIT 1", - (group_id, question, holder_id) - ) - active_task_id = cursor.fetchone()['active_task_id'] + if not active_task_id: + message = f"😔 任务创建失败,请稍后再试!" + return {"message": message, "player_id": player_id} if player_id: message = ( @@ -152,27 +120,25 @@ def assign_random_task(group_id, player_id=None): f"🌈 抢答格式:/a {active_task_id} 答案" ) return {"message": message, "player_id": holder_id} - finally: - cursor.close() - conn.close() + except Exception as e: + print(f"分配任务出错: {e}") + message = f"😔 任务分配出错,请稍后再试!" + return {"message": message, "player_id": player_id} # 提交答案并计分 def submit_answer(group_id, player_id, task_id, answer): - conn = get_db_connection() - cursor = conn.cursor() try: - cursor.execute("SELECT player_name FROM t_encyclopedia_players WHERE group_id = %s AND player_id = %s", - (group_id, player_id)) - player_row = cursor.fetchone() - if not player_row: + # 获取玩家信息 + player = encyclopedia_db.get_player(player_id, group_id) + if not player: message = ( f"😅 嘿,{player_id} 你是路人甲吗?\n" f"🌟 用 /s 先加入群 {group_id} 吧!" ) return {"message": message, "player_id": player_id} - player_name = player_row['player_name'] + player_name = player['player_name'] if not task_id.isdigit(): message = ( @@ -184,11 +150,9 @@ def submit_answer(group_id, player_id, task_id, answer): active_task_id = int(task_id) - cursor.execute( - "SELECT question, answer, score, holder_id, status FROM t_encyclopedia_active_tasks WHERE group_id = %s AND active_task_id = %s", - (group_id, active_task_id) - ) - task_data = cursor.fetchone() + # 获取任务信息 + task_data = encyclopedia_db.get_task_by_id(group_id, active_task_id) + if not task_data: message = ( f"😔 哎哟,任务 task_{active_task_id} 不翼而飞啦!\n" @@ -204,12 +168,14 @@ def submit_answer(group_id, player_id, task_id, answer): ) return {"message": message, "player_id": player_id} - question, correct_answer_db, top_score, holder_id, _ = task_data['question'], task_data['answer'].lower(), \ - task_data['score'], task_data['holder_id'], task_data['status'] + question = task_data['question'] + correct_answer_db = task_data['answer'].lower() + top_score = task_data['score'] + holder_id = task_data['holder_id'] - cursor.execute("SELECT player_name FROM t_encyclopedia_players WHERE group_id = %s AND player_id = %s", - (group_id, holder_id)) - holder_name = cursor.fetchone()['player_name'] + # 获取任务持有者信息 + holder = encyclopedia_db.get_task_holder(group_id, holder_id) + holder_name = holder['player_name'] if holder else "未知玩家" answer_json = {"question": question, "top_score": str(top_score), "answer": answer} result = game_answer_json(answer_json) @@ -218,14 +184,12 @@ def submit_answer(group_id, player_id, task_id, answer): is_correct = points > 0 if is_correct: - cursor.execute( - "UPDATE t_encyclopedia_players SET points = points + %s WHERE group_id = %s AND player_id = %s", - (points, group_id, player_id) - ) - cursor.execute( - "UPDATE t_encyclopedia_active_tasks SET status = 'completed' WHERE group_id = %s AND active_task_id = %s", - (group_id, active_task_id) - ) + # 更新玩家积分 + encyclopedia_db.update_player_points(player_id, group_id, points) + + # 完成任务 + encyclopedia_db.complete_task(active_task_id) + if player_id == holder_id: message = ( f"🎉 {player_name} 你是天才吗?\n" @@ -243,10 +207,8 @@ def submit_answer(group_id, player_id, task_id, answer): f"🎀 彩蛋:{description}" ) else: - cursor.execute( - "UPDATE t_encyclopedia_players SET points = GREATEST(points - 1, 0) WHERE group_id = %s AND player_id = %s", - (group_id, player_id) - ) + # 扣除积分 + encyclopedia_db.update_player_points(player_id, group_id, -1) points = -1 message = ( f"😅 {player_name} 你这是要笑死我吗?\n" @@ -258,11 +220,10 @@ def submit_answer(group_id, player_id, task_id, answer): f"🌟 任务ID: {active_task_id} 还能抢救一下!" ) - cursor.execute( - "INSERT INTO t_encyclopedia_task_history (group_id, active_task_id, player_id, answer, is_correct, points_earned) VALUES (%s, %s, %s, %s, %s, %s)", - (group_id, active_task_id, player_id, answer, is_correct, points) + # 添加任务历史记录 + encyclopedia_db.add_task_history( + group_id, active_task_id, player_id, answer, is_correct, points ) - conn.commit() return {"message": message, "player_id": player_id} except ValueError as e: @@ -281,54 +242,44 @@ def submit_answer(group_id, player_id, task_id, answer): f"🎈 再试一次吧!" ) return {"message": message, "player_id": player_id} - finally: - cursor.close() - conn.close() # 显示排行榜 def show_rank(group_id, player_id): - conn = get_db_connection() - cursor = conn.cursor() try: - cursor.execute( - "SELECT player_name, points FROM t_encyclopedia_players WHERE group_id = %s ORDER BY points DESC LIMIT 10", - (group_id,) - ) - ranks = cursor.fetchall() + # 获取排行榜 + ranks = encyclopedia_db.get_player_ranking(group_id, 10) + if not ranks: message = ( f"😔 群 {group_id} 冷冷清清\n" f"🌟 快来一起燥起来吧!" ) return {"message": message, "player_id": player_id} + rank_text = f"🎉 群 {group_id} 排行榜(Top 10)来啦!\n" for i, row in enumerate(ranks, 1): rank_text += f"🐓 {i}. {row['player_name']}: {row['points']} 分\n" return {"message": rank_text, "player_id": player_id} - finally: - cursor.close() - conn.close() + except Exception as e: + print(f"显示排行榜出错: {e}") + message = f"😔 获取排行榜出错,请稍后再试!" + return {"message": message, "player_id": player_id} # 显示当前活跃任务 def show_active_tasks(group_id, player_id): - conn = get_db_connection() - cursor = conn.cursor() try: - cursor.execute(""" - SELECT a.active_task_id, a.question, p.player_name, p.player_id - FROM t_encyclopedia_active_tasks a - JOIN t_encyclopedia_players p ON a.holder_id = p.player_id AND a.group_id = p.group_id - WHERE a.group_id = %s AND a.status = 'pending' - """, (group_id,)) - tasks = cursor.fetchall() + # 获取活跃任务 + tasks = encyclopedia_db.get_active_tasks_in_group(group_id) + if not tasks: message = ( f"😄 群 {group_id} 现在一片祥和\n" f"🌟 没任务?快用 /t 搞一个!" ) return {"message": message, "player_id": player_id} + task_text = f"🎉 群 {group_id} 活跃任务速递:\n" for task in tasks: task_text += ( @@ -337,29 +288,25 @@ def show_active_tasks(group_id, player_id): f"🌼 大佬:{task['player_name']}\n" ) return {"message": task_text, "player_id": player_id} - finally: - cursor.close() - conn.close() + except Exception as e: + print(f"显示活跃任务出错: {e}") + message = f"😔 获取活跃任务出错,请稍后再试!" + return {"message": message, "player_id": player_id} # 列举所有未完成任务 def list_uncompleted_tasks(group_id, player_id): - conn = get_db_connection() - cursor = conn.cursor() try: - cursor.execute(""" - SELECT a.active_task_id, a.question, p.player_name, p.player_id - FROM t_encyclopedia_active_tasks a - JOIN t_encyclopedia_players p ON a.holder_id = p.player_id AND a.group_id = p.group_id - WHERE a.group_id = %s AND a.status = 'pending' - """, (group_id,)) - tasks = cursor.fetchall() + # 获取未完成任务 + tasks = encyclopedia_db.get_active_tasks_in_group(group_id) + if not tasks: message = ( f"😄 群 {group_id} 全员开挂?\n" f"🌟 没未完成任务,快用 /t 再战!" ) return {"message": message, "player_id": player_id} + task_text = f"🎉 群 {group_id} 未完成任务大曝光:\n" for task in tasks: task_text += ( @@ -368,9 +315,10 @@ def list_uncompleted_tasks(group_id, player_id): f"🌼 主人:{task['player_name']}\n" ) return {"message": task_text, "player_id": player_id} - finally: - cursor.close() - conn.close() + except Exception as e: + print(f"列举未完成任务出错: {e}") + message = f"😔 获取未完成任务出错,请稍后再试!" + return {"message": message, "player_id": player_id} # 定时任务:整点触发,排除23:00-08:00 @@ -477,4 +425,4 @@ 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")) # 查看排行榜 \ No newline at end of file diff --git a/message_report/write_db.py b/message_report/write_db.py index 8e2c923..0b17816 100644 --- a/message_report/write_db.py +++ b/message_report/write_db.py @@ -1,82 +1,58 @@ -import schedule -import time +import logging from datetime import datetime, timedelta -import redis import pymysql +from db.connection import DBConnectionManager +from db.message_storage import MessageStorageDB -# 连接到Redis -r = redis.Redis(host='192.168.2.32', port=6379, db=0) - -# 配置数据库连接 -db_config = { - 'host': '192.168.2.32', # 替换为你的MariaDB服务器地址 - 'user': 'root', # 替换为你的MariaDB用户名 - 'password': 'lw123456', # 替换为你的MariaDB密码 - 'database': 'message_archive' -} - +# 获取数据库连接管理器的单例 +db_manager = DBConnectionManager() +message_db = MessageStorageDB(db_manager) def write_to_db(): - # 连接到数据库 - connection = pymysql.connect(**db_config) - # 获取当前日期的前一天 - yesterday = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d') - - # 遍历Redis中所有与昨天日期相关的key,并写入数据库 - for key in r.keys(f"*:*:{yesterday}:count"): - parts = key.decode('utf-8').split(':') - group_id, wx_id, _date = parts[0], parts[1], parts[2] # _date应该是yesterday,但这里为了完整性还是保留 - count = int(r.hget(key, 'count')) if isinstance(r.hget(key, 'count'), bytes) else 0 # 处理可能的None或空值情况 - # 插入消息信息 - try: - with connection.cursor() as cursor: - # 插入消息信息 - sql = """ - INSERT INTO speech_counts (group_id, wx_id, date, count) VALUES (%s, %s, %s, %s) - """ - cursor.execute(sql, (group_id, wx_id, yesterday, count)) - - # 提交事务 - connection.commit() - print("write_to_db successfully.") - - except Exception as e: - print(f"write_to_db message: {e}") - connection.rollback() - + """将消息统计写入数据库""" + try: + # 获取昨天的日期 + yesterday = (datetime.now() - timedelta(days=1)).strftime("%Y-%m-%d") + + # 获取昨天的消息统计 + message_counts = message_db.get_message_count_by_date(yesterday) + + if not message_counts: + logging.info(f"没有找到 {yesterday} 的消息记录") + return + + logging.info(f"成功统计 {yesterday} 的消息记录: {len(message_counts)} 条") + + # 这里可以添加其他处理逻辑,如发送统计报告等 + + except Exception as e: + logging.error(f"写入数据库出错: {e}") def generate_and_send_ranking(groupId, allContacts: dict): - # 连接到SQLite数据库(假设数据库文件名为database.db) - connection = pymysql.connect(**db_config) - cursor = connection.cursor() - - yesterday = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d') - # 编写SQL查询来获取发言数量前20的用户 - query = """ - SELECT wx_id, count AS speech_count - FROM speech_counts - WHERE date = %s - AND group_id = %s - GROUP BY wx_id - ORDER BY count DESC - LIMIT 20 - """ - - # 执行查询并获取结果 - cursor.execute(query, (yesterday,groupId,)) - results = cursor.fetchall() - - # 格式化输出字符串 - ranking_str = yesterday + "发言数量前20的用户排名:\n" - for rank, (username, speech_count) in enumerate(results, start=1): - ranking_str += f"{rank}. {allContacts.get(username, username)}: {speech_count} 次发言\n" - # 关闭数据库连接 - connection.close() - print(ranking_str) - # 这里我们没有实际“发送”排名,只是返回字符串 - # 如果需要发送,可以在此添加发送逻辑 - return ranking_str - + """生成并发送群聊发言排名""" + try: + yesterday = (datetime.now() - timedelta(days=1)).strftime('%Y-%m-%d') + + # 使用数据库操作类获取排名数据 + results = message_db.get_speech_ranking(yesterday, groupId, limit=20) + + if not results: + logging.info(f"没有找到 {yesterday} 的群聊 {groupId} 发言记录") + return f"{yesterday} 没有发言记录" + + # 格式化输出字符串 + ranking_str = yesterday + "发言数量前20的用户排名:\n" + for rank, result in enumerate(results, start=1): + username = result['wx_id'] + speech_count = result['count'] + ranking_str += f"{rank}. {allContacts.get(username, username)}: {speech_count} 次发言\n" + + logging.info(f"成功生成 {yesterday} 的群聊 {groupId} 发言排名") + return ranking_str + + except Exception as e: + logging.error(f"生成发言排名出错: {e}") + return f"生成发言排名出错: {e}" if __name__ == '__main__': write_to_db() diff --git a/message_sign/main.py b/message_sign/main.py index e72a093..099d27a 100644 --- a/message_sign/main.py +++ b/message_sign/main.py @@ -4,14 +4,17 @@ import mysql.connector.pooling import tomllib import pytz import redis -from typing import Optional, Tuple +from typing import Optional from wcferry import Wcf, WxMsg from message_util import MessageUtil from robot_cmd.robot_command import GroupBotManager, Feature, PermissionStatus +from db.connection import DBConnectionManager +from db.sign_in import SignInDB +from db.sign_in_redis import SignInRedisDB -# 创建表的SQL语句 +# 创建表的SQL语句保留在这里,用于初始化表 CREATE_TABLE_SQL = """ CREATE TABLE IF NOT EXISTS t_sign_record ( id BIGINT AUTO_INCREMENT PRIMARY KEY, @@ -45,8 +48,16 @@ class SignInSystem: self.gbm = gbm self.message_util = message_util self.all_contacts = all_contacts - self.db_pool = db_pool - self.redis_pool = redis_pool + + # 初始化数据库连接管理器 + self.db_manager = DBConnectionManager() + self.db_manager.mysql_pool = db_pool + self.db_manager.redis_pool = redis_pool + + # 初始化数据库操作类 + self.sign_in_db = SignInDB(self.db_manager) + self.sign_in_redis = SignInRedisDB(self.db_manager) + self.command = self.config['command'] self.min_point = self.config['min-point'] self.max_point = self.config['max-point'] @@ -56,49 +67,16 @@ class SignInSystem: self.timezone = 'Asia/Shanghai' # 从 Redis 初始化签到数据 - self.today_signin_count = self._load_signin_count_from_redis() - with self._get_redis_connection() as redis_client: - last_reset_date_str = redis_client.get('group:sign_in:last_reset_date') - if last_reset_date_str: - self.last_reset_date = datetime.strptime(last_reset_date_str, '%Y-%m-%d').date() - else: - self.last_reset_date = datetime.now(tz=pytz.timezone(self.timezone)).date() - self._save_last_reset_date_to_redis() + self.today_signin_count = self.sign_in_redis.load_signin_count() + last_reset_date = self.sign_in_redis.get_last_reset_date() + if last_reset_date: + self.last_reset_date = last_reset_date + else: + self.last_reset_date = datetime.now(tz=pytz.timezone(self.timezone)).date() + self.sign_in_redis.save_last_reset_date(self.last_reset_date) self.LOG.info(f"[签到] 组件初始化完成 {self.command_format}") - def _get_db_connection(self): - """从连接池获取数据库连接""" - return self.db_pool.get_connection() - - def _get_redis_connection(self): - """从连接池获取 Redis 连接""" - return redis.Redis(connection_pool=self.redis_pool) - - def _load_signin_count_from_redis(self) -> dict: - """从 Redis 加载签到人数数据""" - signin_count = {} - with self._get_redis_connection() as redis_client: - keys = redis_client.keys('group:sign_in:*') - for key in keys: - if key == 'group:sign_in:last_reset_date': - continue - group_id = key.replace('group:sign_in:', '') - count = redis_client.get(key) - if count is not None: - signin_count[group_id] = int(count) - return signin_count - - def _save_signin_count_to_redis(self, group_id: str, count: int): - """保存签到人数到 Redis""" - with self._get_redis_connection() as redis_client: - redis_client.set(f'group:sign_in:{group_id}', count) - - def _save_last_reset_date_to_redis(self): - """保存最后重置日期到 Redis""" - with self._get_redis_connection() as redis_client: - redis_client.set('group:sign_in:last_reset_date', self.last_reset_date.strftime('%Y-%m-%d')) - @property def command_format(self): return ','.join(self.command) @@ -109,23 +87,16 @@ class SignInSystem: def initialize_table(self): """初始化数据库表""" - with self._get_db_connection() as conn: - with conn.cursor(dictionary=True) as cursor: # 使用 dictionary=True 返回字典格式 - cursor.execute(CREATE_TABLE_SQL) - conn.commit() + self.sign_in_db.execute_update(CREATE_TABLE_SQL) def reset_today_count_if_needed(self): """检查并重置每日签到计数""" current_date = datetime.now(tz=pytz.timezone(self.timezone)).date() if current_date != self.last_reset_date: self.today_signin_count.clear() - with self._get_redis_connection() as redis_client: - keys = redis_client.keys('group:sign_in:*') - for key in keys: - if key != 'group:sign_in:last_reset_date': - redis_client.delete(key) + self.sign_in_redis.reset_daily_counts() self.last_reset_date = current_date - self._save_last_reset_date_to_redis() + self.sign_in_redis.save_last_reset_date(self.last_reset_date) self.LOG.info(f"[签到] 已重置每日签到计数,日期更新为 {current_date}") def get_today_signin_count(self, group_id: str) -> int: @@ -135,15 +106,7 @@ class SignInSystem: def get_user_record(self, wx_id: str, group_id: str) -> Optional[dict]: """获取用户签到记录""" - with self._get_db_connection() as conn: - with conn.cursor(dictionary=True) as cursor: - query = """ - SELECT wx_id, group_id, wx_nick_name, points, sign_stat, signin_streak - FROM t_sign_record - WHERE wx_id = %s AND group_id = %s - """ - cursor.execute(query, (wx_id, group_id)) - return cursor.fetchone() + return self.sign_in_db.get_user_record(wx_id, group_id) def calculate_points(self, streak: int) -> int: """根据连续签到天数计算积分""" @@ -214,46 +177,22 @@ class SignInSystem: today_signin_rank = self.get_today_signin_count(message.roomid) + 1 self.today_signin_count[message.roomid] = today_signin_rank - self._save_signin_count_to_redis(message.roomid, today_signin_rank) + self.sign_in_redis.save_signin_count(message.roomid, today_signin_rank) points_to_add = self.calculate_points(streak) - with self._get_db_connection() as conn: - with conn.cursor(dictionary=True) as cursor: - if user_record: - update_sql = """ - UPDATE t_sign_record - SET wx_nick_name = %s, points = points + %s, - sign_stat = %s, signin_streak = %s, - update_time = %s - WHERE wx_id = %s AND group_id = %s - """ - cursor.execute(update_sql, ( - wx_nick_name, points_to_add, current_time, streak, - current_time, message.sender, message.roomid - )) - else: - insert_sql = """ - INSERT INTO t_sign_record - (wx_id, group_id, wx_nick_name, points, sign_stat, signin_streak) - VALUES (%s, %s, %s, %s, %s, %s) - """ - cursor.execute(insert_sql, ( - message.sender, message.roomid, wx_nick_name, points_to_add, current_time, streak - )) - conn.commit() - # output = ("\n" - # f"-----Bot-----\n" - # f"@{wx_nick_name} 签到成功!\n" - # f"签到成功!你领到了 {points_to_add} 个积分!✅\n" - # f"你是今天第 {today_signin_rank} 个签到的!🎉\n") - # - # if streak_broken and old_streak > 0: # 只有在真的断签且之前有签到记录时才显示 - # output += f"你断开了 {old_streak} 天的连续签到![心碎]" - # elif streak > 1: - # output += f"你连续签到了 {streak} 天!" - # if streak > 1 and not streak_broken: - # output += "[爱心]" + # 使用数据库操作类更新或创建签到记录 + if user_record: + self.sign_in_db.update_sign_record( + message.sender, message.roomid, wx_nick_name, + points_to_add, current_time, streak + ) + else: + self.sign_in_db.create_sign_record( + message.sender, message.roomid, wx_nick_name, + points_to_add, current_time, streak + ) + output = f"签到成功,加[{points_to_add}]分,第[{today_signin_rank}]个!" if streak_broken and old_streak > 0: # 只有在真的断签且之前有签到记录时才显示 diff --git a/robot.py b/robot.py index d2bbc38..7dbf6a3 100644 --- a/robot.py +++ b/robot.py @@ -58,6 +58,7 @@ from xiuren.meitu_dl import meitu_dowload_pic, meitu_dowload_pub_pic, meitu_dowl from xiuren.random_pic import get_xiuren_pic, get_xiuren_heisi_pic from xiuren.xiuren_pdf import generate_pdf_from_images +from db.connection import DBConnectionManager from message_util import MessageUtil @@ -72,13 +73,16 @@ class Robot(Job): self.wxid = self.wcf.get_self_wxid() self.allContacts = self.get_all_contacts() self.LOG.info(f"DB+REDIS 连接池开始初始化") - # db 配置加载 - self.db_pool = mysql.connector.pooling.MySQLConnectionPool( - **self.config.mariadb # 解包字典,传入 host, user, password 等 + # 初始化数据库连接管理器 + self.db_manager = DBConnectionManager( + mysql_config=self.config.mariadb, + redis_config=self.config.redis ) - self.LOG.info(f"DB连接池加载完成: {self.config.mariadb}") - self.redis_pool = redis.ConnectionPool(**self.config.redis) - self.LOG.info(f"REDIS连接池加载完成: {self.config.redis}") + self.LOG.info(f"数据库连接管理器初始化完成") + + # 为了兼容现有代码,保留原有的连接池 + self.db_pool = self.db_manager.mysql_pool + self.redis_pool = self.db_manager.redis_pool # 初始化消息工具类 self.message_util = MessageUtil(wcf, self.allContacts)