-
Notifications
You must be signed in to change notification settings - Fork 1
/
Leaderboard.py
71 lines (62 loc) · 2.22 KB
/
Leaderboard.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
67
68
69
70
71
#Leaderboard code for magder
import random
import sqlite3
import os.path as os
leaderboardDatabase = "Leaderboard.db"
class Database:
def __init__(self):
if self._checkIfSQLDB(leaderboardDatabase) != True:
self.database = sqlite3.connect(leaderboardDatabase)
self._initialiseDatabase()
else:
self.database = sqlite3.connect(leaderboardDatabase)
def _initialiseDatabase(self):
databaseCursor = self.database.cursor()
databaseCursor.execute("""
CREATE TABLE Leaderboard(
GameID INTEGER PRIMARY KEY NOT NULL,
PlayerName VARCHAR(5) NOT NULL,
Time INTEGER NOT NULL
)
""")
databaseCursor.close()
self.database.commit()
def _checkIfSQLDB(self,fileName):
if not os.isfile(fileName):
print("File does not exist")
return False
if os.getsize(fileName) < 100:
print("File is not large enough to be a db")
return False
with open(fileName,"rb") as file:
header = file.read(100)
return header[:16] == b'SQLite format 3\x00'
def appendToLeaderboard(self,Name,Time):
databaseCursor = self.database.cursor()
while True:
try:
gameID = random.randint(0,999999)
databaseCursor.execute("INSERT INTO Leaderboard VALUES(?,?,?);",(gameID,Name,Time))
except sqlite3.IntegrityError:
pass
else:
databaseCursor.close()
self.database.commit()
break
def readLeaderboard(self):
databaseCursor = self.database.cursor()
databaseCursor.execute("SELECT * FROM Leaderboard;")
Data = databaseCursor.fetchall()
print(Data)
databaseCursor.close()
def deleteFromLeaderboard(self,ID):
databaseCursor = self.database.cursor()
databaseCursor.execute("DELETE FROM Leaderboard WHERE GameID=?",(ID,))
databaseCursor.close()
self.database.commit()
#Test lines to check if DB works
Leaderboard = Database()
Leaderboard.readLeaderboard()
ID = int(input("-->"))
Leaderboard.deleteFromLeaderboard(ID)
Leaderboard.readLeaderboard()