allmaps.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import sqlite3 as sql
  2. import subprocess, traceback
  3. # import contextlib
  4. #
  5. import sys, io, os
  6. # import ctypes
  7. # colors = ctypes.CDLL('./colors.so')
  8. # colors.colorize_name.argtypes = (ctypes.char_p, ctypes.int, ctypes.char_p)
  9. # get all maps in database
  10. def get_map_list(database):
  11. output = []
  12. con = sql.connect(database)
  13. with con:
  14. cursor = con.cursor()
  15. try:
  16. cursor.execute("select distinct mapid from Cts_times;")
  17. output = cursor.fetchall()
  18. except sql.Error:
  19. print("Shit is fucked.")
  20. return output
  21. # if there is no query then it outputs the index file.
  22. def run_cgi(query=None):
  23. cmd = [("./cts")]
  24. proc = subprocess.Popen(cmd, env=query, stdout=subprocess.PIPE, shell=True)
  25. # communicate returns 'bytes' class with function 'decode'
  26. return proc.communicate()[0].decode('utf-8')
  27. def run_colors(player_name):
  28. ret = player_name
  29. result = subprocess.run(['./colors', player_name], capture_output=True, text=True)
  30. if result.returncode == 0:
  31. ret = result.stdout
  32. return ret
  33. def get_speed_record(database, map_id):
  34. message = "{name} traveled the fastest at {speed} qu/s."
  35. query = str()
  36. result = []
  37. with open("queries/fastest-player-of-map.sql") as f:
  38. query = f.read()
  39. # q = query.replace('?', map_id)
  40. # print(q)
  41. with sql.connect(database) as con:
  42. cursor = con.cursor()
  43. try:
  44. cursor.execute(query, (map_id,))
  45. result = cursor.fetchall()
  46. except sql.Error:
  47. pass
  48. player_name = result[0][1]
  49. colored = (run_colors(player_name)).strip()
  50. velocity = round(result[0][0], 2)
  51. return message.format(name=colored, speed=velocity)
  52. def main():
  53. template = ""
  54. with open("overview.html", 'r') as fin:
  55. template = fin.read()
  56. with open("output/index.html", 'w') as fout:
  57. fout.write(template % run_cgi())
  58. # use same template for fastest-players
  59. query = {"QUERY_STRING" : "fastest-players"}
  60. with open("output/fastest-players.html", 'w') as fout:
  61. fout.write(template % run_cgi(query))
  62. maps = get_map_list("db/cts.db")
  63. with open("map.html", 'r') as fin:
  64. template = fin.read()
  65. # for each map generate an html file.
  66. for game_map in maps:
  67. # game_map is a tuple obj.
  68. map_name = game_map[0]
  69. query = {"QUERY_STRING" : ("map=%s" % map_name)}
  70. filename = ("output/maps/%s.html" % map_name)
  71. sentence = get_speed_record("db/cts.db", map_name)
  72. with open(filename, 'w+') as fout:
  73. title = map_name
  74. fout.write(template.format(
  75. title=title,
  76. map_name=map_name,
  77. table=run_cgi(query),
  78. speed=sentence
  79. )
  80. )
  81. # fout.write(template % (title, map_name, table))
  82. return True
  83. if __name__ == "__main__":
  84. success = False
  85. try:
  86. success = main()
  87. except FileNotFoundError:
  88. traceback.print_exc()
  89. print("\n\t The script probably didn't find the page templates needed to generate a page. You can copy minimal working examples from the repository at templates/.")
  90. if success:
  91. print("allmaps.py - Generated pages for all maps.")
  92. pass