log-run.py 5.9 KB

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