PUT.php 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. <?php
  2. /**
  3. * Profile uploader script.
  4. *
  5. * Exposes an API to update an existing profile.
  6. *
  7. * It also saves the data to a JSON file in the data directory.
  8. *
  9. * Mandatory PUT parameters are:
  10. * - response: JSON received from game server on HubUserLogin command.
  11. * - key: User API key.
  12. *
  13. * @author Iñigo Valentin <i@inigovalentin.com>
  14. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  15. * @package SWDB
  16. * @category API
  17. */
  18. /**
  19. * Parses an artifact and saves it to the database.
  20. *
  21. * @param string $player_id Owner's player ID.
  22. * @param JSON artifact JSON fragment of the artifact.
  23. */
  24. function parse_artifact($player_id, $artifact){
  25. $db = get_context()->get_db();
  26. $statement = $db->prepare("
  27. INSERT INTO artifact (
  28. player,
  29. id,
  30. type,
  31. attribute,
  32. archetype,
  33. level,
  34. quality,
  35. original_quality,
  36. value,
  37. efficiency,
  38. max_efficiency,
  39. locked
  40. ) VALUES (
  41. :player,
  42. :id,
  43. :type,
  44. :attribute,
  45. :archetype,
  46. :level,
  47. :quality,
  48. :original_quality,
  49. :value,
  50. :efficiency,
  51. :max_efficiency,
  52. :locked
  53. );
  54. ");
  55. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  56. $statement->bindValue(":id", $artifact->{"rid"}, SQLITE3_INTEGER);
  57. if (intval($artifact->{"attribute"}) == 0){
  58. $statement->bindValue(":attribute", null, SQLITE3_INTEGER);
  59. }
  60. else{
  61. $statement->bindValue(":attribute", $artifact->{"attribute"}, SQLITE3_INTEGER);
  62. }
  63. if (intval($artifact->{"type"}) == 0){
  64. $statement->bindValue(":type", null, SQLITE3_INTEGER);
  65. }
  66. else{
  67. $statement->bindValue(":type", $artifact->{"type"}, SQLITE3_INTEGER);
  68. }
  69. if (intval($artifact->{"unit_style"}) == 0){
  70. $statement->bindValue(":archetype", null, SQLITE3_INTEGER);
  71. }
  72. else{
  73. $statement->bindValue(":archetype", $artifact->{"unit_style"}, SQLITE3_INTEGER);
  74. }
  75. $statement->bindValue(":level", $artifact->{"level"}, SQLITE3_INTEGER);
  76. $statement->bindValue(":quality", $artifact->{"rank"}, SQLITE3_INTEGER);
  77. $statement->bindValue(":original_quality", $artifact->{"natural_rank"}, SQLITE3_INTEGER);
  78. // I don'k know the value formula, but value only depends on quality, so... hardcoding.
  79. switch ($artifact->{"natural_rank"}){
  80. case QUALITY_ID::LEGEND:
  81. $statement->bindValue(":value", 37660, SQLITE3_INTEGER);
  82. break;
  83. case QUALITY_ID::HERO:
  84. $statement->bindValue(":value", 25250, SQLITE3_INTEGER);
  85. break;
  86. case QUALITY_ID::RARE:
  87. $statement->bindValue(":value", 18461, SQLITE3_INTEGER);
  88. break;
  89. case QUALITY_ID::MAGIC:
  90. $statement->bindValue(":value", 9330, SQLITE3_INTEGER);
  91. break;
  92. default:
  93. $statement->bindValue(":value", 3870, SQLITE3_INTEGER);
  94. }
  95. $statement->bindValue(":locked", $artifact->{"locked"}, SQLITE3_INTEGER);
  96. // Efficienccis will be caculated later, when instantiating the artifact.
  97. $statement->bindValue(":efficiency", 0, SQLITE3_FLOAT);
  98. $statement->bindValue(":max_efficiency", 0, SQLITE3_FLOAT);
  99. $statement->execute();
  100. // Main stat
  101. $statement = $db->prepare("
  102. INSERT INTO artifact_stat (
  103. artifact,
  104. stat,
  105. value
  106. ) VALUES (
  107. :artifact,
  108. :stat,
  109. :value
  110. );
  111. ");
  112. $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
  113. $statement->bindValue(":stat", $artifact->{"pri_effect"}[0], SQLITE3_INTEGER);
  114. $statement->bindValue(":value", $artifact->{"pri_effect"}[1], SQLITE3_INTEGER);
  115. $statement->execute();
  116. // Base effect insert query
  117. $effect_query = "
  118. INSERT INTO artifact_effect (
  119. artifact,
  120. slot,
  121. effect,
  122. value,
  123. enchant,
  124. grind,
  125. rolls
  126. ) VALUES (
  127. :artifact,
  128. :slot,
  129. :effect,
  130. :value,
  131. :enchant,
  132. :grind,
  133. :rolls
  134. );
  135. ";
  136. // Effect 1
  137. if (sizeof($artifact->{"sec_effects"}) > 0){
  138. $statement = $db->prepare($effect_query);
  139. $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
  140. $statement->bindValue(":slot", ARTIFACT_EFFECT_SLOT_ID::EFFECT_1, SQLITE3_INTEGER);
  141. $statement->bindValue(":effect", $artifact->{"sec_effects"}[0][0], SQLITE3_INTEGER);
  142. $statement->bindValue(":value", $artifact->{"sec_effects"}[0][1], SQLITE3_INTEGER);
  143. $statement->bindValue(":grind", $artifact->{"sec_effects"}[0][2], SQLITE3_INTEGER);
  144. $statement->bindValue(":enchant", $artifact->{"sec_effects"}[0][3], SQLITE3_INTEGER);
  145. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  146. $statement->execute();
  147. }
  148. // Effect 2
  149. if (sizeof($artifact->{"sec_effects"}) > 1){
  150. $statement = $db->prepare($effect_query);
  151. $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
  152. $statement->bindValue(":slot", ARTIFACT_EFFECT_SLOT_ID::EFFECT_2, SQLITE3_INTEGER);
  153. $statement->bindValue(":effect", $artifact->{"sec_effects"}[1][0], SQLITE3_INTEGER);
  154. $statement->bindValue(":value", $artifact->{"sec_effects"}[1][1], SQLITE3_INTEGER);
  155. $statement->bindValue(":grind", $artifact->{"sec_effects"}[1][2], SQLITE3_INTEGER);
  156. $statement->bindValue(":enchant", $artifact->{"sec_effects"}[1][3], SQLITE3_INTEGER);
  157. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  158. $statement->execute();
  159. }
  160. // Effect 3
  161. if (sizeof($artifact->{"sec_effects"}) > 2){
  162. $statement = $db->prepare($effect_query);
  163. $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
  164. $statement->bindValue(":slot", ARTIFACT_EFFECT_SLOT_ID::EFFECT_3, SQLITE3_INTEGER);
  165. $statement->bindValue(":effect", $artifact->{"sec_effects"}[2][0], SQLITE3_INTEGER);
  166. $statement->bindValue(":value", $artifact->{"sec_effects"}[2][1], SQLITE3_INTEGER);
  167. $statement->bindValue(":grind", $artifact->{"sec_effects"}[2][2], SQLITE3_INTEGER);
  168. $statement->bindValue(":enchant", $artifact->{"sec_effects"}[2][3], SQLITE3_INTEGER);
  169. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  170. $statement->execute();
  171. }
  172. // Effect 4
  173. if (sizeof($artifact->{"sec_effects"}) > 3){
  174. $statement = $db->prepare($effect_query);
  175. $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
  176. $statement->bindValue(":slot", ARTIFACT_EFFECT_SLOT_ID::EFFECT_4, SQLITE3_INTEGER);
  177. $statement->bindValue(":effect", $artifact->{"sec_effects"}[3][0], SQLITE3_INTEGER);
  178. $statement->bindValue(":value", $artifact->{"sec_effects"}[3][1], SQLITE3_INTEGER);
  179. $statement->bindValue(":grind", $artifact->{"sec_effects"}[3][2], SQLITE3_INTEGER);
  180. $statement->bindValue(":enchant", $artifact->{"sec_effects"}[3][3], SQLITE3_INTEGER);
  181. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  182. $statement->execute();
  183. }
  184. // Instantiate and calculate efficiency:
  185. new Artifact($artifact->{"rid"});
  186. // Assigned?
  187. if (intval($artifact->{"occupied_id"}) != 0){
  188. $statement = $db->prepare("
  189. INSERT INTO unit_artifact (unit, artifact, rta)
  190. VALUES (:unit, :artifact, :rta);
  191. ");
  192. $statement->bindValue(":unit", $artifact->{"occupied_id"}, SQLITE3_INTEGER);
  193. $statement->bindValue(":artifact", $artifact->{"rid"}, SQLITE3_INTEGER);
  194. $statement->bindValue(":rta", GAME_MODE_ID::NORMAL, SQLITE3_INTEGER);
  195. $statement->execute();
  196. }
  197. }
  198. /**
  199. * Parses a rune and saves it to the database.
  200. *
  201. * @param string $player_id Owner ID.
  202. * @param JSON rune JSON fragment of the rune.
  203. */
  204. function parse_rune($player_id, $rune, $lock_list){
  205. $db = get_context()->get_db();
  206. $statement = $db->prepare("
  207. INSERT INTO rune (
  208. player,
  209. id,
  210. type,
  211. slot,
  212. stars,
  213. ancient,
  214. level,
  215. quality,
  216. original_quality,
  217. value,
  218. efficiency,
  219. max_efficiency,
  220. locked
  221. ) VALUES (
  222. :player,
  223. :id,
  224. :type,
  225. :slot,
  226. :stars,
  227. :ancient,
  228. :level,
  229. :quality,
  230. :original_quality,
  231. :value,
  232. :efficiency,
  233. :max_efficiency,
  234. :locked
  235. );
  236. ");
  237. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  238. $statement->bindValue(":id", $rune->{"rune_id"}, SQLITE3_INTEGER);
  239. $statement->bindValue(":type", $rune->{"set_id"}, SQLITE3_INTEGER);
  240. $statement->bindValue(":slot", $rune->{"slot_no"}, SQLITE3_INTEGER);
  241. $statement->bindValue(":value", $rune->{"sell_value"}, SQLITE3_INTEGER);
  242. if ($rune->{"class"} > 10){
  243. // Ancient rune
  244. $statement->bindValue(":ancient", 1, SQLITE3_INTEGER);
  245. $statement->bindValue(":stars", intval($rune->{"class"}) - 10, SQLITE3_INTEGER);
  246. $statement->bindValue(":original_quality", intval($rune->{"extra"}) - 10, SQLITE3_INTEGER);
  247. }
  248. else{
  249. // Non Ancient rune
  250. $statement->bindValue(":ancient", 0, SQLITE3_INTEGER);
  251. $statement->bindValue(":stars", $rune->{"class"}, SQLITE3_INTEGER);
  252. $statement->bindValue(":original_quality", $rune->{"extra"}, SQLITE3_INTEGER);
  253. }
  254. $level = $rune->{"upgrade_curr"};
  255. $statement->bindValue(":level", $level, SQLITE3_INTEGER);
  256. $quality = QUALITY_ID::NORMAL;
  257. if ($level >= 12){
  258. $quality = QUALITY_ID::LEGEND;
  259. }
  260. elseif ($level >= 9){
  261. $quality = QUALITY_ID::HERO;
  262. }
  263. elseif ($level >= 6){
  264. $quality = QUALITY_ID::RARE;
  265. }
  266. elseif ($level >= 3){
  267. $quality = QUALITY_ID::MAGIC;
  268. }
  269. if ($rune->{"extra"} > $quality){
  270. $quality = $rune->{"extra"};
  271. }
  272. $statement->bindValue(":quality", $quality, SQLITE3_INTEGER);
  273. $lock = 0;
  274. if (in_array($rune->{"rune_id"}, $lock_list)){
  275. $lock = 1;
  276. }
  277. $statement->bindValue(":locked", $lock, SQLITE3_INTEGER);
  278. $efficiency = null;
  279. $max_efficiency = null;
  280. $statement->bindValue(":efficiency", $efficiency, SQLITE3_FLOAT);
  281. $statement->bindValue(":max_efficiency", $max_efficiency, SQLITE3_FLOAT);
  282. $statement->execute();
  283. $stat_statement = "
  284. INSERT INTO rune_stat (rune, slot, stat, value, enchant, grind, rolls)
  285. VALUES (:rune, :slot, :stat, :value, :enchant, :grind, :rolls);
  286. ";
  287. // Main stat
  288. $statement = $db->prepare($stat_statement);
  289. $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
  290. $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::MAIN, SQLITE3_INTEGER);
  291. $statement->bindValue(":stat", $rune->{"pri_eff"}[0], SQLITE3_INTEGER);
  292. $statement->bindValue(":value", $rune->{"pri_eff"}[1], SQLITE3_INTEGER);
  293. $statement->bindValue(":enchant", 0, SQLITE3_INTEGER);
  294. $statement->bindValue(":grind", 0, SQLITE3_INTEGER);
  295. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  296. $statement->execute();
  297. // Innate stat
  298. if ($rune->{"prefix_eff"}[0] > 0){
  299. // Main stat
  300. $statement = $db->prepare($stat_statement);
  301. $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
  302. $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::INNATE, SQLITE3_INTEGER);
  303. $statement->bindValue(":stat", $rune->{"prefix_eff"}[0], SQLITE3_INTEGER);
  304. $statement->bindValue(":value", $rune->{"prefix_eff"}[1], SQLITE3_INTEGER);
  305. $statement->bindValue(":enchant", 0, SQLITE3_INTEGER);
  306. $statement->bindValue(":grind", 0, SQLITE3_INTEGER);
  307. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  308. $statement->execute();
  309. }
  310. if (sizeof($rune->{"sec_eff"}) > 0){
  311. $statement = $db->prepare($stat_statement);
  312. $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
  313. $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::SUBSTAT_1, SQLITE3_INTEGER);
  314. $statement->bindValue(":stat", $rune->{"sec_eff"}[0][0], SQLITE3_INTEGER);
  315. $statement->bindValue(":value", $rune->{"sec_eff"}[0][1], SQLITE3_INTEGER);
  316. $statement->bindValue(":enchant", $rune->{"sec_eff"}[0][2], SQLITE3_INTEGER);
  317. $statement->bindValue(":grind", $rune->{"sec_eff"}[0][3], SQLITE3_INTEGER);
  318. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  319. $statement->execute();
  320. }
  321. if (sizeof($rune->{"sec_eff"}) > 1){
  322. $statement = $db->prepare($stat_statement);
  323. $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
  324. $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::SUBSTAT_2, SQLITE3_INTEGER);
  325. $statement->bindValue(":stat", $rune->{"sec_eff"}[1][0], SQLITE3_INTEGER);
  326. $statement->bindValue(":value", $rune->{"sec_eff"}[1][1], SQLITE3_INTEGER);
  327. $statement->bindValue(":enchant", $rune->{"sec_eff"}[1][2], SQLITE3_INTEGER);
  328. $statement->bindValue(":grind", $rune->{"sec_eff"}[1][3], SQLITE3_INTEGER);
  329. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  330. $statement->execute();
  331. }
  332. if (sizeof($rune->{"sec_eff"}) > 2){
  333. $statement = $db->prepare($stat_statement);
  334. $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
  335. $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::SUBSTAT_3, SQLITE3_INTEGER);
  336. $statement->bindValue(":stat", $rune->{"sec_eff"}[2][0], SQLITE3_INTEGER);
  337. $statement->bindValue(":value", $rune->{"sec_eff"}[2][1], SQLITE3_INTEGER);
  338. $statement->bindValue(":enchant", $rune->{"sec_eff"}[2][2], SQLITE3_INTEGER);
  339. $statement->bindValue(":grind", $rune->{"sec_eff"}[2][3], SQLITE3_INTEGER);
  340. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  341. $statement->execute();
  342. }
  343. if (sizeof($rune->{"sec_eff"}) > 3){
  344. $statement = $db->prepare($stat_statement);
  345. $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
  346. $statement->bindValue(":slot", RUNE_STAT_SLOT_ID::SUBSTAT_4, SQLITE3_INTEGER);
  347. $statement->bindValue(":stat", $rune->{"sec_eff"}[3][0], SQLITE3_INTEGER);
  348. $statement->bindValue(":value", $rune->{"sec_eff"}[3][1], SQLITE3_INTEGER);
  349. $statement->bindValue(":enchant", $rune->{"sec_eff"}[3][2], SQLITE3_INTEGER);
  350. $statement->bindValue(":grind", $rune->{"sec_eff"}[3][3], SQLITE3_INTEGER);
  351. $statement->bindValue(":rolls", 0, SQLITE3_INTEGER);
  352. $statement->execute();
  353. }
  354. // Instantiate and calculate efficiency:
  355. new Rune($rune->{"rune_id"});
  356. // Assigned?
  357. if (intval($rune->{"occupied_id"}) != 0){
  358. $statement = $db->prepare("
  359. INSERT INTO unit_rune (unit, rune, rta)
  360. VALUES (:unit, :rune, :rta);
  361. ");
  362. $statement->bindValue(":unit", $rune->{"occupied_id"}, SQLITE3_INTEGER);
  363. $statement->bindValue(":rune", $rune->{"rune_id"}, SQLITE3_INTEGER);
  364. $statement->bindValue(":rta", GAME_MODE_ID::NORMAL, SQLITE3_INTEGER);
  365. $statement->execute();
  366. }
  367. }
  368. /**
  369. * Parses the response to the HubUserLogin command and updates profile.
  370. *
  371. * @param resource $db Database connection.
  372. * @param int $player_id Player ID.
  373. * @param mixed[] $json Response.
  374. */
  375. function parse_profile($db, $player_id, $json){
  376. // Save data file
  377. $uname = $json->{"wizard_info"}->{"wizard_name"};
  378. $dtime = (new DateTime())->format("Y-m-dTH:i:s");
  379. $fname = ($_SERVER["DOCUMENT_ROOT"] . "/../data/profile_" . $player_id . "_" . $uname . "_" . $dtime . ".json");
  380. try{
  381. file_put_contents($fname, json_encode($json));
  382. }
  383. catch(Exception $e) {
  384. header("HTTP/1.1 500 Unable to save profile file: " . $e->getMessage());
  385. syslog(LOG_INFO, "[APIv1] HTTP/1.1 500 Unable to save profile file: " . $e->getMessage());
  386. return 500;
  387. }
  388. // Clear previous profile data
  389. $db->query("PRAGMA foreign_keys = OFF;");
  390. $statement = $db->prepare(
  391. "DELETE FROM unit_skill
  392. WHERE unit IN (SELECT id FROM unit WHERE player = :player);
  393. ");
  394. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  395. $statement->execute();
  396. $statement = $db->prepare("
  397. DELETE FROM unit_rune
  398. WHERE unit IN (SELECT id FROM unit WHERE player = :player);
  399. ");
  400. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  401. $statement->execute();
  402. $statement = $db->prepare("
  403. DELETE FROM rune_stat
  404. WHERE rune IN (SELECT id FROM rune WHERE player = :player);
  405. ");
  406. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  407. $statement->execute();
  408. $statement = $db->prepare("
  409. DELETE FROM unit_artifact
  410. WHERE unit IN (SELECT id FROM unit WHERE player = :player);n ");
  411. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  412. $statement->execute();
  413. $statement = $db->prepare("
  414. DELETE FROM artifact_stat
  415. WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
  416. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  417. $statement->execute();
  418. $statement = $db->prepare("
  419. DELETE FROM artifact_effect
  420. WHERE artifact IN (SELECT id FROM artifact WHERE player = :player);n ");
  421. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  422. $statement->execute();
  423. $tables_to_clear = [
  424. "defense",
  425. "unit",
  426. "rune",
  427. "artifact",
  428. "building",
  429. "decoration",
  430. "item",
  431. "enchantment"
  432. ];
  433. foreach ($tables_to_clear as $table){
  434. $statement = $db->prepare("DELETE FROM $table WHERE player = :player;");
  435. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  436. $statement->execute();
  437. }
  438. $db->query("DELETE FROM summon_special"); # For every player, will be reloaded.
  439. // Get rune lock list
  440. $rune_lock_list = $json->{"rune_lock_list"};
  441. // Update player data
  442. $statement = $db->prepare("
  443. UPDATE player
  444. SET
  445. name = :name,
  446. mana = :mana,
  447. crystal = :crystal,
  448. country = :country,
  449. lang = :lang,
  450. level = :level,
  451. experience = :experience,
  452. energy = :energy,
  453. energy_max = :energy_max,
  454. energy_per_min = :energy_per_min,
  455. arena_energy = :arena_energy,
  456. rep = :rep,
  457. social_point = :social_point,
  458. honor_point = :honor_point,
  459. guild_point = :guild_point,
  460. darkportal_energy = :darkportal_energy,
  461. dimension_energy = :dimension_energy,
  462. costume_point = :costume_point,
  463. honor_medal = :honor_medal,
  464. honor_mark = :honor_mark,
  465. event_coin = :event_coin,
  466. storage_slots = :storage_slots,
  467. island = :island_upgrade
  468. WHERE id = :id;
  469. ");
  470. $statement->bindValue(":name", $json->{"wizard_info"}->{"wizard_name"}, SQLITE3_TEXT);
  471. $statement->bindValue(":mana", $json->{"wizard_info"}->{"wizard_mana"}, SQLITE3_INTEGER);
  472. $statement->bindValue(":crystal", $json->{"wizard_info"}->{"wizard_crystal"}, SQLITE3_INTEGER);
  473. $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"}, SQLITE3_TEXT);
  474. $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"}, SQLITE3_TEXT);
  475. $statement->bindValue(":level", $json->{"wizard_info"}->{"wizard_level"}, SQLITE3_INTEGER);
  476. $statement->bindValue(":experience", $json->{"wizard_info"}->{"experience"}, SQLITE3_INTEGER);
  477. $statement->bindValue(":energy", $json->{"wizard_info"}->{"wizard_energy"}, SQLITE3_INTEGER);
  478. $statement->bindValue(":energy_max", $json->{"wizard_info"}->{"energy_max"}, SQLITE3_INTEGER);
  479. $statement->bindValue(":energy_per_min", $json->{"wizard_info"}->{"energy_per_min"}, SQLITE3_INTEGER);
  480. $statement->bindValue(":arena_energy", $json->{"wizard_info"}->{"arena_energy"}, SQLITE3_INTEGER);
  481. $statement->bindValue(":rep", $json->{"wizard_info"}->{"rep_unit_id"}, SQLITE3_INTEGER);
  482. $statement->bindValue(":social_point", $json->{"wizard_info"}->{"social_point_current"}, SQLITE3_INTEGER);
  483. $statement->bindValue(":honor_point", $json->{"wizard_info"}->{"honor_point"}, SQLITE3_INTEGER);
  484. $statement->bindValue(":guild_point", $json->{"wizard_info"}->{"guild_point"}, SQLITE3_INTEGER);
  485. $statement->bindValue(":darkportal_energy", $json->{"wizard_info"}->{"darkportal_energy"}, SQLITE3_INTEGER);
  486. $statement->bindValue(":dimension_energy", $json->{"dimension_hole_info"}->{"energy"}, SQLITE3_INTEGER);
  487. $statement->bindValue(":costume_point", $json->{"wizard_info"}->{"costume_point"}, SQLITE3_INTEGER);
  488. $statement->bindValue(":honor_medal", $json->{"wizard_info"}->{"honor_medal"}, SQLITE3_INTEGER);
  489. $statement->bindValue(":honor_mark", $json->{"wizard_info"}->{"honor_mark"}, SQLITE3_INTEGER);
  490. $statement->bindValue(":event_coin", $json->{"wizard_info"}->{"event_coin"}, SQLITE3_INTEGER);
  491. $statement->bindValue(":storage_slots", $json->{"unit_depository_slots"}->{"number"}, SQLITE3_INTEGER);
  492. $island_upgrade = 0;
  493. foreach ($json->{"island_info"} as $island){
  494. if (intval($island->{"id"}) > 100 && intval($island->{"open"}) == 1){
  495. $island_upgrade = intval($island->{"id"});
  496. }
  497. }
  498. $statement->bindValue(":island_upgrade", $island_upgrade, SQLITE3_INTEGER);
  499. $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
  500. $statement->execute();
  501. // Parse Arena defense
  502. foreach ($json->{"defense_unit_list"} as $defense){
  503. $statement = $db->prepare("
  504. INSERT INTO defense (
  505. player,
  506. area_type,
  507. slot,
  508. unit,
  509. position
  510. ) VALUES (
  511. :player,
  512. :area_type,
  513. :slot,
  514. :unit,
  515. :position
  516. );
  517. ");
  518. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  519. $statement->bindValue(":area_type", AREA_TYPE_ID::ARENA, SQLITE3_INTEGER);
  520. $statement->bindValue(":slot", 0, SQLITE3_INTEGER);
  521. $statement->bindValue(":unit", $defense->{"unit_id"}, SQLITE3_INTEGER);
  522. $statement->bindValue(":position", $defense->{"pos_id"}, SQLITE3_INTEGER);
  523. $statement->execute();
  524. }
  525. // Parse GW defense
  526. if ($json->{"guildwar_defense_unit_list"}[0]){
  527. $position = 1;
  528. $slot = 0;
  529. foreach ($json->{"guildwar_defense_unit_list"}[0] as $defense){
  530. $statement = $db->prepare("
  531. INSERT INTO defense (
  532. player,
  533. area_type,
  534. slot,
  535. unit,
  536. position
  537. ) VALUES (
  538. :player,
  539. :area_type,
  540. :slot,
  541. :unit,
  542. :position
  543. );
  544. ");
  545. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  546. $statement->bindValue(":area_type", AREA_TYPE_ID::GUILD_WAR, SQLITE3_INTEGER);
  547. $statement->bindValue(":slot", $slot, SQLITE3_INTEGER);
  548. $statement->bindValue(":unit", $defense->{"unit_id"}, SQLITE3_INTEGER);
  549. $statement->bindValue(":position", $position, SQLITE3_INTEGER);
  550. $statement->execute();
  551. $position ++;
  552. }
  553. }
  554. if ($json->{"guildwar_defense_unit_list"}[1]){
  555. $position = 1;
  556. $slot = 1;
  557. foreach ($json->{"guildwar_defense_unit_list"}[1] as $defense){
  558. $statement = $db->prepare("
  559. INSERT INTO defense (
  560. player,
  561. area_type,
  562. slot,
  563. unit,
  564. position
  565. ) VALUES (
  566. :player,
  567. :area_type,
  568. :slot,
  569. :unit,
  570. :position
  571. );
  572. ");
  573. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  574. $statement->bindValue(":area_type", AREA_TYPE_ID::GUILD_WAR, SQLITE3_INTEGER);
  575. $statement->bindValue(":slot", $slot, SQLITE3_INTEGER);
  576. $statement->bindValue(":unit", $defense->{"unit_id"}, SQLITE3_INTEGER);
  577. $statement->bindValue(":position", $position, SQLITE3_INTEGER);
  578. $statement->execute();
  579. $position ++;
  580. }
  581. }
  582. // Parse siege defenses.
  583. $i = 0;
  584. foreach ($json->{"guildsiege_defense_unit_list"} as $defense){
  585. $statement = $db->prepare("
  586. INSERT INTO defense (
  587. player,
  588. area_type,
  589. slot,
  590. unit,
  591. position
  592. ) VALUES (
  593. :player,
  594. :area_type,
  595. :slot,
  596. :unit,
  597. :position
  598. );
  599. ");
  600. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  601. $statement->bindValue(":area_type", AREA_TYPE_ID::GUILD_SIEGE, SQLITE3_INTEGER);
  602. $statement->bindValue(":slot", floor($i / 3), SQLITE3_INTEGER);
  603. $statement->bindValue(":unit", $defense, SQLITE3_INTEGER);
  604. $statement->bindValue(":position", ($i % 3) + 1, SQLITE3_INTEGER);
  605. $statement->execute();
  606. $i ++;
  607. }
  608. // Parse buildings
  609. foreach($json->{"building_list"} as $building){
  610. $statement = $db->prepare("
  611. INSERT INTO building (
  612. player,
  613. id,
  614. buildable,
  615. gain
  616. ) VALUES (
  617. :player,
  618. :id,
  619. :buildable,
  620. :gain
  621. );
  622. ");
  623. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  624. $statement->bindValue(":id", $building->{"building_id"}, SQLITE3_INTEGER);
  625. $statement->bindValue(":buildable", $building->{"building_master_id"}, SQLITE3_INTEGER);
  626. $statement->bindValue(":gain", $building->{"gain_per_hour"}, SQLITE3_INTEGER);
  627. $statement->execute();
  628. }
  629. // Parse decorarion
  630. foreach($json->{"deco_list"} as $building){
  631. $statement = $db->prepare("
  632. INSERT INTO decoration (
  633. player,
  634. id,
  635. decorative,
  636. level
  637. ) VALUES (
  638. :player,
  639. :id,
  640. :decorative,
  641. :level
  642. );
  643. ");
  644. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  645. $statement->bindValue(":id", $building->{"deco_id"}, SQLITE3_INTEGER);
  646. $statement->bindValue(":decorative", $building->{"master_id"}, SQLITE3_INTEGER);
  647. $statement->bindValue(":level", $building->{"level"}, SQLITE3_INTEGER);
  648. $statement->execute();
  649. }
  650. // Parse units
  651. $lock_list = $json->{"unit_lock_list"};
  652. foreach ($json->{"unit_list"} as $unit){
  653. $statement = $db->prepare("
  654. INSERT INTO unit (
  655. player,
  656. id,
  657. building,
  658. monster,
  659. level,
  660. stars,
  661. hp,
  662. attack,
  663. defense,
  664. speed,
  665. crit_rate,
  666. crit_damage,
  667. resistance,
  668. accuracy,
  669. experience,
  670. exp_gained,
  671. exp_gain_rate,
  672. costume,
  673. source,
  674. create_time,
  675. homunculus_name,
  676. lock
  677. ) VALUES (
  678. :player,
  679. :id,
  680. :building,
  681. :monster,
  682. :level,
  683. :stars,
  684. :hp,
  685. :attack,
  686. :defense,
  687. :speed,
  688. :crit_rate,
  689. :crit_damage,
  690. :resistance,
  691. :accuracy,
  692. :experience,
  693. :exp_gained,
  694. :exp_gain_rate,
  695. :costume,
  696. :source,
  697. :create_time,
  698. :homunculus_name,
  699. :lock
  700. );
  701. ");
  702. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  703. $statement->bindValue(":id", $unit->{"unit_id"}, SQLITE3_INTEGER);
  704. $statement->bindValue(":building", $unit->{"building_id"}, SQLITE3_INTEGER);
  705. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  706. $statement->bindValue(":level", $unit->{"unit_level"}, SQLITE3_INTEGER);
  707. $statement->bindValue(":stars", $unit->{"class"}, SQLITE3_INTEGER);
  708. $statement->bindValue(":hp", intval($unit->{"con"}) * 15, SQLITE3_INTEGER); # Always x15
  709. $statement->bindValue(":attack", $unit->{"atk"}, SQLITE3_INTEGER);
  710. $statement->bindValue(":defense", $unit->{"def"}, SQLITE3_INTEGER);
  711. $statement->bindValue(":speed", $unit->{"spd"}, SQLITE3_INTEGER);
  712. $statement->bindValue(":crit_rate", $unit->{"critical_rate"}, SQLITE3_INTEGER);
  713. $statement->bindValue(":crit_damage", $unit->{"critical_damage"}, SQLITE3_INTEGER);
  714. $statement->bindValue(":resistance", $unit->{"resist"}, SQLITE3_INTEGER);
  715. $statement->bindValue(":accuracy", $unit->{"accuracy"}, SQLITE3_INTEGER);
  716. $statement->bindValue(":experience", $unit->{"experience"}, SQLITE3_INTEGER);
  717. $statement->bindValue(":exp_gained", $unit->{"exp_gained"}, SQLITE3_INTEGER);
  718. $statement->bindValue(":exp_gain_rate", $unit->{"exp_gain_rate"}, SQLITE3_INTEGER);
  719. $statement->bindValue(":costume", $unit->{"costume_master_id"}, SQLITE3_INTEGER);
  720. $statement->bindValue(":source", $unit->{"source"}, SQLITE3_INTEGER);
  721. $statement->bindValue(":create_time", $unit->{"create_time"}, SQLITE3_INTEGER);
  722. $statement->bindValue(":homunculus_name", $unit->{"homunculus_name"}, SQLITE3_TEXT);
  723. $lock = 0;
  724. if (in_array($unit->{"unit_id"}, $lock_list)){
  725. $lock = 1;
  726. }
  727. $statement->bindValue(":lock", $lock, SQLITE3_INTEGER);
  728. $statement->execute();
  729. foreach ($unit->{"skills"} as $skill){
  730. $statement = $db->prepare("
  731. INSERT INTO unit_skill (
  732. unit,
  733. skill,
  734. level
  735. ) VALUES (
  736. :unit,
  737. :skill,
  738. :level
  739. );
  740. ");
  741. $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
  742. $statement->bindValue(":skill", $skill[0], SQLITE3_INTEGER);
  743. $statement->bindValue(":level", $skill[1], SQLITE3_INTEGER);
  744. $statement->execute();
  745. }
  746. }
  747. // Parse items
  748. foreach ($json->{"inventory_info"} as $item){
  749. $statement = $db->prepare("
  750. INSERT INTO item (
  751. player,
  752. id,
  753. type,
  754. amount
  755. ) VALUES (
  756. :player,
  757. :id,
  758. :type,
  759. :amount
  760. );
  761. ");
  762. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  763. $statement->bindValue(":id", $item->{"item_master_id"}, SQLITE3_INTEGER);
  764. $statement->bindValue(":type", $item->{"item_master_type"}, SQLITE3_INTEGER);
  765. $statement->bindValue(":amount", $item->{"item_quantity"}, SQLITE3_INTEGER);
  766. $statement->execute();
  767. }
  768. // Fix rune craft item type.
  769. $db->query("UPDATE item SET type = 27 WHERE type = 29 AND id IN (2001, 4001, 4002, 4003, 9001, 9002, 9003, 8001, 9004);");
  770. // Set Conversion stone to type Craft Stuff and id 9999.
  771. $db->query("UPDATE item SET type = 27, id = 9999 WHERE type = 75 AND id = 1;");
  772. // Parse Summon Stone summoning list
  773. // TODO: Set propper dates
  774. foreach ($json->{"summon_special_info"}->{"this"} as $unit){
  775. $statement = $db->prepare("
  776. INSERT INTO summon_special (
  777. week,
  778. unit
  779. ) VALUES (
  780. :week,
  781. :unit
  782. );
  783. ");
  784. $statement->bindValue(":week", 0, SQLITE3_INTEGER);
  785. $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
  786. $statement->execute();
  787. }
  788. foreach ($json->{"summon_special_info"}->{"next"} as $unit){
  789. $statement = $db->prepare("
  790. INSERT INTO summon_special (
  791. week,
  792. unit
  793. ) VALUES (
  794. :week,
  795. :unit
  796. );
  797. ");
  798. $statement->bindValue(":week", 1, SQLITE3_INTEGER);
  799. $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
  800. $statement->execute();
  801. }
  802. foreach ($json->{"summon_special_info"}->{"third"} as $unit){
  803. $statement = $db->prepare("
  804. INSERT INTO summon_special (
  805. week,
  806. unit
  807. ) VALUES (
  808. :week,
  809. :unit
  810. );
  811. ");
  812. $statement->bindValue(":week", 2, SQLITE3_INTEGER);
  813. $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
  814. $statement->execute();
  815. }
  816. foreach ($json->{"summon_special_info"}->{"fourth"} as $unit){
  817. $statement = $db->prepare("
  818. INSERT INTO summon_special (
  819. week,
  820. unit
  821. ) VALUES (
  822. :week,
  823. :unit
  824. );
  825. ");
  826. $statement->bindValue(":week", 3, SQLITE3_INTEGER);
  827. $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
  828. $statement->execute();
  829. }
  830. // Parse runes
  831. foreach ($json->{"runes"} as $rune){
  832. parse_rune($player_id, $rune, $rune_lock_list);
  833. }
  834. foreach ($json->{"unit_list"} as $unit){
  835. foreach ($unit->{"runes"} as $rune){
  836. parse_rune($player_id, $rune, $rune_lock_list);
  837. }
  838. }
  839. foreach ($json->{"world_arena_rune_equip_list"} as $rta_rune){
  840. $statement = $db->prepare("
  841. INSERT INTO unit_rune (unit, rune, rta)
  842. VALUES (:unit, :rune, :rta);
  843. ");
  844. $statement->bindValue(":unit", $rta_rune->{"occupied_id"}, SQLITE3_INTEGER);
  845. $statement->bindValue(":rune", $rta_rune->{"rune_id"}, SQLITE3_INTEGER);
  846. $statement->bindValue(":rta", GAME_MODE_ID::RTA, SQLITE3_INTEGER);
  847. $statement->execute();
  848. }
  849. // Parse artifacts
  850. foreach ($json->{"artifacts"} as $artifact){
  851. parse_artifact($player_id, $artifact);
  852. }
  853. foreach ($json->{"unit_list"} as $unit){
  854. foreach ($unit->{"artifacts"} as $artifact){
  855. parse_artifact($player_id, $artifact);
  856. }
  857. }
  858. foreach ($json->{"world_arena_artifact_equip_list"} as $rta_artifact){
  859. $statement = $db->prepare("
  860. INSERT INTO unit_artifact (unit, artifact, rta)
  861. VALUES (:unit, :artifact, :rta);
  862. ");
  863. $statement->bindValue(":unit", $rta_artifact->{"occupied_id"}, SQLITE3_INTEGER);
  864. $statement->bindValue(":artifact", $rta_artifact->{"artifact_id"}, SQLITE3_INTEGER);
  865. $statement->bindValue(":rta", GAME_MODE_ID::RTA, SQLITE3_INTEGER);
  866. $statement->execute();
  867. }
  868. // Parse enchantments
  869. foreach ($json->{"rune_craft_item_list"} as $item){
  870. $statement = $db->prepare("
  871. INSERT INTO enchantment (
  872. player,
  873. id,
  874. type,
  875. quality,
  876. rune,
  877. stat,
  878. value,
  879. amount
  880. ) VALUES (
  881. :player,
  882. :id,
  883. :type,
  884. :quality,
  885. :rune,
  886. :stat,
  887. :value,
  888. :amount
  889. );
  890. ");
  891. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  892. $statement->bindValue(":id", $item->{"craft_item_id"}, SQLITE3_INTEGER);
  893. $statement->bindValue(":type", $item->{"craft_type"}, SQLITE3_INTEGER);
  894. $statement->bindValue(":value", $item->{"sell_value"}, SQLITE3_INTEGER);
  895. $statement->bindValue(":amount", $item->{"amount"}, SQLITE3_INTEGER);
  896. $info = str_pad(intval($item->{"craft_type_id"}), 6, "0", STR_PAD_LEFT);
  897. /*
  898. info : 5 or 6 digit number: RRSSQQ
  899. RR: Rune (may not be padded)
  900. SS: Stat
  901. QQ: Quality
  902. */
  903. $statement->bindValue(":quality", intval(substr($info, 4, 2)), SQLITE3_INTEGER);
  904. $statement->bindValue(":stat", intval(substr($info, 2, 2)), SQLITE3_INTEGER);
  905. $statement->bindValue(":rune", intval(substr($info, 0, 2)), SQLITE3_INTEGER);
  906. $statement->execute();
  907. }
  908. // Parse guild
  909. if ($json->{"guild"}->{"guild_info"} != null){
  910. $guild = $json->{"guild"}->{"guild_info"};
  911. $statement = $db->prepare("DELETE FROM guild WHERE id = :id;");
  912. $statement->bindValue(":id", $guild->{"guild_id"}, SQLITE3_INTEGER);
  913. $statement->execute();
  914. $statement = $db->prepare("DELETE FROM guild_member WHERE guild = :guild;");
  915. $statement->bindValue(":guild", $guild->{"guild_id"}, SQLITE3_INTEGER);
  916. $statement->execute();
  917. $statement = $db->prepare("
  918. INSERT INTO guild (
  919. id,
  920. name,
  921. level,
  922. experience,
  923. recruiting,
  924. members,
  925. leader,
  926. comment,
  927. notice
  928. ) VALUES (
  929. :id,
  930. :name,
  931. :level,
  932. :experience,
  933. :recruiting,
  934. :members,
  935. :leader,
  936. :comment,
  937. :notice
  938. );
  939. ");
  940. $statement->bindValue(":id",$guild->{"guild_id"}, SQLITE3_INTEGER);
  941. $statement->bindValue(":name",$guild->{"name"}, SQLITE3_TEXT);
  942. $statement->bindValue(":level",$guild->{"level"}, SQLITE3_INTEGER);
  943. $statement->bindValue(":experience",$guild->{"experience"}, SQLITE3_INTEGER);
  944. $statement->bindValue(":recruiting",$guild->{"recruit_status"}, SQLITE3_INTEGER);
  945. $statement->bindValue(":members",$guild->{"member_now"}, SQLITE3_INTEGER);
  946. $statement->bindValue(":leader",$guild->{"master_wizard_id"}, SQLITE3_INTEGER);
  947. $statement->bindValue(":comment",$guild->{"comment"}, SQLITE3_TEXT);
  948. $statement->bindValue(":notice",$guild->{"notice"}, SQLITE3_TEXT);
  949. $statement->execute();
  950. foreach ($json->{"guild"}->{"guild_members"} as $member){
  951. $statement = $db->prepare("
  952. INSERT INTO guild_member (
  953. member,
  954. guild,
  955. grade,
  956. name,
  957. level,
  958. rating,
  959. arena_score,
  960. joined,
  961. last_login,
  962. in_war,
  963. has_defense
  964. ) VALUES (
  965. :member,
  966. :guild,
  967. :grade,
  968. :name,
  969. :level,
  970. :rating,
  971. :arena_score,
  972. :joined,
  973. :last_login,
  974. :in_war,
  975. :has_defense
  976. );
  977. ");
  978. $statement->bindValue(":member", $member->{"wizard_id"}, SQLITE3_INTEGER);
  979. $statement->bindValue(":guild", $member->{"guild_id"}, SQLITE3_INTEGER);
  980. $statement->bindValue(":grade", $member->{"grade"}, SQLITE3_INTEGER);
  981. $statement->bindValue(":name", $member->{"wizard_name"}, SQLITE3_TEXT);
  982. $statement->bindValue(":level", $member->{"wizard_level"}, SQLITE3_INTEGER);
  983. $statement->bindValue(":rating", $member->{"rating_id"}, SQLITE3_INTEGER);
  984. $statement->bindValue(":arena_score", $member->{"arena_score"}, SQLITE3_INTEGER);
  985. $statement->bindValue(":joined", $member->{"join_timestamp"}, SQLITE3_TEXT);
  986. $statement->bindValue(":last_login", $member->{"last_login_timestamp"}, SQLITE3_TEXT);
  987. $in_war = 0;
  988. foreach ($json->{"guildwar_member_list"} as $war){
  989. if ($war->{"wizard_id"} == $member->{"wizard_id"}){
  990. $in_war = 1;
  991. break;
  992. }
  993. }
  994. $has_defense = 0;
  995. foreach ($json->{"guild_member_defense_list"} as $defense){
  996. if ($defense->{"wizard_id"} == $member->{"wizard_id"}){
  997. $has_defense = 1;
  998. break;
  999. }
  1000. }
  1001. $statement->bindValue(":in_war", $in_war, SQLITE3_INTEGER);
  1002. $statement->bindValue(":has_defense", $has_defense, SQLITE3_INTEGER);
  1003. $statement->execute();
  1004. }
  1005. }
  1006. // Update invalid teams
  1007. $statement = $db->prepare("
  1008. DELETE FROM team
  1009. WHERE
  1010. id IN (
  1011. SELECT DISTINCT team
  1012. FROM team_unit
  1013. WHERE unit NOT IN (
  1014. SELECT DISTINCT id FROM unit WHERE player = :player
  1015. )
  1016. ) AND
  1017. player = :player
  1018. ");
  1019. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1020. $statement->execute();
  1021. $db->query("
  1022. DELETE FROM team_unit
  1023. WHERE
  1024. unit NOT IN (
  1025. SELECT DISTINCT id FROM unit
  1026. ) OR
  1027. team NOT IN (
  1028. SELECT DISTINCT id FROM team
  1029. );"
  1030. );
  1031. // Return success
  1032. return "201 Created.";
  1033. }
  1034. /**
  1035. * Parses the response to the GetUnitCollection command and updates the
  1036. * user monster collection status.
  1037. *
  1038. * @param resource $db Database connection.
  1039. * @param int $player_id Player ID.
  1040. * @param mixed[] $json Response.
  1041. */
  1042. function parse_collection($db, $player_id, $json){
  1043. // Clear previous collection data
  1044. $db->query("PRAGMA foreign_keys = OFF;");
  1045. $statement = $db->prepare("DELETE FROM collection WHERE player = :player;");
  1046. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1047. $statement->execute();
  1048. foreach ($json->{"collection"} as $unit){
  1049. $statement = $db->prepare("
  1050. INSERT INTO collection (player, monster, open)
  1051. VALUES (:player, :monster, :open);
  1052. ");
  1053. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1054. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  1055. $statement->bindValue(":open", $unit->{"open"}, SQLITE3_INTEGER);
  1056. $statement->execute();
  1057. }
  1058. // Return success
  1059. return "201 Created.";
  1060. }
  1061. /**
  1062. * Parses the response to the GetLobbyWizardLog command and updates the
  1063. * user records.
  1064. *
  1065. * @param resource $db Database connection.
  1066. * @param int $player_id Player ID.
  1067. * @param mixed[] $json Response.
  1068. */
  1069. function parse_records($db, $player_id, $json){
  1070. # Page 1: Palayer stats, Cairos non-elemental
  1071. if (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 1){
  1072. // Update data in player table
  1073. $db->query("PRAGMA foreign_keys = OFF;");
  1074. // TODO Check TOA Hell key and update.
  1075. $statement = $db->prepare("
  1076. UPDATE player SET
  1077. joined = :joined,
  1078. top_rank_arena = :top_rank_arena,
  1079. top_rank_world_arena = :top_rank_world_arena,
  1080. top_rank_special_league = :top_rank_special_league,
  1081. top_rank_gw = :top_rank_gw,
  1082. top_rank_siege = :top_rank_siege,
  1083. top_rank_wboss = :top_rank_wboss,
  1084. top_rank_toan = :top_rank_toan,
  1085. top_rank_toah = :top_rank_toah,
  1086. top_rank_toal = :top_rank_toal
  1087. WHERE id = :id;
  1088. ");
  1089. $statement->bindValue(":joined", $json->{"lobby_wizard_log"}->{"account_create_timestamp"}, SQLITE3_TEXT);
  1090. $statement->bindValue(":top_rank_arena", $json->{"lobby_wizard_log"}->{"pvp_best_rating_id"}, SQLITE3_INTEGER);
  1091. $statement->bindValue(":top_rank_world_arena", $json->{"lobby_wizard_log"}->{"rtpvp_rank_best_rating_id"}, SQLITE3_INTEGER);
  1092. $statement->bindValue(":top_rank_special_league", $json->{"lobby_wizard_log"}->{"rtpvp_contest_best_rating_id"}, SQLITE3_INTEGER);
  1093. $statement->bindValue(":top_rank_gw", $json->{"lobby_wizard_log"}->{"guildwar_best_rating_id"}, SQLITE3_INTEGER);
  1094. $statement->bindValue(":top_rank_siege", $json->{"lobby_wizard_log"}->{"guildsiege_best_rating_id"}, SQLITE3_INTEGER);
  1095. $statement->bindValue(":top_rank_wboss", $json->{"lobby_wizard_log"}->{"world_boss_best_rank_id"}, SQLITE3_INTEGER);
  1096. $statement->bindValue(":top_rank_toan", $json->{"lobby_wizard_log"}->{"trial_tower_normal_best_floor"}, SQLITE3_INTEGER);
  1097. $statement->bindValue(":top_rank_toah", $json->{"lobby_wizard_log"}->{"trial_tower_hard_best_floor"}, SQLITE3_INTEGER);
  1098. $statement->bindValue(":top_rank_toal", $json->{"lobby_wizard_log"}->{"trial_tower_hell_best_star"}, SQLITE3_INTEGER);
  1099. $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
  1100. $statement->execute();
  1101. // Loop Cairos records (non elemental dungeons)
  1102. foreach ($json->{"lobby_wizard_log"}->{"dungeon_best_clear_info_list"} as $record){
  1103. // Delete prevous data
  1104. $statement = $db->prepare("
  1105. DELETE FROM record_party
  1106. WHERE
  1107. player = :player AND
  1108. area_type = :area_type AND
  1109. area = :area;
  1110. ");
  1111. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1112. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1113. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1114. $statement->execute();
  1115. $statement = $db->prepare("
  1116. DELETE FROM record
  1117. WHERE
  1118. player = :player AND
  1119. area_type = :area_type AND
  1120. area = :area;
  1121. ");
  1122. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1123. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1124. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1125. $statement->execute();
  1126. // Insert new data
  1127. $statement = $db->prepare("
  1128. INSERT INTO record
  1129. (player, area_type, area, stage, time, score, rank)
  1130. VALUES
  1131. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1132. ");
  1133. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1134. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1135. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1136. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1137. $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
  1138. $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_INTEGER);
  1139. $statement->bindValue(":score", 0, SQLITE3_INTEGER);
  1140. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  1141. $statement->execute();
  1142. // Loop party
  1143. foreach ($record->{"my_unit_deck_list"} as $party){
  1144. $statement = $db->prepare("
  1145. INSERT INTO record_party
  1146. (player, area_type, area, unit, monster, leader, front)
  1147. VALUES
  1148. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1149. ");
  1150. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1151. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1152. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1153. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1154. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1155. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1156. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1157. $statement->execute();
  1158. }
  1159. }
  1160. // Rift Raid record
  1161. if (sizeof($json->{"lobby_wizard_log"}->{"raid_best_clear_info_list"}) > 0){
  1162. $record = $json->{"lobby_wizard_log"}->{"raid_best_clear_info_list"}[0];
  1163. // Delete prevous data
  1164. $statement = $db->prepare("
  1165. DELETE FROM record_party
  1166. WHERE
  1167. player = :player AND
  1168. area_type = :area_type AND
  1169. area = :area;
  1170. ");
  1171. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1172. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1173. $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
  1174. $statement->execute();
  1175. $statement = $db->prepare("
  1176. DELETE FROM record
  1177. WHERE
  1178. player = :player AND
  1179. area_type = :area_type AND
  1180. area = :area;
  1181. ");
  1182. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1183. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1184. $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
  1185. $statement->execute();
  1186. // Insert new data
  1187. $statement = $db->prepare("
  1188. INSERT INTO record
  1189. (player, area_type, area, stage, time, score, rank)
  1190. VALUES
  1191. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1192. ");
  1193. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1194. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1195. $statement->bindValue(":area", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1196. $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
  1197. $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_TEXT);
  1198. $statement->bindValue(":score", 0, SQLITE3_INTEGER);
  1199. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  1200. $statement->execute();
  1201. // Loop party
  1202. foreach ($record->{"my_unit_deck_list"} as $party){
  1203. $statement = $db->prepare("
  1204. INSERT INTO record_party
  1205. (player, area_type, area, unit, monster, leader, front)
  1206. VALUES
  1207. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1208. ");
  1209. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1210. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1211. $statement->bindValue(":area", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1212. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1213. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1214. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1215. if ($party->{"slot_index"} <= 4){
  1216. $statement->bindValue(":front", 1, SQLITE3_INTEGER);
  1217. }
  1218. else{
  1219. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1220. }
  1221. $statement->execute();
  1222. }
  1223. }
  1224. // Loop Rift Dungeon records
  1225. foreach ( $json->{"lobby_wizard_log"}->{"rift_dungeon_best_clear_info_list"} as $record){
  1226. // Delete prevous data
  1227. $statement = $db->prepare("
  1228. DELETE FROM record_party
  1229. WHERE
  1230. player = :player AND
  1231. area_type = :area_type AND
  1232. area = :area;
  1233. ");
  1234. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1235. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
  1236. $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
  1237. $statement->execute();
  1238. $statement = $db->prepare("
  1239. DELETE FROM record
  1240. WHERE
  1241. player = :player AND
  1242. area_type = :area_type AND
  1243. area = :area;
  1244. ");
  1245. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1246. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
  1247. $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
  1248. $statement->execute();
  1249. // Insert new data
  1250. $statement = $db->prepare("
  1251. INSERT INTO record
  1252. (player, area_type, area, stage, time, score, rank)
  1253. VALUES
  1254. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1255. ");
  1256. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1257. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
  1258. $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
  1259. $statement->bindValue(":stage", 0, SQLITE3_INTEGER);
  1260. $statement->bindValue(":time", 0, SQLITE3_INTEGER);
  1261. $statement->bindValue(":score", $record->{"clear_damage"}, SQLITE3_INTEGER);
  1262. switch (intval($record->{"clear_rating"})){
  1263. case 2:
  1264. $statement->bindValue(":rank", "D", SQLITE3_TEXT);
  1265. break;
  1266. case 3:
  1267. $statement->bindValue(":rank", "C", SQLITE3_TEXT);
  1268. break;
  1269. case 4:
  1270. $statement->bindValue(":rank", "B-", SQLITE3_TEXT);
  1271. break;
  1272. case 5:
  1273. $statement->bindValue(":rank", "B", SQLITE3_TEXT);
  1274. break;
  1275. case 6:
  1276. $statement->bindValue(":rank", "B+", SQLITE3_TEXT);
  1277. break;
  1278. case 7:
  1279. $statement->bindValue(":rank", "A-", SQLITE3_TEXT);
  1280. break;
  1281. case 8:
  1282. $statement->bindValue(":rank", "A", SQLITE3_TEXT);
  1283. break;
  1284. case 9:
  1285. $statement->bindValue(":rank", "A+", SQLITE3_TEXT);
  1286. break;
  1287. case 10:
  1288. $statement->bindValue(":rank", "S", SQLITE3_TEXT);
  1289. break;
  1290. case 11:
  1291. $statement->bindValue(":rank", "SS", SQLITE3_TEXT);
  1292. break;
  1293. case 12:
  1294. $statement->bindValue(":rank", "SSS", SQLITE3_TEXT);
  1295. break;
  1296. default:
  1297. $statement->bindValue(":rank", "F", SQLITE3_TEXT);
  1298. }
  1299. $statement->execute();
  1300. // Loop party
  1301. foreach ($record->{"my_unit_deck_list"} as $party){
  1302. $statement = $db->prepare("
  1303. INSERT INTO record_party
  1304. (player, area_type, area, unit, monster, leader, front)
  1305. VALUES
  1306. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1307. ");
  1308. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1309. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
  1310. $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
  1311. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1312. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1313. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1314. if ($party->{"slot_index"} <= 4){
  1315. $statement->bindValue(":front", 1, SQLITE3_INTEGER);
  1316. }
  1317. else{
  1318. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1319. }
  1320. $statement->execute();
  1321. }
  1322. }
  1323. // Loop Dimension Hole records
  1324. foreach ( $json->{"lobby_wizard_log"}->{"dimension_hole_dungeon_best_clear_info_list"} as $record){
  1325. // Delete prevous data
  1326. $statement = $db->prepare("
  1327. DELETE FROM record_party
  1328. WHERE
  1329. player = :player AND
  1330. area_type = :area_type AND
  1331. area = :area;
  1332. ");
  1333. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1334. $statement->bindValue(":area_type", AREA_TYPE_ID::DIMENSIONAL_HOLE, SQLITE3_INTEGER);
  1335. $statement->bindValue(":area", $record->{"dimension_hole_dungeon_id"}, SQLITE3_INTEGER);
  1336. $statement->execute();
  1337. $statement = $db->prepare("
  1338. DELETE FROM record
  1339. WHERE
  1340. player = :player AND
  1341. area_type = :area_type AND
  1342. area = :area;
  1343. ");
  1344. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1345. $statement->bindValue(":area_type", AREA_TYPE_ID::DIMENSIONAL_HOLE, SQLITE3_INTEGER);
  1346. $statement->bindValue(":area", $record->{"dimension_hole_dungeon_id"}, SQLITE3_INTEGER);
  1347. $statement->execute();
  1348. // Insert new data
  1349. $statement = $db->prepare("
  1350. INSERT INTO record
  1351. (player, area_type, area, stage, time, score, rank)
  1352. VALUES
  1353. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1354. ");
  1355. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1356. $statement->bindValue(":area_type", AREA_TYPE_ID::DIMENSIONAL_HOLE, SQLITE3_INTEGER);
  1357. $statement->bindValue(":area", $record->{"dimension_hole_dungeon_id"}, SQLITE3_INTEGER);
  1358. $statement->bindValue(":stage", $record->{"difficulty"}, SQLITE3_INTEGER);
  1359. $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_INTEGER);
  1360. $statement->bindValue(":score", 0, SQLITE3_INTEGER);
  1361. $statement->bindValue(":rank", "", SQLITE3_TEXT);
  1362. $statement->execute();
  1363. // Loop party
  1364. foreach ($record->{"my_unit_deck_list"} as $party){
  1365. $statement = $db->prepare("
  1366. INSERT INTO record_party
  1367. (player, area_type, area, unit, monster, leader, front)
  1368. VALUES
  1369. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1370. ");
  1371. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1372. $statement->bindValue(":area_type", AREA_TYPE_ID::DIMENSIONAL_HOLE, SQLITE3_INTEGER);
  1373. $statement->bindValue(":area", $record->{"dimension_hole_dungeon_id"}, SQLITE3_INTEGER);
  1374. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1375. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1376. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1377. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1378. $statement->execute();
  1379. }
  1380. }
  1381. }
  1382. # Page 2: All of Cairos dungeons.
  1383. # TODO: Non elementals can be skipped here, they are processed in page 1 too
  1384. elseif (intval($json->{"lobby_wizard_log"}->{"page_no"} == 2)){
  1385. // Loop Cairos records
  1386. foreach ($json->{"lobby_wizard_log"}->{"dungeon_best_clear_info_list"} as $record){
  1387. // Delete prevous data
  1388. $statement = $db->prepare("
  1389. DELETE FROM record_party
  1390. WHERE
  1391. player = :player AND
  1392. area_type = :area_type AND
  1393. area = :area;
  1394. ");
  1395. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1396. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1397. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1398. $statement->execute();
  1399. $statement = $db->prepare("
  1400. DELETE FROM record
  1401. WHERE
  1402. player = :player AND
  1403. area_type = :area_type AND
  1404. area = :area;
  1405. ");
  1406. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1407. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1408. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1409. $statement->execute();
  1410. // Insert new data
  1411. $statement = $db->prepare("
  1412. INSERT INTO record
  1413. (player, area_type, area, stage, time, score, rank)
  1414. VALUES
  1415. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1416. ");
  1417. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1418. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1419. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1420. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1421. $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
  1422. $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_INTEGER);
  1423. $statement->bindValue(":score", 0, SQLITE3_INTEGER);
  1424. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  1425. $statement->execute();
  1426. // Loop party
  1427. foreach ($record->{"my_unit_deck_list"} as $party){
  1428. $statement = $db->prepare("
  1429. INSERT INTO record_party
  1430. (player, area_type, area, unit, monster, leader, front)
  1431. VALUES
  1432. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1433. ");
  1434. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1435. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1436. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1437. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1438. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1439. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1440. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1441. $statement->execute();
  1442. }
  1443. }
  1444. }
  1445. // Page 3 has Rift Raid and Dungeons, can be skipped (data in page 1 too).
  1446. elseif (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 3){
  1447. return "204 Page not logged (only pages 1 and 2 are needed).";
  1448. }
  1449. // Page 4 has Dimension Hole Dungeons, can be skipped (data in page 1 too).
  1450. elseif (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 4){
  1451. return "204 Page not logged (only pages 1 and 2 are needed).";
  1452. }
  1453. else{
  1454. return "400 Unknown page.";
  1455. }
  1456. // Return success
  1457. return "201 Created.";
  1458. }
  1459. /**
  1460. * Parses the request and response to the getUnitStorageList command and
  1461. * adds the units in the Sealed Shrine as items.
  1462. *
  1463. * @param resource $db Database connection.
  1464. * @param int $player_id Player ID.
  1465. * @param mixed[] $json Response.
  1466. */
  1467. function parse_sealed_shrine($db, $player_id, $json){
  1468. foreach ($json->{"unit_storage_list"} as $monster){
  1469. $id = $monster->{"unit_master_id"};
  1470. // Delete prevous data
  1471. $statement = $db->prepare("
  1472. DELETE FROM item
  1473. WHERE
  1474. player = :player AND
  1475. type = :type AND
  1476. id = :id;
  1477. ");
  1478. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1479. $statement->bindValue(":type", INVENTORY_TYPE_ID::SEALED_MONSTER, SQLITE3_INTEGER);
  1480. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  1481. $statement->execute();
  1482. $statement = $db->prepare("
  1483. INSERT INTO item (player, id, type, amount)
  1484. VALUES (:player, :id, :type, :amount);
  1485. ");
  1486. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1487. $statement->bindValue(":type", INVENTORY_TYPE_ID::SEALED_MONSTER, SQLITE3_INTEGER);
  1488. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  1489. $statement->bindValue(":amount", $monster->{"quantity"}, SQLITE3_INTEGER);
  1490. $statement->execute();
  1491. }
  1492. // Return success
  1493. return "201 Created.";
  1494. }
  1495. try{
  1496. header("Content-type: application/json; charset=utf-8");
  1497. // Get put data
  1498. $_PUT = [];
  1499. parse_str(file_get_contents("php://input"), $_PUT);
  1500. // Check API key.
  1501. $key = $_PUT["key"];
  1502. if ($key == null || $key == false){
  1503. header("HTTP/1.1 400 API key not received.");
  1504. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 API key not received.");
  1505. return 400;
  1506. }
  1507. // Check data
  1508. $data = $_PUT["response"];
  1509. if ($data == null || $data == false){
  1510. header("HTTP/1.1 400 No data received.");
  1511. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 No data received.");
  1512. return 400;
  1513. }
  1514. // Try to get request text, required for some commands.
  1515. $request = null;
  1516. $json_request = null;
  1517. if (isset($_PUT["request"])){
  1518. $request = $_PUT["request"];
  1519. $json_request = json_decode($request);
  1520. }
  1521. // Check data format.
  1522. $json = json_decode($data);
  1523. if ($json === null){
  1524. header("HTTP/1.1 400 Data is no valid JSON.");
  1525. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Data is no valid JSON.");
  1526. return 400;
  1527. }
  1528. // Get and validate command
  1529. $command = $json->{"command"};
  1530. $accepted_commands = [
  1531. "HubUserLogin", // Login, full profile
  1532. "GetUnitCollection", // Unit collection
  1533. "GetLobbyWizardLog", // Records
  1534. "getUnitStorageList" // Sealed units
  1535. ];
  1536. if (!in_array($command, $accepted_commands)){
  1537. header("HTTP/1.1 405 Command not implemented.");
  1538. syslog(LOG_INFO, "[APIv1] HTTP/1.1 405 Command not implemented.");
  1539. return 405;
  1540. }
  1541. $db = get_context()->get_db();
  1542. // Get user info and Authenticate
  1543. switch ($command){
  1544. case "HubUserLogin":
  1545. $player_id = $json->{"wizard_id"};
  1546. $statement = $db->prepare("
  1547. SELECT COUNT(player.id) AS c
  1548. FROM
  1549. user,
  1550. player
  1551. WHERE
  1552. user.id = player.user AND
  1553. player.id = :player AND
  1554. user.api_key = :api_key;");
  1555. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1556. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1557. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1558. // Player doesnt exists. If there is a user, create player.
  1559. $statement = $db->prepare("
  1560. SELECT id
  1561. FROM user
  1562. WHERE api_key = :api_key;");
  1563. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1564. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  1565. if (! $r){
  1566. header("HTTP/1.1 401 Invalid credentials.");
  1567. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1568. return 401;
  1569. }
  1570. $statement = $db->prepare("
  1571. INSERT INTO player (
  1572. id,
  1573. user,
  1574. server,
  1575. country,
  1576. lang,
  1577. name,
  1578. mana,
  1579. crystal,
  1580. level,
  1581. experience,
  1582. energy,
  1583. energy_max,
  1584. energy_per_min,
  1585. arena_energy,
  1586. rep,
  1587. social_point,
  1588. honor_point,
  1589. guild_point,
  1590. darkportal_energy,
  1591. dimension_energy,
  1592. costume_point,
  1593. honor_medal,
  1594. honor_mark,
  1595. event_coin,
  1596. storage_slots,
  1597. island,
  1598. joined,
  1599. top_rank_arena,
  1600. top_rank_world_arena,
  1601. top_rank_special_league,
  1602. top_rank_gw,
  1603. top_rank_siege,
  1604. top_rank_wboss,
  1605. top_rank_toan,
  1606. top_rank_toah,
  1607. top_rank_toal,
  1608. public,
  1609. mode,
  1610. last_access
  1611. ) VALUES (
  1612. :id,
  1613. :user,
  1614. :server,
  1615. :country,
  1616. :lang,
  1617. '', --name,
  1618. 0, --mana,
  1619. 0, --crystal,
  1620. 1, --level,
  1621. 0, --experience,
  1622. 0, --energy,
  1623. 0, --energy_max,
  1624. 0, --energy_per_min,
  1625. 0, --arena_energy,
  1626. null, --rep,
  1627. 0, --social_point,
  1628. 0, --honor_point,
  1629. 0, --guild_point,
  1630. 0, --darkportal_energy,
  1631. 0, --dimension_energy,
  1632. 0, --costume_point,
  1633. 0, --honor_medal,
  1634. 0, --honor_mark,
  1635. 0, --event_coin,
  1636. 0, --storage_slots,
  1637. 0, --island,
  1638. null, --joined,
  1639. 0, --top_rank_arena,
  1640. 0, --top_rank_world_arena,
  1641. 0, --top_rank_special_league,
  1642. 0, --top_rank_gw,
  1643. 0, --top_rank_siege,
  1644. 0, --top_rank_wboss,
  1645. 0, --top_rank_toan,
  1646. 0, --top_rank_toah,
  1647. 0, --top_rank_toal,
  1648. 0, --public,
  1649. 0, --mode,
  1650. null --last_access,
  1651. )");
  1652. $statement->bindValue(":id", $json->{"wizard_id"}, SQLITE3_INTEGER);
  1653. $statement->bindValue(":user", $r["id"], SQLITE3_INTEGER);
  1654. // TODO: Default server EUROPE, detect and fix
  1655. $statement->bindValue(":server", 2, SQLITE3_INTEGER);
  1656. $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"}, SQLITE3_TEXT);
  1657. $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"}, SQLITE3_TEXT);
  1658. $statement->execute();
  1659. }
  1660. break;
  1661. case "GetUnitCollection":
  1662. // TODO: The new user/player distinction wont work here
  1663. // No identification in this JSON, get player id from databases
  1664. header("HTTP/1.1 501 Not implemented.");
  1665. syslog(LOG_INFO, "[APIv1] HTTP/1.1 501 Not implemented.");
  1666. return 501;
  1667. $statement = $db->prepare("SELECT id FROM player WHERE api_key = :api_key;");
  1668. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1669. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  1670. if ($r){
  1671. $uid = $r["id"];
  1672. }
  1673. else{
  1674. header("HTTP/1.1 401 Invalid credentials.");
  1675. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1676. return 401;
  1677. }
  1678. break;
  1679. case "GetLobbyWizardLog":
  1680. $player_id = $json->{"lobby_wizard_log"}->{"wizard_id"};
  1681. $statement = $db->prepare("
  1682. SELECT COUNT(player.id) AS c
  1683. FROM
  1684. user,
  1685. player
  1686. WHERE
  1687. user.id = player.user AND
  1688. player.id = :player AND
  1689. user.api_key = :api_key;");
  1690. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1691. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1692. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1693. header("HTTP/1.1 401 Invalid credentials.");
  1694. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1695. return 401;
  1696. }
  1697. break;
  1698. case "getUnitStorageList":
  1699. if ($request == null || $request == false){
  1700. header("HTTP/1.1 400 Request data required for getUnitStorageList action.");
  1701. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Request data required for getUnitStorageList action.");
  1702. return 400;
  1703. }
  1704. $player_id = $json_request->{"wizard_id"};
  1705. $statement = $db->prepare("
  1706. SELECT COUNT(player.id) AS c
  1707. FROM
  1708. user,
  1709. player
  1710. WHERE
  1711. user.id = player.user AND
  1712. player.id = :player AND
  1713. user.api_key = :api_key;");
  1714. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1715. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1716. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1717. header("HTTP/1.1 401 Invalid credentials.");
  1718. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1719. return 401;
  1720. }
  1721. break;
  1722. }
  1723. if ($command == "GetUnitCollection"){ // This command doesnt provide wizard_id
  1724. // TODO: The new user/player distinction wont work here
  1725. $statement = $db->prepare("SELECT COUNT(id) AS c FROM player WHERE id = :id AND api_key = :api_key;");
  1726. $statement->bindValue(":id", $uid, SQLITE3_INTEGER);
  1727. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1728. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1729. header("HTTP/1.1 401 Invalid credentials.");
  1730. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1731. return 401;
  1732. }
  1733. }
  1734. // Run parser
  1735. $result = "500 Unknown Error";
  1736. switch($command){
  1737. case "HubUserLogin":
  1738. $result = parse_profile($db, $player_id, $json);
  1739. break;
  1740. case "GetUnitCollection":
  1741. $result = parse_collection($db, $player_id, $json);
  1742. break;
  1743. case "GetLobbyWizardLog":
  1744. $result = parse_records($db, $player_id, $json);
  1745. break;
  1746. case "getUnitStorageList":
  1747. $result = parse_sealed_shrine($db, $player_id, $json);
  1748. break;
  1749. }
  1750. // Process result
  1751. $status_code = intval(substr($result, 0, 3));
  1752. $status_message = substr($result, 4);
  1753. if ($status_code == 0 || $status_code < 100 || $status_code > 599){
  1754. header("HTTP/1.1 500 Unknown error.");
  1755. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unknown error.");
  1756. return 500;
  1757. }
  1758. else{
  1759. header("HTTP/1.1 $status_code $status_message");
  1760. syslog(LOG_INFO, "[APIv1] HTTP/1.1 $status_code $status_message");
  1761. return $status_code;
  1762. }
  1763. }
  1764. catch(Exception $e) {
  1765. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  1766. syslog(LOG_INFO, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  1767. return 500;
  1768. }