PUT.php 63 KB

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