PUT.php 70 KB

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