PUT.php 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  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. $statement->execute();
  1550. }
  1551. break;
  1552. case "GetUnitCollection":
  1553. // TODO: The new user/player distinction wont work here
  1554. // No identification in this JSON, get player id from databases
  1555. header("HTTP/1.1 501 Not implemented.");
  1556. syslog(LOG_INFO, "[APIv1] HTTP/1.1 501 Not implemented.");
  1557. return 501;
  1558. $statement = $db->prepare("SELECT id FROM player WHERE api_key = :api_key;");
  1559. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1560. $r = $statement->execute()->fetchArray(SQLITE3_ASSOC);
  1561. if ($r){
  1562. $uid = $r["id"];
  1563. }
  1564. else{
  1565. header("HTTP/1.1 401 Invalid credentials.");
  1566. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1567. return 401;
  1568. }
  1569. break;
  1570. case "GetLobbyWizardLog":
  1571. $player_id = $json->{"lobby_wizard_log"}->{"wizard_id"};
  1572. $statement = $db->prepare("
  1573. SELECT COUNT(player.id) AS c
  1574. FROM
  1575. user,
  1576. player
  1577. WHERE
  1578. user.id = player.user AND
  1579. player.id = :player AND
  1580. user.api_key = :api_key;");
  1581. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1582. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1583. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1584. header("HTTP/1.1 401 Invalid credentials.");
  1585. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1586. return 401;
  1587. }
  1588. break;
  1589. }
  1590. if ($command == "GetUnitCollection"){ // This command doesnt provide wizard_id
  1591. // TODO: The new user/player distinction wont work here
  1592. $statement = $db->prepare("SELECT COUNT(id) AS c FROM player WHERE id = :id AND api_key = :api_key;");
  1593. $statement->bindValue(":id", $uid, SQLITE3_INTEGER);
  1594. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1595. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1596. header("HTTP/1.1 401 Invalid credentials.");
  1597. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1598. return 401;
  1599. }
  1600. }
  1601. // Run parser
  1602. $result = "500 Unknown Error";
  1603. switch($command){
  1604. case "HubUserLogin":
  1605. $result = parse_profile($db, $player_id, $json);
  1606. break;
  1607. case "GetUnitCollection":
  1608. $result = parse_collection($db, $player_id, $json);
  1609. break;
  1610. case "GetLobbyWizardLog":
  1611. $result = parse_records($db, $player_id, $json);
  1612. break;
  1613. }
  1614. // Process result
  1615. $status_code = intval(substr($result, 0, 3));
  1616. $status_message = substr($result, 4);
  1617. if ($status_code == 0 || $status_code < 100 || $status_code > 599){
  1618. header("HTTP/1.1 500 Unknown error.");
  1619. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unknown error.");
  1620. return 500;
  1621. }
  1622. else{
  1623. header("HTTP/1.1 $status_code $status_message");
  1624. syslog(LOG_INFO, "[APIv1] HTTP/1.1 $status_code $status_message");
  1625. return $status_code;
  1626. }
  1627. }
  1628. catch(Exception $e) {
  1629. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  1630. syslog(LOG_INFO, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  1631. return 500;
  1632. }