diff --git a/python/backends/database.py b/python/backends/database.py index 1dd6210..535f79c 100644 --- a/python/backends/database.py +++ b/python/backends/database.py @@ -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 diff --git a/python/httpAPI.py b/python/httpAPI.py index cc2eec6..e201827 100644 --- a/python/httpAPI.py +++ b/python/httpAPI.py @@ -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: diff --git a/python/players.sqlite.init b/python/players.sqlite.init index 68af421..73fe62f 100644 --- a/python/players.sqlite.init +++ b/python/players.sqlite.init @@ -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);