POST.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556
  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 a cairos run and saves the data.
  168. *
  169. * @param SQLiteConn $db Database connection.
  170. * @param int $uid Player UID.
  171. * @param JSON[] $json List of received JSONs.
  172. */
  173. function log_cairos_run($db, $uid, $json){
  174. // Only the result request and response JSONs are needed:
  175. $response = $json["result_res"];
  176. if ($json["result_req"] === null){
  177. return ("400 Result request is required to parse Cairos runs.");
  178. }
  179. $request = $json["result_req"];
  180. // Check if already in DB
  181. $dtime = $response->{"tvaluelocal"};
  182. $statement = $db->prepare("SELECT count(id) AS c FROM run WHERE uid = :uid AND dtime = :dtime;");
  183. $statement->bindValue(":uid", $uid);
  184. $statement->bindValue(":dtime", $dtime);
  185. if (0 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  186. return ("409 Run already in database.");
  187. }
  188. // Get new ID
  189. $q = $db->query("SELECT max(id) + 1 AS id FROM run;");
  190. $r = $q->fetchArray(SQLITE3_ASSOC);
  191. if ($r){
  192. $id = $r["id"];
  193. }
  194. else{
  195. $id = 1;
  196. }
  197. // Prepare the "run" insert.
  198. $statement = $db->prepare("
  199. INSERT INTO run (
  200. uid,
  201. id,
  202. dtime,
  203. area_type,
  204. area,
  205. stage,
  206. difficulty,
  207. win,
  208. time,
  209. mana,
  210. energy,
  211. crystal,
  212. guild_points,
  213. helper,
  214. score,
  215. rank
  216. ) VALUES (
  217. :uid,
  218. :id,
  219. :dtime,
  220. :area_type,
  221. :area,
  222. :stage,
  223. :difficulty,
  224. :win,
  225. :time,
  226. :mana,
  227. :energy,
  228. :crystal,
  229. :guild_points,
  230. :helper,
  231. :score,
  232. :rank
  233. );
  234. ");
  235. // Bind data and insert
  236. $statement->bindValue(":uid", $uid);
  237. $statement->bindValue(":id", $id);
  238. $statement->bindValue(":dtime", $dtime);
  239. $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
  240. $statement->bindValue(":area", $request->{"dungeon_id"});
  241. $statement->bindValue(":stage", $request->{"stage_id"});
  242. $statement->bindValue(":difficulty", null);
  243. $statement->bindValue(":win", $response->{"win_lose"});
  244. $statement->bindValue(":time", $response->{"clear_time"}->{"current_time"});
  245. if (array_key_exists("mana", $response->{"reward"})){
  246. $statement->bindValue(":mana", $response->{"reward"}->{"mana"});
  247. }
  248. else{
  249. $statement->bindValue(":mana", 0);
  250. }
  251. if (array_key_exists("energy", $response->{"reward"})){
  252. $statement->bindValue(":energy", $response->{"reward"}->{"energy"});
  253. }
  254. else{
  255. $statement->bindValue(":energy", 0);
  256. }
  257. if (array_key_exists("crystal", $response->{"reward"})){
  258. $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"});
  259. }
  260. else{
  261. $statement->bindValue(":crystal", 0);
  262. }
  263. $statement->bindValue(":guild_points", 0);
  264. $statement->bindValue(":helper", 0); // TODO Where is this information??
  265. $statement->bindValue(":score", null);
  266. $statement->bindValue(":rank", null);
  267. $statement->execute();
  268. // Parse various types of reward
  269. if (array_key_exists("changed_item_list", $response)){
  270. $reward_type = $response->{"changed_item_list"}[0]->{"type"};
  271. switch ($reward_type){
  272. case INVENTORY_TYPE_ID::MONSTER: // Unit
  273. $statement = $db->prepare("INSERT INTO run_drop_unit (run, unit) VALUES (:run, :unit);");
  274. $statement->bindValue(":run", $id);
  275. $statement->bindValue(":unit", $response->{"changed_item_list"}[0]->{"view"}->{"item_master_id"});
  276. $statement->execute();
  277. break;
  278. case INVENTORY_TYPE_ID::SCROLL:
  279. case INVENTORY_TYPE_ID::ESSENCES:
  280. $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
  281. $statement->bindValue(":run", $id);
  282. $statement->bindValue(":item", $response->{"changed_item_list"}[0]->{"view"}->{"item_master_id"});
  283. $statement->bindValue(":amount", $response->{"changed_item_list"}[0]->{"view"}->{"item_quantity"});
  284. $statement->execute();
  285. break;
  286. case INVENTORY_TYPE_ID::RUNE:
  287. $rune = $response->{"changed_item_list"}[0]->{"info"};
  288. $statement = $db->prepare("
  289. INSERT INTO run_drop_rune (
  290. run,
  291. id,
  292. type,
  293. slot,
  294. stars,
  295. ancient,
  296. quality,
  297. value,
  298. efficiency,
  299. max_efficiency,
  300. main_stat,
  301. main_stat_value,
  302. innate_stat,
  303. innate_stat_value,
  304. substat_1,
  305. substat_1_value,
  306. substat_2,
  307. substat_2_value,
  308. substat_3,
  309. substat_3_value,
  310. substat_4,
  311. substat_4_value
  312. ) VALUES (
  313. :run,
  314. :id,
  315. :type,
  316. :slot,
  317. :stars,
  318. :ancient,
  319. :quality,
  320. :value,
  321. :efficiency,
  322. :max_efficiency,
  323. :main_stat,
  324. :main_stat_value,
  325. :innate_stat,
  326. :innate_stat_value,
  327. :substat_1,
  328. :substat_1_value,
  329. :substat_2,
  330. :substat_2_value,
  331. :substat_3,
  332. :substat_3_value,
  333. :substat_4,
  334. :substat_4_value
  335. );
  336. ");
  337. $statement->bindValue(":run", $id);
  338. $statement->bindValue(":id", $rune->{"rune_id"});
  339. $statement->bindValue(":type", $rune->{"set_id"});
  340. $statement->bindValue(":slot", $rune->{"slot_no"});
  341. $statement->bindValue(":value", $rune->{"sell_value"});
  342. if ($rune->{"class"} > 10){
  343. // Ancient rune
  344. $statement->bindValue(":ancient", 1);
  345. $statement->bindValue(":stars", intval($rune->{"class"}) - 10);
  346. $statement->bindValue(":quality", intval($rune->{"rank"}) - 10);
  347. }
  348. else{
  349. // Non Ancient rune
  350. $statement->bindValue(":ancient", 0);
  351. $statement->bindValue(":stars", $rune->{"class"});
  352. $statement->bindValue(":quality", $rune->{"rank"});
  353. }
  354. $efficiency = calculate_efficiency($rune);
  355. $max_efficiency = calculate_maximum_efficiency($rune);
  356. $statement->bindValue(":efficiency", $efficiency);
  357. $statement->bindValue(":max_efficiency", $max_efficiency);
  358. $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0]);
  359. $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1]);
  360. if ($rune->{"prefix_eff"}[0] > 0){
  361. $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0]);
  362. $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1]);
  363. }
  364. else{
  365. $statement->bindValue(":innate_stat", null);
  366. $statement->bindValue(":innate_stat_value", 0);
  367. }
  368. if (sizeof($rune->{"sec_eff"}) > 0){
  369. $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0]);
  370. $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1]);
  371. }
  372. else{
  373. $statement->bindValue(":substat_1", null);
  374. $statement->bindValue(":substat_1_value", 0);
  375. }
  376. if (sizeof($rune->{"sec_eff"}) > 1){
  377. $statement->bindValue(":substat_2", $rune->{"sec_eff"}[1][0]);
  378. $statement->bindValue(":substat_2_value", $rune->{"sec_eff"}[1][1]);
  379. }
  380. else{
  381. $statement->bindValue(":substat_2", null);
  382. $statement->bindValue(":substat_2_value", 0);
  383. }
  384. if (sizeof($rune->{"sec_eff"}) > 2){
  385. $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0]);
  386. $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1]);
  387. }
  388. else{
  389. $statement->bindValue(":substat_3", null);
  390. $statement->bindValue(":substat_3_value", 0);
  391. }
  392. if (sizeof($rune->{"sec_eff"}) > 3){
  393. $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0]);
  394. $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1]);
  395. }
  396. else{
  397. $statement->bindValue(":substat_4", null);
  398. $statement->bindValue(":substat_4_value", 0);
  399. }
  400. $statement->execute();
  401. break;
  402. }
  403. }
  404. // Parse party
  405. $leader = 1;
  406. foreach ($response->{"unit_list"} as $unit){
  407. $statement = $db->prepare("
  408. INSERT INTO run_party
  409. (run, unit, k_unit, leader, front)
  410. VALUES
  411. (:run, :unit, :k_unit, :leader, :front);"
  412. );
  413. $statement->bindValue(":run", $id);
  414. $statement->bindValue(":unit", $unit->{"unit_id"});
  415. $statement->bindValue(":k_unit", $unit->{"unit_master_id"});
  416. $statement->bindValue(":leader", $leader);
  417. $leader = 0;
  418. $statement->bindValue(":front", 0);
  419. $statement->execute();
  420. }
  421. // Return success
  422. return "201 Created.";
  423. }
  424. global $db;
  425. try{
  426. header("Content-type: application/json; charset=utf-8");
  427. // Get put data
  428. parse_str(file_get_contents("php://input"), $_POST);
  429. // Check API key.
  430. if (!array_key_exists("key", $_POST)){
  431. header("HTTP/1.1 400 API key not received.");
  432. syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 API key not received.");
  433. return 400;
  434. }
  435. $key = $_POST["key"];
  436. // Get all JSON files
  437. $json = [
  438. "start_req" => null,
  439. "start_res" => null,
  440. "result_req" => null,
  441. "result_res" => null,
  442. ];
  443. // Result response - mandatory
  444. if (!array_key_exists("result_response", $_POST)){
  445. header("HTTP/1.1 400 No result response data received.");
  446. syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 No result response data received.");
  447. return 400;
  448. }
  449. $data_json = json_decode($_POST["result_response"]);
  450. if ($data_json === null){
  451. header("HTTP/1.1 400 Result response data is no valid JSON.");
  452. syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 Result response data is no valid JSON.");
  453. return 400;
  454. }
  455. $json["result_res"] = $data_json;
  456. // Result request - optional
  457. if (array_key_exists("result_request", $_POST)){
  458. $data_raw = $_POST["result_request"];
  459. if ($data_raw != null && $data_raw != false){
  460. $data_json = json_decode($data_raw);
  461. if ($data_json != null){
  462. $json["result_req"] = $data_json;
  463. }
  464. }
  465. }
  466. // Start result - optional
  467. if (array_key_exists("start_response", $_POST)){
  468. $data_raw = $_POST["start_response"];
  469. if ($data_raw != null && $data_raw != false){
  470. $data_json = json_decode($data_raw);
  471. if ($data_json != null){
  472. $json["start_res"] = $data_json;
  473. }
  474. }
  475. }
  476. // Start result - optional
  477. if (array_key_exists("start_request", $_POST)){
  478. $data_raw = $_POST["start_request"];
  479. if ($data_raw != null && $data_raw != false){
  480. $data_json = json_decode($data_raw);
  481. if ($data_json != null){
  482. $json["start_req"] = $data_json;
  483. }
  484. }
  485. }
  486. // Extract basic data
  487. $uid = $json["result_res"]->{"wizard_info"}->{"wizard_id"};
  488. $command = $json["result_res"]->{"command"};
  489. // Authenticate
  490. $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key';");
  491. $statement->bindValue(":uid", $uid);
  492. $statement->bindValue(":api_key", $key);
  493. if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
  494. header("HTTP/1.1 401 Invalid credentials.");
  495. syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials.");
  496. return 401;
  497. }
  498. // Check for implemented command
  499. $accepted_commands = [
  500. "BattleDungeonResult_V2" // Dungeon RUN
  501. ];
  502. if (!in_array($command, $accepted_commands)){
  503. header("HTTP/1.1 405 Command not implemented.");
  504. syslog(LOG_INFO, "[APIv3] HTTP/1.1 405 Command not implemented.");
  505. return 405;
  506. }
  507. // Run parsing function
  508. $result = "500 Unknown Error";
  509. switch($command){
  510. case "BattleDungeonResult_V2":
  511. $result = log_cairos_run($db, $uid, $json);
  512. break;
  513. }
  514. // Process result
  515. $status_code = intval(substr($result, 0, 3));
  516. $status_message = substr($result, 4);
  517. if ($status_code == 0 || $status_code < 100 || $status_code > 599){
  518. header("HTTP/1.1 500 Unknown error.");
  519. syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unknown error.");
  520. return 500;
  521. }
  522. else{
  523. header("HTTP/1.1 $status_code $status_message");
  524. syslog(LOG_INFO, "[APIv3] HTTP/1.1 $status_code $status_message");
  525. return $status_code;
  526. }
  527. }
  528. catch(Exception $e) {
  529. header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
  530. syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
  531. return 500;
  532. }
  533. ?>