{"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 an artifact and saves it to the database. * * @param string $uid Owner ID. * @param JSON artifact JSON fragment of the artifact. */ function parse_artifact($uid, $artifact){ global $db; $statement = $db->prepare(" INSERT INTO artifact ( uid, id, assigned_to, type, attribute, archetype, level, quality, original_quality, value, efficiency, max_efficiency, main_stat, main_stat_value, effect_1, effect_1_value, effect_1_enchant, effect_1_grind, effect_2, effect_2_value, effect_2_enchant, effect_2_grind, effect_3, effect_3_value, effect_3_enchant, effect_3_grind, effect_4, effect_4_value, effect_4_enchant, effect_4_grind, locked ) VALUES ( :uid, :id, :assigned_to, :type, :attribute, :archetype, :level, :quality, :original_quality, :value, :efficiency, :max_efficiency, :main_stat, :main_stat_value, :effect_1, :effect_1_value, :effect_1_enchant, :effect_1_grind, :effect_2, :effect_2_value, :effect_2_enchant, :effect_2_grind, :effect_3, :effect_3_value, :effect_3_enchant, :effect_3_grind, :effect_4, :effect_4_value, :effect_4_enchant, :effect_4_grind, :locked ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":id", $artifact->{"rid"}); if (intval($artifact->{"occupied_id"}) == 0){ $statement->bindValue(":assigned_to", null); } else{ $statement->bindValue(":assigned_to", $artifact->{"occupied_id"}); } if (intval($artifact->{"attribute"}) == 0){ $statement->bindValue(":attribute", null); } else{ $statement->bindValue(":attribute", $artifact->{"attribute"}); } if (intval($artifact->{"type"}) == 0){ $statement->bindValue(":type", null); } else{ $statement->bindValue(":type", $artifact->{"type"}); } if (intval($artifact->{"unit_style"}) == 0){ $statement->bindValue(":archetype", null); } else{ $statement->bindValue(":archetype", $artifact->{"unit_style"}); } $statement->bindValue(":level", $artifact->{"level"}); $statement->bindValue(":quality", $artifact->{"rank"}); $statement->bindValue(":original_quality", $artifact->{"natural_rank"}); $statement->bindValue(":value", 0); // Not in JSON file $statement->bindValue(":locked", $artifact->{"locked"}); $efficiency = 0; // Not standard formula yet $max_efficiency = 0; // Not standard formula yet $statement->bindValue(":efficiency", $efficiency); $statement->bindValue(":max_efficiency", $max_efficiency); $statement->bindValue(":main_stat", $artifact->{"pri_effect"}[0]); $statement->bindValue(":main_stat_value", $artifact->{"pri_effect"}[1]); if (sizeof($artifact->{"sec_effects"}) > 0){ $statement->bindValue(":effect_1", $artifact->{"sec_effects"}[0][0]); $statement->bindValue(":effect_1_value", $artifact->{"sec_effects"}[0][1]); $statement->bindValue(":effect_1_enchant", $artifact->{"sec_effects"}[0][2]); $statement->bindValue(":effect_1_grind", $artifact->{"sec_effects"}[0][3]); } else{ $statement->bindValue(":effect_1", null); $statement->bindValue(":effect_1_value", 0); $statement->bindValue(":effect_1_enchant", 0); $statement->bindValue(":effect_1_grind", 0); } if (sizeof($artifact->{"sec_effects"}) > 1){ $statement->bindValue(":effect_2", $artifact->{"sec_effects"}[1][0]); $statement->bindValue(":effect_2_value", $artifact->{"sec_effects"}[1][1]); $statement->bindValue(":effect_2_enchant", $artifact->{"sec_effects"}[1][2]); $statement->bindValue(":effect_2_grind", $artifact->{"sec_effects"}[1][3]); } else{ $statement->bindValue(":effect_2", null); $statement->bindValue(":effect_2_value", 0); $statement->bindValue(":effect_2_enchant", 0); $statement->bindValue(":effect_2_grind", 0); } if (sizeof($artifact->{"sec_effects"}) > 2){ $statement->bindValue(":effect_3", $artifact->{"sec_effects"}[2][0]); $statement->bindValue(":effect_3_value", $artifact->{"sec_effects"}[2][1]); $statement->bindValue(":effect_3_enchant", $artifact->{"sec_effects"}[2][2]); $statement->bindValue(":effect_3_grind", $artifact->{"sec_effects"}[2][3]); } else{ $statement->bindValue(":effect_3", null); $statement->bindValue(":effect_3_value", 0); $statement->bindValue(":effect_3_enchant", 0); $statement->bindValue(":effect_3_grind", 0); } if (sizeof($artifact->{"sec_effects"}) > 3){ $statement->bindValue(":effect_4", $artifact->{"sec_effects"}[3][0]); $statement->bindValue(":effect_4_value", $artifact->{"sec_effects"}[3][1]); $statement->bindValue(":effect_4_enchant", $artifact->{"sec_effects"}[3][2]); $statement->bindValue(":effect_4_grind", $artifact->{"sec_effects"}[3][3]); } else{ $statement->bindValue(":effect_4", null); $statement->bindValue(":effect_4_value", 0); $statement->bindValue(":effect_4_enchant", 0); $statement->bindValue(":effect_4_grind", 0); } $statement->execute(); } /** * Parses a rune and saves it to the database. * * @param string $uid Owner ID. * @param JSON rune JSON fragment of the rune. */ function parse_rune($uid, $rune, $lock_list){ global $db; $statement = $db->prepare(" INSERT INTO rune ( uid, id, assigned_to, type, slot, stars, ancient, level, quality, original_quality, value, efficiency, max_efficiency, main_stat, main_stat_value, innate_stat, innate_stat_value, substat_1, substat_1_value, substat_1_enchant, substat_1_grind, substat_2, substat_2_value, substat_2_enchant, substat_2_grind, substat_3, substat_3_value, substat_3_enchant, substat_3_grind, substat_4, substat_4_value, substat_4_enchant, substat_4_grind, locked ) VALUES ( :uid, :id, :assigned_to, :type, :slot, :stars, :ancient, :level, :quality, :original_quality, :value, :efficiency, :max_efficiency, :main_stat, :main_stat_value, :innate_stat, :innate_stat_value, :substat_1, :substat_1_value, :substat_1_enchant, :substat_1_grind, :substat_2, :substat_2_value, :substat_2_enchant, :substat_2_grind, :substat_3, :substat_3_value, :substat_3_enchant, :substat_3_grind, :substat_4, :substat_4_value, :substat_4_enchant, :substat_4_grind, :locked ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":id", $rune->{"rune_id"}); if (intval($rune->{"occupied_id"}) == 0){ $statement->bindValue(":assigned_to", null); } else{ $statement->bindValue(":assigned_to", $rune->{"occupied_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(":original_quality", intval($rune->{"extra"}) - 10); } else{ // Non Ancient rune $statement->bindValue(":ancient", 0); $statement->bindValue(":stars", $rune->{"class"}); $statement->bindValue(":original_quality", $rune->{"extra"}); } $level = $rune->{"upgrade_curr"}; $statement->bindValue(":level", $level); $quality = QUALITY_ID::NORMAL; if ($level >= 12){ $quality = QUALITY_ID::LEGEND; } elseif ($level >= 9){ $quality = QUALITY_ID::HERO; } elseif ($level >= 6){ $quality = QUALITY_ID::RARE; } elseif ($level >= 3){ $quality = QUALITY_ID::MAGIC; } if ($rune->{"extra"} > $quality){ $quality = $rune->{"extra"}; } $statement->bindValue(":quality", $quality); $lock = 0; if (in_array($rune->{"rune_id"}, $lock_list)){ $lock = 1; } $statement->bindValue(":locked", $lock); $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]); $statement->bindValue(":substat_1_enchant", $rune->{"sec_eff"}[0][2]); $statement->bindValue(":substat_1_grind", $rune->{"sec_eff"}[0][3]); } else{ $statement->bindValue(":substat_1", null); $statement->bindValue(":substat_1_value", 0); $statement->bindValue(":substat_1_enchant", 0); $statement->bindValue(":substat_1_grind", 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]); $statement->bindValue(":substat_2_enchant", $rune->{"sec_eff"}[1][2]); $statement->bindValue(":substat_2_grind", $rune->{"sec_eff"}[1][3]); } else{ $statement->bindValue(":substat_2", null); $statement->bindValue(":substat_2_value", 0); $statement->bindValue(":substat_2_enchant", 0); $statement->bindValue(":substat_2_grind", 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]); $statement->bindValue(":substat_3_enchant", $rune->{"sec_eff"}[2][2]); $statement->bindValue(":substat_3_grind", $rune->{"sec_eff"}[2][3]); } else{ $statement->bindValue(":substat_3", null); $statement->bindValue(":substat_3_value", 0); $statement->bindValue(":substat_3_enchant", 0); $statement->bindValue(":substat_3_grind", 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]); $statement->bindValue(":substat_4_enchant", $rune->{"sec_eff"}[3][2]); $statement->bindValue(":substat_4_grind", $rune->{"sec_eff"}[3][3]); } else{ $statement->bindValue(":substat_4", null); $statement->bindValue(":substat_4_value", 0); $statement->bindValue(":substat_4_enchant", 0); $statement->bindValue(":substat_4_grind", 0); } $statement->execute(); } /** * Parses the response to the HubUserLogin command and updates profile. * * @param SQLiteConn $db Database connection. * @param int $uid Player UID. * @param JSON $json Response. */ function parse_profile($db, $uid, $json){ // Save data file $uname = $json->{"wizard_info"}->{"wizard_name"}; $dtime = (new DateTime())->format("Y-m-dTH:i:s"); $fname = ($_SERVER["DOCUMENT_ROOT"] . "/../data/profile_" . $uid . "_" . $uname . "_" . $dtime . ".json"); try{ file_put_contents($fname, json_encode($json)); } catch(Exception $e) { header("HTTP/1.1 500 Unable to save profile file: " . $e->getMessage()); syslog(LOG_INFO, "[APIv3] HTTP/1.1 500 Unable to save profile file: " . $e->getMessage()); return 500; } // Clear previous profile data $db->query("PRAGMA foreign_keys = OFF;"); $statement = $db->prepare("DELETE FROM unit_skill WHERE unit IN (SELECT id FROM unit WHERE uid = :uid);"); $statement->bindValue(":uid", $uid); $statement->execute(); $tables_to_clear = [ "scenario", "defense", "gw_defense", "unit", "rune", "artifact", "building", "decoration", "inventory", "enchantment" ]; foreach ($tables_to_clear as $table){ $statement = $db->prepare("DELETE FROM $table WHERE uid = :uid;"); $statement->bindValue(":uid", $uid); $statement->execute(); } $db->query("DELETE FROM summon_special"); # For every player, will be reloaded. // Get rune lock list $rune_lock_list = $json->{"rune_lock_list"}; // Update player data $statement = $db->prepare(" UPDATE player SET name = :name AND mana = :mana AND crystal = :crystal AND country = :country AND lang = :lang AND level = :level AND experience = :experience AND energy = :energy AND energy_max = :energy_max AND energy_per_min = :energy_per_min AND arena_energy = :arena_energy AND arena_energy_max = :arena_energy_max AND rep = :rep AND social_point = :social_point AND honor_point = :honor_point AND guild_point = :guild_point AND darkportal_energy = :darkportal_energy AND darkportal_energy_max = :darkportal_energy_max AND dimension_energy = :dimension_energy AND dimension_energy_max = :dimension_energy_max AND costume_point = :costume_point AND costume_point_max = :costume_point_max AND honor_medal = :honor_medal AND honor_mark = :honor_mark AND event_coin = :event_coin AND storage_slots = :storage_slots AND island = :island_upgrade WHERE uid = :uid; "); $statement->bindValue(":name", $json->{"wizard_info"}->{"wizard_name"}); $statement->bindValue(":mana", $json->{"wizard_info"}->{"wizard_mana"}); $statement->bindValue(":crystal", $json->{"wizard_info"}->{"wizard_crystal"}); $statement->bindValue(":country", $json->{"wizard_info"}->{"wizard_last_country"}); $statement->bindValue(":lang", $json->{"wizard_info"}->{"wizard_last_lang"}); $statement->bindValue(":level", $json->{"wizard_info"}->{"wizard_level"}); $statement->bindValue(":experience", $json->{"wizard_info"}->{"experience"}); $statement->bindValue(":energy", $json->{"wizard_info"}->{"wizard_energy"}); $statement->bindValue(":energy_max", $json->{"wizard_info"}->{"energy_max"}); $statement->bindValue(":energy_per_min", $json->{"wizard_info"}->{"energy_per_min"}); $statement->bindValue(":arena_energy", $json->{"wizard_info"}->{"arena_energy"}); $statement->bindValue(":arena_energy_max", $json->{"wizard_info"}->{"arena_energy_max"}); $statement->bindValue(":rep", $json->{"wizard_info"}->{"rep_unit_id"}); $statement->bindValue(":social_point", $json->{"wizard_info"}->{"social_point_current"}); $statement->bindValue(":honor_point", $json->{"wizard_info"}->{"honor_point"}); $statement->bindValue(":guild_point", $json->{"wizard_info"}->{"guild_point"}); $statement->bindValue(":darkportal_energy", $json->{"wizard_info"}->{"darkportal_energy"}); $statement->bindValue(":darkportal_energy_max", $json->{"wizard_info"}->{"darkportal_energy_max"}); $statement->bindValue(":dimension_energy", $json->{"dimension_hole_info"}->{"energy"}); $statement->bindValue(":dimension_energy_max", $json->{"dimension_hole_info"}->{"energy_max"}); $statement->bindValue(":costume_point", $json->{"wizard_info"}->{"costume_point"}); $statement->bindValue(":costume_point_max", $json->{"wizard_info"}->{"costume_point_max"}); $statement->bindValue(":honor_medal", $json->{"wizard_info"}->{"honor_medal"}); $statement->bindValue(":honor_mark", $json->{"wizard_info"}->{"honor_mark"}); $statement->bindValue(":event_coin", $json->{"wizard_info"}->{"event_coin"}); $statement->bindValue(":storage_slots", $json->{"unit_depository_slots"}->{"number"}); $island_upgrade = 0; foreach ($json->{"island_info"} as $island){ if (intval($island->{"id"}) > 100 && intval($island->{"open"}) == 1){ $island_upgrade = intval($island->{"id"}); } } $statement->bindValue(":island_upgrade", $island_upgrade); $statement->bindValue(":uid", $uid); $statement->execute(); // Update scenarion completion foreach ($json->{"scenario_list"} as $sce){ $statement = $db->prepare(" INSERT INTO scenario ( uid, region, difficulty, cleared, max_cleared ) VALUES ( :uid, :region, :difficulty, :cleared, :max_cleared ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":region", $sce->{"region_id"}); $statement->bindValue(":difficulty", $sce->{"difficulty"}); $statement->bindValue(":cleared", $sce->{"cleared"}); $max_cleared = 0; foreach ($sce->{"stage_list"} as $stage){ if (intval($stage->{"cleared"}) == 1){ $max_cleared = intval($stage->{"stage_no"}); } } $statement->bindValue(":max_cleared", $max_cleared); $statement->execute(); } // Parse Arena defense foreach ($json->{"defense_unit_list"} as $defense){ $statement = $db->prepare(" INSERT INTO defense ( uid, unit, position ) VALUES ( :uid, :unit, :position ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":unit", $defense->{"unit_id"}); $statement->bindValue(":position", $defense->{"pos_id"}); $statement->execute(); } // Parse GW defense if ($json->{"guildwar_defense_unit_list"}[0]){ $position = 1; foreach ($json->{"guildwar_defense_unit_list"}[0] as $defense){ $statement = $db->prepare(" INSERT INTO gw_defense ( uid, unit, position ) VALUES ( :uid, :unit, :position ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":unit", $defense->{"unit_id"}); $statement->bindValue(":position", $position); $statement->execute(); $position ++; } } if ($json->{"guildwar_defense_unit_list"}[1]){ $position = 4; foreach ($json->{"guildwar_defense_unit_list"}[1] as $defense){ $statement = $db->prepare(" INSERT INTO gw_defense ( uid, unit, position ) VALUES ( :uid, :unit, :position ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":unit", $defense->{"unit_id"}); $statement->bindValue(":position", $position); $statement->execute(); $position ++; } } // Parse buildings foreach($json->{"building_list"} as $building){ $statement = $db->prepare(" INSERT INTO building ( uid, id, building, gain ) VALUES ( :uid, :id, :building, :gain ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":id", $building->{"building_id"}); $statement->bindValue(":building", $building->{"building_master_id"}); $statement->bindValue(":gain", $building->{"gain_per_hour"}); $statement->execute(); } // Parse decorarion foreach($json->{"deco_list"} as $building){ $statement = $db->prepare(" INSERT INTO decoration ( uid, id, decoration, level ) VALUES ( :uid, :id, :decoration, :level ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":id", $building->{"deco_id"}); $statement->bindValue(":decoration", $building->{"master_id"}); $statement->bindValue(":level", $building->{"level"}); $statement->execute(); } // Parse units $lock_list = $json->{"unit_lock_list"}; foreach ($json->{"unit_list"} as $unit){ $statement = $db->prepare(" INSERT INTO unit ( uid, id, building, unit, level, stars, hp, attack, defense, speed, crit_rate, crit_damage, resistance, accuracy, experience, exp_gained, exp_gain_rate, costume, attribute, source, create_time, homunculus, homunculus_name, lock ) VALUES ( :uid, :id, :building, :unit, :level, :stars, :hp, :attack, :defense, :speed, :crit_rate, :crit_damage, :resistance, :accuracy, :experience, :exp_gained, :exp_gain_rate, :costume, :attribute, :source, :create_time, :homunculus, :homunculus_name, :lock ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":id", $unit->{"unit_id"}); $statement->bindValue(":building", $unit->{"building_id"}); $statement->bindValue(":unit", $unit->{"unit_master_id"}); $statement->bindValue(":level", $unit->{"unit_level"}); $statement->bindValue(":stars", $unit->{"class"}); $statement->bindValue(":hp", intval($unit->{"con"}) * 15); # Always x15 $statement->bindValue(":attack", $unit->{"atk"}); $statement->bindValue(":defense", $unit->{"def"}); $statement->bindValue(":speed", $unit->{"spd"}); $statement->bindValue(":crit_rate", $unit->{"critical_rate"}); $statement->bindValue(":crit_damage", $unit->{"critical_damage"}); $statement->bindValue(":resistance", $unit->{"resist"}); $statement->bindValue(":accuracy", $unit->{"accuracy"}); $statement->bindValue(":experience", $unit->{"experience"}); $statement->bindValue(":exp_gained", $unit->{"exp_gained"}); $statement->bindValue(":exp_gain_rate", $unit->{"exp_gain_rate"}); $statement->bindValue(":costume", $unit->{"costume_master_id"}); $statement->bindValue(":attribute", $unit->{"attribute"}); $statement->bindValue(":source", $unit->{"source"}); $statement->bindValue(":create_time", $unit->{"create_time"}); $statement->bindValue(":homunculus", $unit->{"homunculus"}); $statement->bindValue(":homunculus_name", $unit->{"homunculus_name"}); $lock = 0; if (in_array($unit->{"unit_id"}, $lock_list)){ $lock = 1; } $statement->bindValue(":lock", $lock); $statement->execute(); foreach ($unit->{"skills"} as $skill){ $statement = $db->prepare(" INSERT INTO unit_skill ( unit, skill, level ) VALUES ( :unit, :skill, :level ); "); $statement->bindValue(":unit", $unit->{"unit_id"}); $statement->bindValue(":skill", $skill[0]); $statement->bindValue(":level", $skill[1]); $statement->execute(); } } // Parse invetory foreach ($json->{"inventory_info"} as $item){ $statement = $db->prepare(" INSERT INTO inventory ( uid, id, type, amount ) VALUES ( :uid, :id, :type, :amount ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":id", $item->{"item_master_id"}); $statement->bindValue(":type", $item->{"item_master_type"}); $statement->bindValue(":amount", $item->{"item_quantity"}); $statement->execute(); } // Fix rune craft item type. $db->query("UPDATE inventory SET type = 27 WHERE type = 29 AND id IN (2001, 4001, 4002, 4003, 9001, 9002, 9003, 8001);"); // Parse Summon Stone summoning list // TODO: Set propper dates foreach ($json->{"summon_special_info"}->{"this"} as $unit){ $statement = $db->prepare(" INSERT INTO summon_special ( week, unit ) VALUES ( :week, :unit ); "); $statement->bindValue(":week", 0); $statement->bindValue(":unit", $unit); $statement->execute(); } foreach ($json->{"summon_special_info"}->{"next"} as $unit){ $statement = $db->prepare(" INSERT INTO summon_special ( week, unit ) VALUES ( :week, :unit ); "); $statement->bindValue(":week", 1); $statement->bindValue(":unit", $unit); $statement->execute(); } foreach ($json->{"summon_special_info"}->{"third"} as $unit){ $statement = $db->prepare(" INSERT INTO summon_special ( week, unit ) VALUES ( :week, :unit ); "); $statement->bindValue(":week", 2); $statement->bindValue(":unit", $unit); $statement->execute(); } foreach ($json->{"summon_special_info"}->{"fourth"} as $unit){ $statement = $db->prepare(" INSERT INTO summon_special ( week, unit ) VALUES ( :week, :unit ); "); $statement->bindValue(":week", 3); $statement->bindValue(":unit", $unit); $statement->execute(); } // Parse runes foreach ($json->{"runes"} as $rune){ parse_rune($uid, $rune, $rune_lock_list); } foreach ($json->{"unit_list"} as $unit){ foreach ($unit->{"runes"} as $rune){ parse_rune($uid, $rune, $rune_lock_list); } } // Parse artifacts foreach ($json->{"artifacts"} as $artifact){ parse_artifact($uid, $artifact); } foreach ($json->{"unit_list"} as $unit){ foreach ($unit->{"artifacts"} as $artifact){ parse_artifact($uid, $artifact); } } // Parse enchantments foreach ($json->{"rune_craft_item_list"} as $item){ $statement = $db->prepare(" INSERT INTO enchantment ( uid, id, type, quality, rune, stat, value, amount ) VALUES ( :uid, :id, :type, :quality, :rune, :stat, :value, :amount ); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":id", $item->{"craft_item_id"}); $statement->bindValue(":type", $item->{"craft_type"}); $statement->bindValue(":value", $item->{"sell_value"}); $statement->bindValue(":amount", $item->{"amount"}); $info = str_pad(intval($item->{"craft_type_id"}), 6, "0", STR_PAD_LEFT); /* info : 5 or 6 digit number: RRSSQQ RR: Rune (may not be padded) SS: Stat QQ: Quality */ $statement->bindValue(":quality", intval(substr($info, 4, 2))); $statement->bindValue(":stat", intval(substr($info, 2, 2))); $statement->bindValue(":rune", intval(substr($info, 0, 2))); $statement->execute(); } // Parse guild if ($json->{"guild"}->{"guild_info"} != null){ $guild = $json->{"guild"}->{"guild_info"}; $statement = $db->prepare("DELETE FROM guild WHERE id = :id;"); $statement->bindValue(":id", $guild->{"guild_id"}); $statement->execute(); $statement = $db->prepare("DELETE FROM guild_member WHERE guild = :guild;"); $statement->bindValue(":guild", $guild->{"guild_id"}); $statement->execute(); $statement = $db->prepare(" INSERT INTO guild ( id, name, level, experience, recruiting, members, leader, comment, notice ) VALUES ( :id, :name, :level, :experience, :recruiting, :members, :leader, :comment, :notice ); "); $statement->bindValue(":id",$guild->{"guild_id"}); $statement->bindValue(":name",$guild->{"name"}); $statement->bindValue(":level",$guild->{"level"}); $statement->bindValue(":experience",$guild->{"experience"}); $statement->bindValue(":recruiting",$guild->{"recruit_status"}); $statement->bindValue(":members",$guild->{"member_now"}); $statement->bindValue(":leader",$guild->{"master_wizard_id"}); $statement->bindValue(":comment",$guild->{"comment"}); $statement->bindValue(":notice",$guild->{"notice"}); $statement->execute(); foreach ($json->{"guild"}->{"guild_members"} as $member){ $statement = $db->prepare(" INSERT INTO guild_member ( id, guild, grade, name, level, rating, arena_score, joined, last_login, in_war, has_defense ) VALUES ( :id, :guild, :grade, :name, :level, :rating, :arena_score, :joined, :last_login, :in_war, :has_defense ); "); $statement->bindValue(":id", $member->{"wizard_id"}); $statement->bindValue(":guild", $member->{"guild_id"}); $statement->bindValue(":grade", $member->{"grade"}); $statement->bindValue(":name", $member->{"wizard_name"}); $statement->bindValue(":level", $member->{"wizard_level"}); $statement->bindValue(":rating", $member->{"rating_id"}); $statement->bindValue(":arena_score", $member->{"arena_score"}); $statement->bindValue(":joined", $member->{"join_timestamp"}); $statement->bindValue(":last_login", $member->{"last_login_timestamp"}); $in_war = 0; foreach ($json->{"guildwar_member_list"} as $war){ if ($war->{"wizard_id"} == $member->{"wizard_id"}){ $in_war = 1; break; } } $has_defense = 0; foreach ($json->{"guild_member_defense_list"} as $defense){ if ($defense->{"wizard_id"} == $member->{"wizard_id"}){ $has_defense = 1; break; } } $statement->bindValue(":in_war", $in_war); $statement->bindValue(":has_defense", $has_defense); $statement->execute(); } } // Update invalid teams $statement = $db->prepare(" DELETE FROM team WHERE id IN ( SELECT DISTINCT team FROM team_unit WHERE unit NOT IN ( SELECT DISTINCT id FROM unit WHERE uid = :uid ) ) AND uid = :uid "); $statement->bindValue(":uid", $uid); $statement->execute(); $db->query(" DELETE FROM team_unit WHERE unit NOT IN ( SELECT DISTINCT id FROM unit ) OR team NOT IN ( SELECT DISTINCT id FROM team );" ); // Return success return "201 Created."; } /** * Parses the response to the GetUnitCollection command and updates the * user monster collection status. * * @param SQLiteConn $db Database connection. * @param int $uid Player UID. * @param JSON $json Response. */ function parse_collection($db, $uid, $json){ // Clear previous collection data $db->query("PRAGMA foreign_keys = OFF;"); $statement = $db->prepare("DELETE FROM collection WHERE uid = :uid;"); $statement->bindValue(":uid", $uid); $statement->execute(); foreach ($json->{"collection"} as $unit){ $statement = $db->prepare(" INSERT INTO collection (uid, unit, open) VALUES (:uid, :unit, :open); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":unit", $unit->{"unit_master_id"}); $statement->bindValue(":open", $unit->{"open"}); $statement->execute(); } // Return success return "201 Created."; } /** * Parses the response to the GetLobbyWizardLog command and updates the * user records. * * @param SQLiteConn $db Database connection. * @param int $uid Player UID. * @param JSON $json Response. */ function parse_records($db, $uid, $json){ # Page 1: Cairos non-elemental, rift raid, rift dungeon. if (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 1){ // Update data in player table $db->query("PRAGMA foreign_keys = OFF;"); $statement = $db->prepare(" UPDATE player SET joined = :joined, top_rank_arena = :top_rank_arena, top_rank_world_arena = :top_rank_world_arena, top_rank_special_league = :top_rank_special_league, top_rank_gw = :top_rank_gw, top_rank_siege = :top_rank_siege, top_rank_wboss = :top_rank_wboss, top_rank_toan = :top_rank_toan, top_rank_toah = :top_rank_toah WHERE uid = :uid; "); $statement->bindValue(":joined", $json->{"lobby_wizard_log"}->{"account_create_timestamp"}); $statement->bindValue(":top_rank_arena", $json->{"lobby_wizard_log"}->{"pvp_best_rating_id"}); $statement->bindValue(":top_rank_world_arena", $json->{"lobby_wizard_log"}->{"rtpvp_rank_best_rating_id"}); $statement->bindValue(":top_rank_special_league", $json->{"lobby_wizard_log"}->{"rtpvp_contest_best_rating_id"}); $statement->bindValue(":top_rank_gw", $json->{"lobby_wizard_log"}->{"guildwar_best_rating_id"}); $statement->bindValue(":top_rank_siege", $json->{"lobby_wizard_log"}->{"guildsiege_best_rating_id"}); $statement->bindValue(":top_rank_wboss", $json->{"lobby_wizard_log"}->{"world_boss_best_rank_id"}); $statement->bindValue(":top_rank_toan", $json->{"lobby_wizard_log"}->{"trial_tower_normal_best_floor"}); $statement->bindValue(":top_rank_toah", $json->{"lobby_wizard_log"}->{"trial_tower_hard_best_floor"}); $statement->bindValue(":uid", $uid); $statement->execute(); // Loop Cairos records foreach ($json->{"lobby_wizard_log"}->{"dungeon_best_clear_info_list"} as $record){ // Delete prevous data $statement = $db->prepare(" DELETE FROM record_party WHERE uid = :uid AND area_type = :area_type AND area = :area; "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->execute(); $statement = $db->prepare(" DELETE FROM record WHERE uid = :uid AND area_type = :area_type AND area = :area; "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->execute(); // Insert new data $statement = $db->prepare(" INSERT INTO record (uid, area_type, area, stage, time, score, rank) VALUES (:uid, :area_type, :area, :stage, :time, :score, :rank); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->bindValue(":stage", $record->{"stage_id"}); $statement->bindValue(":time", $record->{"clear_time"}); $statement->bindValue(":score", 0); $statement->bindValue(":rank", null); $statement->execute(); // Loop party foreach ($record->{"my_unit_deck_list"} as $party){ $statement = $db->prepare(" INSERT INTO record_party (uid, area_type, area, unit, k_unit, leader, front) VALUES (:uid, :area_type, :area, :unit, :k_unit, :leader, :front); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->bindValue(":unit", $party->{"unit_id"}); $statement->bindValue(":k_unit", $party->{"unit_master_id"}); $statement->bindValue(":leader", $party->{"leader"}); $statement->bindValue(":front", 0); $statement->execute(); } } // Rift Raid record if (sizeof($json->{"lobby_wizard_log"}->{"raid_best_clear_info_list"}) > 0){ $record = $json->{"lobby_wizard_log"}->{"raid_best_clear_info_list"}[0]; // Delete prevous data $statement = $db->prepare(" DELETE FROM record_party WHERE uid = :uid AND area_type = :area_type AND area = :area; "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID); $statement->bindValue(":area", $record->{"stage_id"}); $statement->execute(); $statement = $db->prepare(" DELETE FROM record WHERE uid = :uid AND area_type = :area_type AND area = :area; "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID); $statement->bindValue(":area", $record->{"stage_id"}); $statement->execute(); // Insert new data $statement = $db->prepare(" INSERT INTO record (uid, area_type, area, stage, time, score, rank) VALUES (:uid, :area_type, :area, :stage, :time, :score, :rank); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID); $statement->bindValue(":area", $record->{"stage_id"}); $statement->bindValue(":stage", $record->{"stage_id"}); $statement->bindValue(":time", $record->{"clear_time"}); $statement->bindValue(":score", 0); $statement->bindValue(":rank", null); $statement->execute(); // Loop party foreach ($record->{"my_unit_deck_list"} as $party){ $statement = $db->prepare(" INSERT INTO record_party (uid, area_type, area, unit, k_unit, leader, front) VALUES (:uid, :area_type, :area, :unit, :k_unit, :leader, :front); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_RAID); $statement->bindValue(":area", $record->{"stage_id"}); $statement->bindValue(":unit", $party->{"unit_id"}); $statement->bindValue(":k_unit", $party->{"unit_master_id"}); $statement->bindValue(":leader", $party->{"leader"}); if ($party->{"slot_index"} <= 4){ $statement->bindValue(":front", 1); } else{ $statement->bindValue(":front", 0); } $statement->execute(); } } // Loop Rift Dungeon records foreach ( $json->{"lobby_wizard_log"}->{"rift_dungeon_best_clear_info_list"} as $record){ // Delete prevous data $statement = $db->prepare(" DELETE FROM record_party WHERE uid = :uid AND area_type = :area_type AND area = :area; "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON); $statement->bindValue(":area", $record->{"rift_dungeon_id"}); $statement->execute(); $statement = $db->prepare(" DELETE FROM record WHERE uid = :uid AND area_type = :area_type AND area = :area; "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON); $statement->bindValue(":area", $record->{"rift_dungeon_id"}); $statement->execute(); // Insert new data $statement = $db->prepare(" INSERT INTO record (uid, area_type, area, stage, time, score, rank) VALUES (:uid, :area_type, :area, :stage, :time, :score, :rank); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON); $statement->bindValue(":area", $record->{"rift_dungeon_id"}); $statement->bindValue(":stage", 0); $statement->bindValue(":time", 0); $statement->bindValue(":score", $record->{"clear_damage"}); switch (intval($record->{"clear_rating"})){ case 2: $statement->bindValue(":rank", "D"); break; case 3: $statement->bindValue(":rank", "C"); break; case 4: $statement->bindValue(":rank", "B-"); break; case 5: $statement->bindValue(":rank", "B"); break; case 6: $statement->bindValue(":rank", "B+"); break; case 7: $statement->bindValue(":rank", "A-"); break; case 8: $statement->bindValue(":rank", "A"); break; case 9: $statement->bindValue(":rank", "A+"); break; case 10: $statement->bindValue(":rank", "S"); break; case 11: $statement->bindValue(":rank", "SS"); break; case 12: $statement->bindValue(":rank", "SS"); break; default: $statement->bindValue(":rank", "F"); } $statement->execute(); // Loop party foreach ($record->{"my_unit_deck_list"} as $party){ $statement = $db->prepare(" INSERT INTO record_party (uid, area_type, area, unit, k_unit, leader, front) VALUES (:uid, :area_type, :area, :unit, :k_unit, :leader, :front); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::RIFT_DUNGEON); $statement->bindValue(":area", $record->{"rift_dungeon_id"}); $statement->bindValue(":unit", $party->{"unit_id"}); $statement->bindValue(":k_unit", $party->{"unit_master_id"}); $statement->bindValue(":leader", $party->{"leader"}); if ($party->{"slot_index"} <= 4){ $statement->bindValue(":front", 1); } else{ $statement->bindValue(":front", 0); } $statement->execute(); } } } # Page 2: Cairos non-elemental, rift raid, rift dungeon. elseif (intval($json->{"lobby_wizard_log"}->{"page_no"} == 2)){ // Loop Cairos records foreach ($json->{"lobby_wizard_log"}->{"dungeon_best_clear_info_list"} as $record){ // Delete prevous data $statement = $db->prepare(" DELETE FROM record_party WHERE uid = :uid AND area_type = :area_type AND area = :area; "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->execute(); $statement = $db->prepare(" DELETE FROM record WHERE uid = :uid AND area_type = :area_type AND area = :area; "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->execute(); // Insert new data $statement = $db->prepare(" INSERT INTO record (uid, area_type, area, stage, time, score, rank) VALUES (:uid, :area_type, :area, :stage, :time, :score, :rank); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->bindValue(":stage", $record->{"stage_id"}); $statement->bindValue(":time", $record->{"clear_time"}); $statement->bindValue(":score", 0); $statement->bindValue(":rank", null); $statement->execute(); // Loop party foreach ($record->{"my_unit_deck_list"} as $party){ $statement = $db->prepare(" INSERT INTO record_party (uid, area_type, area, unit, k_unit, leader, front) VALUES (:uid, :area_type, :area, :unit, :k_unit, :leader, :front); "); $statement->bindValue(":uid", $uid); $statement->bindValue(":area_type", AREA_TYPE_ID::CAIROS_DUNGEON); $statement->bindValue(":area", $record->{"dungeon_id"}); $statement->bindValue(":unit", $party->{"unit_id"}); $statement->bindValue(":k_unit", $party->{"unit_master_id"}); $statement->bindValue(":leader", $party->{"leader"}); $statement->bindValue(":front", 0); $statement->execute(); } } } elseif (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 3){ return "204 Page not logged (only pages 1 and 2 are needed)."; } elseif (intval($json->{"lobby_wizard_log"}->{"page_no"}) == 4){ return "204 Page not logged (only pages 1 and 2 are needed)."; } else{ return "400 Unknown page."; } // Return success return "201 Created."; } try{ header("Content-type: application/json; charset=utf-8"); // Get put data parse_str(file_get_contents("php://input"), $_PUT); // Check API key. $key = $_PUT["key"]; if ($key == null || $key == false){ header("HTTP/1.1 400 API key not received."); syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 API key not received."); return 400; } // Check data $data = $_PUT["response"]; if ($data == null || $data == false){ header("HTTP/1.1 400 No data received."); syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 No data received."); return 400; } // Check data format. $json = json_decode($data); if ($json === null){ header("HTTP/1.1 400 Data is no valid JSON."); syslog(LOG_INFO, "[APIv3] HTTP/1.1 400 Data is no valid JSON."); return 400; } // Get and validate command $command = $json->{"command"}; $accepted_commands = [ "HubUserLogin", // Login, full profile "GetUnitCollection", // Unit collection "GetLobbyWizardLog" // Records ]; 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; } // Get user info and Authenticate switch ($command){ case "HubUserLogin": $uid = $json->{"wizard_id"}; $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; } break; case "GetUnitCollection": // No identification in this JSON, get uid from databases $statement = $db->prepare("SELECT uid FROM player WHERE api_key = :api_key;"); $statement->bindValue(":api_key", $key); $r = $statement->execute()->fetchArray(SQLITE3_ASSOC); if ($r){ $uid = $r["uid"]; } else{ header("HTTP/1.1 401 Invalid credentials."); syslog(LOG_INFO, "[APIv3] HTTP/1.1 401 Invalid credentials."); return 401; } break; case "GetLobbyWizardLog": $uid = $json->{"lobby_wizard_log"}->{"wizard_id"}; $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; } break; } if ($command == "GetUnitCollection"){ // This command doesnt provide wizard_id $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; } } // Run parser $result = "500 Unknown Error"; switch($command){ case "HubUserLogin": $result = parse_profile($db, $uid, $json); break; case "GetUnitCollection": $result = parse_collection($db, $uid, $json); break; case "GetLobbyWizardLog": $result = parse_records($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_INFO, "[APIv3] HTTP/1.1 500 Unexpected error: " . $e->getMessage()); return 500; } ?>