PUT.php 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  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. try{
  519. header("Content-type: application/json; charset=utf-8");
  520. // Get put data
  521. parse_str(file_get_contents("php://input"), $_PUT);
  522. // Check API key.
  523. $key = $_PUT["key"];
  524. if ($key == null || $key == false){
  525. header("HTTP/1.1 400 API key not received.");
  526. syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 API key not received.");
  527. return 400;
  528. }
  529. // Check data
  530. $data = $_PUT["response"];
  531. if ($data == null || $data == false){
  532. header("HTTP/1.1 400 No data received.");
  533. syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 No data received.");
  534. return 400;
  535. }
  536. // Check data format.
  537. $json = json_decode($data);
  538. if ($json === null){
  539. header("HTTP/1.1 400 Data is no valid JSON.");
  540. syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 Data is no valid JSON.");
  541. return 400;
  542. }
  543. // Authenticate
  544. $uid = $json->{"wizard_id"};
  545. $uname = $json->{"wizard_info"}->{"wizard_name"};
  546. $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key';");
  547. $statement->bindValue(":uid", $uid);
  548. $statement->bindValue(":api_key", $key);
  549. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  550. header("HTTP/1.1 401 Invalid credentials.");
  551. syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials.");
  552. return 401;
  553. }
  554. // Save data file
  555. $dtime = (new DateTime())->format("Y-m-dTH:i:s");
  556. $fname = ($_SERVER["DOCUMENT_ROOT"] . "/../data/profile_" . $uid . "_" . $uname . "_" . $dtime . ".json");
  557. try{
  558. file_put_contents($fname, $data);
  559. }
  560. catch(Exception $e) {
  561. header("HTTP/1.1 500 Unable to save profile file: " . $e->getMessage());
  562. syslog(LOG_INFO, "[APIv3] HTTP/1.1 500 Unable to save profile file: " . $e->getMessage());
  563. return 500;
  564. }
  565. // Clear previous profile data
  566. $db->query("PRAGMA foreign_keys = OFF;");
  567. $statement = $db->prepare("DELETE FROM unit_skill WHERE unit IN (SELECT id FROM unit WHERE uid = :uid);");
  568. $statement->bindValue(":uid", $uid);
  569. $statement->execute();
  570. $tables_to_clear = [
  571. "scenario",
  572. "defense",
  573. "gw_defense",
  574. "unit",
  575. "rune",
  576. "artifact",
  577. "building",
  578. "decoration",
  579. "inventory",
  580. "rune_craft"
  581. ];
  582. foreach ($tables_to_clear as $table){
  583. $statement = $db->prepare("DELETE FROM $table WHERE uid = :uid;");
  584. $statement->bindValue(":uid", $uid);
  585. $statement->execute();
  586. }
  587. $db->query("DELETE FROM summon_special"); # For every player, will be reloaded.
  588. // Get rune lock list
  589. $rune_lock_list = $json->{"rune_lock_list"};
  590. // Update player data
  591. $statement = $db->prepare("
  592. UPDATE player
  593. SET
  594. name = :name AND
  595. mana = :mana AND
  596. crystal = :crystal AND
  597. country = :country AND
  598. lang = :lang AND
  599. level = :level AND
  600. experience = :experience AND
  601. energy = :energy AND
  602. energy_max = :energy_max AND
  603. energy_per_min = :energy_per_min AND
  604. arena_energy = :arena_energy AND
  605. arena_energy_max = :arena_energy_max AND
  606. rep = :rep AND
  607. social_point = :social_point AND
  608. honor_point = :honor_point AND
  609. guild_point = :guild_point AND
  610. darkportal_energy = :darkportal_energy AND
  611. darkportal_energy_max = :darkportal_energy_max AND
  612. dimension_energy = :dimension_energy AND
  613. dimension_energy_max = :dimension_energy_max AND
  614. costume_point = :costume_point AND
  615. costume_point_max = :costume_point_max AND
  616. honor_medal = :honor_medal AND
  617. honor_mark = :honor_mark AND
  618. event_coin = :event_coin AND
  619. storage_slots = :storage_slots AND
  620. island = :island_upgrade
  621. WHERE uid = :uid;
  622. ");
  623. $statement->bindValue(":name", $json->{"wizard_info"}->{"wizard_name"});
  624. $statement->bindValue(":mana", $json->{"wizard_info"}->{"wizard_mana"});
  625. $statement->bindValue(":crystal", $json->{"wizard_info"}->{"wizard_crystal"});
  626. $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"});
  627. $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"});
  628. $statement->bindValue(":level", $json->{"wizard_info"}->{"wizard_level"});
  629. $statement->bindValue(":experience", $json->{"wizard_info"}->{"experience"});
  630. $statement->bindValue(":energy", $json->{"wizard_info"}->{"wizard_energy"});
  631. $statement->bindValue(":energy_max", $json->{"wizard_info"}->{"energy_max"});
  632. $statement->bindValue(":energy_per_min", $json->{"wizard_info"}->{"energy_per_min"});
  633. $statement->bindValue(":arena_energy", $json->{"wizard_info"}->{"arena_energy"});
  634. $statement->bindValue(":arena_energy_max", $json->{"wizard_info"}->{"arena_energy_max"});
  635. $statement->bindValue(":rep", $json->{"wizard_info"}->{"rep_unit_id"});
  636. $statement->bindValue(":social_point", $json->{"wizard_info"}->{"social_point_current"});
  637. $statement->bindValue(":honor_point", $json->{"wizard_info"}->{"honor_point"});
  638. $statement->bindValue(":guild_point", $json->{"wizard_info"}->{"guild_point"});
  639. $statement->bindValue(":darkportal_energy", $json->{"wizard_info"}->{"darkportal_energy"});
  640. $statement->bindValue(":darkportal_energy_max", $json->{"wizard_info"}->{"darkportal_energy_max"});
  641. $statement->bindValue(":dimension_energy", $json->{"dimension_hole_info"}->{"energy"});
  642. $statement->bindValue(":dimension_energy_max", $json->{"dimension_hole_info"}->{"energy_max"});
  643. $statement->bindValue(":costume_point", $json->{"wizard_info"}->{"costume_point"});
  644. $statement->bindValue(":costume_point_max", $json->{"wizard_info"}->{"costume_point_max"});
  645. $statement->bindValue(":honor_medal", $json->{"wizard_info"}->{"honor_medal"});
  646. $statement->bindValue(":honor_mark", $json->{"wizard_info"}->{"honor_mark"});
  647. $statement->bindValue(":event_coin", $json->{"wizard_info"}->{"event_coin"});
  648. $statement->bindValue(":storage_slots", $json->{"unit_depository_slots"}->{"number"});
  649. $island_upgrade = 0;
  650. foreach ($json->{"island_info"} as $island){
  651. if (intval($island->{"id"}) > 100 && intval($island->{"open"}) == 1){
  652. $island_upgrade = intval($island->{"id"});
  653. }
  654. }
  655. $statement->bindValue(":island_upgrade", $island_upgrade);
  656. $statement->bindValue(":uid", $uid);
  657. $statement->execute();
  658. // Update scenarion completion
  659. foreach ($json->{"scenario_list"} as $sce){
  660. $statement = $db->prepare("
  661. INSERT INTO scenario (
  662. uid,
  663. region,
  664. difficulty,
  665. cleared,
  666. max_cleared
  667. ) VALUES (
  668. :uid,
  669. :region,
  670. :difficulty,
  671. :cleared,
  672. :max_cleared
  673. );
  674. ");
  675. $statement->bindValue(":uid", $uid);
  676. $statement->bindValue(":region", $sce->{"region_id"});
  677. $statement->bindValue(":difficulty", $sce->{"difficulty"});
  678. $statement->bindValue(":cleared", $sce->{"cleared"});
  679. $max_cleared = 0;
  680. foreach ($sce->{"stage_list"} as $stage){
  681. if (intval($stage->{"cleared"}) == 1){
  682. $max_cleared = intval($stage->{"stage_no"});
  683. }
  684. }
  685. $statement->bindValue(":max_cleared", $max_cleared);
  686. $statement->execute();
  687. }
  688. // Parse Arena defense
  689. foreach ($json->{"defense_unit_list"} as $defense){
  690. $statement = $db->prepare("
  691. INSERT INTO defense (
  692. uid,
  693. unit,
  694. position
  695. ) VALUES (
  696. :uid,
  697. :unit,
  698. :position
  699. );
  700. ");
  701. $statement->bindValue(":uid", $uid);
  702. $statement->bindValue(":unit", $defense->{"unit_id"});
  703. $statement->bindValue(":position", $defense->{"pos_id"});
  704. $statement->execute();
  705. }
  706. // Parse GW defense
  707. if ($json->{"guildwar_defense_unit_list"}[0]){
  708. $position = 1;
  709. foreach ($json->{"guildwar_defense_unit_list"}[0] as $defense){
  710. $statement = $db->prepare("
  711. INSERT INTO gw_defense (
  712. uid,
  713. unit,
  714. position
  715. ) VALUES (
  716. :uid,
  717. :unit,
  718. :position
  719. );
  720. ");
  721. $statement->bindValue(":uid", $uid);
  722. $statement->bindValue(":unit", $defense->{"unit_id"});
  723. $statement->bindValue(":position", $position);
  724. $statement->execute();
  725. $position ++;
  726. }
  727. }
  728. if ($json->{"guildwar_defense_unit_list"}[1]){
  729. $position = 4;
  730. foreach ($json->{"guildwar_defense_unit_list"}[1] as $defense){
  731. $statement = $db->prepare("
  732. INSERT INTO gw_defense (
  733. uid,
  734. unit,
  735. position
  736. ) VALUES (
  737. :uid,
  738. :unit,
  739. :position
  740. );
  741. ");
  742. $statement->bindValue(":uid", $uid);
  743. $statement->bindValue(":unit", $defense->{"unit_id"});
  744. $statement->bindValue(":position", $position);
  745. $statement->execute();
  746. $position ++;
  747. }
  748. }
  749. // Parse buildings
  750. foreach($json->{"building_list"} as $building){
  751. $statement = $db->prepare("
  752. INSERT INTO building (
  753. uid,
  754. id,
  755. building,
  756. gain
  757. ) VALUES (
  758. :uid,
  759. :id,
  760. :building,
  761. :gain
  762. );
  763. ");
  764. $statement->bindValue(":uid", $uid);
  765. $statement->bindValue(":id", $building->{"building_id"});
  766. $statement->bindValue(":building", $building->{"building_master_id"});
  767. $statement->bindValue(":gain", $building->{"gain_per_hour"});
  768. $statement->execute();
  769. }
  770. // Parse decorarion
  771. foreach($json->{"deco_list"} as $building){
  772. $statement = $db->prepare("
  773. INSERT INTO decoration (
  774. uid,
  775. id,
  776. decoration,
  777. level
  778. ) VALUES (
  779. :uid,
  780. :id,
  781. :decoration,
  782. :level
  783. );
  784. ");
  785. $statement->bindValue(":uid", $uid);
  786. $statement->bindValue(":id", $building->{"deco_id"});
  787. $statement->bindValue(":decoration", $building->{"master_id"});
  788. $statement->bindValue(":level", $building->{"level"});
  789. $statement->execute();
  790. }
  791. // Parse units
  792. $lock_list = $json->{"unit_lock_list"};
  793. foreach ($json->{"unit_list"} as $unit){
  794. $statement = $db->prepare("
  795. INSERT INTO unit (
  796. uid,
  797. id,
  798. building,
  799. unit,
  800. level,
  801. stars,
  802. hp,
  803. attack,
  804. defense,
  805. speed,
  806. crit_rate,
  807. crit_damage,
  808. resistance,
  809. accuracy,
  810. experience,
  811. exp_gained,
  812. exp_gain_rate,
  813. costume,
  814. attribute,
  815. source,
  816. create_time,
  817. homunculus,
  818. homunculus_name,
  819. lock
  820. ) VALUES (
  821. :uid,
  822. :id,
  823. :building,
  824. :unit,
  825. :level,
  826. :stars,
  827. :hp,
  828. :attack,
  829. :defense,
  830. :speed,
  831. :crit_rate,
  832. :crit_damage,
  833. :resistance,
  834. :accuracy,
  835. :experience,
  836. :exp_gained,
  837. :exp_gain_rate,
  838. :costume,
  839. :attribute,
  840. :source,
  841. :create_time,
  842. :homunculus,
  843. :homunculus_name,
  844. :lock
  845. );
  846. ");
  847. $statement->bindValue(":uid", $uid);
  848. $statement->bindValue(":id", $unit->{"unit_id"});
  849. $statement->bindValue(":building", $unit->{"building_id"});
  850. $statement->bindValue(":unit", $unit->{"unit_master_id"});
  851. $statement->bindValue(":level", $unit->{"unit_level"});
  852. $statement->bindValue(":stars", $unit->{"class"});
  853. $statement->bindValue(":hp", intval($unit->{"con"}) * 15); # Always x15
  854. $statement->bindValue(":attack", $unit->{"atk"});
  855. $statement->bindValue(":defense", $unit->{"def"});
  856. $statement->bindValue(":speed", $unit->{"spd"});
  857. $statement->bindValue(":crit_rate", $unit->{"critical_rate"});
  858. $statement->bindValue(":crit_damage", $unit->{"critical_damage"});
  859. $statement->bindValue(":resistance", $unit->{"resist"});
  860. $statement->bindValue(":accuracy", $unit->{"accuracy"});
  861. $statement->bindValue(":experience", $unit->{"experience"});
  862. $statement->bindValue(":exp_gained", $unit->{"exp_gained"});
  863. $statement->bindValue(":exp_gain_rate", $unit->{"exp_gain_rate"});
  864. $statement->bindValue(":costume", $unit->{"costume_master_id"});
  865. $statement->bindValue(":attribute", $unit->{"attribute"});
  866. $statement->bindValue(":source", $unit->{"source"});
  867. $statement->bindValue(":create_time", $unit->{"create_time"});
  868. $statement->bindValue(":homunculus", $unit->{"homunculus"});
  869. $statement->bindValue(":homunculus_name", $unit->{"homunculus_name"});
  870. $lock = 0;
  871. if (in_array($unit->{"unit_id"}, $lock_list)){
  872. $lock = 1;
  873. }
  874. $statement->bindValue(":lock", $lock);
  875. $statement->execute();
  876. foreach ($unit->{"skills"} as $skill){
  877. $statement = $db->prepare("
  878. INSERT INTO unit_skill (
  879. unit,
  880. skill,
  881. level
  882. ) VALUES (
  883. :unit,
  884. :skill,
  885. :level
  886. );
  887. ");
  888. $statement->bindValue(":unit", $unit->{"unit_id"});
  889. $statement->bindValue(":skill", $skill[0]);
  890. $statement->bindValue(":level", $skill[1]);
  891. $statement->execute();
  892. }
  893. }
  894. // Parse invetory
  895. foreach ($json->{"inventory_info"} as $item){
  896. $statement = $db->prepare("
  897. INSERT INTO inventory (
  898. uid,
  899. id,
  900. type,
  901. amount
  902. ) VALUES (
  903. :uid,
  904. :id,
  905. :type,
  906. :amount
  907. );
  908. ");
  909. $statement->bindValue(":uid", $uid);
  910. $statement->bindValue(":id", $item->{"item_master_id"});
  911. $statement->bindValue(":type", $item->{"item_master_type"});
  912. $statement->bindValue(":amount", $item->{"item_quantity"});
  913. $statement->execute();
  914. }
  915. // Fix rune craft item type.
  916. $db->query("UPDATE inventory SET type = 27 WHERE type = 29 AND id IN (2001, 4001, 4002, 4003, 9001, 9002, 9003, 8001);");
  917. // Parse Summon Stone summoning list
  918. // TODO: Set propper dates
  919. foreach ($json->{"summon_special_info"}->{"this"} as $unit){
  920. $statement = $db->prepare("
  921. INSERT INTO summon_special (
  922. week,
  923. unit
  924. ) VALUES (
  925. :week,
  926. :unit
  927. );
  928. ");
  929. $statement->bindValue(":week", 0);
  930. $statement->bindValue(":unit", $unit);
  931. $statement->execute();
  932. }
  933. foreach ($json->{"summon_special_info"}->{"next"} as $unit){
  934. $statement = $db->prepare("
  935. INSERT INTO summon_special (
  936. week,
  937. unit
  938. ) VALUES (
  939. :week,
  940. :unit
  941. );
  942. ");
  943. $statement->bindValue(":week", 1);
  944. $statement->bindValue(":unit", $unit);
  945. $statement->execute();
  946. }
  947. foreach ($json->{"summon_special_info"}->{"third"} as $unit){
  948. $statement = $db->prepare("
  949. INSERT INTO summon_special (
  950. week,
  951. unit
  952. ) VALUES (
  953. :week,
  954. :unit
  955. );
  956. ");
  957. $statement->bindValue(":week", 2);
  958. $statement->bindValue(":unit", $unit);
  959. $statement->execute();
  960. }
  961. foreach ($json->{"summon_special_info"}->{"fourth"} as $unit){
  962. $statement = $db->prepare("
  963. INSERT INTO summon_special (
  964. week,
  965. unit
  966. ) VALUES (
  967. :week,
  968. :unit
  969. );
  970. ");
  971. $statement->bindValue(":week", 3);
  972. $statement->bindValue(":unit", $unit);
  973. $statement->execute();
  974. }
  975. // Parse runes
  976. foreach ($json->{"runes"} as $rune){
  977. parse_rune($uid, $rune, $rune_lock_list);
  978. }
  979. foreach ($json->{"unit_list"} as $unit){
  980. foreach ($unit->{"runes"} as $rune){
  981. parse_rune($uid, $rune, $rune_lock_list);
  982. }
  983. }
  984. // Parse artifacts
  985. foreach ($json->{"artifacts"} as $artifact){
  986. parse_artifact($uid, $artifact);
  987. }
  988. foreach ($json->{"unit_list"} as $unit){
  989. foreach ($unit->{"artifacts"} as $artifact){
  990. parse_artifact($uid, $artifact);
  991. }
  992. }
  993. // Parse enchantments
  994. foreach ($json->{"rune_craft_item_list"} as $item){
  995. $statement = $db->prepare("
  996. INSERT INTO rune_craft (
  997. uid,
  998. id,
  999. type,
  1000. quality,
  1001. rune,
  1002. stat,
  1003. value,
  1004. amount
  1005. ) VALUES (
  1006. :uid,
  1007. :id,
  1008. :type,
  1009. :quality,
  1010. :rune,
  1011. :stat,
  1012. :value,
  1013. :amount
  1014. );
  1015. ");
  1016. $statement->bindValue(":uid", $uid);
  1017. $statement->bindValue(":id", $item->{"craft_item_id"});
  1018. $statement->bindValue(":type", $item->{"craft_type"});
  1019. $statement->bindValue(":value", $item->{"sell_value"});
  1020. $statement->bindValue(":amount", $item->{"amount"});
  1021. $info = str_pad(intval($item->{"craft_type_id"}), 6, "0", STR_PAD_LEFT);
  1022. /*
  1023. info : 5 or 6 digit number: RRSSQQ
  1024. RR: Rune (may not be padded)
  1025. SS: Stat
  1026. QQ: Quality
  1027. */
  1028. $statement->bindValue(":quality", intval(substr($info, 4, 2)));
  1029. $statement->bindValue(":stat", intval(substr($info, 2, 2)));
  1030. $statement->bindValue(":rune", intval(substr($info, 0, 2)));
  1031. $statement->execute();
  1032. }
  1033. // Parse guild
  1034. if ($json->{"guild"}->{"guild_info"} != null){
  1035. $guild = $json->{"guild"}->{"guild_info"};
  1036. $statement = $db->prepare("DELETE FROM guild WHERE id = :id;");
  1037. $statement->bindValue(":id", $guild->{"guild_id"});
  1038. $statement->execute();
  1039. $statement = $db->prepare("DELETE FROM guild_member WHERE guild = :guild;");
  1040. $statement->bindValue(":guild", $guild->{"guild_id"});
  1041. $statement->execute();
  1042. $statement = $db->prepare("
  1043. INSERT INTO guild (
  1044. id,
  1045. name,
  1046. level,
  1047. experience,
  1048. recruiting,
  1049. members,
  1050. leader,
  1051. comment,
  1052. notice
  1053. ) VALUES (
  1054. :id,
  1055. :name,
  1056. :level,
  1057. :experience,
  1058. :recruiting,
  1059. :members,
  1060. :leader,
  1061. :comment,
  1062. :notice
  1063. );
  1064. ");
  1065. $statement->bindValue(":id",$guild->{"guild_id"});
  1066. $statement->bindValue(":name",$guild->{"name"});
  1067. $statement->bindValue(":level",$guild->{"level"});
  1068. $statement->bindValue(":experience",$guild->{"experience"});
  1069. $statement->bindValue(":recruiting",$guild->{"recruit_status"});
  1070. $statement->bindValue(":members",$guild->{"member_now"});
  1071. $statement->bindValue(":leader",$guild->{"master_wizard_id"});
  1072. $statement->bindValue(":comment",$guild->{"comment"});
  1073. $statement->bindValue(":notice",$guild->{"notice"});
  1074. $statement->execute();
  1075. foreach ($json->{"guild"}->{"guild_members"} as $member){
  1076. $statement = $db->prepare("
  1077. INSERT INTO guild_member (
  1078. id,
  1079. guild,
  1080. grade,
  1081. name,
  1082. level,
  1083. rating,
  1084. arena_score,
  1085. joined,
  1086. last_login,
  1087. in_war,
  1088. has_defense
  1089. ) VALUES (
  1090. :id,
  1091. :guild,
  1092. :grade,
  1093. :name,
  1094. :level,
  1095. :rating,
  1096. :arena_score,
  1097. :joined,
  1098. :last_login,
  1099. :in_war,
  1100. :has_defense
  1101. );
  1102. ");
  1103. $statement->bindValue(":id", $member->{"wizard_id"});
  1104. $statement->bindValue(":guild", $member->{"guild_id"});
  1105. $statement->bindValue(":grade", $member->{"grade"});
  1106. $statement->bindValue(":name", $member->{"wizard_name"});
  1107. $statement->bindValue(":level", $member->{"wizard_level"});
  1108. $statement->bindValue(":rating", $member->{"rating_id"});
  1109. $statement->bindValue(":arena_score", $member->{"arena_score"});
  1110. $statement->bindValue(":joined", $member->{"join_timestamp"});
  1111. $statement->bindValue(":last_login", $member->{"last_login_timestamp"});
  1112. $in_war = 0;
  1113. foreach ($json->{"guildwar_member_list"} as $war){
  1114. if ($war->{"wizard_id"} == $member->{"wizard_id"}){
  1115. $in_war = 1;
  1116. break;
  1117. }
  1118. }
  1119. $has_defense = 0;
  1120. foreach ($json->{"guild_member_defense_list"} as $defense){
  1121. if ($defense->{"wizard_id"} == $member->{"wizard_id"}){
  1122. $has_defense = 1;
  1123. break;
  1124. }
  1125. }
  1126. $statement->bindValue(":in_war", $in_war);
  1127. $statement->bindValue(":has_defense", $has_defense);
  1128. $statement->execute();
  1129. }
  1130. }
  1131. // Update invalid teams
  1132. $statement = $db->prepare("
  1133. DELETE FROM team
  1134. WHERE
  1135. id IN (
  1136. SELECT DISTINCT team
  1137. FROM team_unit
  1138. WHERE unit NOT IN (
  1139. SELECT DISTINCT id FROM unit WHERE uid = :uid
  1140. )
  1141. ) AND
  1142. uid = :uid
  1143. ");
  1144. $statement->bindValue(":uid", $uid);
  1145. $statement->execute();
  1146. $db->query("
  1147. DELETE FROM team_unit
  1148. WHERE
  1149. unit NOT IN (
  1150. SELECT DISTINCT id FROM unit
  1151. ) OR
  1152. team NOT IN (
  1153. SELECT DISTINCT id FROM team
  1154. );"
  1155. );
  1156. // At this point, status code should be 200
  1157. header("HTTP/1.1 204 Profile imported.");
  1158. syslog(LOG_INFO, "[APIv3] HTTP/1.1 204 Profile imported.");
  1159. return 204;
  1160. }
  1161. catch(Exception $e) {
  1162. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  1163. syslog(LOG_INFO, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  1164. return 500;
  1165. }
  1166. ?>