install_user.py 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921
  1. #!/usr/bin/python3
  2. import urllib3
  3. import sys
  4. import os
  5. import requests
  6. import urllib.request
  7. import sqlite3
  8. import wget
  9. import json
  10. import math
  11. FILE = "/home/ivalentin/.SWEX/IVV0-8114048.json"
  12. def readFile(file):
  13. with open(file) as json_file:
  14. data = json.load(json_file)
  15. return data
  16. """
  17. Opens the database file and deletes from the user tables
  18. :param name: The path to the sqlite database.
  19. :returns: Connection to the database.
  20. :raises IntegrityError: The queryes couldn't bre executed.
  21. :raises IOError: The sqlite file couldn't be created.
  22. """
  23. def openDatabase(name):
  24. global OPT_CREATE_DB
  25. print('Configuring database...')
  26. try:
  27. db = sqlite3.connect(name)
  28. cursor = db.cursor()
  29. cursor.execute('''DELETE FROM scenario''')
  30. cursor.execute('''DELETE FROM defense''')
  31. cursor.execute('''DELETE FROM unit''')
  32. cursor.execute('''DELETE FROM unit_skill''')
  33. cursor.execute('''DELETE FROM rune''')
  34. cursor.execute('''DELETE FROM player''')
  35. cursor.execute('''DELETE FROM building''')
  36. cursor.execute('''DELETE FROM decoration''')
  37. cursor.execute('''DELETE FROM inventory''')
  38. cursor.execute('''DELETE FROM summon_special''')
  39. cursor.execute('''DELETE FROM guild''')
  40. cursor.execute('''DELETE FROM guild_member''')
  41. cursor.execute('''DELETE FROM rune_craft''')
  42. db.commit()
  43. cursor.close()
  44. return db
  45. except sqlite3.IntegrityError as e:
  46. print("Error executing statement: " + str(e))
  47. raise
  48. except IOError as e:
  49. print("I/O Error creating database " + name + ": " + str(e))
  50. raise
  51. """
  52. Inserts a row into the database.
  53. :param db: Connection to the database.
  54. :param table: Name of the table to insert into.
  55. :param values: List of values to insert.
  56. :raises IntegrityError: The insert query was unsuccesfull.
  57. """
  58. def insert(db, table, values):
  59. cursor = db.cursor()
  60. placeholders = ''
  61. for x in range(0, len(values)):
  62. placeholders = placeholders + '?, '
  63. placeholders = placeholders[:len(placeholders) - 2]
  64. query = 'INSERT INTO ' + table + ' VALUES (' + placeholders + ')'
  65. try:
  66. cursor.execute(query, values)
  67. except sqlite3.IntegrityError as e:
  68. print('Error Inserting into ' + table + ' with values (' + str(values) + '): ' + str(e))
  69. raise
  70. cursor.close;
  71. """
  72. Parses player (table player).
  73. :param db: Sqlite database connection.
  74. :param data: Data in SWEX json file.
  75. """
  76. def parsePlayer(db, data):
  77. print("Parsing player...")
  78. uid = data["wizard_info"]["wizard_id"]
  79. name = data["wizard_info"]["wizard_name"]
  80. mana = data["wizard_info"]["wizard_mana"]
  81. crystal = data["wizard_info"]["wizard_crystal"]
  82. country = data["wizard_info"]["wizard_last_country"]
  83. lang = data["wizard_info"]["wizard_last_lang"]
  84. level = data["wizard_info"]["wizard_level"]
  85. experience = data["wizard_info"]["experience"]
  86. energy = data["wizard_info"]["wizard_energy"]
  87. energy_max = data["wizard_info"]["energy_max"]
  88. energy_per_min = data["wizard_info"]["energy_per_min"]
  89. arena_energy = data["wizard_info"]["arena_energy"]
  90. arena_energy_max = data["wizard_info"]["arena_energy_max"]
  91. rep = data["wizard_info"]["rep_unit_id"]
  92. social_point = data["wizard_info"]["social_point_current"]
  93. honor_point = data["wizard_info"]["honor_point"]
  94. guild_point = data["wizard_info"]["guild_point"]
  95. darkportal_energy = data["wizard_info"]["darkportal_energy"]
  96. darkportal_energy_max = data["wizard_info"]["darkportal_energy_max"]
  97. dimension_energy = data["dimension_hole_info"]["energy"]
  98. dimension_energy_max = data["dimension_hole_info"]["energy_max"]
  99. costume_point = data["wizard_info"]["costume_point"]
  100. costume_point_max = data["wizard_info"]["costume_point_max"]
  101. honor_medal = data["wizard_info"]["honor_medal"]
  102. honor_mark = data["wizard_info"]["honor_mark"]
  103. event_coin = data["wizard_info"]["event_coin"]
  104. storage_slots = data["unit_depository_slots"]["number"]
  105. island_upgrade = 0
  106. for island in data["island_info"]:
  107. if island["id"] <= 7 and island["open"] == 1:
  108. island_upgrade = island["id"]
  109. insert(db, "player", (uid, name, mana, crystal, country, lang, level, experience, energy, energy_max, energy_per_min, arena_energy, arena_energy_max, rep, social_point, honor_point, guild_point, darkportal_energy, darkportal_energy_max, dimension_energy, dimension_energy_max, costume_point, costume_point_max, honor_medal, honor_medal, event_coin, storage_slots, island_upgrade))
  110. db.commit()
  111. """
  112. Parses scenarios (table scenario).
  113. :param db: Sqlite database connection.
  114. :param data: Data in SWEX json file.
  115. """
  116. def parseScenarios(db, data):
  117. print("Parsing scenarios...")
  118. uid = data["wizard_info"]["wizard_id"]
  119. for sce in data["scenario_list"]:
  120. region = sce["region_id"]
  121. difficulty = sce["difficulty"]
  122. cleared = sce["cleared"]
  123. max_cleared = 0
  124. for stage in sce["stage_list"]:
  125. if stage["cleared"] == 1:
  126. max_cleared = stage["stage_no"]
  127. insert(db, "scenario", (uid, region, difficulty, cleared, max_cleared))
  128. db.commit()
  129. """
  130. Parses arena defense units (table defense).
  131. :param db: Sqlite database connection.
  132. :param data: Data in SWEX json file.
  133. """
  134. def parseDefense(db, data):
  135. print("Parsing arena defense...")
  136. uid = data["wizard_info"]["wizard_id"]
  137. for defense in data["defense_unit_list"]:
  138. unit = defense["unit_id"]
  139. position = defense["pos_id"]
  140. insert(db, "defense", (uid, unit, position))
  141. db.commit()
  142. """
  143. Parses buildings (table building).
  144. :param db: Sqlite database connection.
  145. :param data: Data in SWEX json file.
  146. """
  147. def parseBuildings(db, data):
  148. print("Parsing buildings...")
  149. uid = data["wizard_info"]["wizard_id"]
  150. for bui in data["building_list"]:
  151. building = bui["building_master_id"]
  152. gain = bui["gain_per_hour"]
  153. insert(db, "building", (uid, building, gain))
  154. db.commit()
  155. """
  156. Parses decoration buildings (table dcoration).
  157. :param db: Sqlite database connection.
  158. :param data: Data in SWEX json file.
  159. """
  160. def parseDecorations(db, data):
  161. print("Parsing decorations...")
  162. uid = data["wizard_info"]["wizard_id"]
  163. for bui in data["deco_list"]:
  164. building = bui["master_id"]
  165. level = bui["level"]
  166. insert(db, "decoration", (uid, building, level))
  167. db.commit()
  168. """
  169. Parses unit data (tables unit, unit_skill).
  170. :param db: Sqlite database connection.
  171. :param data: Data in SWEX json file.
  172. """
  173. def parseUnits(db, data):
  174. print("Parsing monsters...")
  175. uid = data["wizard_info"]["wizard_id"]
  176. lock_list = data["unit_lock_list"]
  177. for mon in data["unit_list"]:
  178. id = mon["unit_id"]
  179. building = mon["building_id"]
  180. monster = mon["unit_master_id"]
  181. level = mon["unit_level"]
  182. stars = mon["class"]
  183. hp = mon["con"] * 15 # Always x15
  184. attack = mon["atk"]
  185. defense = mon["def"]
  186. speed = mon["spd"]
  187. crit_rate = mon["critical_rate"]
  188. crit_damage = mon["critical_damage"]
  189. resistance = mon["resist"]
  190. accuracy = mon["accuracy"]
  191. experience = mon["experience"]
  192. exp_gained = mon["exp_gained"]
  193. exp_gain_rate = mon["exp_gain_rate"]
  194. costume = mon["costume_master_id"]
  195. attribute = mon["attribute"]
  196. source = mon["source"]
  197. create_time = mon["create_time"]
  198. homunculus = mon["homunculus"]
  199. homunculus_name = mon["homunculus_name"]
  200. lock = 0
  201. if id in lock_list:
  202. lock = 1
  203. 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))
  204. for skill in mon["skills"]:
  205. skill_id = skill[0]
  206. skill_level = skill[1]
  207. insert(db, "unit_skill", (uid, id, skill_id, skill_level))
  208. db.commit()
  209. """
  210. Parses inventory data (table inventory).
  211. :param db: Sqlite database connection.
  212. :param data: Data in SWEX json file.
  213. """
  214. def parseInventory(db, data):
  215. print("Parsing inventory...")
  216. uid = data["wizard_info"]["wizard_id"]
  217. for item in data["inventory_info"]:
  218. id = item["item_master_id"]
  219. type = item["item_master_type"]
  220. # TODO: Master id?
  221. amount = item["item_quantity"]
  222. insert(db, "inventory", (uid, id, type, amount))
  223. """
  224. Parses summon stone list (table summon_special).
  225. :param db: Sqlite database connection.
  226. :param data: Data in SWEX json file.
  227. """
  228. def parseSummonSpecial(db, data):
  229. print("Parsing Summon stone monster list...")
  230. uid = data["wizard_info"]["wizard_id"]
  231. for unit in data["summon_special_info"]["this"]:
  232. insert(db, "summon_special", (0, unit))
  233. for unit in data["summon_special_info"]["next"]:
  234. insert(db, "summon_special", (1, unit))
  235. for unit in data["summon_special_info"]["third"]:
  236. insert(db, "summon_special", (2, unit))
  237. for unit in data["summon_special_info"]["fourth"]:
  238. insert(db, "summon_special", (3, unit))
  239. """
  240. Parses rune data (table rune).
  241. :param db: Sqlite database connection.
  242. :param data: Data in SWEX json file.
  243. """
  244. def parseRunes(db, data):
  245. print("Parsing runes...")
  246. STAT_HP = 1
  247. STAT_HP_PCT = 2
  248. STAT_ATK = 3
  249. STAT_ATK_PCT = 4
  250. STAT_DEF = 5
  251. STAT_DEF_PCT = 6
  252. STAT_SPD = 8
  253. STAT_CRIT_RATE_PCT = 9
  254. STAT_CRIT_DMG_PCT = 10
  255. STAT_RESIST_PCT = 11
  256. STAT_ACCURACY_PCT = 12
  257. MAIN_STAT_VALUES = {
  258. # [stat][stars][level]: value
  259. STAT_HP: {
  260. 1: [40, 85, 130, 175, 220, 265, 310, 355, 400, 445, 490, 535, 580, 625, 670, 804],
  261. 2: [70, 130, 190, 250, 310, 370, 430, 490, 550, 610, 670, 730, 790, 850, 910, 1092],
  262. 3: [100, 175, 250, 325, 400, 475, 550, 625, 700, 775, 850, 925, 1000, 1075, 1150, 1380],
  263. 4: [160, 250, 340, 430, 520, 610, 700, 790, 880, 970, 1060, 1150, 1240, 1330, 1420, 1704],
  264. 5: [270, 375, 480, 585, 690, 795, 900, 1005, 1110, 1215, 1320, 1425, 1530, 1635, 1740, 2088],
  265. 6: [360, 480, 600, 720, 840, 960, 1080, 1200, 1320, 1440, 1560, 1680, 1800, 1920, 2040, 2448],
  266. },
  267. STAT_HP_PCT: {
  268. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  269. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  270. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  271. 4: [5, 7, 9, 11, 13, 16, 18, 20, 22, 24, 27, 29, 31, 33, 36, 43],
  272. 5: [8, 10, 12, 15, 17, 20, 22, 24, 27, 29, 32, 34, 37, 40, 43, 51],
  273. 6: [11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 63],
  274. },
  275. STAT_ATK: {
  276. 1: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 54],
  277. 2: [5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 73],
  278. 3: [7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77, 92],
  279. 4: [10, 16, 22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94, 112],
  280. 5: [15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85, 92, 99, 106, 113, 135],
  281. 6: [22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 160],
  282. },
  283. STAT_ATK_PCT: {
  284. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  285. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  286. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  287. 4: [5, 7, 9, 11, 13, 16, 18, 20, 22, 24, 27, 29, 31, 33, 36, 43],
  288. 5: [8, 10, 12, 15, 17, 20, 22, 24, 27, 29, 32, 34, 37, 40, 43, 51],
  289. 6: [11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 63],
  290. },
  291. STAT_DEF: {
  292. 1: [3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 54],
  293. 2: [5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 73],
  294. 3: [7, 12, 17, 22, 27, 32, 37, 42, 47, 52, 57, 62, 67, 72, 77, 92],
  295. 4: [10, 16, 22, 28, 34, 40, 46, 52, 58, 64, 70, 76, 82, 88, 94, 112],
  296. 5: [15, 22, 29, 36, 43, 50, 57, 64, 71, 78, 85, 92, 99, 106, 113, 135],
  297. 6: [22, 30, 38, 46, 54, 62, 70, 78, 86, 94, 102, 110, 118, 126, 134, 160],
  298. },
  299. STAT_DEF_PCT: {
  300. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  301. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  302. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  303. 4: [5, 7, 9, 11, 13, 16, 18, 20, 22, 24, 27, 29, 31, 33, 36, 43],
  304. 5: [8, 10, 12, 15, 17, 20, 22, 24, 27, 29, 32, 34, 37, 40, 43, 51],
  305. 6: [11, 14, 17, 20, 23, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 63],
  306. },
  307. STAT_SPD: {
  308. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  309. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  310. 3: [3, 4, 5, 6, 8, 9, 10, 12, 13, 14, 16, 17, 18, 19, 21, 25],
  311. 4: [4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22, 23, 25, 30],
  312. 5: [5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 39],
  313. 6: [7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 42],
  314. },
  315. STAT_CRIT_RATE_PCT: {
  316. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  317. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  318. 3: [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 37],
  319. 4: [4, 6, 8, 11, 13, 15, 17, 19, 22, 24, 26, 28, 30, 33, 35, 41],
  320. 5: [5, 7, 10, 12, 15, 17, 19, 22, 24, 27, 29, 31, 34, 36, 39, 47],
  321. 6: [7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46, 49, 58],
  322. },
  323. STAT_CRIT_DMG_PCT: {
  324. 1: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  325. 2: [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 37],
  326. 3: [4, 6, 9, 11, 13, 16, 18, 20, 22, 25, 27, 29, 32, 34, 36, 43],
  327. 4: [6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 57],
  328. 5: [8, 11, 15, 18, 21, 25, 28, 31, 34, 38, 41, 44, 48, 51, 54, 65],
  329. 6: [11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51, 55, 59, 63, 67, 80],
  330. },
  331. STAT_RESIST_PCT: {
  332. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  333. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  334. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  335. 4: [6, 8, 10, 13, 15, 17, 19, 21, 24, 26, 28, 30, 32, 35, 37, 44],
  336. 5: [9, 11, 14, 16, 19, 21, 23, 26, 28, 31, 33, 35, 38, 40, 43, 51],
  337. 6: [12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 64],
  338. },
  339. STAT_ACCURACY_PCT: {
  340. 1: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 18],
  341. 2: [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 19],
  342. 3: [4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 38],
  343. 4: [6, 8, 10, 13, 15, 17, 19, 21, 24, 26, 28, 30, 32, 35, 37, 44],
  344. 5: [9, 11, 14, 16, 19, 21, 23, 26, 28, 31, 33, 35, 38, 40, 43, 51],
  345. 6: [12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, 54, 64],
  346. },
  347. }
  348. SUBSTAT_INCREMENTS = {
  349. # [stat][stars]: value
  350. # Max possible substat value can be found by multiplying by 5
  351. STAT_HP: {
  352. 1: 60,
  353. 2: 105,
  354. 3: 165,
  355. 4: 225,
  356. 5: 300,
  357. 6: 375,
  358. },
  359. STAT_HP_PCT: {
  360. 1: 2,
  361. 2: 3,
  362. 3: 5,
  363. 4: 6,
  364. 5: 7,
  365. 6: 8,
  366. },
  367. STAT_ATK: {
  368. 1: 4,
  369. 2: 5,
  370. 3: 8,
  371. 4: 10,
  372. 5: 15,
  373. 6: 20,
  374. },
  375. STAT_ATK_PCT: {
  376. 1: 2,
  377. 2: 3,
  378. 3: 5,
  379. 4: 6,
  380. 5: 7,
  381. 6: 8,
  382. },
  383. STAT_DEF: {
  384. 1: 4,
  385. 2: 5,
  386. 3: 8,
  387. 4: 10,
  388. 5: 15,
  389. 6: 20,
  390. },
  391. STAT_DEF_PCT: {
  392. 1: 2,
  393. 2: 3,
  394. 3: 5,
  395. 4: 6,
  396. 5: 7,
  397. 6: 8,
  398. },
  399. STAT_SPD: {
  400. 1: 1,
  401. 2: 2,
  402. 3: 3,
  403. 4: 4,
  404. 5: 5,
  405. 6: 6,
  406. },
  407. STAT_CRIT_RATE_PCT: {
  408. 1: 1,
  409. 2: 2,
  410. 3: 3,
  411. 4: 4,
  412. 5: 5,
  413. 6: 6,
  414. },
  415. STAT_CRIT_DMG_PCT: {
  416. 1: 2,
  417. 2: 3,
  418. 3: 4,
  419. 4: 5,
  420. 5: 6,
  421. 6: 7,
  422. },
  423. STAT_RESIST_PCT: {
  424. 1: 2,
  425. 2: 3,
  426. 3: 5,
  427. 4: 6,
  428. 5: 7,
  429. 6: 8,
  430. },
  431. STAT_ACCURACY_PCT: {
  432. 1: 2,
  433. 2: 3,
  434. 3: 5,
  435. 4: 6,
  436. 5: 7,
  437. 6: 8,
  438. },
  439. }
  440. uid = data["wizard_info"]["wizard_id"]
  441. # Unequipped runes
  442. for rune in data["runes"]:
  443. id = rune["rune_id"]
  444. assigned_to = rune["occupied_id"]
  445. if assigned_to == 0:
  446. assigned_to = None
  447. runeset = rune["set_id"]
  448. slot = rune["slot_no"]
  449. stars = rune["class"]
  450. ancient = 0
  451. level = rune["upgrade_curr"]
  452. original_quality = rune["extra"]
  453. value = rune["sell_value"]
  454. if stars > 10:
  455. ancient = 1
  456. stars = stars - 10
  457. original_quality = original_quality - 10
  458. if level >= 12:
  459. quality = 4
  460. elif level >= 9:
  461. quality = 3
  462. elif level >= 6:
  463. quality = 2
  464. elif level >= 3:
  465. quality = 1
  466. else:
  467. quality = 0
  468. if original_quality > quality:
  469. quality = original_quality
  470. # TODO: Calculate
  471. #efficiency = rune["efficiency"]
  472. #max_efficiency = rune["max_efficiency"]
  473. efficiency = 0
  474. max_efficiency = 0
  475. main_stat = rune["pri_eff"][0]
  476. main_stat_value = rune["pri_eff"][1]
  477. if rune["prefix_eff"][0] > 0:
  478. innate_stat = rune["prefix_eff"][0]
  479. innate_stat_value = rune["prefix_eff"][1]
  480. else:
  481. innate_stat = None
  482. innate_stat_value = 0
  483. if len(rune["sec_eff"]) > 0:
  484. substat_1 = rune["sec_eff"][0][0]
  485. substat_1_value = rune["sec_eff"][0][1]
  486. substat_1_enchant = rune["sec_eff"][0][2]
  487. substat_1_grind = rune["sec_eff"][0][3]
  488. else:
  489. substat_1 = None
  490. substat_1_value = 0
  491. substat_1_enchant = 0
  492. substat_1_grind = 0
  493. if len(rune["sec_eff"]) > 1:
  494. substat_2 = rune["sec_eff"][1][0]
  495. substat_2_value = rune["sec_eff"][1][1]
  496. substat_2_enchant = rune["sec_eff"][1][2]
  497. substat_2_grind = rune["sec_eff"][1][3]
  498. else:
  499. substat_2 = None
  500. substat_2_value = 0
  501. substat_2_enchant = 0
  502. substat_2_grind = 0
  503. if len(rune["sec_eff"]) > 2:
  504. substat_3 = rune["sec_eff"][2][0]
  505. substat_3_value = rune["sec_eff"][2][1]
  506. substat_3_enchant = rune["sec_eff"][2][2]
  507. substat_3_grind = rune["sec_eff"][2][3]
  508. else:
  509. substat_3 = None
  510. substat_3_value = 0
  511. substat_3_enchant = 0
  512. substat_3_grind = 0
  513. if len(rune["sec_eff"]) > 3:
  514. substat_4 = rune["sec_eff"][0][0]
  515. substat_4_value = rune["sec_eff"][3][1]
  516. substat_4_enchant = rune["sec_eff"][3][2]
  517. substat_4_grind = rune["sec_eff"][3][3]
  518. else:
  519. substat_4 = None
  520. substat_4_value = 0
  521. substat_4_enchant = 0
  522. substat_4_grind = 0
  523. # Calculate efficiences
  524. efficiency = 0
  525. substats = []
  526. efficiency += float(MAIN_STAT_VALUES[main_stat][stars][15]) / float(MAIN_STAT_VALUES[main_stat][6][15])
  527. if innate_stat is not None:
  528. efficiency += innate_stat_value / float(SUBSTAT_INCREMENTS[innate_stat][6] * 5)
  529. if substat_1 is not None:
  530. substats.append(substat_1)
  531. efficiency += substat_1_value / float(SUBSTAT_INCREMENTS[substat_1][6] * 5)
  532. if substat_2 is not None:
  533. substats.append(substat_2)
  534. efficiency += substat_2_value / float(SUBSTAT_INCREMENTS[substat_2][6] * 5)
  535. if substat_3 is not None:
  536. substats.append(substat_3)
  537. efficiency += substat_3_value / float(SUBSTAT_INCREMENTS[substat_3][6] * 5)
  538. if substat_4 is not None:
  539. substats.append(substat_4)
  540. efficiency += substat_4_value / float(SUBSTAT_INCREMENTS[substat_4][6] * 5)
  541. efficiency = efficiency / 2.8 * 100
  542. max_efficiency = get_max_efficiency(substats, efficiency, level, stars, original_quality)
  543. 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))
  544. # Equipped runes
  545. for mon in data["unit_list"]:
  546. for rune in mon["runes"]:
  547. id = rune["rune_id"]
  548. assigned_to = rune["occupied_id"]
  549. if assigned_to == 0:
  550. assigned_to = None
  551. runeset = rune["set_id"]
  552. slot = rune["slot_no"]
  553. stars = rune["class"]
  554. ancient = 0
  555. level = rune["upgrade_curr"]
  556. original_quality = rune["extra"]
  557. value = rune["sell_value"]
  558. if stars > 10:
  559. ancient = 1
  560. stars = stars - 10
  561. original_quality = original_quality - 10
  562. if level >= 12:
  563. quality = 4
  564. elif level >= 9:
  565. quality = 3
  566. elif level >= 6:
  567. quality = 2
  568. elif level >= 3:
  569. quality = 1
  570. else:
  571. quality = 0
  572. if original_quality > quality:
  573. quality = original_quality
  574. # TODO: Calculate
  575. #efficiency = rune["efficiency"]
  576. #max_efficiency = rune["max_efficiency"]
  577. efficiency = 0
  578. max_efficiency = 0
  579. main_stat = rune["pri_eff"][0]
  580. main_stat_value = rune["pri_eff"][1]
  581. if rune["prefix_eff"][0] > 0:
  582. innate_stat = rune["prefix_eff"][0]
  583. innate_stat_value = rune["prefix_eff"][1]
  584. else:
  585. innate_stat = None
  586. innate_stat_value = 0
  587. if len(rune["sec_eff"]) > 0:
  588. substat_1 = rune["sec_eff"][0][0]
  589. substat_1_value = rune["sec_eff"][0][1]
  590. substat_1_enchant = rune["sec_eff"][0][2]
  591. substat_1_grind = rune["sec_eff"][0][3]
  592. else:
  593. substat_1 = None
  594. substat_1_value = 0
  595. substat_1_enchant = 0
  596. substat_1_grind = 0
  597. if len(rune["sec_eff"]) > 1:
  598. substat_2 = rune["sec_eff"][1][0]
  599. substat_2_value = rune["sec_eff"][1][1]
  600. substat_2_enchant = rune["sec_eff"][1][2]
  601. substat_2_grind = rune["sec_eff"][1][3]
  602. else:
  603. substat_2 = None
  604. substat_2_value = 0
  605. substat_2_enchant = 0
  606. substat_2_grind = 0
  607. if len(rune["sec_eff"]) > 2:
  608. substat_3 = rune["sec_eff"][2][0]
  609. substat_3_value = rune["sec_eff"][2][1]
  610. substat_3_enchant = rune["sec_eff"][2][2]
  611. substat_3_grind = rune["sec_eff"][2][3]
  612. else:
  613. substat_3 = None
  614. substat_3_value = 0
  615. substat_3_enchant = 0
  616. substat_3_grind = 0
  617. if len(rune["sec_eff"]) > 3:
  618. substat_4 = rune["sec_eff"][3][0]
  619. substat_4_value = rune["sec_eff"][3][1]
  620. substat_4_enchant = rune["sec_eff"][3][2]
  621. substat_4_grind = rune["sec_eff"][3][3]
  622. else:
  623. substat_4 = None
  624. substat_4_value = 0
  625. substat_4_enchant = 0
  626. substat_4_grind = 0
  627. # Calculate efficiences
  628. efficiency = 0
  629. substats = []
  630. efficiency += float(MAIN_STAT_VALUES[main_stat][stars][15]) / float(MAIN_STAT_VALUES[main_stat][6][15])
  631. if innate_stat is not None:
  632. efficiency += innate_stat_value / float(SUBSTAT_INCREMENTS[innate_stat][6] * 5)
  633. if substat_1 is not None:
  634. substats.append(substat_1)
  635. efficiency += substat_1_value / float(SUBSTAT_INCREMENTS[substat_1][6] * 5)
  636. if substat_2 is not None:
  637. substats.append(substat_2)
  638. efficiency += substat_2_value / float(SUBSTAT_INCREMENTS[substat_2][6] * 5)
  639. if substat_3 is not None:
  640. substats.append(substat_3)
  641. efficiency += substat_3_value / float(SUBSTAT_INCREMENTS[substat_3][6] * 5)
  642. if substat_4 is not None:
  643. substats.append(substat_4)
  644. efficiency += substat_4_value / float(SUBSTAT_INCREMENTS[substat_4][6] * 5)
  645. efficiency = efficiency / 2.8 * 100
  646. max_efficiency = get_max_efficiency(substats, efficiency, level, stars, original_quality)
  647. 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))
  648. db.commit()
  649. """
  650. calculates the max efficiency of a rune.
  651. :param substats: Array of substat types, dont include empty ones.
  652. :param efficiency: Current rune efficiency.
  653. :param level: Current rune level.
  654. :param stars: Rune stars.
  655. :param quality: Rune original quality.
  656. :return Max. efficiency.
  657. """
  658. def get_max_efficiency(substats, efficiency, level, stars, quality):
  659. STAT_HP = 1
  660. STAT_HP_PCT = 2
  661. STAT_ATK = 3
  662. STAT_ATK_PCT = 4
  663. STAT_DEF = 5
  664. STAT_DEF_PCT = 6
  665. STAT_SPD = 8
  666. STAT_CRIT_RATE_PCT = 9
  667. STAT_CRIT_DMG_PCT = 10
  668. STAT_RESIST_PCT = 11
  669. STAT_ACCURACY_PCT = 12
  670. SUBSTAT_INCREMENTS = {
  671. # [stat][stars]: value
  672. STAT_HP: {
  673. 1: 60,
  674. 2: 105,
  675. 3: 165,
  676. 4: 225,
  677. 5: 300,
  678. 6: 375,
  679. },
  680. STAT_HP_PCT: {
  681. 1: 2,
  682. 2: 3,
  683. 3: 5,
  684. 4: 6,
  685. 5: 7,
  686. 6: 8,
  687. },
  688. STAT_ATK: {
  689. 1: 4,
  690. 2: 5,
  691. 3: 8,
  692. 4: 10,
  693. 5: 15,
  694. 6: 20,
  695. },
  696. STAT_ATK_PCT: {
  697. 1: 2,
  698. 2: 3,
  699. 3: 5,
  700. 4: 6,
  701. 5: 7,
  702. 6: 8,
  703. },
  704. STAT_DEF: {
  705. 1: 4,
  706. 2: 5,
  707. 3: 8,
  708. 4: 10,
  709. 5: 15,
  710. 6: 20,
  711. },
  712. STAT_DEF_PCT: {
  713. 1: 2,
  714. 2: 3,
  715. 3: 5,
  716. 4: 6,
  717. 5: 7,
  718. 6: 8,
  719. },
  720. STAT_SPD: {
  721. 1: 1,
  722. 2: 2,
  723. 3: 3,
  724. 4: 4,
  725. 5: 5,
  726. 6: 6,
  727. },
  728. STAT_CRIT_RATE_PCT: {
  729. 1: 1,
  730. 2: 2,
  731. 3: 3,
  732. 4: 4,
  733. 5: 5,
  734. 6: 6,
  735. },
  736. STAT_CRIT_DMG_PCT: {
  737. 1: 2,
  738. 2: 3,
  739. 3: 4,
  740. 4: 5,
  741. 5: 6,
  742. 6: 7,
  743. },
  744. STAT_RESIST_PCT: {
  745. 1: 2,
  746. 2: 3,
  747. 3: 5,
  748. 4: 6,
  749. 5: 7,
  750. 6: 8,
  751. },
  752. STAT_ACCURACY_PCT: {
  753. 1: 2,
  754. 2: 3,
  755. 3: 5,
  756. 4: 6,
  757. 5: 7,
  758. 6: 8,
  759. },
  760. }
  761. UPGRADE_VALUES = {
  762. rune_type: {
  763. stars: value/level_data[6]
  764. for stars, value in level_data.items()
  765. }
  766. for rune_type, level_data in SUBSTAT_INCREMENTS.items()
  767. }
  768. #substat_upgrades_remaining = (4 - math.floor((min(level, 12) / 3))) - (3 - quality)
  769. substat_upgrades_remaining = (4 - math.floor((min(level, 12) / 3))) - max((3 - quality), 0)
  770. new_stats = max(min(4 - len(substats), substat_upgrades_remaining), 0)
  771. old_stats = substat_upgrades_remaining - new_stats
  772. #if id == 16042097882:
  773. # print("substat_upgrades_remaining: " + str(substat_upgrades_remaining))
  774. # print("quality: " + str(quality))
  775. # print("level: " + str(level))
  776. # print("math.floor((min(level, 12) / 3)): " + str(math.floor((min(level, 12) / 3))))
  777. # print("old_stats: " + str(old_stats))
  778. # print("new_stats: " + str(new_stats))
  779. if old_stats > 0:
  780. # we can repeatedly upgrade the most value of the existing stats
  781. best_stat = max(
  782. 0, # ensure max() doesn't error if we only have one stat
  783. *[UPGRADE_VALUES[stat][stars] for stat in substats]
  784. )
  785. efficiency += best_stat * old_stats * 0.2 / 2.8 * 100
  786. if new_stats:
  787. # add the top N stats
  788. available_upgrades = sorted(
  789. [
  790. upgrade_value[stars]
  791. for stat, upgrade_value in UPGRADE_VALUES.items()
  792. if stat not in substats
  793. ],
  794. reverse=True
  795. )
  796. efficiency += sum(available_upgrades[:new_stats]) * 0.2 / 2.8 * 100
  797. return efficiency
  798. """
  799. Parses rune craft item data (table rune_craft).
  800. :param db: Sqlite database connection.
  801. :param data: Data in SWEX json file.
  802. """
  803. def parseRuneCraft(db, data):
  804. print("Parsing rune craft itmes...")
  805. uid = data["wizard_info"]["wizard_id"]
  806. for item in data["rune_craft_item_list"]:
  807. id = item["craft_item_id"]
  808. type = item["craft_type"]
  809. value = item["sell_value"]
  810. info = str(item["craft_type_id"])
  811. """
  812. info : 5 or 6 digit number: RRSSQ
  813. RR: Rune
  814. SS: Stat
  815. Q: Quality
  816. """
  817. quality = int(info[-1:])
  818. stat = int(info[-4:-2])
  819. rune = int(info[:-4])
  820. insert(db, "rune_craft", (uid, id, type, quality, rune, stat, value))
  821. """
  822. Parses guild data (tables guild, guild_member).
  823. :param db: Sqlite database connection.
  824. :param data: Data in SWEX json file.
  825. """
  826. def parseGuild(db, data):
  827. print("Parsing guild...")
  828. if len(data["guild"]["guild_info"]) < 1:
  829. return
  830. guild = data["guild"]["guild_info"]
  831. id = guild["guild_id"]
  832. name = guild["name"]
  833. level = guild["level"]
  834. experience = guild["experience"]
  835. recruiting = guild["recruit_status"]
  836. members = guild["member_now"]
  837. leader = guild["master_wizard_id"]
  838. comment = guild["comment"]
  839. notice = guild["notice"]
  840. insert(db, "guild", (id, name, level, experience, recruiting, members, leader, comment, notice))
  841. # Single quates ahead, dirty fix
  842. for k, member in data["guild"]["guild_members"].items():
  843. m = json.loads(str(member).replace("'", '"'))
  844. id = m["wizard_id"]
  845. guild = member["guild_id"]
  846. grade = member["grade"]
  847. name = member["wizard_name"]
  848. level = member["wizard_level"]
  849. rating = member["rating_id"]
  850. arena_score = member["arena_score"]
  851. joined = member["join_timestamp"]
  852. last_login = member["last_login_timestamp"]
  853. in_war = 0 # Checked later
  854. has_defense = 0 # Checked later
  855. insert(db, "guild_member", (id, guild, grade, name, level, rating, arena_score, joined, last_login, in_war, has_defense))
  856. cursor = db.cursor()
  857. for war in data["guildwar_member_list"]:
  858. cursor.execute("UPDATE guild_member SET in_war = 1 WHERE guild = ? AND id = ?;", (war["guild_id"], war["wizard_id"]))
  859. for defense in data["guild_member_defense_list"]:
  860. if len(defense["unit_list"]) > 0:
  861. cursor.execute("UPDATE guild_member SET has_defense = 1 WHERE id = '" + str(defense["wizard_id"]) + "';")
  862. db.commit()
  863. cursor.close()
  864. """
  865. Begin script
  866. """
  867. db = openDatabase('application/sw.sqlite')
  868. data = readFile(FILE)
  869. parsePlayer(db, data)
  870. parseScenarios(db, data)
  871. parseDefense(db, data)
  872. parseBuildings(db, data)
  873. parseDecorations(db, data)
  874. # TODO homunculus_skill_list
  875. parseUnits(db, data)
  876. parseSummonSpecial(db, data)
  877. parseInventory(db, data)
  878. parseRunes(db, data)
  879. parseRuneCraft(db, data)
  880. parseGuild(db, data)