-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.py
238 lines (195 loc) · 8.91 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
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
227
228
229
230
231
232
233
234
235
236
237
#!/usr/bin/python3
import json
import sqlite3
import player
import os
import datetime
import Round
DATABASE_PLAYERS = "players.sqlite"
DATABASE_ROUNDS = "rounds.sqlite"
DATABASE_HISTORICAL = "players.sqlite"
class DatabaseConnection:
def __init__(self, basepath):
self.dbFormatString = "file:{}?mode=ro"
self.databasePlayers = self.dbFormatString.format(os.path.join(basepath, DATABASE_PLAYERS))
self.databaseRounds = self.dbFormatString.format(os.path.join(basepath, DATABASE_ROUNDS))
self.databaseHistorical = self.dbFormatString.format(
os.path.join(basepath, DATABASE_HISTORICAL))
self.connPlayers = sqlite3.connect(self.databasePlayers, uri=True)
self.connRounds = sqlite3.connect(self.databaseRounds, uri=True)
self.connHistorical = sqlite3.connect(self.databaseHistorical, uri=True)
def __del__(self):
self.connPlayers.close();
self.connRounds.close();
self.connHistorical.close();
def getTotalPlayers(self):
'''Get the total number of players in the database'''
cursor = self.connPlayers.cursor()
cursor.execute("SELECT Count(*) FROM players where games >= 10 and not lastgame is null")
count = cursor.fetchone()[0]
return count
def getHistoricalForPlayerId(self, playerId):
'''Get historical data for a player'''
cursor = self.connHistorical.cursor()
cursor.execute("SELECT * FROM playerHistoricalData where id = ? order by timestamp ASC", (playerId,))
rows = cursor.fetchall()
PLAYER_ID = 0
TIMESTAMP = 1
MU = 2
SIGMA = 3
playerIdDict = dict()
for r in rows:
timestampDict = dict()
timestampDict.update({ "mu" : r[MU] })
timestampDict.update({ "sigma" : r[SIGMA]})
playerIdDict.update({ r[TIMESTAMP] : timestampDict })
try:
retDict = { rows[0][PLAYER_ID] : playerIdDict }
except IndexError:
retDict = None
return retDict
def getPlayerById(self, playerId):
'''Get a player by his id'''
cursor = self.connPlayers.cursor()
cursor.execute("SELECT * FROM players where id = ?", (playerId,))
row = cursor.fetchone()
if(row):
playerInLeaderboard = player.PlayerInLeaderboard(row)
else:
playerInLeaderboard = None
return playerInLeaderboard
def getRankRange(self, start, end):
'''Get a range of players by rank'''
cursor = self.connPlayers.cursor()
limit = end - start
sqlQuery = '''Select * FROM players where games >= 10
and not lastgame is null
ORDER BY (mu - 2*sigma) DESC LIMIT ? OFFSET ?'''
cursor.execute(sqlQuery, (limit, start))
rows = cursor.fetchall()
playerList = []
for row in rows:
playerList += [player.PlayerInLeaderboard(row)]
return playerList
def findPlayerByName(self, playerName):
'''Find a player by his name (prefer fullmatch)'''
cursor = self.connPlayers.cursor()
playerNamePrepared = "%{}%".format(playerName.replace("%", "%%"))
cursor.execute("SELECT * FROM players WHERE name == ?", (playerName,))
row = cursor.fetchone()
# if there is exactly one hit for the exact name just return that #
if row and not cursor.fetchone():
p = player.PlayerInLeaderboard(row)
p.rank = self.getPlayerRank(p)
return [p]
playerRows = []
cursor.execute("SELECT * FROM players WHERE name LIKE ? ORDER BY games DESC", (playerNamePrepared,))
count = 0
for pr in cursor:
if count > 50:
break;
p = player.PlayerInLeaderboard(pr)
p.rank = self.getPlayerRank(p)
playerRows += [p]
count += 1
return playerRows
def getPlayerRank(self, player):
'''Calculate player rank - a player rank may change rapidly and
can't and shouldn't be used to identify a player'''
cursor = self.connPlayers.cursor()
if(player.games < 10):
return -1
cursor.execute('''SELECT COUNT(*) from players where games >= 10
and not lastgame is null
and (mu-2*sigma) > (?-2*?);''',
(player.mu, player.sigma))
rank = cursor.fetchone()[0]
return rank
def getLiveGames(self):
'''Get current live games'''
cursor = self.connPlayers.cursor()
cursor.execute('''SELECT * FROM live WHERE time > ? ORDER BY time DESC LIMIT 2''', (
(datetime.datetime.now() - datetime.timedelta(minutes=40)).timestamp(),))
liveRounds = []
for row in cursor:
trackingID, time, duration, players = row
insurgent = [] #2
security = [] #3
for p in json.loads(players):
if p["team"] == 2:
insurgent += [p]
elif p["team"] == 3:
security += [p]
p.update({"active_time":-1})
dbRow = [time, json.dumps(insurgent), json.dumps(security), -1, "N/A", duration, 0 ,0]
r = Round.Round(dbRow)
r.winners = [ self.getPlayerById(p.playerId) for p in r.winners]
r.losers = [ self.getPlayerById(p.playerId) for p in r.losers]
r.id = trackingID
liveRounds += [r]
return liveRounds
def roundsBetweenDates(self, start, end):
'''Get rounds played between two times'''
cursor = self.connRounds.cursor()
cursor.execute('''SELECT * FROM rounds WHERE timestamp between ? and ?
AND duration > 120.0
order by timestamp DESC''', (start.timestamp(), end.timestamp()))
rounds = []
for row in cursor:
rounds += [Round.Round(row)]
return rounds
def calcRatingChanges(self, roundObj):
'''Calculates and sets rating changes in the player objects of this round'''
cursorHist = self.connHistorical.cursor()
for p in roundObj.winners + roundObj.losers:
cursorHist.execute('''SELECT count(*) FROM playerHistoricalData
WHERE timestamp < ? AND id = ?''',
(roundObj.startTime.timestamp(), p.playerId))
if(cursorHist.fetchone()[0] < 10):
p.ratingChangeString = "Placements"
continue
cursorHist.execute('''SELECT mu,sima FROM playerHistoricalData
WHERE timestamp < ? AND id = ? order by timestamp DESC LIMIT 1 ''',
(roundObj.startTime.timestamp(), p.playerId))
tupelPrev = cursorHist.fetchone()
cursorHist.execute('''SELECT mu,sima FROM playerHistoricalData
WHERE timestamp == ? AND id = ? LIMIT 1''',
(roundObj.startTime.timestamp(), p.playerId))
tupelAfter = cursorHist.fetchone()
if tupelPrev and tupelAfter:
muPrev, sigmaPrev = tupelPrev
muAfter, sigmaAfter = tupelAfter
p.mu = muPrev
p.sigma = sigmaPrev
p.muChange = muAfter - muPrev
p.sigmaChange = sigmaAfter - sigmaPrev
ratingChange = int( (muAfter-muPrev) - 2*(sigmaAfter-sigmaPrev) )
if abs(ratingChange) > 500:
p.ratingChangeString = "N/A"
continue
if(ratingChange < 0):
p.ratingChangeString = "- {:x>5}".format(abs(ratingChange))
else:
p.ratingChangeString = "+ {:x>5}".format(ratingChange)
p.ratingChangeString = p.ratingChangeString.replace("x", " ")
roundObj.invalid = ""
roundObj.teamPtRatio = 0
if roundObj.teamPtRatio > 2.1:
roundObj.invalid += "Not rated because of playtime imbalance."
if roundObj.duration < datetime.timedelta(seconds=120):
if roundObj.invalid:
roundObj.invalid += "<br>"
roundObj.invalid += "Not rated because too short."
return roundObj
def getRoundByTimestamp(self, timestamp):
'''Get a round by it's start time (more or less it primary key)'''
cursorRounds = self.connRounds.cursor()
cursorRounds.execute('''SELECT * from rounds where timestamp = ?''', (timestamp,))
row = cursorRounds.fetchone()
if not row:
return None
return Round.Round(row)
def distinctMaps(self):
'''Get all distinct maps from rounds database'''
cursorRounds = self.connRounds.cursor()
return cursorRounds.execute('''SELECT DISTINCT map from rounds''')