-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
66 lines (52 loc) · 1.75 KB
/
database.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import os
import sqlite3
if os.path.isfile("game_data.db"):
conn = sqlite3.connect("game_data.db")
print("Database exists. Skipping initial commands.")
cursor = conn.cursor()
else:
conn = sqlite3.connect("game_data.db")
cursor = conn.cursor()
statements = [
"""CREATE TABLE IF NOT EXISTS easy (
highest_score INTEGER,
current_tier INTEGER
);""",
"""CREATE TABLE IF NOT EXISTS medium (
highest_score INTEGER,
current_tier INTEGER
);""",
"""CREATE TABLE IF NOT EXISTS hard (
highest_score INTEGER,
current_tier INTEGER
);"""
]
for statement in statements:
cursor.execute(statement)
initial_values = [
("easy", 200, 0),
("medium", 200, 0),
("hard", 200, 0),
]
for level, score, tier in initial_values:
cursor.execute(f"""
INSERT INTO {level} (highest_score, current_tier)
VALUES (?, ?)
""", (score, tier))
conn.commit()
def get_highest_score(level):
"""Retrieves the highest score for the specified level."""
cursor.execute(f"SELECT highest_score FROM {level}")
return cursor.fetchone()[0]
def update_highest_score(level, new_score):
"""Updates the highest score for the specified level."""
cursor.execute(f"UPDATE {level} SET highest_score = ?", (new_score,))
conn.commit()
def get_current_tier(level):
"""Retrieves the current tier for the specified level."""
cursor.execute(f"SELECT current_tier FROM {level}")
return cursor.fetchone()[0]
def update_current_tier(level, new_tier):
"""Updates the current tier for the specified level."""
cursor.execute(f"UPDATE {level} SET current_tier = ?", (new_tier,))
conn.commit()