PUT.php 68 KB

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