Lowking commited on
Commit
9e5b3e5
·
verified ·
1 Parent(s): cf268d1

Update achievements.py

Browse files
Files changed (1) hide show
  1. achievements.py +36 -21
achievements.py CHANGED
@@ -1,24 +1,37 @@
1
- import sqlite3
2
  from datetime import datetime, timedelta
3
  import pytz
 
4
 
5
  tw_tz = pytz.timezone('Asia/Taipei')
6
 
 
 
 
 
7
  def update_user_stats_and_get_awards(user_id, db_file, action="chat"):
8
- conn = sqlite3.connect(db_file, timeout=20)
9
- c = conn.cursor()
 
 
 
 
10
  today_str = datetime.now(tw_tz).strftime('%Y-%m-%d')
11
  yesterday_str = (datetime.now(tw_tz) - timedelta(days=1)).strftime('%Y-%m-%d')
12
  awards = []
13
 
14
  try:
15
- c.execute("SELECT count, streak, last_active_date FROM users WHERE user_id = ?", (user_id,))
16
- row = c.fetchone()
17
- if not row: return []
 
18
 
19
- current_count, current_streak, last_active_date = row
 
 
 
20
 
21
- # 1. 互動與連續天數結算
22
  if action == "chat":
23
  current_count += 1
24
  if last_active_date == yesterday_str:
@@ -26,30 +39,31 @@ def update_user_stats_and_get_awards(user_id, db_file, action="chat"):
26
  elif last_active_date != today_str:
27
  current_streak = 1
28
 
29
- c.execute("UPDATE users SET count = ?, streak = ?, last_active_date = ? WHERE user_id = ?",
30
- (current_count, current_streak, today_str, user_id))
31
- conn.commit()
 
 
 
32
 
33
- # 2. 算反饋次數
34
- c.execute("SELECT COUNT(*) FROM user_surveys WHERE user_id = ? AND suggestion IS NOT NULL", (user_id,))
35
- feedback_count = c.fetchone()[0]
36
 
37
- # 3. 檢查級距 (改成字典格式)
38
  # 🥇 勤學獎 (天數) -> 綠色
39
  if action == "chat" and last_active_date != today_str:
40
  s_m = {1: "🐣【破殼而出】", 3: "🥉【初燃火種】", 7: "🥈【週日守候】", 15: "🥇【半月恆心】", 30: "💎【月滿族語】",
41
  60: "🌊【雙月波瀾】", 100: "⛰️【百日築基】", 200: "⚔️【堅韌意志】", 300: "☀️【歲月傳人】"}
42
  if current_streak in s_m:
43
- # 💡 針對第 1 天給予專屬的歡迎詞
44
  desc = "踏出學習的第一步,歡迎加入族語的大家庭!" if current_streak == 1 else f"連續堅持 {current_streak} 天,族語火種永不熄滅!"
45
  awards.append({"type": "勤學獎", "title": s_m[current_streak], "desc": desc})
46
 
47
  # 🏹 積極獎 (次數) -> 黑色
48
  if action == "chat":
49
  c_m = {1: "💬【初次發聲】", 10: "🌱【初試啼聲】", 30: "🍃【語感啟蒙】", 50: "🦌【林間尋蹤】", 100: "🏹【語林勇士】",
50
- 300: "🦅【文化獵人】", 500: "🗺️【部落嚮導】", 700: "🧭【語海導航】", 1000: "👑【傳奇語者】"}
51
  if current_count in c_m:
52
- # 💡 針對第 1 次互動給予專屬的鼓勵詞
53
  desc = "勇敢說出第一句族語,您的聲音已經被聽見了!" if current_count == 1 else f"累積互動 {current_count} 次,您的聲音已被大家看見!"
54
  awards.append({"type": "積極獎", "title": c_m[current_count], "desc": desc})
55
 
@@ -58,9 +72,10 @@ def update_user_stats_and_get_awards(user_id, db_file, action="chat"):
58
  f_m = {1: "💌【首份心意】", 10: "🔍【細心觀察家】", 30: "🪓【磨刀尖兵】", 50: "🧶【編織能手】", 100: "🛡️【族語護衛】",
59
  300: "🌾【智慧播種者】", 500: "💡【指路明燈】", 700: "🧱【文化奠基石】", 1000: "🧬【語脈建築師】"}
60
  if feedback_count in f_m:
61
- # 💡 針對第 1 次建議給予專屬的感謝詞
62
  desc = "送出第一份寶貴建議,您是幫助 AI 成長的重要推手!" if feedback_count == 1 else f"您已提供 {feedback_count} 次建議,是 AI 精進的重要養分!"
63
  awards.append({"type": "反饋獎", "title": f_m[feedback_count], "desc": desc})
64
- except Exception as e: print(f"Achievement Error: {e}")
65
- finally: conn.close()
 
 
66
  return awards
 
1
+ import os
2
  from datetime import datetime, timedelta
