implement live game logging

This commit is contained in:
2020-12-22 11:13:01 +01:00
parent f1746445b7
commit 09772130c0
3 changed files with 40 additions and 0 deletions

View File

@@ -36,6 +36,41 @@ def check():
else:
return (count, avgPred)
def logLiveState(jsonDict, trackingId):
'''Log live state for leaderboard'''
conn = sqlite3.connect(DATABASE)
cursor = conn.cursor()
cursor.execute("SELECT time FROM live WHERE id = ?", (trackingId,))
row = cursor.fetchone()
if row:
startTime = dt.datetime.fromtimestamp(int(row[0]))
duration = dt.datetime.now() - startTime
# check age of entry #
if duration < dt.timedelta(minutes=60):
cursor.execute("UPDATE live SET duration = ?, players = ? WHERE id = ?", (
int(duration.total_seconds()),
json.dumps(jsonDict["players"]),
trackingId))
conn.commit()
conn.close()
return
else:
cursor.execute("DELETE FROM live WHERE id = ?", (trackingId,))
startTime = dt.datetime.now()
duration = dt.timedelta(0)
cursor.execute("INSERT INTO live VALUES (?,?,?,?)", (
trackingId,
int(startTime.timestamp()),
int(duration.total_seconds()),
json.dumps(jsonDict["players"])))
conn.commit()
conn.close()
def logHistoricalData(player, timestamp):
if not timestamp:
return

View File

@@ -102,6 +102,10 @@ def singleEvent():
print("Removed orphaned session file: {}".format(fullPath), file=sys.stderr)
os.remove(fullPath)
# update live stats #
if jsonDict["etype"] == "active_players":
db.logLiveState(jsonDict, session)
if jsonDict["etype"] == ROUND_END_IDENT:
events = []
with open(fullPath, "r") as f:

View File

@@ -8,3 +8,4 @@ CREATE TABLE players(
games INTEGER);
create TABLE playerHistoricalData (id TEXT, timestamp TEXT, mu REAL, sima REAL);
create TABLE live (id text, time INTEGER, duration INTEGER, players TEXT);