-
Notifications
You must be signed in to change notification settings - Fork 0
/
gamedb.py
226 lines (205 loc) · 8.5 KB
/
gamedb.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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
"""
GameDB. A platform to manage video games and platforms
Author: Francesco Tattoli
Creation Date: 15-03-2021
"""
import sqlite3
import sys
from prettytable import PrettyTable
from prettytable import from_db_cursor
#Create SQLite3 connection and cursor
con = sqlite3.Connection(".\GamesDB.sqlite")
def gameinsert():
#Choice #1 of the main menu. Inserting a new game into the library
print("-" * 40)
print(" INSERTING A NEW GAME ")
print("-" * 40)
#Gathering data to store in the Library
gamename = input("Game Name: ")
platform1 = input("First Platform Name: ")
platform2 = input("Second Platform Name: ")
platform3 = input("Third Platform Name: ")
platform4 = input("Fourth Platform Name: ")
platform5 = input("Fifth Platform Name: ")
metascore = input("Metascore: ")
#If user chooses not to input a Metascore a default of 0 is assigned
if metascore == "":
metascore = "0"
#Prompt the user to select whether it's a DLC entry or not. For future printing
isdlc = input("Is a DLC (Y/N) ? Default (N): ")
#By default, if user doesn't type anything at the DLC prompt, default N is assumed
if isdlc == "":
isdlc = "N"
#All done. Let's save data into db
try:
with con:
cur = con.cursor()
cur.execute("INSERT INTO GameLibrary(GameName, PlatformName1, PlatformName2, PlatformName3, PlatformName4, PlatformName5, Metascore,IsDLC) \
VALUES(?,?,?,?,?,?,?,?)", (gamename, platform1, platform2, platform3, platform4, platform5, metascore, isdlc))
con.commit()
print("All done.")
print("Do you want to insert another game (y/Y) or exit to main menu (n/N)")
choice = input("Y/N :")
if choice == "Y" or choice == "y":
gameinsert()
else:
main()
except sqlite3.Error as e:
print("Database Error. {}".format(e))
main()
def platforminsert():
#Choice #2 of the main menu. Inserting a new platform with details into the library
#Pretty similar to function #1. NO PASSWORD WILL BE STORED (EVER).
#This is just to recall which game on which platform user own games
print("-" * 40)
print(" INSERTING A NEW PLATFORM ")
print("-" * 40)
#Gathering data to store in the DB
platformname = input("Platform Name: ")
platformaccount = input("Platform Account Name: ")
#All done. Let's save data into db
try:
with con:
cur = con.cursor()
cur.execute("INSERT INTO PlatformDetails(PlatformName, PlatformAccount) \
VALUES(?,?)", (platformname, platformaccount))
con.commit()
print("All done.")
print("Do you want to insert another platform (y/Y) or exit to main menu (n/N)")
choice = input("Y/N :")
if choice == "Y" or choice == "y":
platforminsert()
else:
main()
except sqlite3.Error as e:
print("Database Error. {}".format(e))
main()
def printgamesdb():
#Choice #3 of the main menu. Printing games library ordered by Platform
print("-" * 40)
print(" PRINTING GAMES LIBRARY ")
print(" Ordered by Platform Name")
print("-" * 40)
with con:
cur = con.cursor()
#Prompt the user whether wants to include DLCs in the report
includedlc = input("Do you want to include DLCs in the report (Y/N)?: ")
if includedlc == "y" or includedlc == "Y":
stringsql = ("SELECT * FROM GameLibrary ORDER By PlatformName1")
cur.execute(stringsql)
elif includedlc == "n" or includedlc == "N":
stringsql = ("SELECT * FROM GameLibrary WHERE IsDLC = 'n' or IsDLC = 'Y' ORDER BY PlatformName1")
cur.execute(stringsql)
else:
print("Unrecognized choice.")
printgamesdb()
tb = from_db_cursor(cur)
tb.field_names = ["Game ID","Game Name","Platform #1","Platform #2","Platform #3","Platform #4","Platform #5","Metascore", "DLC"]
tb.align["Metascore"] = "r"
print(tb.get_string(title="Your Games Library ordered by Platform"))
_ = input("Press a key to exit")
main()
def printplatformdb():
#Choice #4 of the main menu. Printing platform details
print("-" * 40)
print(" PRINTING PLATFORM LIBRARY ")
print(" AND ACCOUNT DETAILS ")
print("-" * 40)
with con:
cur = con.cursor()
cur.execute("SELECT * FROM PlatformDetails")
tb = from_db_cursor(cur)
tb.field_names = ["Platform", "Account Name"]
print(tb.get_string(title="Your Platform Library with Account Details"))
_ = input("Press a key to exit")
main()
def searchgamebyname():
print("-" * 40)
print(" SEARCH GAME BY ITS NAME ")
print("-" * 40)
gamename = input("Type some character of the game you want to search: ")
if gamename == "": #Nothing typed. Showing the whole game library
with con:
cur = con.cursor()
cur.execute("SELECT * FROM GameLibrary")
tb = from_db_cursor(cur)
tb.field_names = ["Game ID","Game Name","Platform #1","Platform #2","Platform #3","Platform #4","Platform #5","Metascore", "DLC"]
tb.align["Metascore"] = "r"
print(tb.get_string(title="Games Found"))
_ = input("Press a key to exit")
main()
else:
with con:
cur = con.cursor()
cur.execute("SELECT * FROM GameLibrary WHERE GameName LIKE ?", ('%{}%'.format(gamename),))
tb = from_db_cursor(cur)
tb.field_names = ["Game ID","Game Name","Platform #1","Platform #2","Platform #3","Platform #4","Platform #5","Metascore", "DLC"]
tb.align["Metascore"] = "r"
print(tb.get_string(title="Games Found"))
_ = input("Press a key to exit")
main()
def searchplatformbyname():
print("-" * 40)
print(" SEARCH PLATFORM BY ITS NAME ")
print("-" * 40)
platformname = input("Type some character of the platform you want to search: ")
if platformname == "": #Nothing typed. Showing the whole platform library
with con:
cur = con.cursor()
cur.execute("SELECT * FROM PlatformDetails")
tb = from_db_cursor(cur)
tb.field_names = ["Platform Name", "Platform Account"]
print(tb.get_string(title="Match(es) Found"))
_ = input("Press a key to exit")
main()
else:
with con:
cur = con.cursor()
cur.execute("SELECT * FROM PlatformDetails WHERE PlatformName LIKE ?", ('%{}%'.format(platformname),))
tb = from_db_cursor(cur)
tb.field_names = ["Platform Name", "Platform Account"]
print(tb.get_string(title="Match(es) Found"))
_ = input("Press a key to exit")
main()
def main():
print("-" * 40)
print(" THE GAMES LIBRARY ")
print("-" * 40)
mainchoice = ["1", "2", "3", "4", "5", "6", "9"] #List containing all the valid menu entries
print("SELECT FUNCTION:")
print("1 - Insert a Game")
print("2 - Insert a Platform/Account")
print("3 - Print Games Library")
print("4 - Print Platform Details")
print("5 - Search Game by Name")
print("6 - Search Platform by Name")
print("9 - Exit")
mychoice = input("Choose a function :")
if mychoice in mainchoice:
if mychoice == "1":
#User chose to insert a new game into the library
gameinsert()
elif mychoice == "2":
#User chose to insert a new platform and account details
platforminsert()
elif mychoice == "3":
#User chose to print Games Library
printgamesdb()
elif mychoice == "4":
#User chose to print Platform Library
printplatformdb()
elif mychoice == "5":
#User chose to search a Game by its name
searchgamebyname()
elif mychoice == "6":
#User chose to search a Platform and its details by Platform name
searchplatformbyname()
elif mychoice == "9":
#User chose to exit the application
sys.exit()
else:
#Unidentified command. Sending user back to main menu
print("Unrecognized function.")
main()
if __name__ == "__main__":
main()