POST.php 61 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. <?php
  2. /**
  3. * Run logger script.
  4. *
  5. * Exposes an API to log a run.
  6. *
  7. * Mandatory PUT parameters are:
  8. * - result_response: JSON received from game server on run end.
  9. * - key: User API key.
  10. *
  11. * Optional POST parameters are:
  12. * - result_request: JSON sent to game server on run end.
  13. * - start_response: JSON received from game server on run start.
  14. * - start_request: JSON sent to game server on run start.
  15. *
  16. * @author Iñigo Valentin <i@inigovalentin.com>
  17. * @license https://www.gnu.org/licenses/gpl-3.0.en.html GNU General Public License V3
  18. * @package SWDB
  19. * @category API
  20. */
  21. /**
  22. * Calculates the currrent efficiency of a rune.
  23. *
  24. * @param mixed[] $rune JSON fragment of the rune.
  25. * @return int Efficiency (0-100)
  26. */
  27. function calculate_efficiency($rune){
  28. $efficiency = 0.0;
  29. $level = $rune->{"upgrade_curr"};
  30. $stars = $rune->{"class"};
  31. // Ancient stars are + 10
  32. if ($stars > 10){
  33. $stars -= 10;
  34. }
  35. $list = null;
  36. // Main stat
  37. $main_stat = $rune->{"pri_eff"}[0];
  38. switch ($main_stat){
  39. case RUNE_STAT_ID::HP:
  40. $list = RUNE_STAT_VALUES::HP;
  41. break;
  42. case RUNE_STAT_ID::HP_P:
  43. $list = RUNE_STAT_VALUES::HP_P;
  44. break;
  45. case RUNE_STAT_ID::ATK:
  46. $list = RUNE_STAT_VALUES::ATK;
  47. break;
  48. case RUNE_STAT_ID::ATK_P:
  49. $list = RUNE_STAT_VALUES::ATK_P;
  50. break;
  51. case RUNE_STAT_ID::DEF:
  52. $list = RUNE_STAT_VALUES::DEF;
  53. break;
  54. case RUNE_STAT_ID::DEF_P:
  55. $list = RUNE_STAT_VALUES::DEF_P;
  56. break;
  57. case RUNE_STAT_ID::SPD:
  58. $list = RUNE_STAT_VALUES::SPD;
  59. break;
  60. case RUNE_STAT_ID::CRR:
  61. $list = RUNE_STAT_VALUES::CRR;
  62. break;
  63. case RUNE_STAT_ID::CRD:
  64. $list = RUNE_STAT_VALUES::CRD;
  65. break;
  66. case RUNE_STAT_ID::ACC:
  67. $list = RUNE_STAT_VALUES::ACC;
  68. break;
  69. case RUNE_STAT_ID::RES:
  70. $list = RUNE_STAT_VALUES::RES;
  71. break;
  72. }
  73. $efficiency += floatval($list[$stars][$level]) / floatval($list[6][15]);
  74. // Innate stat
  75. if ($rune->{"prefix_eff"}[0] > 0){
  76. $innate_stat = $rune->{"pri_eff"}[0];
  77. $innate_stat_value = $rune->{"pri_eff"}[1];
  78. $efficiency += floatval($innate_stat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$innate_stat][6]));
  79. }
  80. // Substats
  81. for ($i = 0; $i <= 3; $i ++){
  82. if (sizeof($rune->{"sec_eff"}) > $i){
  83. $substat = $rune->{"sec_eff"}[$i][0];
  84. $substat_value = $rune->{"sec_eff"}[$i][1];
  85. $efficiency += floatval($substat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][6]));
  86. }
  87. }
  88. $efficiency = $efficiency / 2.8 * 100;
  89. if ($efficiency > 100){
  90. $efficiency = 100;
  91. }
  92. return $efficiency;
  93. }
  94. /**
  95. * Calculates the maximum efficiency of a rune.
  96. *
  97. * @param mixed[] $rune JSON fragment of the rune.
  98. * @return int Maximum efficiency (0-100)
  99. */
  100. function calculate_maximum_efficiency($rune){
  101. $efficiency = 0.0;
  102. $stars = $rune->{"class"};
  103. // Ancient stars are + 10
  104. if ($stars > 10){
  105. $stars -= 10;
  106. }
  107. $list = null;
  108. // Main stat
  109. $main_stat = $rune->{"pri_eff"}[0];
  110. switch ($main_stat){
  111. case RUNE_STAT_ID::HP:
  112. $list = RUNE_STAT_VALUES::HP;
  113. break;
  114. case RUNE_STAT_ID::HP_P:
  115. $list = RUNE_STAT_VALUES::HP_P;
  116. break;
  117. case RUNE_STAT_ID::ATK:
  118. $list = RUNE_STAT_VALUES::ATK;
  119. break;
  120. case RUNE_STAT_ID::ATK_P:
  121. $list = RUNE_STAT_VALUES::ATK_P;
  122. break;
  123. case RUNE_STAT_ID::DEF:
  124. $list = RUNE_STAT_VALUES::DEF;
  125. break;
  126. case RUNE_STAT_ID::DEF_P:
  127. $list = RUNE_STAT_VALUES::DEF_P;
  128. break;
  129. case RUNE_STAT_ID::SPD:
  130. $list = RUNE_STAT_VALUES::SPD;
  131. break;
  132. case RUNE_STAT_ID::CRR:
  133. $list = RUNE_STAT_VALUES::CRR;
  134. break;
  135. case RUNE_STAT_ID::CRD:
  136. $list = RUNE_STAT_VALUES::CRD;
  137. break;
  138. case RUNE_STAT_ID::ACC:
  139. $list = RUNE_STAT_VALUES::ACC;
  140. break;
  141. case RUNE_STAT_ID::RES:
  142. $list = RUNE_STAT_VALUES::RES;
  143. break;
  144. }
  145. $efficiency += floatval($list[$stars][15]) / floatval($list[6][15]);
  146. // Innate stat
  147. if ($rune->{"prefix_eff"}[0] > 0){
  148. $innate_stat = $rune->{"pri_eff"}[0];
  149. $innate_stat_value = $rune->{"pri_eff"}[1];
  150. $efficiency += floatval($innate_stat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$innate_stat][6]));
  151. }
  152. // Substats
  153. for ($i = 0; $i <= 3; $i ++){
  154. if (sizeof($rune->{"sec_eff"}) > $i){
  155. $substat = $rune->{"sec_eff"}[$i][0];
  156. $efficiency += floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][$stars]) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][6]));
  157. }
  158. }
  159. $efficiency = $efficiency / 2.8 * 100;
  160. if ($efficiency > 100){
  161. $efficiency = 100;
  162. }
  163. return $efficiency;
  164. }
  165. /**
  166. * Parses the JSONs relateds to an Arena run and saves the data.
  167. *
  168. * @param resource $db Database connection.
  169. * @param int $player_id Player ID.
  170. * @param int $id Run ID.
  171. * @param mixed[] $json List of received JSONs.
  172. */
  173. function log_arena_run($db, $id, $player_id, $json){
  174. // Only the result request and response JSONs are needed:
  175. $response = $json["result_res"];
  176. // Prepare the "run" insert.
  177. $statement = $db->prepare("
  178. INSERT INTO run (
  179. player,
  180. id,
  181. dtime,
  182. area_type,
  183. area,
  184. stage,
  185. difficulty,
  186. win,
  187. time,
  188. mana,
  189. energy,
  190. crystal,
  191. guild_points,
  192. honor_points,
  193. helper,
  194. score,
  195. rank
  196. ) VALUES (
  197. :player,
  198. :id,
  199. :dtime,
  200. :area_type,
  201. :area,
  202. :stage,
  203. :difficulty,
  204. :win,
  205. :time,
  206. :mana,
  207. :energy,
  208. :crystal,
  209. :guild_points,
  210. :honor_points,
  211. :helper,
  212. :score,
  213. :rank
  214. );
  215. ");
  216. // Bind data and insert
  217. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  218. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  219. $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_TEXT);
  220. $statement->bindValue(":area_type", AREA_TYPE_ID::ARENA, SQLITE3_INTEGER);
  221. $statement->bindValue(":area", AREA_TYPE_ID::ARENA, SQLITE3_INTEGER);
  222. $statement->bindValue(":stage", null, SQLITE3_INTEGER);
  223. $statement->bindValue(":difficulty", null, SQLITE3_INTEGER);
  224. $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
  225. $statement->bindValue(":time", 1, SQLITE3_INTEGER);
  226. if (array_key_exists("mana", $response->{"reward"})){
  227. $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
  228. }
  229. else{
  230. $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
  231. }
  232. if (array_key_exists("energy", $response->{"reward"})){
  233. $statement->bindValue(":energy", $response->{"reward"}->{"energy"}, SQLITE3_INTEGER);
  234. }
  235. else{
  236. $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
  237. }
  238. if (array_key_exists("crystal", $response->{"reward"})){
  239. $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
  240. }
  241. else{
  242. $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
  243. }
  244. $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
  245. if (array_key_exists("honor_point", $response->{"reward"})){
  246. $statement->bindValue(":honor_points", $response->{"reward"}->{"honor_point"}, SQLITE3_INTEGER);
  247. }
  248. else{
  249. $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
  250. }
  251. $statement->bindValue(":helper", 0, SQLITE3_INTEGER);
  252. $statement->bindValue(":score", null, SQLITE3_INTEGER);
  253. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  254. $statement->execute();
  255. // Parse party
  256. $leader = 1;
  257. foreach ($response->{"unit_list"} as $unit){
  258. $statement = $db->prepare("
  259. INSERT INTO run_party
  260. (run, unit, monster, leader, front)
  261. VALUES
  262. (:run, :unit, :monster, :leader, :front);"
  263. );
  264. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  265. $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
  266. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  267. $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
  268. $leader = 0;
  269. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  270. $statement->execute();
  271. }
  272. // Return success
  273. return "201 Created.";
  274. }
  275. /**
  276. * Parses the JSONs relateds to a Dimensional Rift run and saves the data.
  277. *
  278. * @param resource $db Database connection.
  279. * @param int $player_id Player ID.
  280. * @param int $id Run ID.
  281. * @param mixed[] $json List of received JSONs.
  282. */
  283. function log_dark_portal_run($db, $id, $player_id, $json){
  284. // Only the result request and response JSONs are needed:
  285. $response = $json["result_res"];
  286. // Prepare the "run" insert.
  287. $statement = $db->prepare("
  288. INSERT INTO run (
  289. player,
  290. id,
  291. dtime,
  292. area_type,
  293. area,
  294. stage,
  295. difficulty,
  296. win,
  297. time,
  298. mana,
  299. energy,
  300. crystal,
  301. guild_points,
  302. honor_points,
  303. helper,
  304. score,
  305. rank
  306. ) VALUES (
  307. :player,
  308. :id,
  309. :dtime,
  310. :area_type,
  311. :area,
  312. :stage,
  313. :difficulty,
  314. :win,
  315. :time,
  316. :mana,
  317. :energy,
  318. :crystal,
  319. :guild_points,
  320. :honor_points,
  321. :helper,
  322. :score,
  323. :rank
  324. );
  325. ");
  326. // Bind data and insert
  327. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  328. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  329. $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_INTEGER);
  330. $statement->bindValue(":area_type", AREA_TYPE_ID::DIMENSIONAL_RIFT, SQLITE3_INTEGER);
  331. // The area is the region the portal is open in.
  332. $statement->bindValue(":area", $response->{"region_id"}, SQLITE3_INTEGER);
  333. $statement->bindValue(":stage", null, SQLITE3_INTEGER);
  334. $statement->bindValue(":difficulty", $response->{"difficulty"}, SQLITE3_INTEGER);
  335. $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
  336. $statement->bindValue(":time", 1, SQLITE3_INTEGER);
  337. if (array_key_exists("mana", $response->{"reward"})){
  338. $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
  339. }
  340. else{
  341. $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
  342. }
  343. if (array_key_exists("energy", $response->{"reward"})){
  344. $statement->bindValue(":energy", $response->{"reward"}->{"energy"});
  345. }
  346. else{
  347. $statement->bindValue(":energy", 0);
  348. }
  349. if (array_key_exists("crystal", $response->{"reward"})){
  350. $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
  351. }
  352. else{
  353. $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
  354. }
  355. $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
  356. $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
  357. $statement->bindValue(":helper", 0, SQLITE3_INTEGER);
  358. $statement->bindValue(":score", null, SQLITE3_INTEGER);
  359. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  360. $statement->execute();
  361. // Parse party
  362. $leader = 1;
  363. foreach ($response->{"unit_list"} as $unit){
  364. $statement = $db->prepare("
  365. INSERT INTO run_party
  366. (run, unit, monster, leader, front)
  367. VALUES
  368. (:run, :unit, :monster, :leader, :front);"
  369. );
  370. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  371. $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
  372. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  373. $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
  374. $leader = 0;
  375. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  376. $statement->execute();
  377. }
  378. // Return success
  379. return "201 Created.";
  380. }
  381. /**
  382. * Parses the JSONs relateds to a scenario run and saves the data.
  383. *
  384. * @param resource $db Database connection.
  385. * @param int $player_id Player ID.
  386. * @param int $id Run ID.
  387. * @param mixed[] $json List of received JSONs.
  388. */
  389. function log_scenario_run($db, $id, $player_id, $json){
  390. // Only the result request and response JSONs are needed:
  391. $response = $json["result_res"];
  392. if ($json["start_res"] === null){
  393. return ("400 Start response is required to parse scenario runs.");
  394. }
  395. $request = $json["start_res"];
  396. // Prepare the "run" insert.
  397. $statement = $db->prepare("
  398. INSERT INTO run (
  399. player,
  400. id,
  401. dtime,
  402. area_type,
  403. area,
  404. stage,
  405. difficulty,
  406. win,
  407. time,
  408. mana,
  409. energy,
  410. crystal,
  411. guild_points,
  412. honor_points,
  413. helper,
  414. score,
  415. rank
  416. ) VALUES (
  417. :player,
  418. :id,
  419. :dtime,
  420. :area_type,
  421. :area,
  422. :stage,
  423. :difficulty,
  424. :win,
  425. :time,
  426. :mana,
  427. :energy,
  428. :crystal,
  429. :guild_points,
  430. :honor_points,
  431. :helper,
  432. :score,
  433. :rank
  434. );
  435. ");
  436. // Bind data and insert
  437. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  438. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  439. $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_TEXT);
  440. $statement->bindValue(":area_type", AREA_TYPE_ID::SCENARIO, SQLITE3_INTEGER);
  441. $statement->bindValue(":area", $response->{"scenario_info"}->{"region_id"}, SQLITE3_INTEGER);
  442. $statement->bindValue(":stage", $response->{"scenario_info"}->{"stage_no"}, SQLITE3_INTEGER);
  443. $statement->bindValue(":difficulty", $response->{"scenario_info"}->{"difficulty"}, SQLITE3_INTEGER);
  444. $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
  445. $statement->bindValue(":time", $response->{"clear_time"}->{"current_time"}, SQLITE3_INTEGER);
  446. if (array_key_exists("mana", $response->{"reward"})){
  447. $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
  448. }
  449. else{
  450. $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
  451. }
  452. if (array_key_exists("energy", $response->{"reward"})){
  453. $statement->bindValue(":energy", $response->{"reward"}->{"energy"}, SQLITE3_INTEGER);
  454. }
  455. else{
  456. $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
  457. }
  458. if (array_key_exists("crystal", $response->{"reward"})){
  459. $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
  460. }
  461. else{
  462. $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
  463. }
  464. $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
  465. $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
  466. if (array_key_exists("helper_unit_list", $request) && sizeof($request->{"helper_unit_list"}) > 0){
  467. $statement->bindValue(":helper", 1, SQLITE3_INTEGER);
  468. }
  469. else{
  470. $statement->bindValue(":helper", 0, SQLITE3_INTEGER);
  471. }
  472. $statement->bindValue(":score", null, SQLITE3_INTEGER);
  473. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  474. $statement->execute();
  475. // Parse various types of reward
  476. if (array_key_exists("crate", $response->{"reward"})){
  477. $crate = $response->{"reward"}->{"crate"};
  478. if (array_key_exists("unit_info", $crate)){
  479. $unit = $crate->{"unit_info"};
  480. $statement = $db->prepare("INSERT INTO run_drop_unit (run, unit) VALUES (:run, :unit);");
  481. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  482. $statement->bindValue(":unit", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  483. $statement->execute();
  484. }
  485. if (array_key_exists("craft_stuff", $crate)){
  486. $item = $crate->{"craft_stuff"};
  487. $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
  488. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  489. $statement->bindValue(":item", $item->{"item_master_id"}, SQLITE3_INTEGER);
  490. $statement->bindValue(":amount", $item->{"item_quantity"}, SQLITE3_INTEGER);
  491. $statement->execute();
  492. }
  493. if (array_key_exists("random_scroll", $crate)){
  494. $item = $crate->{"random_scroll"};
  495. $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
  496. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  497. $statement->bindValue(":item", $item->{"item_master_id"}, SQLITE3_INTEGER);
  498. $statement->bindValue(":amount", $item->{"item_quantity"}, SQLITE3_INTEGER);
  499. $statement->execute();
  500. }
  501. if (array_key_exists("rune", $crate)){
  502. $rune = $crate->{"rune"};
  503. $statement = $db->prepare("
  504. INSERT INTO run_drop_rune (
  505. run,
  506. id,
  507. type,
  508. slot,
  509. stars,
  510. ancient,
  511. quality,
  512. value,
  513. efficiency,
  514. max_efficiency,
  515. main_stat,
  516. main_stat_value,
  517. innate_stat,
  518. innate_stat_value,
  519. substat_1,
  520. substat_1_value,
  521. substat_2,
  522. substat_2_value,
  523. substat_3,
  524. substat_3_value,
  525. substat_4,
  526. substat_4_value
  527. ) VALUES (
  528. :run,
  529. :id,
  530. :type,
  531. :slot,
  532. :stars,
  533. :ancient,
  534. :quality,
  535. :value,
  536. :efficiency,
  537. :max_efficiency,
  538. :main_stat,
  539. :main_stat_value,
  540. :innate_stat,
  541. :innate_stat_value,
  542. :substat_1,
  543. :substat_1_value,
  544. :substat_2,
  545. :substat_2_value,
  546. :substat_3,
  547. :substat_3_value,
  548. :substat_4,
  549. :substat_4_value
  550. );
  551. ");
  552. $statement->bindValue(":run", $id);
  553. $statement->bindValue(":id", $rune->{"rune_id"});
  554. $statement->bindValue(":type", $rune->{"set_id"});
  555. $statement->bindValue(":slot", $rune->{"slot_no"});
  556. $statement->bindValue(":value", $rune->{"sell_value"});
  557. if ($rune->{"class"} > 10){
  558. // Ancient rune
  559. $statement->bindValue(":ancient", 1, SQLITE3_INTEGER);
  560. $statement->bindValue(":stars", intval($rune->{"class"}) - 10, SQLITE3_INTEGER);
  561. $statement->bindValue(":quality", intval($rune->{"rank"}) - 10, SQLITE3_INTEGER);
  562. }
  563. else{
  564. // Non Ancient rune
  565. $statement->bindValue(":ancient", 0, SQLITE3_INTEGER);
  566. $statement->bindValue(":stars", $rune->{"class"}, SQLITE3_INTEGER);
  567. $statement->bindValue(":quality", $rune->{"rank"}, SQLITE3_INTEGER);
  568. }
  569. $efficiency = calculate_efficiency($rune);
  570. $max_efficiency = calculate_maximum_efficiency($rune);
  571. $statement->bindValue(":efficiency", $efficiency, SQLITE3_FLOAT);
  572. $statement->bindValue(":max_efficiency", $max_efficiency, SQLITE3_FLOAT);
  573. $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0], SQLITE3_INTEGER);
  574. $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1], SQLITE3_INTEGER);
  575. if ($rune->{"prefix_eff"}[0] > 0){
  576. $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0], SQLITE3_INTEGER);
  577. $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1], SQLITE3_INTEGER);
  578. }
  579. else{
  580. $statement->bindValue(":innate_stat", null, SQLITE3_INTEGER);
  581. $statement->bindValue(":innate_stat_value", 0, SQLITE3_INTEGER);
  582. }
  583. if (sizeof($rune->{"sec_eff"}) > 0){
  584. $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0], SQLITE3_INTEGER);
  585. $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1], SQLITE3_INTEGER);
  586. }
  587. else{
  588. $statement->bindValue(":substat_1", null, SQLITE3_INTEGER);
  589. $statement->bindValue(":substat_1_value", 0, SQLITE3_INTEGER);
  590. }
  591. if (sizeof($rune->{"sec_eff"}) > 1){
  592. $statement->bindValue(":substat_2", $rune->{"sec_eff"}[1][0], SQLITE3_INTEGER);
  593. $statement->bindValue(":substat_2_value", $rune->{"sec_eff"}[1][1], SQLITE3_INTEGER);
  594. }
  595. else{
  596. $statement->bindValue(":substat_2", null, SQLITE3_INTEGER);
  597. $statement->bindValue(":substat_2_value", 0, SQLITE3_INTEGER);
  598. }
  599. if (sizeof($rune->{"sec_eff"}) > 2){
  600. $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0], SQLITE3_INTEGER);
  601. $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1], SQLITE3_INTEGER);
  602. }
  603. else{
  604. $statement->bindValue(":substat_3", null, SQLITE3_INTEGER);
  605. $statement->bindValue(":substat_3_value", 0, SQLITE3_INTEGER);
  606. }
  607. if (sizeof($rune->{"sec_eff"}) > 3){
  608. $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0], SQLITE3_INTEGER);
  609. $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1], SQLITE3_INTEGER);
  610. }
  611. else{
  612. $statement->bindValue(":substat_4", null, SQLITE3_INTEGER);
  613. $statement->bindValue(":substat_4_value", 0, SQLITE3_INTEGER);
  614. }
  615. $statement->execute();
  616. }
  617. }
  618. // Parse party
  619. $leader = 1;
  620. foreach ($response->{"unit_list"} as $unit){
  621. $statement = $db->prepare("
  622. INSERT INTO run_party
  623. (run, unit, monster, leader, front)
  624. VALUES
  625. (:run, :unit, :monster, :leader, :front);"
  626. );
  627. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  628. $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
  629. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  630. $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
  631. $leader = 0;
  632. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  633. $statement->execute();
  634. }
  635. // Return success
  636. return "201 Created.";
  637. }
  638. /**
  639. * Parses the JSONs relateds to a cairos run and saves the data.
  640. *
  641. * @param resource $db Database connection.
  642. * @param int $player_id Player ID.
  643. * @param int $id Run ID.
  644. * @param mixed[] $json List of received JSONs.
  645. */
  646. function log_rift_raid_run($db, $id, $player_id, $json){
  647. // Only the start and result requests JSONs are needed:
  648. $result = $json["result_res"];
  649. if ($json["start_res"] === null){
  650. return ("400 Start response is required to parse Rift Raid runs.");
  651. }
  652. $start = $json["start_res"];
  653. // Prepare the "run" insert.
  654. $statement = $db->prepare("
  655. INSERT INTO run (
  656. player,
  657. id,
  658. dtime,
  659. area_type,
  660. area,
  661. stage,
  662. difficulty,
  663. win,
  664. time,
  665. mana,
  666. energy,
  667. crystal,
  668. guild_points,
  669. honor_points,
  670. helper,
  671. score,
  672. rank
  673. ) VALUES (
  674. :player,
  675. :id,
  676. :dtime,
  677. :area_type,
  678. :area,
  679. :stage,
  680. :difficulty,
  681. :win,
  682. :time,
  683. :mana,
  684. :energy,
  685. :crystal,
  686. :guild_points,
  687. :honor_points,
  688. :helper,
  689. :score,
  690. :rank
  691. );
  692. ");
  693. // Bind data and insert
  694. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  695. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  696. $statement->bindValue(":dtime", $result->{"tvaluelocal"}, SQLITE3_TEXT);
  697. $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  698. $statement->bindValue(":area", AREA_TYPE_ID::RIFT_RAID, SQLITE3_INTEGER);
  699. $statement->bindValue(":stage", $start->{"battle_info"}->{"stage_id"}, SQLITE3_INTEGER);
  700. $statement->bindValue(":difficulty", null, SQLITE3_INTEGER);
  701. $statement->bindValue(":win", $result->{"win_lose"}, SQLITE3_INTEGER);
  702. if ($result->{"win_lose"} == 1){
  703. $statement->bindValue(":time", $result->{"clear_time"}->{"current_time"}, SQLITE3_TEXT);
  704. }
  705. else{
  706. $statement->bindValue(":time", 1, SQLITE3_INTEGER);
  707. }
  708. $statement->bindValue(":mana", 0, SQLITE3_INTEGER); // If reward is mana, i will be updated later
  709. $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
  710. $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
  711. $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
  712. $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
  713. $statement->bindValue(":helper", 0, SQLITE3_INTEGER);
  714. $statement->bindValue(":score", null, SQLITE3_INTEGER);
  715. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  716. $statement->execute();
  717. // Parse various types of reward
  718. if ($result->{"win_lose"} == 1 && array_key_exists("battle_reward_list", $result)){
  719. foreach ($result->{"battle_reward_list"} as $reward_list){
  720. if ($reward_list->{"wizard_id"} == $player_id){
  721. $reward = $reward_list->{"reward_list"}[0];
  722. $reward_type = $reward->{"item_master_type"};
  723. switch ($reward_type){
  724. case INVENTORY_TYPE_ID::MONSTER: // Unit
  725. $statement = $db->prepare("INSERT INTO run_drop_unit (run, unit) VALUES (:run, :unit);");
  726. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  727. $statement->bindValue(":unit", $reward->{"item_master_id"}, SQLITE3_INTEGER);
  728. $statement->execute();
  729. break;
  730. case INVENTORY_TYPE_ID::SCROLL:
  731. case INVENTORY_TYPE_ID::ESSENCES:
  732. $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
  733. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  734. $statement->bindValue(":item", $reward->{"item_master_id"}, SQLITE3_INTEGER);
  735. $statement->bindValue(":amount", $reward->{"item_quantity"}, SQLITE3_INTEGER);
  736. $statement->execute();
  737. break;
  738. case INVENTORY_TYPE_ID::ENCHANTMENT:
  739. $statement = $db->prepare("
  740. INSERT INTO run_drop_enchantment (run, id, type, quality, rune, stat, value)
  741. VALUES (:run, :id, :type, :quality, :rune, :stat, :value);
  742. ");
  743. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  744. $statement->bindValue(":id", $reward->{"runecraft_item_id"}, SQLITE3_INTEGER);
  745. $statement->bindValue(":type", $reward->{"runecraft_type"}, SQLITE3_INTEGER);
  746. $statement->bindValue(":quality", $reward->{"runecraft_rank"}, SQLITE3_INTEGER);
  747. $statement->bindValue(":rune", $reward->{"runecraft_set_id"}, SQLITE3_INTEGER);
  748. $statement->bindValue(":stat", $reward->{"runecraft_effect_id"}, SQLITE3_INTEGER);
  749. $statement->bindValue(":value", 0, SQLITE3_INTEGER);
  750. $statement->execute();
  751. break;
  752. case 102: //: Mana
  753. $statement = $db->prepare("UPDATE run SET mana = :mana WHERE run = :run;");
  754. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  755. $statement->bindValue(":id", $reward->{"runecraft_item_id"}, SQLITE3_INTEGER);
  756. $statement->execute();
  757. break;
  758. }
  759. }
  760. }
  761. }
  762. // Parse party
  763. foreach ($start->{"battle_info"}->{"user_list"} as $user){
  764. if ($user->{"wizard_id"} == $player_id){
  765. foreach ($user->{"deck_list"} as $unit){
  766. $statement = $db->prepare("
  767. INSERT INTO run_party
  768. (run, unit, monster, leader, front)
  769. VALUES
  770. (:run, :unit, :monster, :leader, :front);"
  771. );
  772. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  773. $statement->bindValue(":unit", $unit->{"unit_info"}->{"unit_id"}, SQLITE3_INTEGER);
  774. $statement->bindValue(":monster", $unit->{"unit_info"}->{"unit_master_id"}, SQLITE3_INTEGER);
  775. $statement->bindValue(":leader", $unit->{"leader"}, SQLITE3_INTEGER);
  776. if (intval($unit->{"index"}) <= 4){
  777. $statement->bindValue(":front", 1, SQLITE3_INTEGER);
  778. }
  779. else{
  780. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  781. }
  782. $statement->execute();
  783. }
  784. }
  785. }
  786. // Return success
  787. return "201 Created.";
  788. }
  789. /**
  790. * Parses the JSONs relateds to a cairos run and saves the data.
  791. *
  792. * @param resource $db Database connection.
  793. * @param int $player_id Player ID.
  794. * @param int $id Run ID.
  795. * @param mixed[] $json List of received JSONs.
  796. */
  797. function log_cairos_run($db, $id, $player_id, $json){
  798. // Only the result request and response JSONs are needed:
  799. $response = $json["result_res"];
  800. if ($json["result_req"] === null){
  801. return ("400 Result request is required to parse Cairos runs.");
  802. }
  803. $request = $json["result_req"];
  804. // Prepare the "run" insert.
  805. $statement = $db->prepare("
  806. INSERT INTO run (
  807. player,
  808. id,
  809. dtime,
  810. area_type,
  811. area,
  812. stage,
  813. difficulty,
  814. win,
  815. time,
  816. mana,
  817. energy,
  818. crystal,
  819. guild_points,
  820. honor_points,
  821. helper,
  822. score,
  823. rank
  824. ) VALUES (
  825. :player,
  826. :id,
  827. :dtime,
  828. :area_type,
  829. :area,
  830. :stage,
  831. :difficulty,
  832. :win,
  833. :time,
  834. :mana,
  835. :energy,
  836. :crystal,
  837. :guild_points,
  838. :honor_points,
  839. :helper,
  840. :score,
  841. :rank
  842. );
  843. ");
  844. // Bind data and insert
  845. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  846. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  847. $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_TEXT);
  848. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON, SQLITE3_INTEGER);
  849. $statement->bindValue(":area", $request->{"dungeon_id"}, SQLITE3_INTEGER);
  850. $statement->bindValue(":stage", $request->{"stage_id"}, SQLITE3_INTEGER);
  851. $statement->bindValue(":difficulty", null, SQLITE3_INTEGER);
  852. $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
  853. $statement->bindValue(":time", $response->{"clear_time"}->{"current_time"}, SQLITE3_INTEGER);
  854. if (array_key_exists("mana", $response->{"reward"})){
  855. $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
  856. }
  857. else{
  858. $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
  859. }
  860. if (array_key_exists("energy", $response->{"reward"})){
  861. $statement->bindValue(":energy", $response->{"reward"}->{"energy"}, SQLITE3_INTEGER);
  862. }
  863. else{
  864. $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
  865. }
  866. if (array_key_exists("crystal", $response->{"reward"})){
  867. $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
  868. }
  869. else{
  870. $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
  871. }
  872. $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
  873. $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
  874. $statement->bindValue(":helper", 0, SQLITE3_INTEGER); // TODO Where is this information??
  875. $statement->bindValue(":score", null, SQLITE3_INTEGER);
  876. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  877. $statement->execute();
  878. // Parse various types of reward
  879. if (array_key_exists("changed_item_list", $response)){
  880. $reward_type = $response->{"changed_item_list"}[0]->{"type"};
  881. switch ($reward_type){
  882. case INVENTORY_TYPE_ID::MONSTER: // Unit
  883. $statement = $db->prepare("INSERT INTO run_drop_unit (run, unit) VALUES (:run, :unit);");
  884. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  885. $statement->bindValue(":unit", $response->{"changed_item_list"}[0]->{"view"}->{"item_master_id"}, SQLITE3_INTEGER);
  886. $statement->execute();
  887. break;
  888. case INVENTORY_TYPE_ID::SCROLL:
  889. case INVENTORY_TYPE_ID::ESSENCES:
  890. $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
  891. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  892. $statement->bindValue(":item", $response->{"changed_item_list"}[0]->{"view"}->{"item_master_id"}, SQLITE3_INTEGER);
  893. $statement->bindValue(":amount", $response->{"changed_item_list"}[0]->{"view"}->{"item_quantity"}, SQLITE3_INTEGER);
  894. $statement->execute();
  895. break;
  896. case INVENTORY_TYPE_ID::RUNE:
  897. $rune = $response->{"changed_item_list"}[0]->{"info"};
  898. $statement = $db->prepare("
  899. INSERT INTO run_drop_rune (
  900. run,
  901. id,
  902. type,
  903. slot,
  904. stars,
  905. ancient,
  906. quality,
  907. value,
  908. efficiency,
  909. max_efficiency,
  910. main_stat,
  911. main_stat_value,
  912. innate_stat,
  913. innate_stat_value,
  914. substat_1,
  915. substat_1_value,
  916. substat_2,
  917. substat_2_value,
  918. substat_3,
  919. substat_3_value,
  920. substat_4,
  921. substat_4_value
  922. ) VALUES (
  923. :run,
  924. :id,
  925. :type,
  926. :slot,
  927. :stars,
  928. :ancient,
  929. :quality,
  930. :value,
  931. :efficiency,
  932. :max_efficiency,
  933. :main_stat,
  934. :main_stat_value,
  935. :innate_stat,
  936. :innate_stat_value,
  937. :substat_1,
  938. :substat_1_value,
  939. :substat_2,
  940. :substat_2_value,
  941. :substat_3,
  942. :substat_3_value,
  943. :substat_4,
  944. :substat_4_value
  945. );
  946. ");
  947. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  948. $statement->bindValue(":id", $rune->{"rune_id"}, SQLITE3_INTEGER);
  949. $statement->bindValue(":type", $rune->{"set_id"}, SQLITE3_INTEGER);
  950. $statement->bindValue(":slot", $rune->{"slot_no"}, SQLITE3_INTEGER);
  951. $statement->bindValue(":value", $rune->{"sell_value"}, SQLITE3_INTEGER);
  952. if ($rune->{"class"} > 10){
  953. // Ancient rune
  954. $statement->bindValue(":ancient", 1, SQLITE3_INTEGER);
  955. $statement->bindValue(":stars", intval($rune->{"class"}) - 10, SQLITE3_INTEGER);
  956. $statement->bindValue(":quality", intval($rune->{"rank"}) - 10, SQLITE3_INTEGER);
  957. }
  958. else{
  959. // Non Ancient rune
  960. $statement->bindValue(":ancient", 0, SQLITE3_INTEGER);
  961. $statement->bindValue(":stars", $rune->{"class"}, SQLITE3_INTEGER);
  962. $statement->bindValue(":quality", $rune->{"rank"}, SQLITE3_INTEGER);
  963. }
  964. $efficiency = calculate_efficiency($rune);
  965. $max_efficiency = calculate_maximum_efficiency($rune);
  966. $statement->bindValue(":efficiency", $efficiency, SQLITE3_FLOAT);
  967. $statement->bindValue(":max_efficiency", $max_efficiency, SQLITE3_FLOAT);
  968. $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0], SQLITE3_INTEGER);
  969. $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1], SQLITE3_INTEGER);
  970. if ($rune->{"prefix_eff"}[0] > 0){
  971. $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0], SQLITE3_INTEGER);
  972. $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1], SQLITE3_INTEGER);
  973. }
  974. else{
  975. $statement->bindValue(":innate_stat", null, SQLITE3_INTEGER);
  976. $statement->bindValue(":innate_stat_value", 0, SQLITE3_INTEGER);
  977. }
  978. if (sizeof($rune->{"sec_eff"}) > 0){
  979. $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0], SQLITE3_INTEGER);
  980. $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1], SQLITE3_INTEGER);
  981. }
  982. else{
  983. $statement->bindValue(":substat_1", null, SQLITE3_INTEGER);
  984. $statement->bindValue(":substat_1_value", 0, SQLITE3_INTEGER);
  985. }
  986. if (sizeof($rune->{"sec_eff"}) > 1){
  987. $statement->bindValue(":substat_2", $rune->{"sec_eff"}[1][0]);
  988. $statement->bindValue(":substat_2_value", $rune->{"sec_eff"}[1][1]);
  989. }
  990. else{
  991. $statement->bindValue(":substat_2", null, SQLITE3_INTEGER);
  992. $statement->bindValue(":substat_2_value", 0, SQLITE3_INTEGER);
  993. }
  994. if (sizeof($rune->{"sec_eff"}) > 2){
  995. $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0], SQLITE3_INTEGER);
  996. $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1], SQLITE3_INTEGER);
  997. }
  998. else{
  999. $statement->bindValue(":substat_3", null, SQLITE3_INTEGER);
  1000. $statement->bindValue(":substat_3_value", 0, SQLITE3_INTEGER);
  1001. }
  1002. if (sizeof($rune->{"sec_eff"}) > 3){
  1003. $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0], SQLITE3_INTEGER);
  1004. $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1], SQLITE3_INTEGER);
  1005. }
  1006. else{
  1007. $statement->bindValue(":substat_4", null, SQLITE3_INTEGER);
  1008. $statement->bindValue(":substat_4_value", 0, SQLITE3_INTEGER);
  1009. }
  1010. $statement->execute();
  1011. break;
  1012. case INVENTORY_TYPE_ID::ARTIFACT:
  1013. $artifact = $response->{"changed_item_list"}[0]->{"info"};
  1014. $statement = $db->prepare("
  1015. INSERT INTO run_drop_artifact (
  1016. run,
  1017. id,
  1018. type,
  1019. attribute,
  1020. archetype,
  1021. quality,
  1022. value,
  1023. efficiency,
  1024. max_efficiency,
  1025. stat,
  1026. stat_value,
  1027. effect_1,
  1028. effect_1_value,
  1029. effect_2,
  1030. effect_2_value,
  1031. effect_3,
  1032. effect_3_value,
  1033. effect_4,
  1034. effect_4_value
  1035. ) VALUES (
  1036. :run,
  1037. :id,
  1038. :type,
  1039. :attribute,
  1040. :archetype,
  1041. :quality,
  1042. :value,
  1043. :efficiency,
  1044. :max_efficiency,
  1045. :stat,
  1046. :stat_value,
  1047. :effect_1,
  1048. :effect_1_value,
  1049. :effect_2,
  1050. :effect_2_value,
  1051. :effect_3,
  1052. :effect_3_value,
  1053. :effect_4,
  1054. :effect_4_value
  1055. );
  1056. ");
  1057. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  1058. $statement->bindValue(":id", $artifact->{"rid"}, SQLITE3_INTEGER);
  1059. $statement->bindValue(":type", $artifact->{"type"}, SQLITE3_INTEGER);
  1060. $statement->bindValue(":attribute", $artifact->{"attribute"}, SQLITE3_INTEGER);
  1061. $statement->bindValue(":archetype", $artifact->{"unit_style"}, SQLITE3_INTEGER);
  1062. $statement->bindValue(":quality", $artifact->{"rank"}, SQLITE3_INTEGER);
  1063. switch ($artifact->{"rank"}){
  1064. case QUALITY_ID::LEGEND:
  1065. $statement->bindValue(":value", 37660, SQLITE3_INTEGER);
  1066. break;
  1067. case QUALITY_ID::HERO:
  1068. $statement->bindValue(":value", 25250, SQLITE3_INTEGER);
  1069. break;
  1070. case QUALITY_ID::RARE:
  1071. $statement->bindValue(":value", 18461, SQLITE3_INTEGER);
  1072. break;
  1073. case QUALITY_ID::MAGIC:
  1074. $statement->bindValue(":value", 9330, SQLITE3_INTEGER);
  1075. break;
  1076. default:
  1077. $statement->bindValue(":value", 3870, SQLITE3_INTEGER);
  1078. }
  1079. // TODO:
  1080. //$efficiency = calculate_artifact_efficiency($artifact);
  1081. //$max_efficiency = calculate_maximum_efficiency($artifact);
  1082. $statement->bindValue(":efficiency", null, SQLITE3_FLOAT);
  1083. $statement->bindValue(":max_efficiency", null, SQLITE3_FLOAT);
  1084. $statement->bindValue(":stat", $artifact->{"pri_effect"}[0], SQLITE3_INTEGER);
  1085. $statement->bindValue(":stat_value", $artifact->{"pri_effect"}[1], SQLITE3_INTEGER);
  1086. if (sizeof($artifact->{"sec_effects"}) > 0){
  1087. $statement->bindValue(":effect_1", $artifact->{"sec_effects"}[0][0], SQLITE3_INTEGER);
  1088. $statement->bindValue(":effect_1_value", $artifact->{"sec_effects"}[0][1], SQLITE3_INTEGER);
  1089. }
  1090. else{
  1091. $statement->bindValue(":effect_1", null, SQLITE3_INTEGER);
  1092. $statement->bindValue(":effect_1_value", 0, SQLITE3_INTEGER);
  1093. }
  1094. if (sizeof($artifact->{"sec_effects"}) > 1){
  1095. $statement->bindValue(":effect_2", $artifact->{"sec_effects"}[1][0]);
  1096. $statement->bindValue(":effect_2_value", $artifact->{"sec_effects"}[1][1]);
  1097. }
  1098. else{
  1099. $statement->bindValue(":effect_2", null, SQLITE3_INTEGER);
  1100. $statement->bindValue(":effect_2_value", 0, SQLITE3_INTEGER);
  1101. }
  1102. if (sizeof($artifact->{"sec_effects"}) > 2){
  1103. $statement->bindValue(":effect_3", $artifact->{"sec_effects"}[2][0], SQLITE3_INTEGER);
  1104. $statement->bindValue(":effect_3_value", $artifact->{"sec_effects"}[2][1], SQLITE3_INTEGER);
  1105. }
  1106. else{
  1107. $statement->bindValue(":effect_3", null, SQLITE3_INTEGER);
  1108. $statement->bindValue(":effect_3_value", 0, SQLITE3_INTEGER);
  1109. }
  1110. if (sizeof($artifact->{"sec_effects"}) > 3){
  1111. $statement->bindValue(":effect_4", $artifact->{"sec_effects"}[3][0], SQLITE3_INTEGER);
  1112. $statement->bindValue(":effect_4_value", $artifact->{"sec_effects"}[3][1], SQLITE3_INTEGER);
  1113. }
  1114. else{
  1115. $statement->bindValue(":effect_4", null, SQLITE3_INTEGER);
  1116. $statement->bindValue(":effect_4_value", 0, SQLITE3_INTEGER);
  1117. }
  1118. $statement->execute();
  1119. // Instantiate so efficiencies are calculated.
  1120. new Artifact($artifact->{"rid"});
  1121. break;
  1122. }
  1123. }
  1124. // Parse party
  1125. $leader = 1;
  1126. foreach ($response->{"unit_list"} as $unit){
  1127. $statement = $db->prepare("
  1128. INSERT INTO run_party
  1129. (run, unit, monster, leader, front)
  1130. VALUES
  1131. (:run, :unit, :monster, :leader, :front);"
  1132. );
  1133. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  1134. $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
  1135. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  1136. $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
  1137. $leader = 0;
  1138. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1139. $statement->execute();
  1140. }
  1141. // Return success
  1142. return "201 Created.";
  1143. }
  1144. /**
  1145. * Parses the JSONs relateds to a Dimension Hole run and saves the data.
  1146. *
  1147. * @param resource $db Database connection.
  1148. * @param int $player_id Player ID.
  1149. * @param int $id Run ID.
  1150. * @param mixed[] $json List of received JSONs.
  1151. */
  1152. function log_dimension_hole_run($db, $id, $player_id, $json){
  1153. // Only the result request and response JSONs are needed:
  1154. $response = $json["result_res"];
  1155. if ($json["result_req"] === null){
  1156. return ("400 Result request is required to parse Dimension Hole runs.");
  1157. }
  1158. // Prepare the "run" insert.
  1159. $statement = $db->prepare("
  1160. INSERT INTO run (
  1161. player,
  1162. id,
  1163. dtime,
  1164. area_type,
  1165. area,
  1166. stage,
  1167. difficulty,
  1168. win,
  1169. time,
  1170. mana,
  1171. energy,
  1172. crystal,
  1173. guild_points,
  1174. honor_points,
  1175. helper,
  1176. score,
  1177. rank
  1178. ) VALUES (
  1179. :player,
  1180. :id,
  1181. :dtime,
  1182. :area_type,
  1183. :area,
  1184. :stage,
  1185. :difficulty,
  1186. :win,
  1187. :time,
  1188. :mana,
  1189. :energy,
  1190. :crystal,
  1191. :guild_points,
  1192. :honor_points,
  1193. :helper,
  1194. :score,
  1195. :rank
  1196. );
  1197. ");
  1198. // Bind data and insert
  1199. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1200. $statement->bindValue(":id", $id, SQLITE3_INTEGER);
  1201. $statement->bindValue(":dtime", $response->{"tvaluelocal"}, SQLITE3_TEXT);
  1202. $statement->bindValue(":area_type", AREA_TYPE_ID::DIMENSIONAL_HOLE, SQLITE3_INTEGER);
  1203. $statement->bindValue(":area", $response->{"dungeon_id"}, SQLITE3_INTEGER);
  1204. $statement->bindValue(":stage", $response->{"difficulty"}, SQLITE3_INTEGER); // Difficulty is stage
  1205. $statement->bindValue(":difficulty", null, SQLITE3_INTEGER);
  1206. $statement->bindValue(":win", $response->{"win_lose"}, SQLITE3_INTEGER);
  1207. $statement->bindValue(":time", $response->{"clear_time"}->{"current_time"}, SQLITE3_INTEGER);
  1208. if (array_key_exists("mana", $response->{"reward"})){
  1209. $statement->bindValue(":mana", $response->{"reward"}->{"mana"}, SQLITE3_INTEGER);
  1210. }
  1211. else{
  1212. $statement->bindValue(":mana", 0, SQLITE3_INTEGER);
  1213. }
  1214. if (array_key_exists("energy", $response->{"reward"})){
  1215. $statement->bindValue(":energy", $response->{"reward"}->{"energy"}, SQLITE3_INTEGER);
  1216. }
  1217. $statement->bindValue(":energy", 0, SQLITE3_INTEGER);
  1218. if (array_key_exists("crystal", $response->{"reward"})){
  1219. $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"}, SQLITE3_INTEGER);
  1220. }
  1221. else{
  1222. $statement->bindValue(":crystal", 0, SQLITE3_INTEGER);
  1223. }
  1224. $statement->bindValue(":guild_points", 0, SQLITE3_INTEGER);
  1225. $statement->bindValue(":honor_points", 0, SQLITE3_INTEGER);
  1226. $statement->bindValue(":helper", 0, SQLITE3_INTEGER); // TODO Where is this information??
  1227. $statement->bindValue(":score", null, SQLITE3_INTEGER);
  1228. $statement->bindValue(":rank", null, SQLITE3_TEXT);
  1229. $statement->execute();
  1230. // Parse various types of reward
  1231. if (array_key_exists("changed_item_list", $response)){
  1232. foreach ($response->{"changed_item_list"} as $drop){
  1233. $reward_type = $drop->{"type"};
  1234. switch ($reward_type){
  1235. case INVENTORY_TYPE_ID::CRAFT_STUFF:
  1236. $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
  1237. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  1238. $statement->bindValue(":item", $drop->{"view"}->{"item_master_id"}, SQLITE3_INTEGER);
  1239. $statement->bindValue(":amount", $drop->{"view"}->{"item_quantity"}, SQLITE3_INTEGER);
  1240. $statement->execute();
  1241. break;
  1242. case INVENTORY_TYPE_ID::RUNE:
  1243. // TODO: Investigate with no awaken dungeons
  1244. /*$rune = $response->{"changed_item_list"}[0]->{"info"};
  1245. $statement = $db->prepare("
  1246. INSERT INTO run_drop_rune (
  1247. run,
  1248. id,
  1249. type,
  1250. slot,
  1251. stars,
  1252. ancient,
  1253. quality,
  1254. value,
  1255. efficiency,
  1256. max_efficiency,
  1257. main_stat,
  1258. main_stat_value,
  1259. innate_stat,
  1260. innate_stat_value,
  1261. substat_1,
  1262. substat_1_value,
  1263. substat_2,
  1264. substat_2_value,
  1265. substat_3,
  1266. substat_3_value,
  1267. substat_4,
  1268. substat_4_value
  1269. ) VALUES (
  1270. :run,
  1271. :id,
  1272. :type,
  1273. :slot,
  1274. :stars,
  1275. :ancient,
  1276. :quality,
  1277. :value,
  1278. :efficiency,
  1279. :max_efficiency,
  1280. :main_stat,
  1281. :main_stat_value,
  1282. :innate_stat,
  1283. :innate_stat_value,
  1284. :substat_1,
  1285. :substat_1_value,
  1286. :substat_2,
  1287. :substat_2_value,
  1288. :substat_3,
  1289. :substat_3_value,
  1290. :substat_4,
  1291. :substat_4_value
  1292. );
  1293. ");
  1294. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  1295. $statement->bindValue(":id", $rune->{"rune_id"}, SQLITE3_INTEGER);
  1296. $statement->bindValue(":type", $rune->{"set_id"}, SQLITE3_INTEGER);
  1297. $statement->bindValue(":slot", $rune->{"slot_no"}, SQLITE3_INTEGER);
  1298. $statement->bindValue(":value", $rune->{"sell_value"}, SQLITE3_INTEGER);
  1299. if ($rune->{"class"} > 10){
  1300. // Ancient rune
  1301. $statement->bindValue(":ancient", 1, SQLITE3_INTEGER);
  1302. $statement->bindValue(":stars", intval($rune->{"class"}) - 10, SQLITE3_INTEGER);
  1303. $statement->bindValue(":quality", intval($rune->{"rank"}) - 10, SQLITE3_INTEGER);
  1304. }
  1305. else{
  1306. // Non Ancient rune
  1307. $statement->bindValue(":ancient", 0, SQLITE3_INTEGER);
  1308. $statement->bindValue(":stars", $rune->{"class"}, SQLITE3_INTEGER);
  1309. $statement->bindValue(":quality", $rune->{"rank"}, SQLITE3_INTEGER);
  1310. }
  1311. $efficiency = calculate_efficiency($rune);
  1312. $max_efficiency = calculate_maximum_efficiency($rune);
  1313. $statement->bindValue(":efficiency", $efficiency, SQLITE3_FLOAT);
  1314. $statement->bindValue(":max_efficiency", $max_efficiency, SQLITE3_FLOAT);
  1315. $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0], SQLITE3_INTEGER);
  1316. $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1], SQLITE3_INTEGER);
  1317. if ($rune->{"prefix_eff"}[0] > 0){
  1318. $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0], SQLITE3_INTEGER);
  1319. $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1], SQLITE3_INTEGER);
  1320. }
  1321. else{
  1322. $statement->bindValue(":innate_stat", null, SQLITE3_INTEGER);
  1323. $statement->bindValue(":innate_stat_value", 0, SQLITE3_INTEGER);
  1324. }
  1325. if (sizeof($rune->{"sec_eff"}) > 0){
  1326. $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0], SQLITE3_INTEGER);
  1327. $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1], SQLITE3_INTEGER);
  1328. }
  1329. else{
  1330. $statement->bindValue(":substat_1", null, SQLITE3_INTEGER);
  1331. $statement->bindValue(":substat_1_value", 0, SQLITE3_INTEGER);
  1332. }
  1333. if (sizeof($rune->{"sec_eff"}) > 1){
  1334. $statement->bindValue(":substat_2", $rune->{"sec_eff"}[1][0], SQLITE3_INTEGER);
  1335. $statement->bindValue(":substat_2_value", $rune->{"sec_eff"}[1][1], SQLITE3_INTEGER);
  1336. }
  1337. else{
  1338. $statement->bindValue(":substat_2", null, SQLITE3_INTEGER);
  1339. $statement->bindValue(":substat_2_value", 0, SQLITE3_INTEGER);
  1340. }
  1341. if (sizeof($rune->{"sec_eff"}) > 2){
  1342. $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0], SQLITE3_INTEGER);
  1343. $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1], SQLITE3_INTEGER);
  1344. }
  1345. else{
  1346. $statement->bindValue(":substat_3", null, SQLITE3_INTEGER);
  1347. $statement->bindValue(":substat_3_value", 0, SQLITE3_INTEGER);
  1348. }
  1349. if (sizeof($rune->{"sec_eff"}) > 3){
  1350. $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0], SQLITE3_INTEGER);
  1351. $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1], SQLITE3_INTEGER);
  1352. }
  1353. else{
  1354. $statement->bindValue(":substat_4", null, SQLITE3_INTEGER);
  1355. $statement->bindValue(":substat_4_value", 0, SQLITE3_INTEGER);
  1356. }
  1357. $statement->execute();*/
  1358. break;
  1359. }
  1360. }
  1361. }
  1362. // Parse party
  1363. $leader = 1;
  1364. foreach ($response->{"unit_list"} as $unit){
  1365. $statement = $db->prepare("
  1366. INSERT INTO run_party
  1367. (run, unit, monster, leader, front)
  1368. VALUES
  1369. (:run, :unit, :monster, :leader, :front);"
  1370. );
  1371. $statement->bindValue(":run", $id, SQLITE3_INTEGER);
  1372. $statement->bindValue(":unit", $unit->{"unit_id"}, SQLITE3_INTEGER);
  1373. $statement->bindValue(":monster", $unit->{"unit_master_id"}, SQLITE3_INTEGER);
  1374. $statement->bindValue(":leader", $leader, SQLITE3_INTEGER);
  1375. $leader = 0;
  1376. $statement->bindValue(":front", 0, SQLITE3_INTEGER);
  1377. $statement->execute();
  1378. }
  1379. // Return success
  1380. return "201 Created.";
  1381. }
  1382. try{
  1383. header("Content-type: application/json; charset=utf-8");
  1384. // Get put data
  1385. parse_str(file_get_contents("php://input"), $_POST);
  1386. // Check API key.
  1387. if (!array_key_exists("key", $_POST)){
  1388. header("HTTP/1.1 400 API key not received.");
  1389. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 API key not received.");
  1390. return 400;
  1391. }
  1392. $key = $_POST["key"];
  1393. // Get all JSON files
  1394. $json = [
  1395. "start_req" => null,
  1396. "start_res" => null,
  1397. "result_req" => null,
  1398. "result_res" => null,
  1399. ];
  1400. // Result response - mandatory
  1401. if (!array_key_exists("result_response", $_POST)){
  1402. header("HTTP/1.1 400 No result response data received.");
  1403. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 No result response data received.");
  1404. return 400;
  1405. }
  1406. $data_json = json_decode($_POST["result_response"]);
  1407. if ($data_json === null){
  1408. header("HTTP/1.1 400 Result response data is no valid JSON.");
  1409. syslog(LOG_INFO, "[APIv1] HTTP/1.1 400 Result response data is no valid JSON.");
  1410. return 400;
  1411. }
  1412. $json["result_res"] = $data_json;
  1413. // Result request - optional
  1414. if (array_key_exists("result_request", $_POST)){
  1415. $data_raw = $_POST["result_request"];
  1416. if ($data_raw != null && $data_raw != false){
  1417. $data_json = json_decode($data_raw);
  1418. if ($data_json != null){
  1419. $json["result_req"] = $data_json;
  1420. }
  1421. }
  1422. }
  1423. // Start result - optional
  1424. if (array_key_exists("start_response", $_POST)){
  1425. $data_raw = $_POST["start_response"];
  1426. if ($data_raw != null && $data_raw != false){
  1427. $data_json = json_decode($data_raw);
  1428. if ($data_json != null){
  1429. $json["start_res"] = $data_json;
  1430. }
  1431. }
  1432. }
  1433. // Start result - optional
  1434. if (array_key_exists("start_request", $_POST)){
  1435. $data_raw = $_POST["start_request"];
  1436. if ($data_raw != null && $data_raw != false){
  1437. $data_json = json_decode($data_raw);
  1438. if ($data_json != null){
  1439. $json["start_req"] = $data_json;
  1440. }
  1441. }
  1442. }
  1443. // Extract basic data
  1444. $player_id = $json["result_res"]->{"wizard_info"}->{"wizard_id"};
  1445. $command = $json["result_res"]->{"command"};
  1446. $db = get_context()->get_db();
  1447. // Authenticate
  1448. $statement = $db->prepare("
  1449. SELECT COUNT(player.id) AS c
  1450. FROM
  1451. user,
  1452. player
  1453. WHERE
  1454. user.id = player.user AND
  1455. player.id = :player AND
  1456. user.api_key = :api_key;
  1457. ");
  1458. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1459. $statement->bindValue(":api_key", $key, SQLITE3_TEXT);
  1460. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1461. header("HTTP/1.1 401 Invalid credentials.");
  1462. syslog(LOG_INFO, "[APIv1] HTTP/1.1 401 Invalid credentials.");
  1463. return 401;
  1464. }
  1465. // Check for implemented command
  1466. $accepted_commands = [
  1467. "BattleDungeonResult_V2", // Dungeon Run
  1468. "BattleDimensionHoleDungeonResult_v2", // Dimension Hole Run
  1469. "BattleRiftOfWorldsRaidResult", // Rift Raid Run
  1470. "BattleScenarioResult", // Scenario Run
  1471. "BattleArenaResult", // Arena Run
  1472. "BattleDarkPortalResult" // Dimensinal Rift Run
  1473. ];
  1474. if (!in_array($command, $accepted_commands)){
  1475. header("HTTP/1.1 405 Command not implemented.");
  1476. syslog(LOG_INFO, "[APIv1] HTTP/1.1 405 Command not implemented.");
  1477. return 405;
  1478. }
  1479. // Check if already in DB
  1480. $dtime = $json["result_res"]->{"tvaluelocal"};
  1481. $statement = $db->prepare("SELECT count(id) AS c FROM run WHERE player = :player AND dtime = :dtime;");
  1482. $statement->bindValue(":player", $player_id, SQLITE3_INTEGER);
  1483. $statement->bindValue(":dtime", $dtime, SQLITE3_TEXT);
  1484. if (0 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  1485. header("HTTP/1.1 409 Run already in database.");
  1486. syslog(LOG_INFO, "[APIv1] HTTP/1.1 409 Run already in database.");
  1487. return 409;
  1488. }
  1489. // Get new ID
  1490. $q = $db->query("SELECT max(id) + 1 AS id FROM run;");
  1491. $r = $q->fetchArray(SQLITE3_ASSOC);
  1492. if ($r){
  1493. $id = $r["id"];
  1494. }
  1495. else{
  1496. $id = 1;
  1497. }
  1498. // Run parsing function
  1499. $result = "500 Unknown Error";
  1500. switch($command){
  1501. case "BattleDungeonResult_V2":
  1502. $result = log_cairos_run($db, $id, $player_id, $json);
  1503. break;
  1504. case "BattleDimensionHoleDungeonResult_v2":
  1505. $result = log_dimension_hole_run($db, $id, $player_id, $json);
  1506. break;
  1507. case "BattleRiftOfWorldsRaidResult":
  1508. $result = log_rift_raid_run($db, $id, $player_id, $json);
  1509. break;
  1510. case "BattleScenarioResult":
  1511. $result = log_scenario_run($db, $id, $player_id, $json);
  1512. break;
  1513. case "BattleArenaResult":
  1514. $result = log_arena_run($db, $id, $player_id, $json);
  1515. break;
  1516. case "BattleDarkPortalResult":
  1517. $result = log_dark_portal_run($db, $id, $player_id, $json);
  1518. break;
  1519. }
  1520. // Process result
  1521. $status_code = intval(substr($result, 0, 3));
  1522. $status_message = substr($result, 4);
  1523. if ($status_code == 0 || $status_code < 100 || $status_code > 599){
  1524. header("HTTP/1.1 500 Unknown error.");
  1525. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unknown error.");
  1526. return 500;
  1527. }
  1528. else{
  1529. header("HTTP/1.1 $status_code $status_message");
  1530. syslog(LOG_INFO, "[APIv1] HTTP/1.1 $status_code $status_message");
  1531. return $status_code;
  1532. }
  1533. }
  1534. catch(Exception $e) {
  1535. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  1536. syslog(LOG_ERR, "[APIv1] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  1537. return 500;
  1538. }