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