POST.php 57 KB

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