log-run.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. #!/usr/bin/python3
  2. import sqlite3
  3. import json
  4. import sys
  5. """
  6. Reads the API KEY, that must be passed as first command line argument.
  7. :returns: Recovered API KEY.
  8. :raises Exception: Th KEY couldn't be red.
  9. """
  10. def readKey():
  11. try:
  12. #print(sys.argv[0])
  13. key = sys.argv[1]
  14. return key
  15. except Exception as e:
  16. print("Error parsing API KEY: " + str(e))
  17. raise
  18. """
  19. Reads the JSON data, that must be passed as second command line argument.
  20. :returns: Recovered data, in JSON format.
  21. :raises Exception: The data couldn't be red or converted to JSON.
  22. """
  23. def readData():
  24. try:
  25. #print(sys.argv[1])
  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["wizard_info"]["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(name):
  62. print('Configuring database...')
  63. try:
  64. db = sqlite3.connect(name)
  65. return db
  66. except IOError as e:
  67. print("I/O Error connecting to database " + name + ": " + str(e))
  68. raise
  69. """
  70. Inserts a row into the database.
  71. :param db: Connection to the database.
  72. :param table: Name of the table to insert into.
  73. :param values: List of values to insert.
  74. :raises IntegrityError: The insert query was unsuccesfull.
  75. """
  76. def insert(db, table, values):
  77. cursor = db.cursor()
  78. placeholders = ''
  79. for x in range(0, len(values)):
  80. placeholders = placeholders + '?, '
  81. placeholders = placeholders[:len(placeholders) - 2]
  82. query = 'INSERT INTO ' + table + ' VALUES (' + placeholders + ')'
  83. try:
  84. cursor.execute(query, values)
  85. except sqlite3.IntegrityError as e:
  86. print('Error Inserting into ' + table + ' with values (' + str(values) + '): ' + str(e))
  87. raise
  88. cursor.close;
  89. """
  90. Parses run data (tables run, run_party, run_drop_rune, run_drop_runecraft,
  91. run_drop_item, run_drop_unit, run_drop_sd, run_drop_unit_pieces,
  92. run_drop_shapeshifting).
  93. :param db: Sqlite database connection.
  94. :param data: JSON data.
  95. """
  96. def parseRun(db, data):
  97. print("Parsing run...")
  98. cursor = db.cursor()
  99. uid = data["uid"]
  100. dtime = data["dtime"]
  101. # Check if run has already been inserted.
  102. cursor.execute("SELECT count(id) AS c FROM run WHERE uid = ? AND dtime = ?;", (uid, dtime))
  103. if (cursor.fetchone()[0] > 0):
  104. print(" Run already in database. Stopping....")
  105. return;
  106. area = data["area"]
  107. stage = data["stage"]
  108. difficulty = data["difficulty"]
  109. win = data["win"]
  110. time = data["time"]
  111. mana = data["mana"]
  112. energy = data["energy"]
  113. crystal = data["crystal"]
  114. helper = data["helper"]
  115. cursor.execute("SELECT max(id) + 1 AS id FROM run;")
  116. id = cursor.fetchone()[0]
  117. insert(db, "run", (uid, id, dtime, area, stage, difficulty, win, time, mana, energy, crystal, helper))
  118. if data["shapeshifting"] > 0:
  119. insert(db, "run_drop_shapeshifting", (id, data["shapeshifting"]))
  120. if data["sd"] > 0:
  121. insert(db, "run_drop_sd", (id, data["sd"]))
  122. if data["sd"] > 0:
  123. insert(db, "run_drop_unit", (id, data["unit"]))
  124. for rune in data["rune"]:
  125. rune_id = rune["id"]
  126. rune_type = rune["type"]
  127. slot = rune["slot"]
  128. stars = rune["stars"]
  129. ancient = rune["ancient"]
  130. quality = rune["quality"]
  131. value = rune["value"]
  132. efficiency = rune["efficiency"]
  133. main_stat = rune["main_stat"]
  134. main_stat_value = rune["main_stat_value"]
  135. innate_stat = rune["innate_stat"]
  136. innate_stat_value = rune["innate_stat_value"]
  137. substat_1 = rune["substat_1"]
  138. substat_1_value = rune["substat_1_value"]
  139. substat_2 = rune["substat_2"]
  140. substat_2_value = rune["substat_2_value"]
  141. substat_3 = rune["substat_3"]
  142. substat_3_value = rune["substat_3_value"]
  143. substat_4 = rune["substat_4"]
  144. substat_4_value = rune["substat_4_value"]
  145. 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))
  146. for item in data["item"]:
  147. item_id = item["id"]
  148. quantity = item["quantity"]
  149. insert(db, "run_drop_item", (id, item_id, quantity))
  150. for pieces in data["unit_pieces"]:
  151. pieces_id = pieces["id"]
  152. quantity = pieces["quantity"]
  153. insert(db, "run_drop_unit_pieces", (id, pieces_id, quantity))
  154. for party in data["party"]:
  155. unit_id = party["unit_id"]
  156. unit_master_id = party["unit_master_id"]
  157. insert(db, "run_party", (id, unit_master_id, unit_id))
  158. db.commit()
  159. """
  160. Begin script
  161. """
  162. data = readData()
  163. data = readKey()
  164. db = openDatabase('../../../../application/sw.sqlite')
  165. if verifyKey(db, data, key) == False:
  166. print("Invalid API KEY...")
  167. return -1
  168. else:
  169. parseRun(db, data)
  170. return 0