update_logbook.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #!/usr/bin/python3
  2. import sqlite3
  3. import json
  4. import sys
  5. import os
  6. """
  7. Reads the API KEY, that must be passed as first command line argument.
  8. :returns: Recovered API KEY.
  9. :raises Exception: Th KEY couldn't be red.
  10. """
  11. def readKey():
  12. try:
  13. #print(sys.argv[0])
  14. key = sys.argv[1]
  15. return key
  16. except Exception as e:
  17. print("Error parsing API KEY: " + str(e))
  18. raise
  19. """
  20. Reads the JSON data, that must be passed as second command line argument.
  21. :returns: Recovered data, in JSON format.
  22. :raises Exception: The data couldn't be red or converted to JSON.
  23. """
  24. def readData():
  25. try:
  26. data = json.loads(sys.argv[2])
  27. return data
  28. except Exception as e:
  29. print("Error parsing data: " + str(e))
  30. raise
  31. """
  32. Verifies that the API key matches the player data and that it exists in th DB.
  33. :param db: Connection to the database.
  34. :returns: Connection to the database.
  35. :param data: Data in json format.
  36. :param key: API KEY.
  37. :returns: True if key and player match, False otherwise.
  38. :raises IntegrityError: The queryes couldn't bre executed.
  39. """
  40. def verifyKey(db, data, key):
  41. print('Verifying KEY...')
  42. status = False
  43. try:
  44. uid = data["lobby_wizard_log"]["wizard_id"]
  45. cursor = db.cursor()
  46. cursor.execute('SELECT count(uid) AS c FROM player WHERE uid = ? AND api_key = ?;', (uid, key))
  47. if cursor.fetchone()[0] == 1:
  48. status = True
  49. cursor.close()
  50. except sqlite3.IntegrityError as e:
  51. print("Error executing statement: " + str(e))
  52. raise
  53. return status
  54. """
  55. Opens the database file and deletes from the user tables
  56. :param name: The path to the sqlite database.
  57. :returns: Connection to the database.
  58. :raises IntegrityError: The queryes couldn't bre executed.
  59. :raises IOError: The sqlite file couldn't be created.
  60. """
  61. def openDatabase():
  62. kdb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../../../sw.sqlite'
  63. udb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../../../../data/sw.sqlite'
  64. print('Configuring database...')
  65. try:
  66. db = sqlite3.connect(kdb)
  67. cursor = db.cursor()
  68. cursor.execute('attach "' + udb + '" as data;')
  69. cursor.close()
  70. return db
  71. except sqlite3.IntegrityError as e:
  72. print("Error executing statement: " + str(e))
  73. raise
  74. except IOError as e:
  75. print("I/O Error creating database " + name + ": " + str(e))
  76. raise
  77. """
  78. Inserts a row into the database.
  79. :param db: Connection to the database.
  80. :param table: Name of the table to insert into.
  81. :param values: List of values to insert.
  82. :raises IntegrityError: The insert query was unsuccesfull.
  83. """
  84. def insert(db, table, values):
  85. cursor = db.cursor()
  86. placeholders = ''
  87. for x in range(0, len(values)):
  88. placeholders = placeholders + '?, '
  89. placeholders = placeholders[:len(placeholders) - 2]
  90. query = 'INSERT INTO ' + table + ' VALUES (' + placeholders + ')'
  91. try:
  92. cursor.execute(query, values)
  93. except sqlite3.IntegrityError as e:
  94. print('Error Inserting into ' + table + ' with values (' + str(values) + '): ' + str(e))
  95. raise
  96. cursor.close;
  97. """
  98. Parses run data (tables run, run_party, run_drop_rune, run_drop_runecraft,
  99. run_drop_item, run_drop_unit, run_drop_sd, run_drop_unit_pieces,
  100. run_drop_shapeshifting).
  101. :param db: Sqlite database connection.
  102. :param data: JSON data.
  103. """
  104. def parseData(db, data):
  105. cursor = db.cursor()
  106. # Page 1: Cairos non-elemental, rift raid, rift dungeon.
  107. if (data["lobby_wizard_log"]["page_no"] == 1):
  108. print("Parsing logbook data...")
  109. uid = data["lobby_wizard_log"]["wizard_id"]
  110. joined = data["lobby_wizard_log"]["account_create_timestamp"]
  111. top_rank_arena = data["lobby_wizard_log"]["pvp_best_rating_id"]
  112. top_rank_world_arena = data["lobby_wizard_log"]["rtpvp_rank_best_rating_id"]
  113. top_rank_special_league = data["lobby_wizard_log"]["rtpvp_contest_best_rating_id"]
  114. top_rank_gw = data["lobby_wizard_log"]["guildwar_best_rating_id"]
  115. top_rank_siege = data["lobby_wizard_log"]["guildsiege_best_rating_id"]
  116. top_rank_wboss = data["lobby_wizard_log"]["world_boss_best_rank_id"]
  117. top_rank_toan = data["lobby_wizard_log"]["trial_tower_normal_best_floor"]
  118. top_rank_toah = data["lobby_wizard_log"]["trial_tower_hard_best_floor"]
  119. cursor.execute("""
  120. UPDATE player SET
  121. joined = ?,
  122. top_rank_arena = ?,
  123. top_rank_world_arena = ?,
  124. top_rank_special_league = ?,
  125. top_rank_gw = ?,
  126. top_rank_siege = ?,
  127. top_rank_wboss = ?,
  128. top_rank_toan = ?,
  129. top_rank_toah = ?
  130. WHERE uid = ?;
  131. """,
  132. (
  133. joined,
  134. top_rank_arena,
  135. top_rank_world_arena,
  136. top_rank_special_league,
  137. top_rank_gw,
  138. top_rank_siege,
  139. top_rank_wboss,
  140. top_rank_toan,
  141. top_rank_toah,
  142. uid,
  143. )
  144. )
  145. # Loop Cairos records
  146. for record in data["lobby_wizard_log"]["dungeon_best_clear_info_list"]:
  147. area_type = 2 # Cairos Dungeons
  148. area = record["dungeon_id"]
  149. stage = record["stage_id"]
  150. time = record["clear_time"]
  151. score = 0 # No scores in Cairos
  152. rank = None # No rank in Cairos
  153. cursor.execute("DELETE FROM record_party WHERE uid = ? AND area_type = ? AND area = ?;", (uid, area_type, area))
  154. cursor.execute("DELETE FROM record WHERE uid = ? AND area_type = ? AND area = ?;", (uid, area_type, area))
  155. insert(db, "record", (uid, area_type, area, stage, time, score, rank))
  156. for party in record["my_unit_deck_list"]:
  157. unit_id = party["unit_id"]
  158. unit_master_id = party["unit_master_id"]
  159. leader = party["leader"]
  160. front = 0 # No frontline in Cairos
  161. insert(db, "record_party", (uid, area_type, area, unit_id, unit_master_id, leader, front))
  162. # Rift Raid record
  163. if data["lobby_wizard_log"]["raid_best_clear_info_list"][0]:
  164. area_type = 4
  165. stage = data["lobby_wizard_log"]["raid_best_clear_info_list"][0]["stage_id"]
  166. area = stage
  167. time = data["lobby_wizard_log"]["raid_best_clear_info_list"][0]["clear_time"]
  168. score = 0 # No scores in Rift Raid
  169. rank = None # No rank in Rift Raid
  170. cursor.execute("DELETE FROM record_party WHERE uid = ? AND area_type = ? AND area = ?;", (uid, area_type, area))
  171. cursor.execute("DELETE FROM record WHERE uid = ? AND area_type = ? AND area = ?;", (uid, area_type, area))
  172. insert(db, "record", (uid, area_type, area, stage, time, score, rank))
  173. for party in data["lobby_wizard_log"]["raid_best_clear_info_list"][0]["my_unit_deck_list"]:
  174. unit_id = party["unit_id"]
  175. unit_master_id = party["unit_master_id"]
  176. leader = party["leader"]
  177. if party["slot_index"] <= 4:
  178. front = 1
  179. else:
  180. front = 0
  181. insert(db, "record_party", (uid, area_type, area, unit_id, unit_master_id, leader, front))
  182. # Loop Rift Dungeon records
  183. for record in data["lobby_wizard_log"]["rift_dungeon_best_clear_info_list"]:
  184. area_type = 3 # Rift Elemental Dungeons
  185. area = record["rift_dungeon_id"]
  186. stage = 0 # No stage in Rift Dungeons
  187. time = 0 # No time in Rift Dungeons
  188. score = record["clear_damage"]
  189. raw_rank = raw_rank = record["clear_rating"]
  190. if raw_rank == 2:
  191. rank = "D"
  192. elif raw_rank == 3:
  193. rank = "C"
  194. elif raw_rank == 4:
  195. rank = "B-"
  196. elif raw_rank == 5:
  197. rank = "B"
  198. elif raw_rank == 6:
  199. rank = "B+"
  200. elif raw_rank == 7:
  201. rank = "A-"
  202. elif raw_rank == 8:
  203. rank = "A"
  204. elif raw_rank == 9:
  205. rank = "A+"
  206. elif raw_rank == 90:
  207. rank = "S"
  208. elif raw_rank == 11:
  209. rank = "SS"
  210. elif raw_rank == 12:
  211. rank = "SSS"
  212. else:
  213. rank = None
  214. cursor.execute("DELETE FROM record_party WHERE uid = ? AND area_type = ? AND area = ?;", (uid, area_type, area))
  215. cursor.execute("DELETE FROM record WHERE uid = ? AND area_type = ? AND area = ?;", (uid, area_type, area))
  216. insert(db, "record", (uid, area_type, area, stage, time, score, rank))
  217. for party in record["my_unit_deck_list"]:
  218. unit_id = party["unit_id"]
  219. unit_master_id = party["unit_master_id"]
  220. leader = party["leader"]
  221. if party["slot_index"] <= 4:
  222. front = 1
  223. else:
  224. front = 0
  225. insert(db, "record_party", (uid, area_type, area, unit_id, unit_master_id, leader, front))
  226. db.commit()
  227. # Page 2: Cairos non-elemental, rift raid, rift dungeon.
  228. elif (data["lobby_wizard_log"]["page_no"] == 2):
  229. uid = data["lobby_wizard_log"]["wizard_id"]
  230. # Loop Cairos records
  231. for record in data["lobby_wizard_log"]["dungeon_best_clear_info_list"]:
  232. area_type = 2 # Cairos Dungeons
  233. area = record["dungeon_id"]
  234. stage = record["stage_id"]
  235. time = record["clear_time"]
  236. score = 0 # No scores in Cairos
  237. rank = None # No rank in Cairos
  238. cursor.execute("DELETE FROM record_party WHERE uid = ? AND area_type = ? AND area = ?;", (uid, area_type, area))
  239. cursor.execute("DELETE FROM record WHERE uid = ? AND area_type = ? AND area = ?;", (uid, area_type, area))
  240. insert(db, "record", (uid, area_type, area, stage, time, score, rank))
  241. for party in record["my_unit_deck_list"]:
  242. unit_id = party["unit_id"]
  243. unit_master_id = party["unit_master_id"]
  244. leader = party["leader"]
  245. front = 0 # No frontline in Cairos
  246. insert(db, "record_party", (uid, area_type, area, unit_id, unit_master_id, leader, front))
  247. db.commit()
  248. """
  249. Begin script
  250. """
  251. data = readData()
  252. key = readKey()
  253. db = openDatabase()
  254. if verifyKey(db, data, key) == False:
  255. print("Invalid API KEY...")
  256. sys.exit(-1)
  257. else:
  258. parseData(db, data)
  259. sys.exit(0)