From a7db15c267a4390e9bfb8afb0544ac1f3cdb589c Mon Sep 17 00:00:00 2001 From: Yimura Date: Tue, 21 Sep 2021 01:44:14 +0200 Subject: [PATCH] feat(Handling): Added saved profiles tab --- BigBaseV2/src/api/api.hpp | 19 +++++++- .../handling/handling_saved_profiles.cpp | 4 +- .../gui/window/handling/handling_search.cpp | 9 +++- BigBaseV2/src/gui/window/window_handling.cpp | 1 + BigBaseV2/src/services/vehicle_service.cpp | 46 ++++++++++++++----- 5 files changed, 63 insertions(+), 16 deletions(-) diff --git a/BigBaseV2/src/api/api.hpp b/BigBaseV2/src/api/api.hpp index 5d6b798f..dca8c301 100644 --- a/BigBaseV2/src/api/api.hpp +++ b/BigBaseV2/src/api/api.hpp @@ -3,8 +3,8 @@ namespace big::api { - //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://home.damon.sh:8089/api/v1"; + //const std::string domain = "http://localhost:8080/api/v1"; inline std::string session_id; namespace util @@ -141,6 +141,21 @@ namespace big::api 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) { if (!util::signed_in()) return false; diff --git a/BigBaseV2/src/gui/window/handling/handling_saved_profiles.cpp b/BigBaseV2/src/gui/window/handling/handling_saved_profiles.cpp index 0540af83..8ea7b75b 100644 --- a/BigBaseV2/src/gui/window/handling/handling_saved_profiles.cpp +++ b/BigBaseV2/src/gui/window/handling/handling_saved_profiles.cpp @@ -10,9 +10,9 @@ namespace big ImGui::Text("Loading profiles..."); 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."); - 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()) { diff --git a/BigBaseV2/src/gui/window/handling/handling_search.cpp b/BigBaseV2/src/gui/window/handling/handling_search.cpp index e25d6993..1a7030a7 100644 --- a/BigBaseV2/src/gui/window/handling/handling_search.cpp +++ b/BigBaseV2/src/gui/window/handling/handling_search.cpp @@ -66,7 +66,14 @@ namespace big g_vehicle_service->set_handling_profile(profile); ImGui::SameLine(); 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(); } diff --git a/BigBaseV2/src/gui/window/window_handling.cpp b/BigBaseV2/src/gui/window/window_handling.cpp index 3dadf6e5..528a5073 100644 --- a/BigBaseV2/src/gui/window/window_handling.cpp +++ b/BigBaseV2/src/gui/window/window_handling.cpp @@ -22,6 +22,7 @@ namespace big ImGui::BeginTabBar("handling_profiles"); tab_handling::tab_current_profile(); tab_handling::tab_my_profiles(); + tab_handling::tab_saved_profiles(); tab_handling::tab_search(); ImGui::EndTabBar(); diff --git a/BigBaseV2/src/services/vehicle_service.cpp b/BigBaseV2/src/services/vehicle_service.cpp index d6647273..883fcb93 100644 --- a/BigBaseV2/src/services/vehicle_service.cpp +++ b/BigBaseV2/src/services/vehicle_service.cpp @@ -157,24 +157,48 @@ namespace big 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()) 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; - 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; - busy = false; - up_to_date = true; + return; + } + + this->m_saved_profiles.clear(); + for (auto& el : json["data"]) + { + LOG(INFO) << "Registered profile '" << el["name"].get().c_str() << "' with share code " << el["share_code"].get().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; + 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) @@ -272,17 +296,17 @@ namespace big return; } - m_my_profiles.clear(); + this->m_my_profiles.clear(); for (auto& el : json["data"]) { LOG(INFO) << "Registered profile '" << el["name"].get().c_str() << "' with share code " << el["share_code"].get().c_str(); 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; - else m_handling_profiles.emplace(el["share_code"], profile); - m_my_profiles.push_back(el["share_code"]); + else this->m_handling_profiles.emplace(el["share_code"], profile); + this->m_my_profiles.push_back(el["share_code"]); } busy = false;