merged branch 'speed-records', squashed

This commit is contained in:
2024-10-18 21:21:02 -07:00
parent ded4e29594
commit 4d1816e0ce
11 changed files with 275 additions and 79 deletions

View File

@ -1,6 +1,13 @@
import sqlite3 as sql
import subprocess, traceback
# import contextlib
#
import sys, io, os
# import ctypes
# colors = ctypes.CDLL('./colors.so')
# colors.colorize_name.argtypes = (ctypes.char_p, ctypes.int, ctypes.char_p)
# get all maps in database
def getmaps(database):
output = []
@ -15,26 +22,50 @@ def getmaps(database):
return output
# if there is no query then it outputs the index file.
def getcontent(query=None):
def run_cgi(query=None):
cmd = [("./cts")]
proc = subprocess.Popen(cmd, env=query, stdout=subprocess.PIPE, shell=True)
# communicate returns 'bytes' class with function 'decode'
return proc.communicate()[0].decode('utf-8')
def renderindex(template):
# no env variable
table = getcontent()
filename = "./output/index.html"
with open(filename, 'w+') as fout:
fout.write(template % (table))
fout.close
pass
def run_colors(player_name):
ret = player_name
result = subprocess.run(['./colors', player_name], capture_output=True, text=True)
if result.returncode == 0:
ret = result.stdout
return ret
def get_speed_record(database, map_id):
message = "{name} traveled the fastest at {speed} qu/s."
query = str()
result = []
with open("queries/fastest-player-of-map.sql") as f:
query = f.read()
# q = query.replace('?', map_id)
# print(q)
with sql.connect(database) as con:
cursor = con.cursor()
try:
cursor.execute(query, (map_id,))
result = cursor.fetchall()
except sql.Error:
pass
player_name = result[0][1]
colored = (run_colors(player_name)).strip()
velocity = round(result[0][0], 2)
return message.format(name=colored, speed=velocity)
def main():
template = ""
with open("overview.html", 'r') as fin:
template = fin.read()
renderindex(template)
with open("output/index.html", 'w') as fout:
fout.write(template % run_cgi())
# use same template for fastest-players
query = {"QUERY_STRING" : "fastest-players"}
with open("output/fastest-players.html", 'w') as fout:
fout.write(template % run_cgi(query))
maps = getmaps("db/cts.db")
with open("map.html", 'r') as fin:
template = fin.read()
@ -43,14 +74,16 @@ def main():
# game_map is a tuple obj.
map_name = game_map[0]
query = {"QUERY_STRING" : ("map=%s" % map_name)}
table = getcontent(query)
filename = ("./output/maps/%s.html" % map_name)
filename = ("output/maps/%s.html" % map_name)
sentence = get_speed_record("db/cts.db", map_name)
with open(filename, 'w+') as fout:
title = map_name
fout.write(template.format(
title=title,
map_name=map_name,
table=table)
table=run_cgi(query),
speed=sentence
)
)
# fout.write(template % (title, map_name, table))
return True

View File

@ -85,9 +85,13 @@ def uid2namefix(row):
# O(n) and organize cts related data into list of rows.
def filters(db):
tt = []
tr = []
ti = []
tt = [] # time (seconds)
tr = [] # ranks
ti = [] # id
# xonotic only stores one player per map
# for speed records (fastest player only)
s = [] # speed
sid = [] # speed id
rank_index = 2
for d in db:
if d.find("uid2name") != -1:
@ -99,15 +103,20 @@ def filters(db):
if d.find("cts100record/time") != -1:
e[rank_index] = int(e[rank_index].replace("time", ""))
tt.append(e)
if d.find("cts100record/crypto_idfp") != -1:
elif d.find("cts100record/crypto_idfp") != -1:
e[3] = unquote(e[3])
e[rank_index] = int(e[rank_index].replace("crypto_idfp", ""))
tr.append(e)
if d.find("cts100record/speed") != -1:
# print(d)
# speed records - not implemented
pass
return tt, tr, ti
elif d.find("cts100record/speed/speed") != -1:
# example:
# ['zeel-omnitek', 'cts100record', 'speed', 'speed', '1584.598511']
# --- note, index 1, 2, 3 are unneeded
s.append([ e[0], unquote(e[-1]) ])
elif d.find("cts100record/speed/crypto_idfp") != -1:
# example:
# ['minideck_cts_v4r4', 'cts100record', 'speed', 'crypto_idfp', 'duHTyaSGpdTk7oebwPFoo899xPoTwP9bja4DUjCjTLo%3D']
sid.append([ e[0], unquote(e[-1]) ])
return tt, tr, ti, s, sid
#------------------------------------------------+
# Functions: Database Creation
@ -133,7 +142,7 @@ def i(d, s):
with con:
csr = con.cursor()
try:
times, ranks, ids = filters(get_list_from_server_txt(s))
times, ranks, ids, speed, speed_ids = filters(get_list_from_server_txt(s))
if times:
inserttodb(csr, "INSERT OR REPLACE INTO Cts_times VALUES(?, ?, ?, ?)", times)
logging.info('\n'.join(y for y in [str(x) for x in times]))
@ -143,6 +152,10 @@ def i(d, s):
if ids:
inserttodb(csr, "INSERT OR REPLACE INTO Id2alias VALUES(?, ?, ?)", ids)
logging.info('\n'.join(y for y in [str(x) for x in ids]))
if speed:
inserttodb(csr, "INSERT OR REPLACE INTO Speed VALUES(?, ?)", speed)
if speed_ids:
inserttodb(csr, "INSERT OR REPLACE INTO Fastest_players VALUES(?, ?)", speed_ids)
except sql.Error:
logging.exception("sql error encountered in function 'i'")
if con:
@ -151,7 +164,7 @@ def i(d, s):
# 'insert' new data into a file i.e sql query file
def f(d, s):
with open(d, 'w', encoding='utf-8') as h:
times, ranks, ids = filters(get_list_from_server_txt(s))
times, ranks, ids, speed, speed_ids = filters(get_list_from_server_txt(s))
for t in times:
h.write("INSERT OR REPLACE INTO Cts_times VALUES(%s, %s, %s, %s)\n" % tuple(t))
pass