log-run.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. print(sys.argv[2])
  27. data = json.loads(sys.argv[2])
  28. return data
  29. except Exception as e:
  30. print("Error parsing data: " + str(e))
  31. raise
  32. """
  33. Verifies that the API key matches the player data and that it exists in th DB.
  34. :param db: Connection to the database.
  35. :returns: Connection to the database.
  36. :param data: Data in json format.
  37. :param key: API KEY.
  38. :returns: True if key and player match, False otherwise.
  39. :raises IntegrityError: The queryes couldn't bre executed.
  40. """
  41. def verifyKey(db, data, key):
  42. print('Verifying KEY...')
  43. status = False
  44. try:
  45. uid = data["uid"]
  46. cursor = db.cursor()
  47. cursor.execute('SELECT count(uid) AS c FROM player WHERE uid = ? AND api_key = ?;', (uid, key))
  48. if cursor.fetchone()[0] == 1:
  49. status = True
  50. cursor.close()
  51. except sqlite3.IntegrityError as e:
  52. print("Error executing statement: " + str(e))
  53. raise
  54. return status
  55. """
  56. Opens the database file and deletes from the user tables
  57. :param name: The path to the sqlite database.
  58. :returns: Connection to the database.
  59. :raises IntegrityError: The queryes couldn't bre executed.
  60. :raises IOError: The sqlite file couldn't be created.
  61. """
  62. def openDatabase():
  63. kdb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../sw.sqlite'
  64. udb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../../data/sw.sqlite'
  65. print('Configuring database...')
  66. try:
  67. db = sqlite3.connect(kdb)
  68. cursor = db.cursor()
  69. cursor.execute('attach "' + udb + '" as data;')
  70. cursor.close()
  71. return db
  72. except sqlite3.IntegrityError as e:
  73. print("Error executing statement: " + str(e))
  74. raise
  75. except IOError as e:
  76. print("I/O Error creating database " + name + ": " + str(e))
  77. raise
  78. """
  79. Inserts a row into the database.
  80. :param db: Connection to the database.
  81. :param table: Name of the table to insert into.
  82. :param values: List of values to insert.
  83. :raises IntegrityError: The insert query was unsuccesfull.
  84. """
  85. def insert(db, table, values):
  86. cursor = db.cursor()
  87. placeholders = ''
  88. for x in range(0, len(values)):
  89. placeholders = placeholders + '?, '
  90. placeholders = placeholders[:len(placeholders) - 2]
  91. query = 'INSERT INTO ' + table + ' VALUES (' + placeholders + ')'
  92. try:
  93. cursor.execute(query, values)
  94. except sqlite3.IntegrityError as e:
  95. print('Error Inserting into ' + table + ' with values (' + str(values) + '): ' + str(e))
  96. raise
  97. cursor.close;
  98. """
  99. Parses run data (tables run, run_party, run_drop_rune, run_drop_runecraft,
  100. run_drop_item, run_drop_unit, run_drop_sd, run_drop_unit_pieces,
  101. run_drop_shapeshifting).
  102. :param db: Sqlite database connection.
  103. :param data: JSON data.
  104. """
  105. def parseRun(db, data):
  106. print("Parsing run...")
  107. cursor = db.cursor()
  108. uid = data["uid"]
  109. dtime = data["dtime"]
  110. # Check if run has already been inserted.
  111. cursor.execute("SELECT count(id) AS c FROM run WHERE uid = ? AND dtime = ?;", (uid, dtime))
  112. if (cursor.fetchone()[0] > 0):
  113. print(" Run already in database. Stopping....")
  114. return;
  115. area = data["area"]
  116. stage = data["stage"]
  117. difficulty = data["difficulty"]
  118. win = data["win"]
  119. time = data["time"]
  120. mana = data["mana"]
  121. energy = data["energy"]
  122. crystal = data["crystal"]
  123. helper = data["helper"]
  124. cursor.execute("SELECT max(id) + 1 AS id FROM run;")
  125. id = cursor.fetchone()[0]
  126. if id == None:
  127. id = 1
  128. insert(db, "run", (uid, id, dtime, area, stage, difficulty, win, time, mana, energy, crystal, helper))
  129. if data["shapeshifting"] > 0:
  130. insert(db, "run_drop_shapeshifting", (id, data["shapeshifting"]))
  131. if data["sd"] > 0:
  132. insert(db, "run_drop_sd", (id, data["sd"]))
  133. if data["sd"] > 0:
  134. insert(db, "run_drop_unit", (id, data["unit"]))
  135. for rune in data["rune"]:
  136. rune_id = rune["id"]
  137. rune_type = rune["type"]
  138. slot = rune["slot"]
  139. stars = rune["stars"]
  140. ancient = rune["ancient"]
  141. quality = rune["quality"]
  142. value = rune["value"]
  143. efficiency = rune["efficiency"]
  144. main_stat = rune["main_stat"]
  145. main_stat_value = rune["main_stat_value"]
  146. innate_stat = rune["innate_stat"]
  147. innate_stat_value = rune["innate_stat_value"]
  148. substat_1 = rune["substat_1"]
  149. substat_1_value = rune["substat_1_value"]
  150. substat_2 = rune["substat_2"]
  151. substat_2_value = rune["substat_2_value"]
  152. substat_3 = rune["substat_3"]
  153. substat_3_value = rune["substat_3_value"]
  154. substat_4 = rune["substat_4"]
  155. substat_4_value = rune["substat_4_value"]
  156. insert(db, "run_drop_rune", (id, rune_id, rune_type, slot, stars, ancient, quality, value, efficiency, main_stat, main_stat_value, innate_stat, innate_stat_value, substat_1, substat_1_value, substat_2, substat_2_value, substat_3, substat_3_value, substat_4, substat_4_value))
  157. for item in data["item"]:
  158. item_id = item["id"]
  159. quantity = item["quantity"]
  160. insert(db, "run_drop_item", (id, item_id, quantity))
  161. for pieces in data["unit_pieces"]:
  162. pieces_id = pieces["id"]
  163. quantity = pieces["quantity"]
  164. insert(db, "run_drop_unit_pieces", (id, pieces_id, quantity))
  165. for party in data["party"]:
  166. unit_id = party["unit_id"]
  167. unit_master_id = party["unit_master_id"]
  168. insert(db, "run_party", (id, unit_master_id, unit_id))
  169. db.commit()
  170. """
  171. Begin script
  172. """
  173. data = readData()
  174. key = readKey()
  175. db = openDatabase()
  176. if verifyKey(db, data, key) == False:
  177. print("Invalid API KEY...")
  178. sys.exit(-1)
  179. else:
  180. parseRun(db, data)
  181. sys.exit(0)