| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556 |
- <?php
- /**
- * Run logger script.
- *
- * Exposes an API to log a run.
- *
- * Mandatory PUT parameters are:
- * - result_response: JSON received from game server on run end.
- * - key: User API key.
- *
- * Optional POST parameters are:
- * - result_request: JSON sent to game server on run end.
- * - start_response: JSON received from game server on run start.
- * - start_request: JSON sent to game server on run start.
- *
- * @category API
- */
- /**
- * Calculates the currrent efficiency of a rune.
- *
- * @param JSON $rune JSON fragment of the rune.
- * @return int Efficiency (0-100)
- */
- function calculate_efficiency($rune){
- $efficiency = 0.0;
- $max_efficiency = 0.0;
- $substats = [];
- $level = $rune->{"upgrade_curr"};
- $stars = $rune->{"class"};
- // Ancient stars are + 10
- if ($stars > 10){
- $stars -= 10;
- }
- $list = null;
- // Main stat
- $main_stat = $rune->{"pri_eff"}[0];
- switch ($main_stat){
- case RUNE_STAT_ID::HP:
- $list = RUNE_STAT_VALUES::HP;
- break;
- case RUNE_STAT_ID::HP_P:
- $list = RUNE_STAT_VALUES::HP_P;
- break;
- case RUNE_STAT_ID::ATK:
- $list = RUNE_STAT_VALUES::ATK;
- break;
- case RUNE_STAT_ID::ATK_P:
- $list = RUNE_STAT_VALUES::ATK_P;
- break;
- case RUNE_STAT_ID::DEF:
- $list = RUNE_STAT_VALUES::DEF;
- break;
- case RUNE_STAT_ID::DEF_P:
- $list = RUNE_STAT_VALUES::DEF_P;
- break;
- case RUNE_STAT_ID::SPD:
- $list = RUNE_STAT_VALUES::SPD;
- break;
- case RUNE_STAT_ID::CRR:
- $list = RUNE_STAT_VALUES::CRR;
- break;
- case RUNE_STAT_ID::CRD:
- $list = RUNE_STAT_VALUES::CRD;
- break;
- case RUNE_STAT_ID::ACC:
- $list = RUNE_STAT_VALUES::ACC;
- break;
- case RUNE_STAT_ID::RES:
- $list = RUNE_STAT_VALUES::RES;
- break;
- }
- $efficiency += floatval($list[$stars][$level]) / floatval($list[6][15]);
- // Innate stat
- if ($rune->{"prefix_eff"}[0] > 0){
- $innate_stat = $rune->{"pri_eff"}[0];
- $innate_stat_value = $rune->{"pri_eff"}[1];
- $efficiency += floatval($innate_stat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$innate_stat][6]));
- }
- // Substats
- for ($i = 0; $i <= 3; $i ++){
- if (sizeof($rune->{"sec_eff"}) > $i){
- $substat = $rune->{"sec_eff"}[$i][0];
- $substat_value = $rune->{"sec_eff"}[$i][1];
- $efficiency += floatval($substat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][6]));
- }
- }
- $efficiency = $efficiency / 2.8 * 100;
- if ($efficiency > 100){
- $efficiency = 100;
- }
- return $efficiency;
- }
- /**
- * Calculates the maximum efficiency of a rune.
- *
- * @param JSON $rune JSON fragment of the rune.
- * @return int Maximum efficiency (0-100)
- */
- function calculate_maximum_efficiency($rune){
- $efficiency = 0.0;
- $max_efficiency = 0.0;
- $substats = [];
- $stars = $rune->{"class"};
- // Ancient stars are + 10
- if ($stars > 10){
- $stars -= 10;
- }
- $list = null;
- // Main stat
- $main_stat = $rune->{"pri_eff"}[0];
- switch ($main_stat){
- case RUNE_STAT_ID::HP:
- $list = RUNE_STAT_VALUES::HP;
- break;
- case RUNE_STAT_ID::HP_P:
- $list = RUNE_STAT_VALUES::HP_P;
- break;
- case RUNE_STAT_ID::ATK:
- $list = RUNE_STAT_VALUES::ATK;
- break;
- case RUNE_STAT_ID::ATK_P:
- $list = RUNE_STAT_VALUES::ATK_P;
- break;
- case RUNE_STAT_ID::DEF:
- $list = RUNE_STAT_VALUES::DEF;
- break;
- case RUNE_STAT_ID::DEF_P:
- $list = RUNE_STAT_VALUES::DEF_P;
- break;
- case RUNE_STAT_ID::SPD:
- $list = RUNE_STAT_VALUES::SPD;
- break;
- case RUNE_STAT_ID::CRR:
- $list = RUNE_STAT_VALUES::CRR;
- break;
- case RUNE_STAT_ID::CRD:
- $list = RUNE_STAT_VALUES::CRD;
- break;
- case RUNE_STAT_ID::ACC:
- $list = RUNE_STAT_VALUES::ACC;
- break;
- case RUNE_STAT_ID::RES:
- $list = RUNE_STAT_VALUES::RES;
- break;
- }
- $efficiency += floatval($list[$stars][15]) / floatval($list[6][15]);
- // Innate stat
- if ($rune->{"prefix_eff"}[0] > 0){
- $innate_stat = $rune->{"pri_eff"}[0];
- $innate_stat_value = $rune->{"pri_eff"}[1];
- $efficiency += floatval($innate_stat_value) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$innate_stat][6]));
- }
- // Substats
- for ($i = 0; $i <= 3; $i ++){
- if (sizeof($rune->{"sec_eff"}) > $i){
- $substat = $rune->{"sec_eff"}[$i][0];
- $efficiency += floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][$stars]) / (5 * floatval(RUNE_SUBSTAT_MAX_VALUES::SUBSTAT[$substat][6]));
- }
- }
- $efficiency = $efficiency / 2.8 * 100;
- if ($efficiency > 100){
- $efficiency = 100;
- }
- return $efficiency;
- }
- /**
- * Parses the JSONs relateds to a cairos run and saves the data.
- *
- * @param SQLiteConn $db Database connection.
- * @param int $uid Player UID.
- * @param JSON[] $json List of received JSONs.
- */
- function log_cairos_run($db, $uid, $json){
- // Only the result request and response JSONs are needed:
- $response = $json["result_res"];
- if ($json["result_req"] === null){
- return ("400 Result request is required to parse Cairos runs.");
- }
- $request = $json["result_req"];
- // Check if already in DB
- $dtime = $response->{"tvaluelocal"};
- $statement = $db->prepare("SELECT count(id) AS c FROM run WHERE uid = :uid AND dtime = :dtime;");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":dtime", $dtime);
- if (0 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- return ("409 Run already in database.");
- }
- // Get new ID
- $q = $db->query("SELECT max(id) + 1 AS id FROM run;");
- $r = $q->fetchArray(SQLITE3_ASSOC);
- if ($r){
- $id = $r["id"];
- }
- else{
- $id = 1;
- }
- // Prepare the "run" insert.
- $statement = $db->prepare("
- INSERT INTO run (
- uid,
- id,
- dtime,
- area_type,
- area,
- stage,
- difficulty,
- win,
- time,
- mana,
- energy,
- crystal,
- guild_points,
- helper,
- score,
- rank
- ) VALUES (
- :uid,
- :id,
- :dtime,
- :area_type,
- :area,
- :stage,
- :difficulty,
- :win,
- :time,
- :mana,
- :energy,
- :crystal,
- :guild_points,
- :helper,
- :score,
- :rank
- );
- ");
- // Bind data and insert
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":id", $id);
- $statement->bindValue(":dtime", $dtime);
- $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON);
- $statement->bindValue(":area", $request->{"dungeon_id"});
- $statement->bindValue(":stage", $request->{"stage_id"});
- $statement->bindValue(":difficulty", null);
- $statement->bindValue(":win", $response->{"win_lose"});
- $statement->bindValue(":time", $response->{"clear_time"}->{"current_time"});
- if (array_key_exists("mana", $response->{"reward"})){
- $statement->bindValue(":mana", $response->{"reward"}->{"mana"});
- }
- else{
- $statement->bindValue(":mana", 0);
- }
- if (array_key_exists("energy", $response->{"reward"})){
- $statement->bindValue(":energy", $response->{"reward"}->{"energy"});
- }
- else{
- $statement->bindValue(":energy", 0);
- }
- if (array_key_exists("crystal", $response->{"reward"})){
- $statement->bindValue(":crystal", $response->{"reward"}->{"crystal"});
- }
- else{
- $statement->bindValue(":crystal", 0);
- }
- $statement->bindValue(":guild_points", 0);
- $statement->bindValue(":helper", 0); // TODO Where is this information??
- $statement->bindValue(":score", null);
- $statement->bindValue(":rank", null);
- $statement->execute();
- // Parse various types of reward
- if (array_key_exists("changed_item_list", $response)){
- $reward_type = $response->{"changed_item_list"}[0]->{"type"};
- switch ($reward_type){
- case INVENTORY_TYPE_ID::MONSTER: // Unit
- $statement = $db->prepare("INSERT INTO run_drop_unit (run, unit) VALUES (:run, :unit);");
- $statement->bindValue(":run", $id);
- $statement->bindValue(":unit", $response->{"changed_item_list"}[0]->{"view"}->{"item_master_id"});
- $statement->execute();
- break;
- case INVENTORY_TYPE_ID::SCROLL:
- case INVENTORY_TYPE_ID::ESSENCES:
- $statement = $db->prepare("INSERT INTO run_drop_item (run, item, amount) VALUES (:run, :item, :amount);");
- $statement->bindValue(":run", $id);
- $statement->bindValue(":item", $response->{"changed_item_list"}[0]->{"view"}->{"item_master_id"});
- $statement->bindValue(":amount", $response->{"changed_item_list"}[0]->{"view"}->{"item_quantity"});
- $statement->execute();
- break;
- case INVENTORY_TYPE_ID::RUNE:
- $rune = $response->{"changed_item_list"}[0]->{"info"};
- $statement = $db->prepare("
- INSERT INTO run_drop_rune (
- run,
- id,
- type,
- slot,
- stars,
- ancient,
- quality,
- value,
- efficiency,
- max_efficiency,
- main_stat,
- main_stat_value,
- innate_stat,
- innate_stat_value,
- substat_1,
- substat_1_value,
- substat_2,
- substat_2_value,
- substat_3,
- substat_3_value,
- substat_4,
- substat_4_value
- ) VALUES (
- :run,
- :id,
- :type,
- :slot,
- :stars,
- :ancient,
- :quality,
- :value,
- :efficiency,
- :max_efficiency,
- :main_stat,
- :main_stat_value,
- :innate_stat,
- :innate_stat_value,
- :substat_1,
- :substat_1_value,
- :substat_2,
- :substat_2_value,
- :substat_3,
- :substat_3_value,
- :substat_4,
- :substat_4_value
- );
- ");
- $statement->bindValue(":run", $id);
- $statement->bindValue(":id", $rune->{"rune_id"});
- $statement->bindValue(":type", $rune->{"set_id"});
- $statement->bindValue(":slot", $rune->{"slot_no"});
- $statement->bindValue(":value", $rune->{"sell_value"});
- if ($rune->{"class"} > 10){
- // Ancient rune
- $statement->bindValue(":ancient", 1);
- $statement->bindValue(":stars", intval($rune->{"class"}) - 10);
- $statement->bindValue(":quality", intval($rune->{"rank"}) - 10);
- }
- else{
- // Non Ancient rune
- $statement->bindValue(":ancient", 0);
- $statement->bindValue(":stars", $rune->{"class"});
- $statement->bindValue(":quality", $rune->{"rank"});
- }
- $efficiency = calculate_efficiency($rune);
- $max_efficiency = calculate_maximum_efficiency($rune);
- $statement->bindValue(":efficiency", $efficiency);
- $statement->bindValue(":max_efficiency", $max_efficiency);
- $statement->bindValue(":main_stat", $rune->{"pri_eff"}[0]);
- $statement->bindValue(":main_stat_value", $rune->{"pri_eff"}[1]);
- if ($rune->{"prefix_eff"}[0] > 0){
- $statement->bindValue(":innate_stat", $rune->{"prefix_eff"}[0]);
- $statement->bindValue(":innate_stat_value", $rune->{"prefix_eff"}[1]);
- }
- else{
- $statement->bindValue(":innate_stat", null);
- $statement->bindValue(":innate_stat_value", 0);
- }
- if (sizeof($rune->{"sec_eff"}) > 0){
- $statement->bindValue(":substat_1", $rune->{"sec_eff"}[0][0]);
- $statement->bindValue(":substat_1_value", $rune->{"sec_eff"}[0][1]);
- }
- else{
- $statement->bindValue(":substat_1", null);
- $statement->bindValue(":substat_1_value", 0);
- }
- if (sizeof($rune->{"sec_eff"}) > 1){
- $statement->bindValue(":substat_2", $rune->{"sec_eff"}[1][0]);
- $statement->bindValue(":substat_2_value", $rune->{"sec_eff"}[1][1]);
- }
- else{
- $statement->bindValue(":substat_2", null);
- $statement->bindValue(":substat_2_value", 0);
- }
- if (sizeof($rune->{"sec_eff"}) > 2){
- $statement->bindValue(":substat_3", $rune->{"sec_eff"}[2][0]);
- $statement->bindValue(":substat_3_value", $rune->{"sec_eff"}[2][1]);
- }
- else{
- $statement->bindValue(":substat_3", null);
- $statement->bindValue(":substat_3_value", 0);
- }
- if (sizeof($rune->{"sec_eff"}) > 3){
- $statement->bindValue(":substat_4", $rune->{"sec_eff"}[3][0]);
- $statement->bindValue(":substat_4_value", $rune->{"sec_eff"}[3][1]);
- }
- else{
- $statement->bindValue(":substat_4", null);
- $statement->bindValue(":substat_4_value", 0);
- }
- $statement->execute();
- break;
- }
- }
- // Parse party
- $leader = 1;
- foreach ($response->{"unit_list"} as $unit){
- $statement = $db->prepare("
- INSERT INTO run_party
- (run, unit, k_unit, leader, front)
- VALUES
- (:run, :unit, :k_unit, :leader, :front);"
- );
- $statement->bindValue(":run", $id);
- $statement->bindValue(":unit", $unit->{"unit_id"});
- $statement->bindValue(":k_unit", $unit->{"unit_master_id"});
- $statement->bindValue(":leader", $leader);
- $leader = 0;
- $statement->bindValue(":front", 0);
- $statement->execute();
- }
- // Return success
- return "201 Created.";
- }
- global $db;
- try{
- header("Content-type: application/json; charset=utf-8");
- // Get put data
- parse_str(file_get_contents("php://input"), $_POST);
- // Check API key.
- if (!array_key_exists("key", $_POST)){
- header("HTTP/1.1 400 API key not received.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 API key not received.");
- return 400;
- }
- $key = $_POST["key"];
- // Get all JSON files
- $json = [
- "start_req" => null,
- "start_res" => null,
- "result_req" => null,
- "result_res" => null,
- ];
- // Result response - mandatory
- if (!array_key_exists("result_response", $_POST)){
- header("HTTP/1.1 400 No result response data received.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 No result response data received.");
- return 400;
- }
- $data_json = json_decode($_POST["result_response"]);
- if ($data_json === null){
- header("HTTP/1.1 400 Result response data is no valid JSON.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 Result response data is no valid JSON.");
- return 400;
- }
- $json["result_res"] = $data_json;
- // Result request - optional
- if (array_key_exists("result_request", $_POST)){
- $data_raw = $_POST["result_request"];
- if ($data_raw != null && $data_raw != false){
- $data_json = json_decode($data_raw);
- if ($data_json != null){
- $json["result_req"] = $data_json;
- }
- }
- }
- // Start result - optional
- if (array_key_exists("start_response", $_POST)){
- $data_raw = $_POST["start_response"];
- if ($data_raw != null && $data_raw != false){
- $data_json = json_decode($data_raw);
- if ($data_json != null){
- $json["start_res"] = $data_json;
- }
- }
- }
- // Start result - optional
- if (array_key_exists("start_request", $_POST)){
- $data_raw = $_POST["start_request"];
- if ($data_raw != null && $data_raw != false){
- $data_json = json_decode($data_raw);
- if ($data_json != null){
- $json["start_req"] = $data_json;
- }
- }
- }
- // Extract basic data
- $uid = $json["result_res"]->{"wizard_info"}->{"wizard_id"};
- $command = $json["result_res"]->{"command"};
- // Authenticate
- $statement = $db->prepare("SELECT COUNT(uid) AS c FROM player WHERE uid = :uid AND api_key = :api_key';");
- $statement->bindValue(":uid", $uid);
- $statement->bindValue(":api_key", $key);
- if (1 != $statement->execute()->fetchArray(SQLITE3_ASSOC)["c"]){
- header("HTTP/1.1 401 Invalid credentials.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials.");
- return 401;
- }
- // Check for implemented command
- $accepted_commands = [
- "BattleDungeonResult_V2" // Dungeon RUN
- ];
- if (!in_array($command, $accepted_commands)){
- header("HTTP/1.1 405 Command not implemented.");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 405 Command not implemented.");
- return 405;
- }
- // Run parsing function
- $result = "500 Unknown Error";
- switch($command){
- case "BattleDungeonResult_V2":
- $result = log_cairos_run($db, $uid, $json);
- break;
- }
- // Process result
- $status_code = intval(substr($result, 0, 3));
- $status_message = substr($result, 4);
- if ($status_code == 0 || $status_code < 100 || $status_code > 599){
- header("HTTP/1.1 500 Unknown error.");
- syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unknown error.");
- return 500;
- }
- else{
- header("HTTP/1.1 $status_code $status_message");
- syslog(LOG_INFO, "[APIv3] HTTP/1.1 $status_code $status_message");
- return $status_code;
- }
- }
- catch(Exception $e) {
- header("HTTP/1.1 500 Unexpeced error: " . $e->getMessage());
- syslog(LOG_ERR, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage());
- return 500;
- }
- ?>
|