优化签到天数。初始化为连签天数
This commit is contained in:
@@ -81,19 +81,44 @@ class SignInRedisDB:
|
||||
"""获取用户总签到次数的Redis键"""
|
||||
return f"{self.prefix}user_total:{wx_id}:{group_id}"
|
||||
|
||||
def increment_user_sign_count(self, wx_id: str, group_id: str) -> int:
|
||||
def increment_user_sign_count(self, wx_id: str, group_id: str, sign_in_db=None) -> int:
|
||||
"""增加用户签到总次数"""
|
||||
key = self.get_user_total_sign_count_key(wx_id, group_id)
|
||||
with self.get_redis_connection() as redis_client:
|
||||
# 先检查是否存在,如果不存在则初始化
|
||||
if not redis_client.exists(key) and sign_in_db:
|
||||
user_record = sign_in_db.get_user_record(wx_id, group_id)
|
||||
if user_record and user_record.get('signin_streak', 0) > 0:
|
||||
# 使用连签天数作为初始值
|
||||
initial_count = user_record['signin_streak']
|
||||
redis_client.set(key, initial_count)
|
||||
# 然后增加1
|
||||
count = redis_client.incr(key)
|
||||
return count
|
||||
|
||||
# 如果已存在或初始化失败,直接增加
|
||||
count = redis_client.incr(key)
|
||||
return count
|
||||
|
||||
def get_user_total_sign_count(self, wx_id: str, group_id: str) -> int:
|
||||
"""获取用户签到总次数"""
|
||||
def get_user_total_sign_count(self, wx_id: str, group_id: str, sign_in_db=None) -> int:
|
||||
"""获取用户签到总次数,如果Redis中没有数据则使用连签天数初始化"""
|
||||
key = self.get_user_total_sign_count_key(wx_id, group_id)
|
||||
with self.get_redis_connection() as redis_client:
|
||||
count = redis_client.get(key)
|
||||
return int(count) if count else 0
|
||||
if count:
|
||||
return int(count)
|
||||
|
||||
# Redis中没有数据,尝试从数据库初始化
|
||||
if sign_in_db:
|
||||
user_record = sign_in_db.get_user_record(wx_id, group_id)
|
||||
if user_record and user_record.get('signin_streak', 0) > 0:
|
||||
# 使用连签天数作为初始总签到次数
|
||||
initial_count = user_record['signin_streak']
|
||||
redis_client.set(key, initial_count)
|
||||
return initial_count
|
||||
|
||||
# 如果数据库也没有记录,返回0
|
||||
return 0
|
||||
|
||||
def set_user_total_sign_count(self, wx_id: str, group_id: str, count: int) -> bool:
|
||||
"""设置用户签到总次数"""
|
||||
|
||||
@@ -253,7 +253,7 @@ class MessageSignPlugin(MessagePluginInterface):
|
||||
# 如果 sign_stat 已经大于或等于今天的零点,则认为用户已经签到过了
|
||||
if sign_stat >= today_start:
|
||||
# 获取总签到次数(不增加)
|
||||
total_sign_count = self.sign_in_redis.get_user_total_sign_count(sender, roomid)
|
||||
total_sign_count = self.sign_in_redis.get_user_total_sign_count(sender, roomid, self.sign_in_db)
|
||||
client_msg_id, create_time, new_msg_id = await self.bot.send_text_message(
|
||||
(roomid if roomid else sender), f"您今天已经签到过了!\n总签到次数:{total_sign_count}次", sender)
|
||||
revoke.add_message_to_revoke(roomid, client_msg_id, create_time, new_msg_id, 4)
|
||||
@@ -326,7 +326,7 @@ class MessageSignPlugin(MessagePluginInterface):
|
||||
)
|
||||
|
||||
# 更新Redis中的签到总次数
|
||||
total_sign_count = self.sign_in_redis.increment_user_sign_count(sender, roomid)
|
||||
total_sign_count = self.sign_in_redis.increment_user_sign_count(sender, roomid, self.sign_in_db)
|
||||
|
||||
# 在输出信息中添加每日词汇和签到总次数
|
||||
output = f"签到成功,加[{points_to_add}]积分,第[{today_signin_rank}]个!"
|
||||
@@ -547,7 +547,7 @@ class MessageSignPlugin(MessagePluginInterface):
|
||||
)
|
||||
|
||||
# 更新Redis中的签到总次数
|
||||
total_sign_count = self.sign_in_redis.increment_user_sign_count(sender, roomid)
|
||||
total_sign_count = self.sign_in_redis.increment_user_sign_count(sender, roomid, self.sign_in_db)
|
||||
|
||||
# 发送成功消息
|
||||
success_message = f"✅ 补签成功!\n💰 消费 {self.makeup_cost} 积分\n"
|
||||
|
||||
Reference in New Issue
Block a user