feat: 抽卡签到功能 - 首次抽卡/三连自动签到获随机积分
- data_manager: 新增 daily_sign_in 表、has_signed_in_today、record_sign_in 方法 - utils: 新增 get_luck_description、format_sign_in_message 函数 - __init__: 新增 try_handle_daily_sign_in 签到入口 - handle_gacha/handle_triple_gacha 成功路径 finish()→send()+签到+return - 签到失败不影响抽卡主流程,UNIQUE约束防并发重复
This commit is contained in:
@@ -97,8 +97,23 @@ class DataManager:
|
||||
total_consecutive_days INTEGER DEFAULT 0
|
||||
)
|
||||
""")
|
||||
|
||||
self._init_sign_in_table(cursor)
|
||||
|
||||
conn.commit()
|
||||
|
||||
def _init_sign_in_table(self, cursor: sqlite3.Cursor) -> None:
|
||||
"""创建每日签到表"""
|
||||
cursor.execute("""
|
||||
CREATE TABLE IF NOT EXISTS daily_sign_in (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id TEXT NOT NULL,
|
||||
sign_date TEXT NOT NULL,
|
||||
points_awarded INTEGER NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
UNIQUE(user_id, sign_date)
|
||||
)
|
||||
""")
|
||||
|
||||
def update_achievement_progress(self, user_id: str, rarity: str) -> List[str]:
|
||||
"""更新用户成就进度,返回新解锁的成就列表"""
|
||||
@@ -321,6 +336,33 @@ class DataManager:
|
||||
def get_today_date(self) -> str:
|
||||
"""获取当前日期字符串"""
|
||||
return datetime.datetime.now().strftime("%Y-%m-%d")
|
||||
|
||||
def has_signed_in_today(self, user_id: str) -> bool:
|
||||
"""检查用户今天是否已签到"""
|
||||
today = self.get_today_date()
|
||||
with sqlite3.connect(config.DB_FILE) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(
|
||||
"SELECT 1 FROM daily_sign_in WHERE user_id = ? AND sign_date = ? LIMIT 1",
|
||||
(user_id, today),
|
||||
)
|
||||
return cursor.fetchone() is not None
|
||||
|
||||
def record_sign_in(self, user_id: str, points_awarded: int) -> bool:
|
||||
"""记录每日签到,重复签到返回False"""
|
||||
today = self.get_today_date()
|
||||
created_at = datetime.datetime.now().isoformat()
|
||||
try:
|
||||
with sqlite3.connect(config.DB_FILE) as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("""
|
||||
INSERT INTO daily_sign_in (user_id, sign_date, points_awarded, created_at)
|
||||
VALUES (?, ?, ?, ?)
|
||||
""", (user_id, today, points_awarded, created_at))
|
||||
conn.commit()
|
||||
return True
|
||||
except sqlite3.IntegrityError:
|
||||
return False
|
||||
|
||||
def get_current_time(self) -> str:
|
||||
"""获取当前时间字符串"""
|
||||
@@ -549,4 +591,4 @@ class DataManager:
|
||||
|
||||
# 更新成就进度
|
||||
unlocked_achievements = self.update_achievement_progress(user_id, rarity)
|
||||
return unlocked_achievements
|
||||
return unlocked_achievements
|
||||
|
||||
Reference in New Issue
Block a user