PUT.php 65 KB

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