PUT.php 68 KB

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