PUT.php 67 KB

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