PUT.php 67 KB

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