3
  import pytz
4
+ from supabase import create_client, Client
5
 
6
  tw_tz = pytz.timezone('Asia/Taipei')
7
 
8
+ # 💡 初始化 Supabase 雲端連線
9
+ SUPABASE_URL = os.getenv("SUPABASE_URL", "")
10
+ SUPABASE_KEY = os.getenv("SUPABASE_KEY", "")
11
+
12
  def update_user_stats_and_get_awards(user_id, db_file, action="chat"):
13
+ # 備註:db_file 參數保留是為了不改動 app.py 的呼叫方式,但實際已不使用
14
+ if not SUPABASE_URL or not SUPABASE_KEY:
15
+ print("Achievement Error: 找不到 Supabase 金鑰")
16
+ return []
17
+
18
+ supabase: Client = create_client(SUPABASE_URL, SUPABASE_KEY)
19
  today_str = datetime.now(tw_tz).strftime('%Y-%m-%d')
20
  yesterday_str = (datetime.now(tw_tz) - timedelta(days=1)).strftime('%Y-%m-%d')
21
  awards = []
22
 
23
  try:
24
+ # 💡 1. 從雲端獲取使用者狀態
25
+ res = supabase.table("users").select("count, streak, last_active_date").eq("user_id", user_id).execute()
26
+ if not res.data:
27
+ return []
28
 
29
+ row = res.data[0]
30
+ current_count = row.get("count") or 0
31
+ current_streak = row.get("streak") or 0
32
+ last_active_date = row.get("last_active_date")
33
 
34
+ # 💡 2. 互動與連續天數結算 (並更新上雲端)
35
  if action == "chat":
36
  current_count += 1
37
  if last_active_date == yesterday_str:
 
39
  elif last_active_date != today_str:
40
  current_streak = 1
41
 
42
+ # 將最新次數與天數寫回雲端金庫
43
+ supabase.table("users").update({
44
+ "count": current_count,
45
+ "streak": current_streak,
46
+ "last_active_date": today_str
47
+ }).eq("user_id", user_id).execute()
48
 
49
+ # 💡 3. 從雲端精算反饋次數
50
+ fb_res = supabase.table("user_surveys").select("id", count="exact").eq("user_id", user_id).not_is("suggestion", "null").execute()
51
+ feedback_count = fb_res.count or 0
52
 
53
+ # 💡 4. 檢查級距與派發獎狀
54
  # 🥇 勤學獎 (天數) -> 綠色
55
  if action == "chat" and last_active_date != today_str:
56
  s_m = {1: "🐣【破殼而出】", 3: "🥉【初燃火種】", 7: "🥈【週日守候】", 15: "🥇【半月恆心】", 30: "💎【月滿族語】",
57
  60: "🌊【雙月波瀾】", 100: "⛰️【百日築基】", 200: "⚔️【堅韌意志】", 300: "☀️【歲月傳人】"}
58
  if current_streak in s_m:
 
59
  desc = "踏出學習的第一步,歡迎加入族語的大家庭!" if current_streak == 1 else f"連續堅持 {current_streak} 天,族語火種永不熄滅!"
60
  awards.append({"type": "勤學獎", "title": s_m[current_streak], "desc": desc})
61
 
62
  # 🏹 積極獎 (次數) -> 黑色
63
  if action == "chat":
64
  c_m = {1: "💬【初次發聲】", 10: "🌱【初試啼聲】", 30: "🍃【語感啟蒙】", 50: "🦌【林間尋蹤】", 100: "🏹【語林勇士】",
65
+ 300: "🦅【文化 সাংস্কৃতিক獵人】", 500: "🗺️【部落嚮導】", 700: "🧭【語海導航】", 1000: "👑【傳奇語者】"}
66
  if current_count in c_m:
 
67
  desc = "勇敢說出第一句族語,您的聲音已經被聽見了!" if current_count == 1 else f"累積互動 {current_count} 次,您的聲音已被大家看見!"
68
  awards.append({"type": "積極獎", "title": c_m[current_count], "desc": desc})
69
 
 
72
  f_m = {1: "💌【首份心意】", 10: "🔍【細心觀察家】", 30: "🪓【磨刀尖兵】", 50: "🧶【編織能手】", 100: "🛡️【族語護衛】",
73
  300: "🌾【智慧播種者】", 500: "💡【指路明燈】", 700: "🧱【文化奠基石】", 1000: "🧬【語脈建築師】"}
74
  if feedback_count in f_m:
 
75
  desc = "送出第一份寶貴建議,您是幫助 AI 成長的重要推手!" if feedback_count == 1 else f"您已提供 {feedback_count} 次建議,是 AI 精進的重要養分!"
76
  awards.append({"type": "反饋獎", "title": f_m[feedback_count], "desc": desc})
77
+
78
+ except Exception as e:
79
+ print(f"Achievement Error: {e}")
80
+
81
  return awards