PUT.php 68 KB

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