-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathdatabase.py
More file actions
150 lines (127 loc) · 4.34 KB
/
database.py
File metadata and controls
150 lines (127 loc) · 4.34 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
from sqlite3 import Error, connect
class Database:
def __init__(self):
self.filename = os.environ['DB_FILENAME']
# Initializes the database.
def initialize(self):
if not self.initialized():
print('Initializing database')
query = """CREATE TABLE solo (
'username' VARCHAR(45) NOT NULL DEFAULT '',
'high' INT unsigned DEFAULT NULL,
PRIMARY KEY ('username'));"""
self.executeQuery(query)
query = """CREATE TABLE duo (
'username' VARCHAR(45) NOT NULL DEFAULT '',
'high' INT UNSIGNED DEFAULT NULL,
PRIMARY KEY ('username'));"""
self.executeQuery(query)
else:
print('Database already initialized')
return True
# Decides if the database is initalized already.
def initialized(self):
resultSolo = self.executeQuery('SELECT name FROM sqlite_master WHERE type="table" AND name="solo";')
resultDuo = self.executeQuery('SELECT name FROM sqlite_master WHERE type="table" AND name="duo";')
if resultSolo != None and len(list(resultSolo)) == 1 and resultDuo != None and len(list(resultDuo)) == 1:
return True
else:
return False
# Executes a query into the database.
def executeQuery(self, query, tuple=()):
# Open a connection to the database.
try:
connection = connect(self.filename)
except Error as error:
print('DATABASE ERROR: could not open a connection to the database')
print(Error)
return None
# Execute the query.
try:
cursor = connection.cursor()
cursor.execute(query, tuple)
result = cursor.fetchall()
except Error as error:
print('DATABASE ERROR: could not execute the query')
print(Error)
return None
# Commit the changes and close the connection.
try:
connection.commit()
connection.close()
except Error as error:
print('DATABASE ERROR: could not close connection to the database')
print(Error)
return None
return result
# Prints the current status of the database.
def printStatus(self):
resultSolo = self.executeQuery("SELECT * FROM solo;")
resultDuo = self.executeQuery("SELECT * FROM duo;")
if resultSolo == None or resultDuo == None:
return False
print('DATABASE STATUS:')
print('Table solo:')
for row in resultSolo:
print(row)
print('Table duo:')
for row in resultDuo:
print(row)
# Get the current high score for the username and mode provided.
def getHighScore(self, username, mode):
# Execute query to the database.
if mode == 'solo':
query = "SELECT * FROM solo WHERE username=?;"
elif mode == 'duo':
query = "SELECT * FROM duo WHERE username=?;"
result = self.executeQuery(query, (username,))
# Check if the result obtained is valid.
if result == None:
print('DATABASE ERROR: could not query current high score of username {}, mode {}'.format(username, mode))
return None
# This username is not in the database.
if len(list(result)) == 0:
return None
else:
print(result)
return result[0][1]
# Function called when a username wants to submit a high score.
def submitHighScore(self, username, mode, score):
# Check that the arguments are valid.
if not (type(username) is str and type(mode) is str and type(score) is int):
print('DATABASE ERROR: invalid arguments to insert high score')
return False
# Query the high score in the database.
currentScore = self.getHighScore(username, mode)
# If user does not exist in database, create a new row.
if currentScore == None:
if mode == 'solo':
query = """INSERT INTO solo (username, high)
VALUES (?, ?);"""
elif mode == 'duo':
query = """INSERT INTO duo (username, high)
VALUES (?, ?);"""
result = self.executeQuery(query, (username, score,))
self.printStatus()
return True
# We have a high score for this user, check if he has obtained a higher score this time.
if score > currentScore:
if mode == 'solo':
query = 'UPDATE solo SET high=? WHERE username=?;'
elif mode == 'duo':
query = 'UPDATE duo SET high=? WHERE username=?;'
self.executeQuery(query, (score, username,))
self.printStatus()
return True
# New high score is not higher than the previous one.
self.printStatus()
return True
# Returns the requested scores of the selected mode.
def getScores(self, mode):
query = "SELECT * FROM " + mode + " ORDER BY high DESC;"
result = self.executeQuery(query)
if result == None:
print('DATABASE: cannot send back the scores')
print(Error)
return result