PUT.php 67 KB

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