PUT.php 67 KB

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