PUT.php 72 KB

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