upload_profile.py 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308
  1. #!/usr/bin/python3
  2. import sys
  3. import os
  4. import sqlite3
  5. import json
  6. import math
  7. """
  8. Reads the API KEY, that must be passed as first command line argument.
  9. :returns: Recovered API KEY.
  10. :raises Exception: Th KEY couldn't be red.
  11. """
  12. def readKey():
  13. try:
  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[1])
  27. #data = json.loads(sys.argv[1])
  28. with open(sys.argv[2], 'r') as f:
  29. content = f.read()
  30. data = json.loads(content)
  31. return data
  32. except Exception as e:
  33. print("Error parsing data: " + str(e))
  34. raise
  35. """
  36. Verifies that the API key matches the player data and that it exists in th DB.
  37. :param db: Connection to the database.
  38. :returns: Connection to the database.
  39. :param data: Data in json format.
  40. :param key: API KEY.
  41. :returns: True if key and player match, False otherwise.
  42. :raises IntegrityError: The queryes couldn't bre executed.
  43. """
  44. def verifyKey(db, data, key):
  45. print('Verifying KEY...')
  46. status = False
  47. try:
  48. uid = data["wizard_info"]["wizard_id"]
  49. cursor = db.cursor()
  50. cursor.execute('SELECT count(uid) AS c FROM player WHERE uid = ? AND api_key = ?;', (uid, key))
  51. if cursor.fetchone()[0] == 1:
  52. status = True
  53. cursor.close()
  54. except sqlite3.IntegrityError as e:
  55. print("Error executing statement: " + str(e))
  56. raise
  57. return status
  58. """
  59. Opens the database file and cleans the user tables.
  60. It also disables referencial integrity.
  61. :param name: The path to the sqlite database.
  62. :returns: Connection to the database.
  63. :raises IntegrityError: The queryes couldn't bre executed.
  64. :raises IOError: The sqlite file couldn't be created.
  65. """
  66. def openDatabase():
  67. kdb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../../../sw.sqlite'
  68. udb = os.path.dirname(os.path.realpath(sys.argv[0])) + '/../../../../data/sw.sqlite'
  69. print('Configuring database...')
  70. try:
  71. db = sqlite3.connect(kdb)
  72. cursor = db.cursor()
  73. cursor.execute("attach '" + udb + "' as data;")
  74. cursor.close()
  75. return db
  76. except sqlite3.IntegrityError as e:
  77. print("Error executing statement: " + str(e))
  78. raise
  79. except IOError as e:
  80. print("I/O Error creating database " + name + ": " + str(e))
  81. raise
  82. """
  83. Clears the user tables.
  84. It also disables referencial integrity.
  85. :param db: Sqlite database connection.
  86. :param data: Data in json format.
  87. :raises IntegrityError: The queryes couldn't bre executed.
  88. """
  89. def clearData(db, data):
  90. print('Clearing previous data...')
  91. try:
  92. uid = str(data["wizard_info"]["wizard_id"])
  93. cursor = db.cursor()
  94. cursor.execute('PRAGMA foreign_keys = OFF;')
  95. cursor.execute('DELETE FROM scenario WHERE uid = ?', [uid])
  96. cursor.execute('DELETE FROM defense WHERE uid = ?', [uid])
  97. cursor.execute('DELETE FROM unit_skill WHERE unit IN (SELECT id FROM unit WHERE uid = ?)', [uid])
  98. cursor.execute('DELETE FROM unit WHERE uid = ?', [uid])
  99. cursor.execute('DELETE FROM rune WHERE uid = ?', [uid])
  100. cursor.execute('DELETE FROM artifact WHERE uid = ?', [uid])
  101. cursor.execute('DELETE FROM building WHERE uid = ?', [uid])
  102. cursor.execute('DELETE FROM decoration WHERE uid = ?', [uid])
  103. cursor.execute('DELETE FROM inventory WHERE uid = ?', [uid])
  104. cursor.execute('DELETE FROM summon_special') # For every player, will be reloaded.
  105. cursor.execute('DELETE FROM rune_craft WHERE uid = ?', [uid])
  106. # 'guild' and 'guild_member' tables are cleared before inserting.
  107. db.commit()
  108. cursor.close();
  109. except sqlite3.IntegrityError as e:
  110. print("Error executing statement: " + str(e))
  111. raise
  112. return
  113. """
  114. Closes the database.
  115. Before doing so, it also disables referencial integrity.
  116. :param db: Connection to the database.
  117. :returns: Connection to the database.
  118. :raises IntegrityError: The queryes couldn't bre executed.
  119. """
  120. def closeDatabase(name):
  121. print('Closing database connection...')
  122. try:
  123. cursor = db.cursor()
  124. cursor.execute('PRAGMA foreign_keys = ON;')
  125. cursor.close()
  126. db.commit()
  127. db.close()
  128. except sqlite3.IntegrityError as e:
  129. print("Error executing statement: " + str(e))
  130. raise
  131. """
  132. Inserts a row into the database.
  133. :param db: Connection to the database.
  134. :param table: Name of the table to insert into.
  135. :param values: List of values to insert.
  136. :raises IntegrityError: The insert query was unsuccesfull.
  137. """
  138. def insert(db, table, values):
  139. cursor = db.cursor()
  140. db.execute('PRAGMA foreign_keys = OFF;')
  141. placeholders = ''
  142. for x in range(0, len(values)):
  143. placeholders = placeholders + '?, '
  144. placeholders = placeholders[:len(placeholders) - 2]
  145. query = 'INSERT INTO ' + table + ' VALUES (' + placeholders + ')'
  146. try:
  147. cursor.execute(query, values)
  148. except sqlite3.IntegrityError as e:
  149. print('Error Inserting into ' + table + ' with values (' + str(values) + '): ' + str(e))
  150. raise
  151. cursor.close;
  152. """
  153. Parses player (table player).
  154. :param db: Sqlite database connection.
  155. :param data: Data in json format.
  156. """
  157. def parsePlayer(db, data):
  158. print("Parsing player...")
  159. cursor = db.cursor()
  160. uid = data["wizard_info"]["wizard_id"]
  161. name = data["wizard_info"]["wizard_name"]
  162. # TODO: Generate data
  163. mail = data["wizard_info"]["wizard_name"]
  164. password = data["wizard_info"]["wizard_name"]
  165. api_key = data["wizard_info"]["wizard_name"]
  166. mana = data["wizard_info"]["wizard_mana"]
  167. crystal = data["wizard_info"]["wizard_crystal"]
  168. country = data["wizard_info"]["wizard_last_country"]
  169. lang = data["wizard_info"]["wizard_last_lang"]
  170. level = data["wizard_info"]["wizard_level"]
  171. experience = data["wizard_info"]["experience"]
  172. energy = data["wizard_info"]["wizard_energy"]
  173. energy_max = data["wizard_info"]["energy_max"]
  174. energy_per_min = data["wizard_info"]["energy_per_min"]
  175. arena_energy = data["wizard_info"]["arena_energy"]
  176. arena_energy_max = data["wizard_info"]["arena_energy_max"]
  177. rep = data["wizard_info"]["rep_unit_id"]
  178. social_point = data["wizard_info"]["social_point_current"]
  179. honor_point = data["wizard_info"]["honor_point"]
  180. guild_point = data["wizard_info"]["guild_point"]
  181. darkportal_energy = data["wizard_info"]["darkportal_energy"]
  182. darkportal_energy_max = data["wizard_info"]["darkportal_energy_max"]
  183. dimension_energy = data["dimension_hole_info"]["energy"]
  184. dimension_energy_max = data["dimension_hole_info"]["energy_max"]
  185. costume_point = data["wizard_info"]["costume_point"]
  186. costume_point_max = data["wizard_info"]["costume_point_max"]
  187. honor_medal = data["wizard_info"]["honor_medal"]
  188. honor_mark = data["wizard_info"]["honor_mark"]
  189. event_coin = data["wizard_info"]["event_coin"]
  190. storage_slots = data["unit_depository_slots"]["number"]
  191. island_upgrade = 0
  192. for island in data["island_info"]:
  193. if island["id"] <= 7 and island["open"] == 1:
  194. island_upgrade = island["id"]
  195. query = '''
  196. UPDATE player
  197. SET mana = ?,
  198. crystal = ?,
  199. country = ?,
  200. lang = ?,
  201. level = ?,
  202. experience = ?,
  203. energy = ?,
  204. energy_max = ?,
  205. energy_per_min = ?,
  206. arena_energy = ?,
  207. arena_energy_max = ?,
  208. rep = ?,
  209. social_point = ?,
  210. honor_point = ?,
  211. guild_point = ?,
  212. darkportal_energy = ?,
  213. darkportal_energy_max = ?,
  214. dimension_energy = ?,
  215. dimension_energy_max = ?,
  216. costume_point = ?,
  217. costume_point_max = ?,
  218. honor_medal = ?,
  219. honor_medal = ?,
  220. event_coin = ?,
  221. storage_slots = ?,
  222. island = ?
  223. WHERE uid = ?;
  224. '''
  225. values = (
  226. mana, crystal, country,
  227. lang, level, experience,
  228. energy, energy_max, energy_per_min,
  229. arena_energy, arena_energy_max, rep,
  230. social_point, honor_point, guild_point,
  231. darkportal_energy, darkportal_energy_max, dimension_energy,
  232. dimension_energy_max, costume_point, costume_point_max,
  233. honor_medal, honor_medal, event_coin,
  234. storage_slots, island_upgrade, uid
  235. )
  236. try:
  237. cursor.execute(query, values)
  238. db.commit()
  239. except sqlite3.IntegrityError as e:
  240. print('Error updating player table with: ' + str(query) + ' <== ' + str(values) + ' || Error message:' + str(e))
  241. raise
  242. """
  243. Parses scenarios (table scenario).
  244. :param db: Sqlite database connection.
  245. :param data: Data in json format.
  246. """
  247. def parseScenarios(db, data):
  248. print("Parsing scenarios...")
  249. uid = data["wizard_info"]["wizard_id"]
  250. for sce in data["scenario_list"]:
  251. region = sce["region_id"]
  252. difficulty = sce["difficulty"]
  253. cleared = sce["cleared"]
  254. max_cleared = 0
  255. for stage in sce["stage_list"]:
  256. if stage["cleared"] == 1:
  257. max_cleared = stage["stage_no"]
  258. insert(db, "scenario", (uid, region, difficulty, cleared, max_cleared))
  259. db.commit()
  260. """
  261. Parses arena defense units (table defense).
  262. :param db: Sqlite database connection.
  263. :param data: Data in json format.
  264. """
  265. def parseDefense(db, data):
  266. print("Parsing arena defense...")
  267. uid = data["wizard_info"]["wizard_id"]
  268. for defense in data["defense_unit_list"]:
  269. unit = defense["unit_id"]
  270. position = defense["pos_id"]
  271. insert(db, "defense", (uid, unit, position))
  272. db.commit()
  273. """
  274. Parses buildings (table building).
  275. :param db: Sqlite database connection.
  276. :param data: Data in json format.
  277. """
  278. def parseBuildings(db, data):
  279. print("Parsing buildings...")
  280. uid = data["wizard_info"]["wizard_id"]
  281. for bui in data["building_list"]:
  282. id = bui["building_id"]
  283. building = bui["building_master_id"]
  284. gain = bui["gain_per_hour"]
  285. insert(db, "building", (uid, id, building, gain))
  286. db.commit()
  287. """
  288. Parses decoration buildings (table dcoration).
  289. :param db: Sqlite database connection.
  290. :param data: Data in json format.
  291. """
  292. def parseDecorations(db, data):
  293. print("Parsing decorations...")
  294. uid = data["wizard_info"]["wizard_id"]
  295. for bui in data["deco_list"]:
  296. id = bui["deco_id"]
  297. building = bui["master_id"]
  298. level = bui["level"]
  299. insert(db, "decoration", (uid, id, building, level))
  300. db.commit()
  301. """
  302. Parses unit data (tables unit, unit_skill).
  303. :param db: Sqlite database connection.
  304. :param data: Data in json format.
  305. """
  306. def parseUnits(db, data):
  307. print("Parsing monsters...")
  308. uid = data["wizard_info"]["wizard_id"]
  309. lock_list = data["unit_lock_list"]
  310. for mon in data["unit_list"]:
  311. id = mon["unit_id"]
  312. building = mon["building_id"]
  313. monster = mon["unit_master_id"]
  314. level = mon["unit_level"]
  315. stars = mon["class"]
  316. hp = mon["con"] * 15 # Always x15
  317. attack = mon["atk"]
  318. defense = mon["def"]
  319. speed = mon["spd"]
  320. crit_rate = mon["critical_rate"]
  321. crit_damage = mon["critical_damage"]
  322. resistance = mon["resist"]
  323. accuracy = mon["accuracy"]
  324. experience = mon["experience"]
  325. exp_gained = mon["exp_gained"]
  326. exp_gain_rate = mon["exp_gain_rate"]
  327. costume = mon["costume_master_id"]
  328. attribute = -1; # TODO: Attribute?
  329. source = mon["source"]
  330. create_time = mon["create_time"]
  331. homunculus = mon["homunculus"]
  332. homunculus_name = mon["homunculus_name"]
  333. lock = 0
  334. if id in lock_list:
  335. lock = 1
  336. insert(db, "unit", (uid, id, building, monster, level, stars, hp, attack, defense, speed, crit_rate, crit_damage, resistance, accuracy, experience, exp_gained, exp_gain_rate, costume, attribute, source, create_time, homunculus, homunculus_name, lock))
  337. for skill in mon["skills"]:
  338. skill_id = skill[0]
  339. skill_level = skill[1]
  340. insert(db, "unit_skill", (id, skill_id, skill_level))
  341. db.commit()
  342. """
  343. Parses inventory data (table inventory).
  344. :param db: Sqlite database connection.
  345. :param data: Data in json format.
  346. """
  347. def parseInventory(db, data):
  348. print("Parsing inventory...")
  349. uid = data["wizard_info"]["wizard_id"]
  350. for item in data["inventory_info"]:
  351. id = item["item_master_id"]
  352. type = item["item_master_type"]
  353. amount = item["item_quantity"]
  354. insert(db, "inventory", (uid, id, type, amount))
  355. # Fix rune craft item type.
  356. cursor = db.cursor()
  357. cursor.execute("UPDATE inventory SET type = 27 WHERE type = 29 AND id IN (2001, 4001, 4002, 4003, 9001, 9002, 9003, 8001);")
  358. cursor.close()
  359. """
  360. Parses summon stone list (table summon_special).
  361. :param db: Sqlite database connection.
  362. :param data: Data in json format.
  363. """
  364. def parseSummonSpecial(db, data):
  365. print("Parsing Summon stone monster list...")
  366. uid = data["wizard_info"]["wizard_id"]
  367. for unit in data["summon_special_info"]["this"]:
  368. insert(db, "summon_special", (0, unit))
  369. for unit in data["summon_special_info"]["next"]:
  370. insert(db, "summon_special", (1, unit))
  371. for unit in data["summon_special_info"]["third"]:
  372. insert(db, "summon_special", (2, unit))
  373. for unit in data["summon_special_info"]["fourth"]:
  374. insert(db, "summon_special", (3, unit))
  375. """
  376. Parses rune data (table rune).
  377. :param db: Sqlite database connection.
  378. :param data: Data in json format.
  379. """
  380. def parseRunes(db, data):
  381. print("Parsing runes...")
  382. STAT_HP = 1
  383. STAT_HP_PCT = 2
  384. STAT_ATK = 3
  385. STAT_ATK_PCT = 4
  386. STAT_DEF = 5
  387. STAT_DEF_PCT = 6
  388. STAT_SPD = 8
  389. STAT_CRIT_RATE_PCT = 9
  390. STAT_CRIT_DMG_PCT = 10
  391. STAT_RESIST_PCT = 11
  392. STAT_ACCURACY_PCT = 12
  393. MAIN_STAT_VALUES = {
  394. # [stat][stars][level]: value
  395. STAT_HP: {
  396. 1: [40, 85, 130, 175, 220, 265, 310, 355, 400, 445, 490, 535, 580, 625, 670, 804],
  397. 2: [70, 130, 190, 250, 310, 370, 430, 490, 550, 610, 670, 730, 790, 850, 910, 1092],
  398. 3: [100, 175, 250, 325, 400, 475, 550, 625, 700, 775, 850, 925, 1000, 1075, 1150, 1380],
  399. 4: [160, 250, 340, 430, 520, 610, 700, 790, 880, 970, 1060, 1150, 1240, 1330, 1420, 1704],
  400. 5: [270, 375, 480, 585, 690, 795, 900, 1005, 1110, 1215, 1320, 1425, 1530, 1635, 1740, 2088],
  401. 6: [360, 480, 600, 720, 840, 960, 1080, 1200, 1320, 1440, 1560, 1680, 1800, 1920, 2040, 2448],
  402. },
  403. STAT_HP_PCT: {
  404. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  405. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  406. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  407. 4: [5, 7, 9, 11, 13, 16, 18, 20, 22, 24, 27, 29, 31, 33, 36, 43],
  408. 5: [8, 10, 12, 15, 17, 20, 22, 24, 27, 29, 32, 34, 37, 40, 43, 51],
  409. 6: [11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 63],
  410. },
  411. STAT_ATK: {
  412. 1: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 54],
  413. 2: [5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 73],
  414. 3: [7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77, 92],
  415. 4: [10, 16, 22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94, 112],
  416. 5: [15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85, 92, 99, 106, 113, 135],
  417. 6: [22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 160],
  418. },
  419. STAT_ATK_PCT: {
  420. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  421. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  422. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  423. 4: [5, 7, 9, 11, 13, 16, 18, 20, 22, 24, 27, 29, 31, 33, 36, 43],
  424. 5: [8, 10, 12, 15, 17, 20, 22, 24, 27, 29, 32, 34, 37, 40, 43, 51],
  425. 6: [11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 63],
  426. },
  427. STAT_DEF: {
  428. 1: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 54],
  429. 2: [5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 73],
  430. 3: [7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77, 92],
  431. 4: [10, 16, 22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94, 112],
  432. 5: [15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85, 92, 99, 106, 113, 135],
  433. 6: [22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 160],
  434. },
  435. STAT_DEF_PCT: {
  436. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  437. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  438. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  439. 4: [5, 7, 9, 11, 13, 16, 18, 20, 22, 24, 27, 29, 31, 33, 36, 43],
  440. 5: [8, 10, 12, 15, 17, 20, 22, 24, 27, 29, 32, 34, 37, 40, 43, 51],
  441. 6: [11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 63],
  442. },
  443. STAT_SPD: {
  444. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  445. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  446. 3: [3, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 21, 25],
  447. 4: [4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 30],
  448. 5: [5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 39],
  449. 6: [7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 42],
  450. },
  451. STAT_CRIT_RATE_PCT: {
  452. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  453. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  454. 3: [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 37],
  455. 4: [4, 6, 8, 11, 13, 15, 17, 19, 22, 24, 26, 28, 30, 33, 35, 41],
  456. 5: [5, 7, 10, 12, 15, 17, 19, 22, 24, 27, 29, 31, 34, 36, 39, 47],
  457. 6: [7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 58],
  458. },
  459. STAT_CRIT_DMG_PCT: {
  460. 1: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  461. 2: [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 37],
  462. 3: [4, 6, 9, 11, 13, 16, 18, 20, 22, 25, 27, 29, 32, 34, 36, 43],
  463. 4: [6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 57],
  464. 5: [8, 11, 15, 18, 21, 25, 28, 31, 34, 38, 41, 44, 48, 51, 54, 65],
  465. 6: [11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 80],
  466. },
  467. STAT_RESIST_PCT: {
  468. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  469. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  470. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  471. 4: [6, 8, 10, 13, 15, 17, 19, 21, 24, 26, 28, 30, 32, 35, 37, 44],
  472. 5: [9, 11, 14, 16, 19, 21, 23, 26, 28, 31, 33, 35, 38, 40, 43, 51],
  473. 6: [12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 64],
  474. },
  475. STAT_ACCURACY_PCT: {
  476. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  477. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  478. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  479. 4: [6, 8, 10, 13, 15, 17, 19, 21, 24, 26, 28, 30, 32, 35, 37, 44],
  480. 5: [9, 11, 14, 16, 19, 21, 23, 26, 28, 31, 33, 35, 38, 40, 43, 51],
  481. 6: [12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 64],
  482. },
  483. }
  484. SUBSTAT_INCREMENTS = {
  485. # [stat][stars]: value
  486. # Max possible substat value can be found by multiplying by 5
  487. STAT_HP: {
  488. 1: 60,
  489. 2: 105,
  490. 3: 165,
  491. 4: 225,
  492. 5: 300,
  493. 6: 375,
  494. },
  495. STAT_HP_PCT: {
  496. 1: 2,
  497. 2: 3,
  498. 3: 5,
  499. 4: 6,
  500. 5: 7,
  501. 6: 8,
  502. },
  503. STAT_ATK: {
  504. 1: 4,
  505. 2: 5,
  506. 3: 8,
  507. 4: 10,
  508. 5: 15,
  509. 6: 20,
  510. },
  511. STAT_ATK_PCT: {
  512. 1: 2,
  513. 2: 3,
  514. 3: 5,
  515. 4: 6,
  516. 5: 7,
  517. 6: 8,
  518. },
  519. STAT_DEF: {
  520. 1: 4,
  521. 2: 5,
  522. 3: 8,
  523. 4: 10,
  524. 5: 15,
  525. 6: 20,
  526. },
  527. STAT_DEF_PCT: {
  528. 1: 2,
  529. 2: 3,
  530. 3: 5,
  531. 4: 6,
  532. 5: 7,
  533. 6: 8,
  534. },
  535. STAT_SPD: {
  536. 1: 1,
  537. 2: 2,
  538. 3: 3,
  539. 4: 4,
  540. 5: 5,
  541. 6: 6,
  542. },
  543. STAT_CRIT_RATE_PCT: {
  544. 1: 1,
  545. 2: 2,
  546. 3: 3,
  547. 4: 4,
  548. 5: 5,
  549. 6: 6,
  550. },
  551. STAT_CRIT_DMG_PCT: {
  552. 1: 2,
  553. 2: 3,
  554. 3: 4,
  555. 4: 5,
  556. 5: 6,
  557. 6: 7,
  558. },
  559. STAT_RESIST_PCT: {
  560. 1: 2,
  561. 2: 3,
  562. 3: 5,
  563. 4: 6,
  564. 5: 7,
  565. 6: 8,
  566. },
  567. STAT_ACCURACY_PCT: {
  568. 1: 2,
  569. 2: 3,
  570. 3: 5,
  571. 4: 6,
  572. 5: 7,
  573. 6: 8,
  574. },
  575. }
  576. uid = data["wizard_info"]["wizard_id"]
  577. # Unequipped runes
  578. for rune in data["runes"]:
  579. id = rune["rune_id"]
  580. assigned_to = rune["occupied_id"]
  581. if assigned_to == 0:
  582. assigned_to = None
  583. runeset = rune["set_id"]
  584. slot = rune["slot_no"]
  585. stars = rune["class"]
  586. ancient = 0
  587. level = rune["upgrade_curr"]
  588. original_quality = rune["extra"]
  589. value = rune["sell_value"]
  590. if stars > 10:
  591. ancient = 1
  592. stars = stars - 10
  593. original_quality = original_quality - 10
  594. if level >= 12:
  595. quality = 5
  596. elif level >= 9:
  597. quality = 4
  598. elif level >= 6:
  599. quality = 3
  600. elif level >= 3:
  601. quality = 2
  602. else:
  603. quality = 1
  604. if original_quality > quality:
  605. quality = original_quality
  606. efficiency = 0
  607. max_efficiency = 0
  608. main_stat = rune["pri_eff"][0]
  609. main_stat_value = rune["pri_eff"][1]
  610. if rune["prefix_eff"][0] > 0:
  611. innate_stat = rune["prefix_eff"][0]
  612. innate_stat_value = rune["prefix_eff"][1]
  613. else:
  614. innate_stat = None
  615. innate_stat_value = 0
  616. if len(rune["sec_eff"]) > 0:
  617. substat_1 = rune["sec_eff"][0][0]
  618. substat_1_value = rune["sec_eff"][0][1]
  619. substat_1_enchant = rune["sec_eff"][0][2]
  620. substat_1_grind = rune["sec_eff"][0][3]
  621. else:
  622. substat_1 = None
  623. substat_1_value = 0
  624. substat_1_enchant = 0
  625. substat_1_grind = 0
  626. if len(rune["sec_eff"]) > 1:
  627. substat_2 = rune["sec_eff"][1][0]
  628. substat_2_value = rune["sec_eff"][1][1]
  629. substat_2_enchant = rune["sec_eff"][1][2]
  630. substat_2_grind = rune["sec_eff"][1][3]
  631. else:
  632. substat_2 = None
  633. substat_2_value = 0
  634. substat_2_enchant = 0
  635. substat_2_grind = 0
  636. if len(rune["sec_eff"]) > 2:
  637. substat_3 = rune["sec_eff"][2][0]
  638. substat_3_value = rune["sec_eff"][2][1]
  639. substat_3_enchant = rune["sec_eff"][2][2]
  640. substat_3_grind = rune["sec_eff"][2][3]
  641. else:
  642. substat_3 = None
  643. substat_3_value = 0
  644. substat_3_enchant = 0
  645. substat_3_grind = 0
  646. if len(rune["sec_eff"]) > 3:
  647. substat_4 = rune["sec_eff"][3][0]
  648. substat_4_value = rune["sec_eff"][3][1]
  649. substat_4_enchant = rune["sec_eff"][3][2]
  650. substat_4_grind = rune["sec_eff"][3][3]
  651. else:
  652. substat_4 = None
  653. substat_4_value = 0
  654. substat_4_enchant = 0
  655. substat_4_grind = 0
  656. # Calculate efficiences
  657. efficiency = 0
  658. substats = []
  659. efficiency += float(MAIN_STAT_VALUES[main_stat][stars][15]) / float(MAIN_STAT_VALUES[main_stat][6][15])
  660. if innate_stat is not None:
  661. efficiency += innate_stat_value / float(SUBSTAT_INCREMENTS[innate_stat][6] * 5)
  662. if substat_1 is not None:
  663. substats.append(substat_1)
  664. efficiency += substat_1_value / float(SUBSTAT_INCREMENTS[substat_1][6] * 5)
  665. if substat_2 is not None:
  666. substats.append(substat_2)
  667. efficiency += substat_2_value / float(SUBSTAT_INCREMENTS[substat_2][6] * 5)
  668. if substat_3 is not None:
  669. substats.append(substat_3)
  670. efficiency += substat_3_value / float(SUBSTAT_INCREMENTS[substat_3][6] * 5)
  671. if substat_4 is not None:
  672. substats.append(substat_4)
  673. efficiency += substat_4_value / float(SUBSTAT_INCREMENTS[substat_4][6] * 5)
  674. efficiency = efficiency / 2.8 * 100
  675. max_efficiency = get_max_efficiency(substats, efficiency, level, stars, original_quality)
  676. if efficiency > 100:
  677. efficiency = 100;
  678. if max_efficiency > 100:
  679. max_efficiency = 100;
  680. # TODO: Locked status not in JSO file.
  681. locked = 0
  682. insert(db, "rune", (uid, id, assigned_to, runeset, slot, stars, ancient, level, quality, original_quality, value, efficiency, max_efficiency, main_stat, main_stat_value, innate_stat, innate_stat_value, substat_1, substat_1_value, substat_1_enchant, substat_1_grind, substat_2, substat_2_value, substat_2_enchant, substat_2_grind, substat_3, substat_3_value, substat_3_enchant, substat_3_grind, substat_4, substat_4_value, substat_4_enchant, substat_4_grind, locked))
  683. # Equipped runes
  684. for mon in data["unit_list"]:
  685. for rune in mon["runes"]:
  686. id = rune["rune_id"]
  687. assigned_to = rune["occupied_id"]
  688. if assigned_to == 0:
  689. assigned_to = None
  690. runeset = rune["set_id"]
  691. slot = rune["slot_no"]
  692. stars = rune["class"]
  693. ancient = 0
  694. level = rune["upgrade_curr"]
  695. original_quality = rune["extra"]
  696. value = rune["sell_value"]
  697. if stars > 10:
  698. ancient = 1
  699. stars = stars - 10
  700. original_quality = original_quality - 10
  701. if level >= 12:
  702. quality = 5
  703. elif level >= 9:
  704. quality = 5
  705. elif level >= 6:
  706. quality = 3
  707. elif level >= 3:
  708. quality = 2
  709. else:
  710. quality = 1
  711. if original_quality > quality:
  712. quality = original_quality
  713. efficiency = 0
  714. max_efficiency = 0
  715. main_stat = rune["pri_eff"][0]
  716. main_stat_value = rune["pri_eff"][1]
  717. if rune["prefix_eff"][0] > 0:
  718. innate_stat = rune["prefix_eff"][0]
  719. innate_stat_value = rune["prefix_eff"][1]
  720. else:
  721. innate_stat = None
  722. innate_stat_value = 0
  723. if len(rune["sec_eff"]) > 0:
  724. substat_1 = rune["sec_eff"][0][0]
  725. substat_1_value = rune["sec_eff"][0][1]
  726. substat_1_enchant = rune["sec_eff"][0][2]
  727. substat_1_grind = rune["sec_eff"][0][3]
  728. else:
  729. substat_1 = None
  730. substat_1_value = 0
  731. substat_1_enchant = 0
  732. substat_1_grind = 0
  733. if len(rune["sec_eff"]) > 1:
  734. substat_2 = rune["sec_eff"][1][0]
  735. substat_2_value = rune["sec_eff"][1][1]
  736. substat_2_enchant = rune["sec_eff"][1][2]
  737. substat_2_grind = rune["sec_eff"][1][3]
  738. else:
  739. substat_2 = None
  740. substat_2_value = 0
  741. substat_2_enchant = 0
  742. substat_2_grind = 0
  743. if len(rune["sec_eff"]) > 2:
  744. substat_3 = rune["sec_eff"][2][0]
  745. substat_3_value = rune["sec_eff"][2][1]
  746. substat_3_enchant = rune["sec_eff"][2][2]
  747. substat_3_grind = rune["sec_eff"][2][3]
  748. else:
  749. substat_3 = None
  750. substat_3_value = 0
  751. substat_3_enchant = 0
  752. substat_3_grind = 0
  753. if len(rune["sec_eff"]) > 3:
  754. substat_4 = rune["sec_eff"][3][0]
  755. substat_4_value = rune["sec_eff"][3][1]
  756. substat_4_enchant = rune["sec_eff"][3][2]
  757. substat_4_grind = rune["sec_eff"][3][3]
  758. else:
  759. substat_4 = None
  760. substat_4_value = 0
  761. substat_4_enchant = 0
  762. substat_4_grind = 0
  763. # Calculate efficiences
  764. efficiency = 0
  765. substats = []
  766. efficiency += float(MAIN_STAT_VALUES[main_stat][stars][15]) / float(MAIN_STAT_VALUES[main_stat][6][15])
  767. if innate_stat is not None:
  768. efficiency += innate_stat_value / float(SUBSTAT_INCREMENTS[innate_stat][6] * 5)
  769. if substat_1 is not None:
  770. substats.append(substat_1)
  771. efficiency += substat_1_value / float(SUBSTAT_INCREMENTS[substat_1][6] * 5)
  772. if substat_2 is not None:
  773. substats.append(substat_2)
  774. efficiency += substat_2_value / float(SUBSTAT_INCREMENTS[substat_2][6] * 5)
  775. if substat_3 is not None:
  776. substats.append(substat_3)
  777. efficiency += substat_3_value / float(SUBSTAT_INCREMENTS[substat_3][6] * 5)
  778. if substat_4 is not None:
  779. substats.append(substat_4)
  780. efficiency += substat_4_value / float(SUBSTAT_INCREMENTS[substat_4][6] * 5)
  781. efficiency = efficiency / 2.8 * 100
  782. max_efficiency = get_max_efficiency(substats, efficiency, level, stars, original_quality)
  783. if efficiency > 100:
  784. efficiency = 100;
  785. if max_efficiency > 100:
  786. max_efficiency = 100;
  787. # TODO: Locked status not in JSO file.
  788. locked = 0
  789. insert(db, "rune", (uid, id, assigned_to, runeset, slot, stars, ancient, level, quality, original_quality, value, efficiency, max_efficiency, main_stat, main_stat_value, innate_stat, innate_stat_value, substat_1, substat_1_value, substat_1_enchant, substat_1_grind, substat_2, substat_2_value, substat_2_enchant, substat_2_grind, substat_3, substat_3_value, substat_3_enchant, substat_3_grind, substat_4, substat_4_value, substat_4_enchant, substat_4_grind, locked))
  790. db.commit()
  791. """
  792. Parses artifact data (table artifact).
  793. :param db: Sqlite database connection.
  794. :param data: Data in json format.
  795. """
  796. def parseArtifacts(db, data):
  797. print("Parsing artifacts...")
  798. uid = data["wizard_info"]["wizard_id"]
  799. # Unequipped artifacts
  800. for artifact in data["artifacts"]:
  801. id = artifact["rid"]
  802. assigned_to = None
  803. type = artifact["type"]
  804. attribute = artifact["attribute"]
  805. if attribute == 0:
  806. attribute = None
  807. archetype = artifact["unit_style"]
  808. if archetype == 0:
  809. archetype = None
  810. level = artifact["level"]
  811. quality = artifact["rank"]
  812. original_quality = artifact["natural_rank"]
  813. # TODO: Vaue not in JSON file. Can it be calculated?
  814. value = 0;
  815. main_stat = artifact["pri_effect"][0]
  816. main_stat_value = artifact["pri_effect"][1]
  817. locked = artifact["locked"]
  818. if len(artifact["sec_effects"]) > 0:
  819. effect_1 = artifact["sec_effects"][0][0]
  820. effect_1_value = artifact["sec_effects"][0][1]
  821. effect_1_enchant = artifact["sec_effects"][0][2]
  822. effect_1_grind = artifact["sec_effects"][0][3]
  823. else:
  824. effect_1 = None
  825. effect_1_value = 0
  826. effect_1_enchant = 0
  827. effect_1_grind = 0
  828. if len(artifact["sec_effects"]) > 1:
  829. effect_2 = artifact["sec_effects"][1][0]
  830. effect_2_value = artifact["sec_effects"][1][1]
  831. effect_2_enchant = artifact["sec_effects"][1][2]
  832. effect_2_grind = artifact["sec_effects"][1][3]
  833. else:
  834. effect_2 = None
  835. effect_2_value = 0
  836. effect_2_enchant = 0
  837. effect_2_grind = 0
  838. if len(artifact["sec_effects"]) > 2:
  839. effect_3 = artifact["sec_effects"][2][0]
  840. effect_3_value = artifact["sec_effects"][2][1]
  841. effect_3_enchant = artifact["sec_effects"][2][2]
  842. effect_3_grind = artifact["sec_effects"][2][3]
  843. else:
  844. effect_3 = None
  845. effect_3_value = 0
  846. effect_3_enchant = 0
  847. effect_3_grind = 0
  848. if len(artifact["sec_effects"]) > 3:
  849. effect_4 = artifact["sec_effects"][3][0]
  850. effect_4_value = artifact["sec_effects"][3][1]
  851. effect_4_enchant = artifact["sec_effects"][3][2]
  852. effect_4_grind = artifact["sec_effects"][3][3]
  853. else:
  854. effect_4 = None
  855. effect_4_value = 0
  856. effect_4_enchant = 0
  857. effect_4_grind = 0
  858. # TODO: efficiences
  859. efficiency = 0
  860. max_efficiency = 0
  861. insert(db, "artifact", (uid, id, assigned_to, type, attribute, archetype, level, quality, original_quality, value, efficiency, max_efficiency, main_stat, main_stat_value, effect_1, effect_1_value, effect_1_enchant, effect_1_grind, effect_2, effect_2_value, effect_2_enchant, effect_2_grind, effect_3, effect_3_value, effect_3_enchant, effect_3_grind, effect_4, effect_4_value, effect_4_enchant, effect_4_grind, locked))
  862. # Equipped artifacts
  863. for mon in data["unit_list"]:
  864. for artifact in mon["artifacts"]:
  865. id = artifact["rid"]
  866. assigned_to = artifact["occupied_id"]
  867. if assigned_to == 0:
  868. assigned_to = None
  869. type = artifact["type"]
  870. attribute = artifact["attribute"]
  871. if attribute == 0:
  872. attribute = None
  873. archetype = artifact["unit_style"]
  874. if archetype == 0:
  875. archetype = None
  876. level = artifact["level"]
  877. quality = artifact["rank"]
  878. original_quality = artifact["natural_rank"]
  879. # TODO: Vaue not in JSON file. Can it be calculated?
  880. value = 0;
  881. main_stat = artifact["pri_effect"][0]
  882. main_stat_value = artifact["pri_effect"][1]
  883. locked = artifact["locked"]
  884. if len(artifact["sec_effects"]) > 0:
  885. effect_1 = artifact["sec_effects"][0][0]
  886. effect_1_value = artifact["sec_effects"][0][1]
  887. effect_1_enchant = artifact["sec_effects"][0][2]
  888. effect_1_grind = artifact["sec_effects"][0][3]
  889. else:
  890. effect_1 = None
  891. effect_1_value = 0
  892. effect_1_enchant = 0
  893. effect_1_grind = 0
  894. if len(artifact["sec_effects"]) > 1:
  895. effect_2 = artifact["sec_effects"][1][0]
  896. effect_2_value = artifact["sec_effects"][1][1]
  897. effect_2_enchant = artifact["sec_effects"][1][2]
  898. effect_2_grind = artifact["sec_effects"][1][3]
  899. else:
  900. effect_2 = None
  901. effect_2_value = 0
  902. effect_2_enchant = 0
  903. effect_2_grind = 0
  904. if len(artifact["sec_effects"]) > 2:
  905. effect_3 = artifact["sec_effects"][2][0]
  906. effect_3_value = artifact["sec_effects"][2][1]
  907. effect_3_enchant = artifact["sec_effects"][2][2]
  908. effect_3_grind = artifact["sec_effects"][2][3]
  909. else:
  910. effect_3 = None
  911. effect_3_value = 0
  912. effect_3_enchant = 0
  913. effect_3_grind = 0
  914. if len(artifact["sec_effects"]) > 3:
  915. effect_4 = artifact["sec_effects"][3][0]
  916. effect_4_value = artifact["sec_effects"][3][1]
  917. effect_4_enchant = artifact["sec_effects"][3][2]
  918. effect_4_grind = artifact["sec_effects"][3][3]
  919. else:
  920. effect_4 = None
  921. effect_4_value = 0
  922. effect_4_enchant = 0
  923. effect_4_grind = 0
  924. # TODO: efficiences
  925. efficiency = 0
  926. max_efficiency = 0
  927. insert(db, "artifact", (uid, id, assigned_to, type, attribute, archetype, level, quality, original_quality, value, efficiency, max_efficiency, main_stat, main_stat_value, effect_1, effect_1_value, effect_1_enchant, effect_1_grind, effect_2, effect_2_value, effect_2_enchant, effect_2_grind, effect_3, effect_3_value, effect_3_enchant, effect_3_grind, effect_4, effect_4_value, effect_4_enchant, effect_4_grind, locked))
  928. """
  929. calculates the max efficiency of a rune.
  930. :param substats: Array of substat types, dont include empty ones.
  931. :param efficiency: Current rune efficiency.
  932. :param level: Current rune level.
  933. :param stars: Rune stars.
  934. :param quality: Rune original quality.
  935. :return Max. efficiency.
  936. """
  937. def get_max_efficiency(substats, efficiency, level, stars, quality):
  938. STAT_HP = 1
  939. STAT_HP_PCT = 2
  940. STAT_ATK = 3
  941. STAT_ATK_PCT = 4
  942. STAT_DEF = 5
  943. STAT_DEF_PCT = 6
  944. STAT_SPD = 8
  945. STAT_CRIT_RATE_PCT = 9
  946. STAT_CRIT_DMG_PCT = 10
  947. STAT_RESIST_PCT = 11
  948. STAT_ACCURACY_PCT = 12
  949. SUBSTAT_INCREMENTS = {
  950. # [stat][stars]: value
  951. STAT_HP: {
  952. 1: 60,
  953. 2: 105,
  954. 3: 165,
  955. 4: 225,
  956. 5: 300,
  957. 6: 375,
  958. },
  959. STAT_HP_PCT: {
  960. 1: 2,
  961. 2: 3,
  962. 3: 5,
  963. 4: 6,
  964. 5: 7,
  965. 6: 8,
  966. },
  967. STAT_ATK: {
  968. 1: 4,
  969. 2: 5,
  970. 3: 8,
  971. 4: 10,
  972. 5: 15,
  973. 6: 20,
  974. },
  975. STAT_ATK_PCT: {
  976. 1: 2,
  977. 2: 3,
  978. 3: 5,
  979. 4: 6,
  980. 5: 7,
  981. 6: 8,
  982. },
  983. STAT_DEF: {
  984. 1: 4,
  985. 2: 5,
  986. 3: 8,
  987. 4: 10,
  988. 5: 15,
  989. 6: 20,
  990. },
  991. STAT_DEF_PCT: {
  992. 1: 2,
  993. 2: 3,
  994. 3: 5,
  995. 4: 6,
  996. 5: 7,
  997. 6: 8,
  998. },
  999. STAT_SPD: {
  1000. 1: 1,
  1001. 2: 2,
  1002. 3: 3,
  1003. 4: 4,
  1004. 5: 5,
  1005. 6: 6,
  1006. },
  1007. STAT_CRIT_RATE_PCT: {
  1008. 1: 1,
  1009. 2: 2,
  1010. 3: 3,
  1011. 4: 4,
  1012. 5: 5,
  1013. 6: 6,
  1014. },
  1015. STAT_CRIT_DMG_PCT: {
  1016. 1: 2,
  1017. 2: 3,
  1018. 3: 4,
  1019. 4: 5,
  1020. 5: 6,
  1021. 6: 7,
  1022. },
  1023. STAT_RESIST_PCT: {
  1024. 1: 2,
  1025. 2: 3,
  1026. 3: 5,
  1027. 4: 6,
  1028. 5: 7,
  1029. 6: 8,
  1030. },
  1031. STAT_ACCURACY_PCT: {
  1032. 1: 2,
  1033. 2: 3,
  1034. 3: 5,
  1035. 4: 6,
  1036. 5: 7,
  1037. 6: 8,
  1038. },
  1039. }
  1040. UPGRADE_VALUES = {
  1041. rune_type: {
  1042. stars: value/level_data[6]
  1043. for stars, value in level_data.items()
  1044. }
  1045. for rune_type, level_data in SUBSTAT_INCREMENTS.items()
  1046. }
  1047. #substat_upgrades_remaining = (4 - math.floor((min(level, 12) / 3))) - (3 - quality)
  1048. substat_upgrades_remaining = (4 - math.floor((min(level, 12) / 3))) - max((3 - quality), 0)
  1049. new_stats = max(min(4 - len(substats), substat_upgrades_remaining), 0)
  1050. old_stats = substat_upgrades_remaining - new_stats
  1051. if old_stats > 0:
  1052. # we can repeatedly upgrade the most value of the existing stats
  1053. best_stat = max(
  1054. 0, # ensure max() doesn't error if we only have one stat
  1055. *[UPGRADE_VALUES[stat][stars] for stat in substats]
  1056. )
  1057. efficiency += best_stat * old_stats * 0.2 / 2.8 * 100
  1058. if new_stats:
  1059. # add the top N stats
  1060. available_upgrades = sorted(
  1061. [
  1062. upgrade_value[stars]
  1063. for stat, upgrade_value in UPGRADE_VALUES.items()
  1064. if stat not in substats
  1065. ],
  1066. reverse=True
  1067. )
  1068. efficiency += sum(available_upgrades[:new_stats]) * 0.2 / 2.8 * 100
  1069. return efficiency
  1070. """
  1071. Parses rune craft item data (table rune_craft).
  1072. :param db: Sqlite database connection.
  1073. :param data: Data in json format.
  1074. """
  1075. def parseRuneCraft(db, data):
  1076. print("Parsing rune craft itmes...")
  1077. uid = data["wizard_info"]["wizard_id"]
  1078. for item in data["rune_craft_item_list"]:
  1079. id = item["craft_item_id"]
  1080. type = item["craft_type"]
  1081. value = item["sell_value"]
  1082. amount = str(item["amount"])
  1083. """
  1084. info : 5 or 6 digit number: RRSSQ
  1085. RR: Rune
  1086. SS: Stat
  1087. Q: Quality
  1088. """
  1089. info = str(item["craft_type_id"])
  1090. quality = int(info[-1:])
  1091. stat = int(info[-4:-2])
  1092. rune = int(info[:-4])
  1093. insert(db, "rune_craft", (uid, id, type, quality, rune, stat, value, amount))
  1094. """
  1095. Parses guild data (tables guild, guild_member).
  1096. :param db: Sqlite database connection.
  1097. :param data: Data in json format.
  1098. """
  1099. def parseGuild(db, data):
  1100. print("Parsing guild...")
  1101. cursor = db.cursor()
  1102. if (data["guild"]["guild_info"] == None):
  1103. # No guild
  1104. return
  1105. if len(data["guild"]["guild_info"]) < 1:
  1106. return
  1107. guild = data["guild"]["guild_info"]
  1108. id = guild["guild_id"]
  1109. name = guild["name"]
  1110. level = guild["level"]
  1111. experience = guild["experience"]
  1112. recruiting = guild["recruit_status"]
  1113. members = guild["member_now"]
  1114. leader = guild["master_wizard_id"]
  1115. comment = guild["comment"]
  1116. notice = guild["notice"]
  1117. # Delete selected guild before inserting
  1118. cursor.execute("DELETE FROM guild WHERE id = ?", [id])
  1119. cursor.execute("DELETE FROM guild_member WHERE guild = ?", [id])
  1120. db.commit()
  1121. insert(db, "guild", (id, name, level, experience, recruiting, members, leader, comment, notice))
  1122. # Single quates ahead, dirty fix
  1123. for k, member in data["guild"]["guild_members"].items():
  1124. m = json.loads(str(member).replace("'", '"'))
  1125. id = m["wizard_id"]
  1126. guild = member["guild_id"]
  1127. grade = member["grade"]
  1128. name = member["wizard_name"]
  1129. level = member["wizard_level"]
  1130. rating = member["rating_id"]
  1131. arena_score = member["arena_score"]
  1132. joined = member["join_timestamp"]
  1133. last_login = member["last_login_timestamp"]
  1134. in_war = 0 # Checked later
  1135. has_defense = 0 # Checked later
  1136. insert(db, "guild_member", (id, guild, grade, name, level, rating, arena_score, joined, last_login, in_war, has_defense))
  1137. for war in data["guildwar_member_list"]:
  1138. cursor.execute("UPDATE guild_member SET in_war = 1 WHERE guild = ? AND id = ?;", (war["guild_id"], war["wizard_id"]))
  1139. for defense in data["guild_member_defense_list"]:
  1140. if len(defense["unit_list"]) > 0:
  1141. cursor.execute("UPDATE guild_member SET has_defense = 1 WHERE id = ?", [defense["wizard_id"]])
  1142. db.commit()
  1143. cursor.close()
  1144. """
  1145. Deletes teams whose members no longer exists.
  1146. :param db: Sqlite database connection.
  1147. """
  1148. def reconfigureTeams(db):
  1149. print("Fixing teams...")
  1150. uid = data["wizard_info"]["wizard_id"]
  1151. cursor = db.cursor()
  1152. cursor.execute("DELETE FROM team WHERE id IN (SELECT DISTINCT team FROM team_unit WHERE unit NOT IN (SELECT DISTINCT id FROM unit WHERE uid = ?)) AND uid = ?", (uid, uid))
  1153. cursor.execute("DELETE FROM team_unit WHERE unit NOT IN (SELECT DISTINCT id FROM unit) OR team NOT IN (SELECT DISTINCT id FROM team);")
  1154. db.commit()
  1155. cursor.close()
  1156. """
  1157. Begin script
  1158. """
  1159. try:
  1160. data = readData()
  1161. except Exception as e:
  1162. print("Error reading data: " + str(e))
  1163. sys.exit(400)
  1164. try:
  1165. key = readKey()
  1166. except Exception as e:
  1167. print("Error fetching API key: " + str(e))
  1168. sys.exit(400)
  1169. try:
  1170. db = openDatabase()
  1171. except Exception as e:
  1172. print("Error opening database: " + str(e))
  1173. sys.exit(500)
  1174. if verifyKey(db, data, key) == False:
  1175. print("Invalid API KEY...")
  1176. sys.exit(401)
  1177. else:
  1178. try:
  1179. clearData(db, data)
  1180. except Exception as e:
  1181. print("Error clearing data: " + str(e))
  1182. sys.exit(500)
  1183. try:
  1184. parsePlayer(db, data)
  1185. except Exception as e:
  1186. print("Error parsing player data: " + str(e))
  1187. sys.exit(400)
  1188. try:
  1189. parseScenarios(db, data)
  1190. except Exception as e:
  1191. print("Error parsing scenario data: " + str(e))
  1192. sys.exit(400)
  1193. try:
  1194. parseDefense(db, data)
  1195. except Exception as e:
  1196. print("Error parsing defense data: " + str(e))
  1197. sys.exit(400)
  1198. try:
  1199. parseBuildings(db, data)
  1200. except Exception as e:
  1201. print("Error parsing building data: " + str(e))
  1202. sys.exit(400)
  1203. try:
  1204. parseDecorations(db, data)
  1205. except Exception as e:
  1206. print("Error parsing decoration data: " + str(e))
  1207. sys.exit(400)
  1208. # TODO homunculus_skill_list
  1209. try:
  1210. parseUnits(db, data)
  1211. except Exception as e:
  1212. print("Error parsing unit data: " + str(e))
  1213. sys.exit(400)
  1214. try:
  1215. parseSummonSpecial(db, data)
  1216. except Exception as e:
  1217. print("Error parsing special summon data: " + str(e))
  1218. sys.exit(400)
  1219. try:
  1220. parseInventory(db, data)
  1221. except Exception as e:
  1222. print("Error parsing inventory data: " + str(e))
  1223. sys.exit(400)
  1224. try:
  1225. parseRunes(db, data)
  1226. except Exception as e:
  1227. print("Error parsing rune data: " + str(e))
  1228. sys.exit(400)
  1229. try:
  1230. parseArtifacts(db, data)
  1231. except Exception as e:
  1232. print("Error parsing artifact data: " + str(e))
  1233. sys.exit(400)
  1234. try:
  1235. parseRuneCraft(db, data)
  1236. except Exception as e:
  1237. print("Error parsing rune craft data: " + str(e))
  1238. sys.exit(400)
  1239. try:
  1240. parseGuild(db, data)
  1241. except Exception as e:
  1242. print("Error parsing guild data: " + str(e))
  1243. sys.exit(400)
  1244. try:
  1245. reconfigureTeams(db)
  1246. except Exception as e:
  1247. print("Error reconfiguring teams: " + str(e))
  1248. sys.exit(400)
  1249. try:
  1250. closeDatabase(db)
  1251. except Exception as e:
  1252. print("Error closing database: " + str(e))
  1253. # No error
  1254. sys.exit(200);