需求:1.加入了用户积分表;2.加入了指令积分扣除功能;3.加入了积分获得与扣除注解。

This commit is contained in:
liuwei
2025-04-09 11:54:03 +08:00
parent 9b8e0c3558
commit dba9c31504
31 changed files with 1435 additions and 527 deletions

View File

@@ -3,19 +3,22 @@ import logging
import pytz
from typing import Dict, Any, List, Optional, Tuple
from wcferry import Wcf, WxMsg
from wcferry import Wcf
from db.connection import DBConnectionManager
from message_util import MessageUtil
from plugin_common.message_plugin_interface import MessagePluginInterface
from plugin_common.plugin_interface import PluginStatus
from plugins.stats_collector.decorators import plugin_stats_decorator
from robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
from utils.decorator.plugin_decorators import plugin_stats_decorator
from utils.robot_cmd.robot_command import Feature, PermissionStatus, GroupBotManager
from db.sign_in import SignInDB
from db.sign_in_redis import SignInRedisDB
import random
import os
from utils.decorator.points_decorator import points_reward_decorator
class MessageSignPlugin(MessagePluginInterface):
"""签到插件"""
@@ -137,7 +140,47 @@ class MessageSignPlugin(MessagePluginInterface):
return command in self._commands
# 添加积分计算函数
def calculate_sign_in_points(self, message, success, response):
"""计算签到奖励积分
Args:
message: 消息内容
success: 处理结果
response: 响应内容
Returns:
int: 奖励积分数量
"""
sender = message.get("sender")
roomid = message.get("roomid", "")
# 获取当前时间,带有时区信息
current_time = datetime.now(tz=pytz.timezone(self.timezone))
today_start = current_time.replace(hour=0, minute=0, second=0, microsecond=0)
yesterday = today_start - timedelta(days=1)
# 获取用户的签到记录
user_record = self.get_user_record(sender, roomid)
# 计算连续签到天数
streak = 1
if user_record and user_record['sign_stat']:
last_sign_date = user_record['sign_stat'].replace(hour=0, minute=0, second=0, microsecond=0)
# 确保 sign_stat 和 today_start 是同一时区对象
if isinstance(last_sign_date, datetime) and last_sign_date.tzinfo is None:
last_sign_date = pytz.timezone(self.timezone).localize(last_sign_date)
if last_sign_date == yesterday:
streak = user_record['signin_streak'] + 1
# 计算积分
points = self.calculate_points(streak)
return points
# 修改process_message方法使用装饰器
@plugin_stats_decorator(plugin_name="签到系统")
@points_reward_decorator(calculate_sign_in_points, "checkin", "每日签到奖励")
def process_message(self, message: Dict[str, Any]) -> Tuple[bool, Optional[str]]:
"""处理消息"""
content = str(message.get("content", "")).strip()
@@ -156,31 +199,27 @@ class MessageSignPlugin(MessagePluginInterface):
try:
# 获取当前时间,带有时区信息
current_time = datetime.now(tz=pytz.timezone(self.timezone))
# 获取当天零点的时间
today_start = current_time.replace(hour=0, minute=0, second=0, microsecond=0)
# 获取昨天的时间
yesterday = today_start - timedelta(days=1)
# 获取用户的签到记录
user_record = self.get_user_record(sender, roomid)
wx_nick_name = all_contacts.get(sender, sender)
# 判断用户是否已经签到过
if user_record and user_record.get('sign_stat'):
sign_stat = user_record['sign_stat']
# 确保 sign_stat 和 today_start 是同一时区对象
if isinstance(sign_stat, datetime) and sign_stat.tzinfo is None:
sign_stat = pytz.timezone(self.timezone).localize(sign_stat)
# 如果 sign_stat 已经大于或等于今天的零点,则认为用户已经签到过了
if sign_stat >= today_start:
self.message_util.send_text_msg(f"您今天已经签到过了!当前积分:{user_record['points']}",
(roomid if roomid else sender), sender)
return True, "已签到"
streak = 0
streak_broken = False
old_streak = 1
@@ -200,28 +239,29 @@ class MessageSignPlugin(MessagePluginInterface):
streak_broken = True
else:
streak = 1
today_signin_rank = self.get_today_signin_count(roomid) + 1
self.today_signin_count[roomid] = today_signin_rank
self.sign_in_redis.save_signin_count(roomid, today_signin_rank)
points_to_add = self.calculate_points(streak)
# 使用数据库操作类更新或创建签到记录
# 注意:不再在这里计算和添加积分,由装饰器处理
points_to_add = self.calculate_points(streak)
if user_record:
self.sign_in_db.update_sign_record(
sender, roomid, wx_nick_name,
points_to_add, current_time, streak
points_to_add, # 不在这里添加积分,由装饰器处理
current_time, streak
)
else:
self.sign_in_db.create_sign_record(
sender, roomid, wx_nick_name,
points_to_add, current_time, streak
points_to_add, # 不在这里添加积分,由装饰器处理
current_time, streak
)
# 在签到成功后添加每日词汇
# 在输出信息中添加每日词汇
output = f"签到成功,加[{points_to_add}]分,第[{today_signin_rank}]个!"
output = f"签到成功,第[{today_signin_rank}]个!"
if streak_broken and old_streak > 0: # 只有在真的断签且之前有签到记录时才显示
output += f"断开了 {old_streak} 天连签!"
@@ -239,7 +279,7 @@ class MessageSignPlugin(MessagePluginInterface):
self.LOG.error(f"处理签到请求出错: {e}")
self.message_util.send_text_msg(f"签到出错:{e}",
(roomid if roomid else sender), sender)
return True, f"处理出错: {e}"
return False, f"处理出错: {e}"
def reset_today_count_if_needed(self):
"""检查并重置每日签到计数"""