PUT.php 68 KB

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