Reworked 'Levels' for progress
This commit is contained in:
51
database.py
51
database.py
@@ -83,10 +83,19 @@ class Database:
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
height_cm REAL,
|
||||
goal_weight_kg REAL,
|
||||
total_xp INTEGER DEFAULT 0,
|
||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
''')
|
||||
|
||||
# Unlocked Achievements Table
|
||||
cursor.execute('''
|
||||
CREATE TABLE IF NOT EXISTS unlocked_achievements (
|
||||
achievement_id TEXT PRIMARY KEY,
|
||||
unlocked_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
''')
|
||||
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
@@ -97,16 +106,56 @@ class Database:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT * FROM user_profile WHERE id = 1")
|
||||
row = cursor.fetchone()
|
||||
|
||||
# Ensure total_xp exists (migration for existing db)
|
||||
if row and 'total_xp' not in row.keys():
|
||||
cursor.execute("ALTER TABLE user_profile ADD COLUMN total_xp INTEGER DEFAULT 0")
|
||||
conn.commit()
|
||||
cursor.execute("SELECT * FROM user_profile WHERE id = 1")
|
||||
row = cursor.fetchone()
|
||||
|
||||
conn.close()
|
||||
return dict(row) if row else None
|
||||
|
||||
def save_user_profile(self, height_cm: float, goal_weight_kg: float):
|
||||
conn = self.get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("INSERT OR REPLACE INTO user_profile (id, height_cm, goal_weight_kg) VALUES (1, ?, ?)", (height_cm, goal_weight_kg))
|
||||
# Check if exists to preserve XP
|
||||
cursor.execute("SELECT total_xp FROM user_profile WHERE id = 1")
|
||||
row = cursor.fetchone()
|
||||
current_xp = row[0] if row else 0
|
||||
|
||||
cursor.execute("INSERT OR REPLACE INTO user_profile (id, height_cm, goal_weight_kg, total_xp) VALUES (1, ?, ?, ?)", (height_cm, goal_weight_kg, current_xp))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def add_xp(self, amount: int):
|
||||
conn = self.get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("UPDATE user_profile SET total_xp = total_xp + ? WHERE id = 1", (amount,))
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
def unlock_achievement(self, achievement_id: str):
|
||||
conn = self.get_connection()
|
||||
cursor = conn.cursor()
|
||||
try:
|
||||
cursor.execute("INSERT INTO unlocked_achievements (achievement_id) VALUES (?)", (achievement_id,))
|
||||
conn.commit()
|
||||
return True # Newly unlocked
|
||||
except sqlite3.IntegrityError:
|
||||
return False # Already unlocked
|
||||
finally:
|
||||
conn.close()
|
||||
|
||||
def get_achievements(self) -> List[str]:
|
||||
conn = self.get_connection()
|
||||
cursor = conn.cursor()
|
||||
cursor.execute("SELECT achievement_id FROM unlocked_achievements")
|
||||
rows = cursor.fetchall()
|
||||
conn.close()
|
||||
return [r[0] for r in rows]
|
||||
|
||||
# --- Daily Log Operations ---
|
||||
def save_daily_log(self, date: str, data: Dict[str, Any]):
|
||||
"""Insert or update a daily log."""
|
||||
|
||||
Reference in New Issue
Block a user