feat(Handling): Added saved profiles tab

This commit is contained in:
Yimura 2021-09-21 01:44:14 +02:00
parent b86cbac0ad
commit a7db15c267
No known key found for this signature in database
GPG Key ID: 3D8FF4397E768682
5 changed files with 63 additions and 16 deletions

View File

@ -3,8 +3,8 @@
namespace big::api namespace big::api
{ {
//const std::string domain = "http://home.damon.sh:8089/api/v1"; const std::string domain = "http://home.damon.sh:8089/api/v1";
const std::string domain = "http://localhost:8080/api/v1"; //const std::string domain = "http://localhost:8080/api/v1";
inline std::string session_id; inline std::string session_id;
namespace util namespace util
@ -141,6 +141,21 @@ namespace big::api
return util::parse_body(res, out); return util::parse_body(res, out);
} }
static bool get_saved_handling(uint32_t handling_hash, nlohmann::json& out)
{
if (!util::signed_in()) return false;
const std::string path = "/vehicle/handling/get_saved?handling_hash=";
http::Request request(domain + path + std::to_string(handling_hash));
http::Response res = request.send("GET", "", {
util::authorization_header()
});
return util::parse_body(res, out);
}
static bool save_profile(std::string share_code) static bool save_profile(std::string share_code)
{ {
if (!util::signed_in()) return false; if (!util::signed_in()) return false;

View File

@ -10,9 +10,9 @@ namespace big
ImGui::Text("Loading profiles..."); ImGui::Text("Loading profiles...");
else else
{ {
if (g_vehicle_service->m_my_profiles.size() == 0) if (g_vehicle_service->m_saved_profiles.size() == 0)
ImGui::Text("You have no saved profiles available for this vehicle."); ImGui::Text("You have no saved profiles available for this vehicle.");
for (auto& key : g_vehicle_service->m_my_profiles) for (auto& key : g_vehicle_service->m_saved_profiles)
{ {
if (auto it = g_vehicle_service->m_handling_profiles.find(key); it != g_vehicle_service->m_handling_profiles.end()) if (auto it = g_vehicle_service->m_handling_profiles.find(key); it != g_vehicle_service->m_handling_profiles.end())
{ {

View File

@ -66,7 +66,14 @@ namespace big
g_vehicle_service->set_handling_profile(profile); g_vehicle_service->set_handling_profile(profile);
ImGui::SameLine(); ImGui::SameLine();
if (ImGui::Button("Save Profile")) if (ImGui::Button("Save Profile"))
g_thread_pool->push([&] { api::vehicle::handling::save_profile(profile.share_code); }); {
g_thread_pool->push([&]
{
api::vehicle::handling::save_profile(profile.share_code);
g_vehicle_service->load_saved_profiles(true);
});
}
ImGui::EndTable(); ImGui::EndTable();
} }

View File

@ -22,6 +22,7 @@ namespace big
ImGui::BeginTabBar("handling_profiles"); ImGui::BeginTabBar("handling_profiles");
tab_handling::tab_current_profile(); tab_handling::tab_current_profile();
tab_handling::tab_my_profiles(); tab_handling::tab_my_profiles();
tab_handling::tab_saved_profiles();
tab_handling::tab_search(); tab_handling::tab_search();
ImGui::EndTabBar(); ImGui::EndTabBar();

View File

@ -157,24 +157,48 @@ namespace big
bool vehicle_service::load_saved_profiles(bool force_update) bool vehicle_service::load_saved_profiles(bool force_update)
{ {
static bool busy = false, up_to_date = false; static bool busy = false;
static uint32_t up_to_date = -1;
if (busy) return false; if (busy)
return false;
if (!safe_to_modify()) if (!safe_to_modify())
return false; return false;
if (!force_update && up_to_date) return true; if (!force_update && up_to_date == g_local_player->m_vehicle->m_handling->m_model_hash)
return true;
busy = true; busy = true;
g_thread_pool->push([]() g_thread_pool->push([&]()
{ {
//api::vehicle::handling::save_profile() nlohmann::json json;
if (!api::vehicle::handling::get_saved_handling(g_local_player->m_vehicle->m_handling->m_model_hash, json) || json == nullptr)
{
busy = false;
return;
}
this->m_saved_profiles.clear();
for (auto& el : json["data"])
{
LOG(INFO) << "Registered profile '" << el["name"].get<std::string>().c_str() << "' with share code " << el["share_code"].get<std::string>().c_str();
HandlingProfile profile = HandlingProfile(el, g_local_player->m_vehicle->m_handling);
if (auto it = this->m_handling_profiles.find(el["share_code"]); it != this->m_handling_profiles.end())
it->second = profile;
else this->m_handling_profiles.emplace(el["share_code"], profile);
this->m_saved_profiles.push_back(el["share_code"]);
}
busy = false; busy = false;
up_to_date = true; up_to_date = g_local_player->m_vehicle->m_handling->m_model_hash;
}); });
return false;
} }
bool vehicle_service::publish_profile(const char* name, const char* description, std::string share_code) bool vehicle_service::publish_profile(const char* name, const char* description, std::string share_code)
@ -272,17 +296,17 @@ namespace big
return; return;
} }
m_my_profiles.clear(); this->m_my_profiles.clear();
for (auto& el : json["data"]) for (auto& el : json["data"])
{ {
LOG(INFO) << "Registered profile '" << el["name"].get<std::string>().c_str() << "' with share code " << el["share_code"].get<std::string>().c_str(); LOG(INFO) << "Registered profile '" << el["name"].get<std::string>().c_str() << "' with share code " << el["share_code"].get<std::string>().c_str();
HandlingProfile profile = HandlingProfile(el, g_local_player->m_vehicle->m_handling); HandlingProfile profile = HandlingProfile(el, g_local_player->m_vehicle->m_handling);
if (auto it = m_handling_profiles.find(el["share_code"]); it != m_handling_profiles.end()) if (auto it = this->m_handling_profiles.find(el["share_code"]); it != this->m_handling_profiles.end())
it->second = profile; it->second = profile;
else m_handling_profiles.emplace(el["share_code"], profile); else this->m_handling_profiles.emplace(el["share_code"], profile);
m_my_profiles.push_back(el["share_code"]); this->m_my_profiles.push_back(el["share_code"]);
} }
busy = false; busy = false;