PUT.php 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702
  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 siege defenses.
  587. $i = 0;
  588. foreach ($json->{"guildsiege_defense_unit_list"} as $defense){
  589. $statement = $db->prepare("
  590. INSERT INTO defense (
  591. player,
  592. area_type,
  593. slot,
  594. unit,
  595. position
  596. ) VALUES (
  597. :player,
  598. :area_type,
  599. :slot,
  600. :unit,
  601. :position
  602. );
  603. ");
  604. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  605. $statement->bindValue(":area_type", AREA_TYPE_ID::GUILD_SIEGE, SQLITE3_INTEGER);
  606. $statement->bindValue(":slot", floor($i / 3), SQLITE3_INTEGER);
  607. $statement->bindValue(":unit", $defense, SQLITE3_INTEGER);
  608. $statement->bindValue(":position", ($i % 3) + 1, SQLITE3_INTEGER);
  609. $statement->execute();
  610. $i ++;
  611. }
  612. // Parse buildings
  613. foreach($json->{"building_list"} as $building){
  614. $statement = $db->prepare("
  615. INSERT INTO building (
  616. player,
  617. id,
  618. buildable,
  619. gain
  620. ) VALUES (
  621. :player,
  622. :id,
  623. :buildable,
  624. :gain
  625. );
  626. ");
  627. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  628. $statement->bindValue(":id", $building->{"building_id"}, SQLITE3_INTEGER);
  629. $statement->bindValue(":buildable", $building->{"building_master_id"}, SQLITE3_INTEGER);
  630. $statement->bindValue(":gain", $building->{"gain_per_hour"}, SQLITE3_INTEGER);
  631. $statement->execute();
  632. }
  633. // Parse decorarion
  634. foreach($json->{"deco_list"} as $building){
  635. $statement = $db->prepare("
  636. INSERT INTO decoration (
  637. player,
  638. id,
  639. decorative,
  640. level
  641. ) VALUES (
  642. :player,
  643. :id,
  644. :decorative,
  645. :level
  646. );
  647. ");
  648. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  649. $statement->bindValue(":id", $building->{"deco_id"}, SQLITE3_INTEGER);
  650. $statement->bindValue(":decorative", $building->{"master_id"}, SQLITE3_INTEGER);
  651. $statement->bindValue(":level", $building->{"level"}, SQLITE3_INTEGER);
  652. $statement->execute();
  653. }
  654. // Parse units
  655. $lock_list = $json->{"unit_lock_list"};
  656. foreach ($json->{"unit_list"} as $unit){
  657. $statement = $db->prepare("
  658. INSERT INTO unit (
  659. player,
  660. id,
  661. building,
  662. monster,
  663. level,
  664. stars,
  665. hp,
  666. attack,
  667. defense,
  668. speed,
  669. crit_rate,
  670. crit_damage,
  671. resistance,
  672. accuracy,
  673. experience,
  674. exp_gained,
  675. exp_gain_rate,
  676. costume,
  677. source,
  678. create_time,
  679. homunculus_name,
  680. lock
  681. ) VALUES (
  682. :player,
  683. :id,
  684. :building,
  685. :monster,
  686. :level,
  687. :stars,
  688. :hp,
  689. :attack,
  690. :defense,
  691. :speed,
  692. :crit_rate,
  693. :crit_damage,
  694. :resistance,
  695. :accuracy,
  696. :experience,
  697. :exp_gained,
  698. :exp_gain_rate,
  699. :costume,
  700. :source,
  701. :create_time,
  702. :homunculus_name,
  703. :lock
  704. );
  705. ");
  706. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  707. $statement->bindValue(":id", $unit->{"unit_id"}, SQLITE3_INTEGER);
  708. $statement->bindValue(":building", $unit->{"building_id"}, SQLITE3_INTEGER);
  709. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  710. $statement->bindValue(":level", $unit->{"unit_level"}, SQLITE3_INTEGER);
  711. $statement->bindValue(":stars", $unit->{"class"}, SQLITE3_INTEGER);
  712. $statement->bindValue(":hp", intval($unit->{"con"}) * 15, SQLITE3_INTEGER); # Always x15
  713. $statement->bindValue(":attack", $unit->{"atk"}, SQLITE3_INTEGER);
  714. $statement->bindValue(":defense", $unit->{"def"}, SQLITE3_INTEGER);
  715. $statement->bindValue(":speed", $unit->{"spd"}, SQLITE3_INTEGER);
  716. $statement->bindValue(":crit_rate", $unit->{"critical_rate"}, SQLITE3_INTEGER);
  717. $statement->bindValue(":crit_damage", $unit->{"critical_damage"}, SQLITE3_INTEGER);
  718. $statement->bindValue(":resistance", $unit->{"resist"}, SQLITE3_INTEGER);
  719. $statement->bindValue(":accuracy", $unit->{"accuracy"}, SQLITE3_INTEGER);
  720. $statement->bindValue(":experience", $unit->{"experience"}, SQLITE3_INTEGER);
  721. $statement->bindValue(":exp_gained", $unit->{"exp_gained"}, SQLITE3_INTEGER);
  722. $statement->bindValue(":exp_gain_rate", $unit->{"exp_gain_rate"}, SQLITE3_INTEGER);
  723. $statement->bindValue(":costume", $unit->{"costume_master_id"}, SQLITE3_INTEGER);
  724. $statement->bindValue(":source", $unit->{"source"}, SQLITE3_INTEGER);
  725. $statement->bindValue(":create_time", $unit->{"create_time"}, SQLITE3_INTEGER);
  726. $statement->bindValue(":homunculus_name", $unit->{"homunculus_name"}, SQLITE3_TEXT);
  727. $lock = 0;
  728. if (in_array($unit->{"unit_id"}, $lock_list)){
  729. $lock = 1;
  730. }
  731. $statement->bindValue(":lock", $lock, SQLITE3_INTEGER);
  732. $statement->execute();
  733. foreach ($unit->{"skills"} as $skill){
  734. $statement = $db->prepare("
  735. INSERT INTO unit_skill (
  736. unit,
  737. skill,
  738. level
  739. ) VALUES (
  740. :unit,
  741. :skill,
  742. :level
  743. );
  744. ");
  745. $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
  746. $statement->bindValue(":skill", $skill[0], SQLITE3_INTEGER);
  747. $statement->bindValue(":level", $skill[1], SQLITE3_INTEGER);
  748. $statement->execute();
  749. }
  750. }
  751. // Parse items
  752. foreach ($json->{"inventory_info"} as $item){
  753. $statement = $db->prepare("
  754. INSERT INTO item (
  755. player,
  756. id,
  757. type,
  758. amount
  759. ) VALUES (
  760. :player,
  761. :id,
  762. :type,
  763. :amount
  764. );
  765. ");
  766. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  767. $statement->bindValue(":id", $item->{"item_master_id"}, SQLITE3_INTEGER);
  768. $statement->bindValue(":type", $item->{"item_master_type"}, SQLITE3_INTEGER);
  769. $statement->bindValue(":amount", $item->{"item_quantity"}, SQLITE3_INTEGER);
  770. $statement->execute();
  771. }
  772. // Fix rune craft item type.
  773. $db->query("UPDATE item SET type = 27 WHERE type = 29 AND id IN (2001, 4001, 4002, 4003, 9001, 9002, 9003, 8001);");
  774. // Parse Summon Stone summoning list
  775. // TODO: Set propper dates
  776. foreach ($json->{"summon_special_info"}->{"this"} as $unit){
  777. $statement = $db->prepare("
  778. INSERT INTO summon_special (
  779. week,
  780. unit
  781. ) VALUES (
  782. :week,
  783. :unit
  784. );
  785. ");
  786. $statement->bindValue(":week", 0, SQLITE3_INTEGER);
  787. $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
  788. $statement->execute();
  789. }
  790. foreach ($json->{"summon_special_info"}->{"next"} as $unit){
  791. $statement = $db->prepare("
  792. INSERT INTO summon_special (
  793. week,
  794. unit
  795. ) VALUES (
  796. :week,
  797. :unit
  798. );
  799. ");
  800. $statement->bindValue(":week", 1, SQLITE3_INTEGER);
  801. $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
  802. $statement->execute();
  803. }
  804. foreach ($json->{"summon_special_info"}->{"third"} as $unit){
  805. $statement = $db->prepare("
  806. INSERT INTO summon_special (
  807. week,
  808. unit
  809. ) VALUES (
  810. :week,
  811. :unit
  812. );
  813. ");
  814. $statement->bindValue(":week", 2, SQLITE3_INTEGER);
  815. $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
  816. $statement->execute();
  817. }
  818. foreach ($json->{"summon_special_info"}->{"fourth"} as $unit){
  819. $statement = $db->prepare("
  820. INSERT INTO summon_special (
  821. week,
  822. unit
  823. ) VALUES (
  824. :week,
  825. :unit
  826. );
  827. ");
  828. $statement->bindValue(":week", 3, SQLITE3_INTEGER);
  829. $statement->bindValue(":unit", $unit, SQLITE3_INTEGER);
  830. $statement->execute();
  831. }
  832. // Parse runes
  833. foreach ($json->{"runes"} as $rune){
  834. parse_rune($player_id, $rune, $rune_lock_list);
  835. }
  836. foreach ($json->{"unit_list"} as $unit){
  837. foreach ($unit->{"runes"} as $rune){
  838. parse_rune($player_id, $rune, $rune_lock_list);
  839. }
  840. }
  841. foreach ($json->{"world_arena_rune_equip_list"} as $rta_rune){
  842. $statement = $db->prepare("
  843. INSERT INTO unit_rune (unit, rune, rta)
  844. VALUES (:unit, :rune, :rta);
  845. ");
  846. $statement->bindValue(":unit", $rta_rune->{"occupied_id"}, SQLITE3_INTEGER);
  847. $statement->bindValue(":rune", $rta_rune->{"rune_id"}, SQLITE3_INTEGER);
  848. $statement->bindValue(":rta", GAME_MODE_ID::RTA, SQLITE3_INTEGER);
  849. $statement->execute();
  850. }
  851. // Parse artifacts
  852. foreach ($json->{"artifacts"} as $artifact){
  853. parse_artifact($player_id, $artifact);
  854. }
  855. foreach ($json->{"unit_list"} as $unit){
  856. foreach ($unit->{"artifacts"} as $artifact){
  857. parse_artifact($player_id, $artifact);
  858. }
  859. }
  860. foreach ($json->{"world_arena_artifact_equip_list"} as $rta_artifact){
  861. $statement = $db->prepare("
  862. INSERT INTO unit_artifact (unit, artifact, rta)
  863. VALUES (:unit, :artifact, :rta);
  864. ");
  865. $statement->bindValue(":unit", $rta_artifact->{"occupied_id"}, SQLITE3_INTEGER);
  866. $statement->bindValue(":artifact", $rta_artifact->{"artifact_id"}, SQLITE3_INTEGER);
  867. $statement->bindValue(":rta", GAME_MODE_ID::RTA, SQLITE3_INTEGER);
  868. $statement->execute();
  869. }
  870. // Parse enchantments
  871. foreach ($json->{"rune_craft_item_list"} as $item){
  872. $statement = $db->prepare("
  873. INSERT INTO enchantment (
  874. player,
  875. id,
  876. type,
  877. quality,
  878. rune,
  879. stat,
  880. value,
  881. amount
  882. ) VALUES (
  883. :player,
  884. :id,
  885. :type,
  886. :quality,
  887. :rune,
  888. :stat,
  889. :value,
  890. :amount
  891. );
  892. ");
  893. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  894. $statement->bindValue(":id", $item->{"craft_item_id"}, SQLITE3_INTEGER);
  895. $statement->bindValue(":type", $item->{"craft_type"}, SQLITE3_INTEGER);
  896. $statement->bindValue(":value", $item->{"sell_value"}, SQLITE3_INTEGER);
  897. $statement->bindValue(":amount", $item->{"amount"}, SQLITE3_INTEGER);
  898. $info = str_pad(intval($item->{"craft_type_id"}), 6, "0", STR_PAD_LEFT);
  899. /*
  900. info : 5 or 6 digit number: RRSSQQ
  901. RR: Rune (may not be padded)
  902. SS: Stat
  903. QQ: Quality
  904. */
  905. $statement->bindValue(":quality", intval(substr($info, 4, 2)), SQLITE3_INTEGER);
  906. $statement->bindValue(":stat", intval(substr($info, 2, 2)), SQLITE3_INTEGER);
  907. $statement->bindValue(":rune", intval(substr($info, 0, 2)), SQLITE3_INTEGER);
  908. $statement->execute();
  909. }
  910. // Parse guild
  911. if ($json->{"guild"}->{"guild_info"} != null){
  912. $guild = $json->{"guild"}->{"guild_info"};
  913. $statement = $db->prepare("DELETE FROM guild WHERE id = :id;");
  914. $statement->bindValue(":id", $guild->{"guild_id"}, SQLITE3_INTEGER);
  915. $statement->execute();
  916. $statement = $db->prepare("DELETE FROM guild_member WHERE guild = :guild;");
  917. $statement->bindValue(":guild", $guild->{"guild_id"}, SQLITE3_INTEGER);
  918. $statement->execute();
  919. $statement = $db->prepare("
  920. INSERT INTO guild (
  921. id,
  922. name,
  923. level,
  924. experience,
  925. recruiting,
  926. members,
  927. leader,
  928. comment,
  929. notice
  930. ) VALUES (
  931. :id,
  932. :name,
  933. :level,
  934. :experience,
  935. :recruiting,
  936. :members,
  937. :leader,
  938. :comment,
  939. :notice
  940. );
  941. ");
  942. $statement->bindValue(":id",$guild->{"guild_id"}, SQLITE3_INTEGER);
  943. $statement->bindValue(":name",$guild->{"name"}, SQLITE3_TEXT);
  944. $statement->bindValue(":level",$guild->{"level"}, SQLITE3_INTEGER);
  945. $statement->bindValue(":experience",$guild->{"experience"}, SQLITE3_INTEGER);
  946. $statement->bindValue(":recruiting",$guild->{"recruit_status"}, SQLITE3_INTEGER);
  947. $statement->bindValue(":members",$guild->{"member_now"}, SQLITE3_INTEGER);
  948. $statement->bindValue(":leader",$guild->{"master_wizard_id"}, SQLITE3_INTEGER);
  949. $statement->bindValue(":comment",$guild->{"comment"}, SQLITE3_TEXT);
  950. $statement->bindValue(":notice",$guild->{"notice"}, SQLITE3_TEXT);
  951. $statement->execute();
  952. foreach ($json->{"guild"}->{"guild_members"} as $member){
  953. $statement = $db->prepare("
  954. INSERT INTO guild_member (
  955. member,
  956. guild,
  957. grade,
  958. name,
  959. level,
  960. rating,
  961. arena_score,
  962. joined,
  963. last_login,
  964. in_war,
  965. has_defense
  966. ) VALUES (
  967. :member,
  968. :guild,
  969. :grade,
  970. :name,
  971. :level,
  972. :rating,
  973. :arena_score,
  974. :joined,
  975. :last_login,
  976. :in_war,
  977. :has_defense
  978. );
  979. ");
  980. $statement->bindValue(":member", $member->{"wizard_id"}, SQLITE3_INTEGER);
  981. $statement->bindValue(":guild", $member->{"guild_id"}, SQLITE3_INTEGER);
  982. $statement->bindValue(":grade", $member->{"grade"}, SQLITE3_INTEGER);
  983. $statement->bindValue(":name", $member->{"wizard_name"}, SQLITE3_TEXT);
  984. $statement->bindValue(":level", $member->{"wizard_level"}, SQLITE3_INTEGER);
  985. $statement->bindValue(":rating", $member->{"rating_id"}, SQLITE3_INTEGER);
  986. $statement->bindValue(":arena_score", $member->{"arena_score"}, SQLITE3_INTEGER);
  987. $statement->bindValue(":joined", $member->{"join_timestamp"}, SQLITE3_TEXT);
  988. $statement->bindValue(":last_login", $member->{"last_login_timestamp"}, SQLITE3_TEXT);
  989. $in_war = 0;
  990. foreach ($json->{"guildwar_member_list"} as $war){
  991. if ($war->{"wizard_id"} == $member->{"wizard_id"}){
  992. $in_war = 1;
  993. break;
  994. }
  995. }
  996. $has_defense = 0;
  997. foreach ($json->{"guild_member_defense_list"} as $defense){
  998. if ($defense->{"wizard_id"} == $member->{"wizard_id"}){
  999. $has_defense = 1;
  1000. break;
  1001. }
  1002. }
  1003. $statement->bindValue(":in_war", $in_war, SQLITE3_INTEGER);
  1004. $statement->bindValue(":has_defense", $has_defense, SQLITE3_INTEGER);
  1005. $statement->execute();
  1006. }
  1007. }
  1008. // Update invalid teams
  1009. $statement = $db->prepare("
  1010. DELETE FROM team
  1011. WHERE
  1012. id IN (
  1013. SELECT DISTINCT team
  1014. FROM team_unit
  1015. WHERE unit NOT IN (
  1016. SELECT DISTINCT id FROM unit WHERE player = :player
  1017. )
  1018. ) AND
  1019. player = :player
  1020. ");
  1021. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1022. $statement->execute();
  1023. $db->query("
  1024. DELETE FROM team_unit
  1025. WHERE
  1026. unit NOT IN (
  1027. SELECT DISTINCT id FROM unit
  1028. ) OR
  1029. team NOT IN (
  1030. SELECT DISTINCT id FROM team
  1031. );"
  1032. );
  1033. // Return success
  1034. return "201 Created.";
  1035. }
  1036. /**
  1037. * Parses the response to the GetUnitCollection command and updates the
  1038. * user monster collection status.
  1039. *
  1040. * @param resource $db Database connection.
  1041. * @param int $player_id Player ID.
  1042. * @param mixed[] $json Response.
  1043. */
  1044. function parse_collection($db, $player_id, $json){
  1045. // Clear previous collection data
  1046. $db->query("PRAGMA foreign_keys = OFF;");
  1047. $statement = $db->prepare("DELETE FROM collection WHERE player = :player;");
  1048. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1049. $statement->execute();
  1050. foreach ($json->{"collection"} as $unit){
  1051. $statement = $db->prepare("
  1052. INSERT INTO collection (player, monster, open)
  1053. VALUES (:player, :monster, :open);
  1054. ");
  1055. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1056. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  1057. $statement->bindValue(":open", $unit->{"open"}, SQLITE3_INTEGER);
  1058. $statement->execute();
  1059. }
  1060. // Return success
  1061. return "201 Created.";
  1062. }
  1063. /**
  1064. * Parses the response to the GetLobbyWizardLog command and updates the
  1065. * user records.
  1066. *
  1067. * @param resource $db Database connection.
  1068. * @param int $player_id Player ID.
  1069. * @param mixed[] $json Response.
  1070. */
  1071. function parse_records($db, $player_id, $json){
  1072. # Page 1: Cairos non-elemental, rift raid, rift dungeon.
  1073. if (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 1){
  1074. // Update data in player table
  1075. $db->query("PRAGMA foreign_keys = OFF;");
  1076. // TODO Check TOA Hell key and update.
  1077. $statement = $db->prepare("
  1078. UPDATE player SET
  1079. joined = :joined,
  1080. top_rank_arena = :top_rank_arena,
  1081. top_rank_world_arena = :top_rank_world_arena,
  1082. top_rank_special_league = :top_rank_special_league,
  1083. top_rank_gw = :top_rank_gw,
  1084. top_rank_siege = :top_rank_siege,
  1085. top_rank_wboss = :top_rank_wboss,
  1086. top_rank_toan = :top_rank_toan,
  1087. top_rank_toah = :top_rank_toah
  1088. WHERE id = :id;
  1089. ");
  1090. $statement->bindValue(":joined", $json->{"lobby_wizard_log"}->{"account_create_timestamp"}, SQLITE3_TEXT);
  1091. $statement->bindValue(":top_rank_arena", $json->{"lobby_wizard_log"}->{"pvp_best_rating_id"}, SQLITE3_INTEGER);
  1092. $statement->bindValue(":top_rank_world_arena", $json->{"lobby_wizard_log"}->{"rtpvp_rank_best_rating_id"}, SQLITE3_INTEGER);
  1093. $statement->bindValue(":top_rank_special_league", $json->{"lobby_wizard_log"}->{"rtpvp_contest_best_rating_id"}, SQLITE3_INTEGER);
  1094. $statement->bindValue(":top_rank_gw", $json->{"lobby_wizard_log"}->{"guildwar_best_rating_id"}, SQLITE3_INTEGER);
  1095. $statement->bindValue(":top_rank_siege", $json->{"lobby_wizard_log"}->{"guildsiege_best_rating_id"}, SQLITE3_INTEGER);
  1096. $statement->bindValue(":top_rank_wboss", $json->{"lobby_wizard_log"}->{"world_boss_best_rank_id"}, SQLITE3_INTEGER);
  1097. $statement->bindValue(":top_rank_toan", $json->{"lobby_wizard_log"}->{"trial_tower_normal_best_floor"}, SQLITE3_INTEGER);
  1098. $statement->bindValue(":top_rank_toah", $json->{"lobby_wizard_log"}->{"trial_tower_hard_best_floor"}, SQLITE3_INTEGER);
  1099. $statement->bindValue(":id", $player_id, SQLITE3_INTEGER);
  1100. $statement->execute();
  1101. // Loop Cairos records
  1102. foreach ($json->{"lobby_wizard_log"}->{"dungeon_best_clear_info_list"} as $record){
  1103. // Delete prevous data
  1104. $statement = $db->prepare("
  1105. DELETE FROM record_party
  1106. WHERE
  1107. player = :player AND
  1108. area_type = :area_type AND
  1109. area = :area;
  1110. ");
  1111. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1112. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1113. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1114. $statement->execute();
  1115. $statement = $db->prepare("
  1116. DELETE FROM record
  1117. WHERE
  1118. player = :player AND
  1119. area_type = :area_type AND
  1120. area = :area;
  1121. ");
  1122. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1123. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1124. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1125. $statement->execute();
  1126. // Insert new data
  1127. $statement = $db->prepare("
  1128. INSERT INTO record
  1129. (player, area_type, area, stage, time, score, rank)
  1130. VALUES
  1131. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1132. ");
  1133. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1134. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1135. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1136. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1137. $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
  1138. $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_INTEGER);
  1139. $statement->bindValue(":score", 0, SQLITE3_INTEGER);
  1140. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  1141. $statement->execute();
  1142. // Loop party
  1143. foreach ($record->{"my_unit_deck_list"} as $party){
  1144. $statement = $db->prepare("
  1145. INSERT INTO record_party
  1146. (player, area_type, area, unit, monster, leader, front)
  1147. VALUES
  1148. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1149. ");
  1150. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1151. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1152. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1153. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1154. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1155. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1156. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1157. $statement->execute();
  1158. }
  1159. }
  1160. // Rift Raid record
  1161. if (sizeof($json->{"lobby_wizard_log"}->{"raid_best_clear_info_list"}) > 0){
  1162. $record = $json->{"lobby_wizard_log"}->{"raid_best_clear_info_list"}[0];
  1163. // Delete prevous data
  1164. $statement = $db->prepare("
  1165. DELETE FROM record_party
  1166. WHERE
  1167. player = :player AND
  1168. area_type = :area_type AND
  1169. area = :area;
  1170. ");
  1171. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1172. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1173. $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
  1174. $statement->execute();
  1175. $statement = $db->prepare("
  1176. DELETE FROM record
  1177. WHERE
  1178. player = :player AND
  1179. area_type = :area_type AND
  1180. area = :area;
  1181. ");
  1182. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1183. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1184. $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
  1185. $statement->execute();
  1186. // Insert new data
  1187. $statement = $db->prepare("
  1188. INSERT INTO record
  1189. (player, area_type, area, stage, time, score, rank)
  1190. VALUES
  1191. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1192. ");
  1193. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1194. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1195. $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
  1196. $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
  1197. $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_TEXT);
  1198. $statement->bindValue(":score", 0, SQLITE3_INTEGER);
  1199. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  1200. $statement->execute();
  1201. // Loop party
  1202. foreach ($record->{"my_unit_deck_list"} as $party){
  1203. $statement = $db->prepare("
  1204. INSERT INTO record_party
  1205. (player, area_type, area, unit, monster, leader, front)
  1206. VALUES
  1207. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1208. ");
  1209. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1210. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  1211. $statement->bindValue(":area", $record->{"stage_id"}, SQLITE3_INTEGER);
  1212. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1213. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1214. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1215. if ($party->{"slot_index"} <= 4){
  1216. $statement->bindValue(":front", 1, SQLITE3_INTEGER);
  1217. }
  1218. else{
  1219. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1220. }
  1221. $statement->execute();
  1222. }
  1223. }
  1224. // Loop Rift Dungeon records
  1225. foreach ( $json->{"lobby_wizard_log"}->{"rift_dungeon_best_clear_info_list"} as $record){
  1226. // Delete prevous data
  1227. $statement = $db->prepare("
  1228. DELETE FROM record_party
  1229. WHERE
  1230. player = :player AND
  1231. area_type = :area_type AND
  1232. area = :area;
  1233. ");
  1234. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1235. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
  1236. $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
  1237. $statement->execute();
  1238. $statement = $db->prepare("
  1239. DELETE FROM record
  1240. WHERE
  1241. player = :player AND
  1242. area_type = :area_type AND
  1243. area = :area;
  1244. ");
  1245. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1246. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
  1247. $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
  1248. $statement->execute();
  1249. // Insert new data
  1250. $statement = $db->prepare("
  1251. INSERT INTO record
  1252. (player, area_type, area, stage, time, score, rank)
  1253. VALUES
  1254. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1255. ");
  1256. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1257. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
  1258. $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
  1259. $statement->bindValue(":stage", 0, SQLITE3_INTEGER);
  1260. $statement->bindValue(":time", 0, SQLITE3_INTEGER);
  1261. $statement->bindValue(":score", $record->{"clear_damage"}, SQLITE3_INTEGER);
  1262. switch (intval($record->{"clear_rating"})){
  1263. case 2:
  1264. $statement->bindValue(":rank", "D", SQLITE3_TEXT);
  1265. break;
  1266. case 3:
  1267. $statement->bindValue(":rank", "C", SQLITE3_TEXT);
  1268. break;
  1269. case 4:
  1270. $statement->bindValue(":rank", "B-", SQLITE3_TEXT);
  1271. break;
  1272. case 5:
  1273. $statement->bindValue(":rank", "B", SQLITE3_TEXT);
  1274. break;
  1275. case 6:
  1276. $statement->bindValue(":rank", "B+", SQLITE3_TEXT);
  1277. break;
  1278. case 7:
  1279. $statement->bindValue(":rank", "A-", SQLITE3_TEXT);
  1280. break;
  1281. case 8:
  1282. $statement->bindValue(":rank", "A", SQLITE3_TEXT);
  1283. break;
  1284. case 9:
  1285. $statement->bindValue(":rank", "A+", SQLITE3_TEXT);
  1286. break;
  1287. case 10:
  1288. $statement->bindValue(":rank", "S", SQLITE3_TEXT);
  1289. break;
  1290. case 11:
  1291. $statement->bindValue(":rank", "SS", SQLITE3_TEXT);
  1292. break;
  1293. case 12:
  1294. $statement->bindValue(":rank", "SS", SQLITE3_TEXT);
  1295. break;
  1296. default:
  1297. $statement->bindValue(":rank", "F", SQLITE3_TEXT);
  1298. }
  1299. $statement->execute();
  1300. // Loop party
  1301. foreach ($record->{"my_unit_deck_list"} as $party){
  1302. $statement = $db->prepare("
  1303. INSERT INTO record_party
  1304. (player, area_type, area, unit, monster, leader, front)
  1305. VALUES
  1306. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1307. ");
  1308. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1309. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON, SQLITE3_INTEGER);
  1310. $statement->bindValue(":area", $record->{"rift_dungeon_id"}, SQLITE3_INTEGER);
  1311. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1312. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1313. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1314. if ($party->{"slot_index"} <= 4){
  1315. $statement->bindValue(":front", 1, SQLITE3_INTEGER);
  1316. }
  1317. else{
  1318. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1319. }
  1320. $statement->execute();
  1321. }
  1322. }
  1323. }
  1324. # Page 2: Cairos non-elemental, rift raid, rift dungeon.
  1325. elseif (intval($json->{"lobby_wizard_log"}->{"page_no"} == 2)){
  1326. // Loop Cairos records
  1327. foreach ($json->{"lobby_wizard_log"}->{"dungeon_best_clear_info_list"} as $record){
  1328. // Delete prevous data
  1329. $statement = $db->prepare("
  1330. DELETE FROM record_party
  1331. WHERE
  1332. player = :player AND
  1333. area_type = :area_type AND
  1334. area = :area;
  1335. ");
  1336. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1337. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1338. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1339. $statement->execute();
  1340. $statement = $db->prepare("
  1341. DELETE FROM record
  1342. WHERE
  1343. player = :player AND
  1344. area_type = :area_type AND
  1345. area = :area;
  1346. ");
  1347. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1348. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1349. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1350. $statement->execute();
  1351. // Insert new data
  1352. $statement = $db->prepare("
  1353. INSERT INTO record
  1354. (player, area_type, area, stage, time, score, rank)
  1355. VALUES
  1356. (:player, :area_type, :area, :stage, :time, :score, :rank);
  1357. ");
  1358. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1359. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1360. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1361. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1362. $statement->bindValue(":stage", $record->{"stage_id"}, SQLITE3_INTEGER);
  1363. $statement->bindValue(":time", $record->{"clear_time"}, SQLITE3_INTEGER);
  1364. $statement->bindValue(":score", 0, SQLITE3_INTEGER);
  1365. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  1366. $statement->execute();
  1367. // Loop party
  1368. foreach ($record->{"my_unit_deck_list"} as $party){
  1369. $statement = $db->prepare("
  1370. INSERT INTO record_party
  1371. (player, area_type, area, unit, monster, leader, front)
  1372. VALUES
  1373. (:player, :area_type, :area, :unit, :monster, :leader, :front);
  1374. ");
  1375. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1376. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  1377. $statement->bindValue(":area", $record->{"dungeon_id"}, SQLITE3_INTEGER);
  1378. $statement->bindValue(":unit", $party->{"unit_id"}, SQLITE3_INTEGER);
  1379. $statement->bindValue(":monster", $party->{"unit_master_id"}, SQLITE3_INTEGER);
  1380. $statement->bindValue(":leader", $party->{"leader"}, SQLITE3_INTEGER);
  1381. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1382. $statement->execute();
  1383. }
  1384. }
  1385. }
  1386. elseif (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 3){
  1387. return "204 Page not logged (only pages 1 and 2 are needed).";
  1388. }
  1389. elseif (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 4){
  1390. return "204 Page not logged (only pages 1 and 2 are needed).";
  1391. }
  1392. else{
  1393. return "400 Unknown page.";
  1394. }
  1395. // Return success
  1396. return "201 Created.";
  1397. }
  1398. try{
  1399. header("Content-type: application/json; charset=utf-8");
  1400. // Get put data
  1401. $_PUT = [];
  1402. parse_str(file_get_contents("php://input"), $_PUT);
  1403. // Check API key.
  1404. $key = $_PUT["key"];
  1405. if ($key == null || $key == false){
  1406. header("HTTP/1.1 400 API key not received.");
  1407. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 API key not received.");
  1408. return 400;
  1409. }
  1410. // Check data
  1411. $data = $_PUT["response"];
  1412. if ($data == null || $data == false){
  1413. header("HTTP/1.1 400 No data received.");
  1414. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 No data received.");
  1415. return 400;
  1416. }
  1417. // Check data format.
  1418. $json = json_decode($data);
  1419. if ($json === null){
  1420. header("HTTP/1.1 400 Data is no valid JSON.");
  1421. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Data is no valid JSON.");
  1422. return 400;
  1423. }
  1424. // Get and validate command
  1425. $command = $json->{"command"};
  1426. $accepted_commands = [
  1427. "HubUserLogin", // Login, full profile
  1428. "GetUnitCollection", // Unit collection
  1429. "GetLobbyWizardLog" // Records
  1430. ];
  1431. if (!in_array($command, $accepted_commands)){
  1432. header("HTTP/1.1 405 Command not implemented.");
  1433. syslog(LOG_INFO, "[APIv1] HTTP/1.1 405 Command not implemented.");
  1434. return 405;
  1435. }
  1436. $db = get_context()->get_db();
  1437. // Get user info and Authenticate
  1438. switch ($command){
  1439. case "HubUserLogin":
  1440. $player_id = $json->{"wizard_id"};
  1441. $statement = $db->prepare("
  1442. SELECT COUNT(player.id) AS c
  1443. FROM
  1444. user,
  1445. player
  1446. WHERE
  1447. user.id = player.user AND
  1448. player.id = :player AND
  1449. user.api_key = :api_key;");
  1450. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1451. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1452. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1453. // Player doesnt exists. If there is a user, create player.
  1454. $statement = $db->prepare("
  1455. SELECT id
  1456. FROM user
  1457. WHERE api_key = :api_key;");
  1458. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1459. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  1460. if (! $r){
  1461. header("HTTP/1.1 401 Invalid credentials.");
  1462. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1463. return 401;
  1464. }
  1465. $statement = $db->prepare("
  1466. INSERT INTO player (
  1467. id,
  1468. user,
  1469. server,
  1470. country,
  1471. lang,
  1472. name,
  1473. mana,
  1474. crystal,
  1475. level,
  1476. experience,
  1477. energy,
  1478. energy_max,
  1479. energy_per_min,
  1480. arena_energy,
  1481. rep,
  1482. social_point,
  1483. honor_point,
  1484. guild_point,
  1485. darkportal_energy,
  1486. dimension_energy,
  1487. costume_point,
  1488. honor_medal,
  1489. honor_mark,
  1490. event_coin,
  1491. storage_slots,
  1492. island,
  1493. joined,
  1494. top_rank_arena,
  1495. top_rank_world_arena,
  1496. top_rank_special_league,
  1497. top_rank_gw,
  1498. top_rank_siege,
  1499. top_rank_wboss,
  1500. top_rank_toan,
  1501. top_rank_toah,
  1502. top_rank_toal,
  1503. public,
  1504. mode,
  1505. last_access
  1506. ) VALUES (
  1507. :id,
  1508. :user,
  1509. :server,
  1510. :country,
  1511. :lang,
  1512. '', --name,
  1513. 0, --mana,
  1514. 0, --crystal,
  1515. 1, --level,
  1516. 0, --experience,
  1517. 0, --energy,
  1518. 0, --energy_max,
  1519. 0, --energy_per_min,
  1520. 0, --arena_energy,
  1521. null, --rep,
  1522. 0, --social_point,
  1523. 0, --honor_point,
  1524. 0, --guild_point,
  1525. 0, --darkportal_energy,
  1526. 0, --dimension_energy,
  1527. 0, --costume_point,
  1528. 0, --honor_medal,
  1529. 0, --honor_mark,
  1530. 0, --event_coin,
  1531. 0, --storage_slots,
  1532. 0, --island,
  1533. null, --joined,
  1534. 0, --top_rank_arena,
  1535. 0, --top_rank_world_arena,
  1536. 0, --top_rank_special_league,
  1537. 0, --top_rank_gw,
  1538. 0, --top_rank_siege,
  1539. 0, --top_rank_wboss,
  1540. 0, --top_rank_toan,
  1541. 0, --top_rank_toah,
  1542. 0, --top_rank_toal,
  1543. 0, --public,
  1544. 0, --mode,
  1545. null --last_access,
  1546. )");
  1547. $statement->bindValue(":id", $json->{"wizard_id"}, SQLITE3_INTEGER);
  1548. $statement->bindValue(":user", $r["id"], SQLITE3_INTEGER);
  1549. // TODO: Default server EUROPE, detect and fix
  1550. $statement->bindValue(":server", 2, SQLITE3_INTEGER);
  1551. $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"}, SQLITE3_TEXT);
  1552. $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"}, SQLITE3_TEXT);
  1553. error_log("CREATE NEW PLAYER: " . $statement->getSQL(true));
  1554. $statement->execute();
  1555. }
  1556. break;
  1557. case "GetUnitCollection":
  1558. // TODO: The new user/player distinction wont work here
  1559. // No identification in this JSON, get player id from databases
  1560. header("HTTP/1.1 501 Not implemented.");
  1561. syslog(LOG_INFO, "[APIv1] HTTP/1.1 501 Not implemented.");
  1562. return 501;
  1563. $statement = $db->prepare("SELECT id FROM player WHERE api_key = :api_key;");
  1564. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1565. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  1566. if ($r){
  1567. $uid = $r["id"];
  1568. }
  1569. else{
  1570. header("HTTP/1.1 401 Invalid credentials.");
  1571. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1572. return 401;
  1573. }
  1574. break;
  1575. case "GetLobbyWizardLog":
  1576. $player_id = $json->{"lobby_wizard_log"}->{"wizard_id"};
  1577. $statement = $db->prepare("
  1578. SELECT COUNT(player.id) AS c
  1579. FROM
  1580. user,
  1581. player
  1582. WHERE
  1583. user.id = player.user AND
  1584. player.id = :player AND
  1585. user.api_key = :api_key;");
  1586. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1587. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1588. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1589. header("HTTP/1.1 401 Invalid credentials.");
  1590. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1591. return 401;
  1592. }
  1593. break;
  1594. }
  1595. if ($command == "GetUnitCollection"){ // This command doesnt provide wizard_id
  1596. // TODO: The new user/player distinction wont work here
  1597. $statement = $db->prepare("SELECT COUNT(id) AS c FROM player WHERE id = :id AND api_key = :api_key;");
  1598. $statement->bindValue(":id", $uid, SQLITE3_INTEGER);
  1599. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1600. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1601. header("HTTP/1.1 401 Invalid credentials.");
  1602. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1603. return 401;
  1604. }
  1605. }
  1606. // Run parser
  1607. $result = "500 Unknown Error";
  1608. switch($command){
  1609. case "HubUserLogin":
  1610. $result = parse_profile($db, $player_id, $json);
  1611. break;
  1612. case "GetUnitCollection":
  1613. $result = parse_collection($db, $player_id, $json);
  1614. break;
  1615. case "GetLobbyWizardLog":
  1616. $result = parse_records($db, $player_id, $json);
  1617. break;
  1618. }
  1619. // Process result
  1620. $status_code = intval(substr($result, 0, 3));
  1621. $status_message = substr($result, 4);
  1622. if ($status_code == 0 || $status_code < 100 || $status_code > 599){
  1623. header("HTTP/1.1 500 Unknown error.");
  1624. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unknown error.");
  1625. return 500;
  1626. }
  1627. else{
  1628. header("HTTP/1.1 $status_code $status_message");
  1629. syslog(LOG_INFO, "[APIv1] HTTP/1.1 $status_code $status_message");
  1630. return $status_code;
  1631. }
  1632. }
  1633. catch(Exception $e) {
  1634. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  1635. syslog(LOG_INFO, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  1636. return 500;
  1637. }