-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreakodb.py
222 lines (196 loc) · 6.79 KB
/
breakodb.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
import sqlite3
class Db:
def __init__(self):
self.conn = sqlite3.connect('breakO.db')
self.userStats = None
def getUsers(self):
cur = self.conn.cursor()
cur.execute("select * from Users")
return cur.fetchall()
def getUser(self, idUser):
cur = self.conn.cursor()
cur.execute("select * from Users where idUser=(?)", (idUser, ))
return cur.fetchall()
def insertUser(self, userName):
cur = self.conn.cursor()
cur.execute("insert into Users (Name) values (?)", (userName, ))
self.conn.commit()
return cur.lastrowid
def getVisibleUsers(self):
cur = self.conn.cursor()
cur.execute("select * from Users WHERE Visible=1")
return cur.fetchall()
def newSession(self, idUser):
if idUser > 0:
cur = self.conn.cursor()
cur.execute("insert into Sessions (idUser) values (?)", (idUser, ))
self.conn.commit()
return cur.lastrowid
return 0
def endSession(self, idSession):
if (idSession >= 0):
cur = self.conn.cursor()
cur.execute("update Sessions set sessionEnd = dateTime('now') where "+
"idSession=(?)", (idSession, ))
self.conn.commit()
def newGame(self, idSession, abnormalEndId):
if idSession > 0:
cur = self.conn.cursor()
cur.execute("insert into Games (idSession, endTrigger) values (?, ?)",
(idSession, abnormalEndId))
self.conn.commit()
return cur.lastrowid
return 0
def endGame(self, idGame, level, endTrigger):
if (idGame >= 0):
cur = self.conn.cursor()
cur.execute(
"update Games set gameEnd = dateTime('now'), levelReached=(?), endTrigger=(?) " +
"where idGame=(?)", (level, endTrigger, idGame))
self.conn.commit()
# self.userStats = None
def newLevel(self, idGame, levelNumber):
if idGame > 0:
cur = self.conn.cursor()
cur.execute("insert into Levels (idGame, levelNumber) values (?, ?)",
(idGame, levelNumber))
self.conn.commit()
return cur.lastrowid
return 0
def endLevel(self, idLevel, maxBalls, pauseDuration):
if (idLevel >= 0):
cur = self.conn.cursor()
cur.execute(
"""
update Levels set levelEnd = dateTime('now'), maxBallsInPlay=?, pauseDuration=?
where idLevel=(?)
""", (maxBalls, pauseDuration, idLevel))
self.conn.commit()
self.userStats = None
def getEventDict(self):
cur = self.conn.cursor()
cur.execute("select * from EventTypes")
evDict = {}
for row in cur.fetchall():
evDict[row[1]] = (row[0], row[2])
return evDict
def saveEvents(self, idLevel, details):
sql = "insert into events (idLevel, idEventType, time) values (%d, ?, ?)" % idLevel
cur = self.conn.cursor()
cur.executemany(sql, details)
self.conn.commit()
def getUserStats(self, idUser):
if self.userStats is None:
sql = """
SELECT et.EventName, count(et.idEventType) Times from sessions s
inner join games g on g.idSession = s.idSession
inner join levels on levels.idGame = g.idGame
inner join Events e on e.idLevel = levels.idLevel
inner join EventTypes et on et.idEventType = e.idEventType
where s.idUser = (?)
GROUP BY e.idEventType;
"""
cur = self.conn.cursor()
cur.execute(sql, (idUser,))
self.userStats = cur.fetchall()
return self.userStats
def getXPDict(self, idUser):
xp={}
sql="SELECT count(s.idSession) from sessions s "
where="where idUser=(?) "
self.xpAdd(xp, sql,where, "Sessions", idUser)
sql += "inner join games g on g.idSession = s.idSession "
self.xpAdd(xp, sql,where, "Games", idUser)
sql += "inner join levels on levels.idGame = g.idGame "
where += "and Levels.levelEnd not null "
self.xpAdd(xp, sql,where, "Levels", idUser)
return xp
cur = self.conn.cursor()
tableName="Levels"
s = "select count(*) from %s where idUser=(?) and levelEnd not null" % tableName
cur.execute(s, (idUser,))
xp[tableName]=int(cur.fetchall()[0][0])
def xpAdd(self, xp, sql, where, tableName, idUser):
cur = self.conn.cursor()
s = sql + where
cur.execute(s, (idUser,))
xp[tableName]=int(cur.fetchall()[0][0])
def getConfigValue(self, setting):
cur = self.conn.cursor()
sql="SELECT SettingValue from Config WHERE Setting=(?)"
cur.execute(sql, (setting,))
return cur.fetchone()[0]
def setConfigValue(self, setting, value):
cur = self.conn.cursor()
sql="UPDATE Config SET SettingValue = (?) WHERE Setting=(?)"
cur.execute(sql, (value, setting))
self.conn.commit()
# Remove all users and game info from database, shrink db.
# BE CAREFUL WITH THIS. Consider making a copy of BreakO.db
# if it is not too large already.
# Note that NEITHER EventTypes table nor Config table is deleted or rebuilt
def restartDb (self):
cur = self.conn.cursor()
cur.execute("DROP TABLE Events;")
cur.execute("DROP TABLE Levels;")
cur.execute("DROP TABLE Games;")
cur.execute("""DROP TABLE Sessions;""")
cur.execute("""DROP TABLE Users;""")
cur.execute("""
CREATE TABLE Users
(
idUser INTEGER NOT NULL ,
Name TEXT NULL,
Visible BOOLEAN NULL DEFAULT 1 ,
PRIMARY KEY (idUser)
);""")
cur.execute("""CREATE TABLE Sessions
(
idSession INTEGER NOT NULL ,
idUser INTEGER NOT NULL ,
sessionStart TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
sessionEnd TIMESTAMP NULL ,
PRIMARY KEY (idSession) ,
FOREIGN KEY (idUser) REFERENCES Users (idUser)
);""")
cur.execute("""
CREATE TABLE Games
(
idGame INTEGER NOT NULL ,
idSession INTEGER NOT NULL ,
gameStart TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
gameEnd TIMESTAMP NULL ,
levelReached INTEGER ,
endTrigger INTEGER ,
PRIMARY KEY (idGame) ,
FOREIGN KEY (endTrigger) REFERENCES EventTypes (idEventType) ,
FOREIGN KEY (idSession) REFERENCES Sessions (idSession)
);""")
cur.execute("""
CREATE TABLE Levels
(
idLevel INTEGER NOT NULL ,
idGame INTEGER NOT NULL ,
levelStart TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
levelEnd TIMESTAMP NULL ,
levelNumber INTEGER NOT NULL ,
maxBallsInPlay INTEGER NULL ,
pauseDuration INTEGER NULL ,
PRIMARY KEY (idLevel) ,
FOREIGN KEY (idGame) REFERENCES Games (idGame)
);""")
cur.execute("""
CREATE TABLE Events
(
idEvent INTEGER NOT NULL ,
idEventType INTEGER NOT NULL ,
idLevel INTEGER NOT NULL ,
time TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (idEvent) ,
FOREIGN KEY (idEventType) REFERENCES EventTypes (idEventType) ,
FOREIGN KEY (idLevel) REFERENCES Levels (idLevel)
);""")
cur.execute("""INSERT INTO Users (Name) VALUES ('Guest');""")
cur.execute("""UPDATE Config SET SettingValue = ('1') WHERE Setting='lastIdUser'""")
self.conn.commit()
cur.execute("VACUUM")