PUT.php 62 KB

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