From 213ffefdb68c58b9c99e687a479c586d5a687bec Mon Sep 17 00:00:00 2001 From: Yimura Date: Mon, 20 Sep 2021 01:11:01 +0200 Subject: [PATCH] feat(SavedProfiles): Prepping for saving handling profiles --- BigBaseV2/src/api/api.hpp | 19 ++++++- .../handling/handling_saved_profiles.cpp | 55 +++++++++++++++++++ .../gui/window/handling/handling_search.cpp | 4 ++ .../src/gui/window/handling/handling_tabs.hpp | 1 + BigBaseV2/src/services/vehicle_service.cpp | 22 ++++++++ BigBaseV2/src/services/vehicle_service.hpp | 3 +- 6 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 BigBaseV2/src/gui/window/handling/handling_saved_profiles.cpp diff --git a/BigBaseV2/src/api/api.hpp b/BigBaseV2/src/api/api.hpp index a427793b..5d6b798f 100644 --- a/BigBaseV2/src/api/api.hpp +++ b/BigBaseV2/src/api/api.hpp @@ -3,7 +3,8 @@ 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"; inline std::string session_id; namespace util @@ -140,6 +141,22 @@ namespace big::api return util::parse_body(res, out); } + static bool save_profile(std::string share_code) + { + if (!util::signed_in()) return false; + + const std::string path = "/vehicle/handling/save_profile"; + + http::Request request(domain + path); + + nlohmann::json body = { { "share_code", share_code } }; + + http::Response res = request.send("POST", body.dump(), { + util::authorization_header() + }); + return util::parse_body(res, body); + } + static bool update(uint32_t handling_hash, const char* name, const char* description, std::string share_code, nlohmann::json &update) { 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 new file mode 100644 index 00000000..0540af83 --- /dev/null +++ b/BigBaseV2/src/gui/window/handling/handling_saved_profiles.cpp @@ -0,0 +1,55 @@ +#include "handling_tabs.hpp" + +namespace big +{ + void tab_handling::tab_saved_profiles() + { + if (ImGui::BeginTabItem("Saved Profiles")) + { + if (!g_vehicle_service->load_saved_profiles()) + ImGui::Text("Loading profiles..."); + else + { + if (g_vehicle_service->m_my_profiles.size() == 0) + ImGui::Text("You have no saved profiles available for this vehicle."); + for (auto& key : g_vehicle_service->m_my_profiles) + { + if (auto it = g_vehicle_service->m_handling_profiles.find(key); it != g_vehicle_service->m_handling_profiles.end()) + { + auto& profile = it->second; + + if (profile.share_code == g_vehicle_service->get_active_profile(profile.handling_hash)) + ImGui::TextColored({ 0.1254f,0.8039f,0.3137f,1.f }, "Active"); + + ImGui::BeginTable("table", 3, ImGuiTableFlags_SizingStretchProp); + + ImGui::TableNextRow(); + + ImGui::TableNextColumn(); + ImGui::Text("Name:"); + ImGui::TableNextColumn(); + ImGui::Text(profile.name.c_str()); + ImGui::TableNextColumn(); + ImGui::Text("Share Code: %s", profile.share_code.c_str()); + + ImGui::TableNextRow(); + + ImGui::TableNextColumn(); + ImGui::Text("Description:"); + ImGui::TableNextColumn(); + ImGui::TextWrapped(profile.description.c_str()); + ImGui::TableNextColumn(); + if (ImGui::Button("Load Profile")) + g_vehicle_service->set_handling_profile(profile); + + ImGui::EndTable(); + + ImGui::Separator(); + } + } + } + + ImGui::EndTabItem(); + } + } +} \ No newline at end of file diff --git a/BigBaseV2/src/gui/window/handling/handling_search.cpp b/BigBaseV2/src/gui/window/handling/handling_search.cpp index f10da6ab..e25d6993 100644 --- a/BigBaseV2/src/gui/window/handling/handling_search.cpp +++ b/BigBaseV2/src/gui/window/handling/handling_search.cpp @@ -1,3 +1,4 @@ +#include "api/api.hpp" #include "fiber_pool.hpp" #include "handling_tabs.hpp" #include "natives.hpp" @@ -63,6 +64,9 @@ namespace big ImGui::TableNextColumn(); if (ImGui::Button("Load Profile")) 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); }); ImGui::EndTable(); } diff --git a/BigBaseV2/src/gui/window/handling/handling_tabs.hpp b/BigBaseV2/src/gui/window/handling/handling_tabs.hpp index fcd9e424..b4cf9636 100644 --- a/BigBaseV2/src/gui/window/handling/handling_tabs.hpp +++ b/BigBaseV2/src/gui/window/handling/handling_tabs.hpp @@ -8,6 +8,7 @@ namespace big public: static void tab_current_profile(); static void tab_my_profiles(); + static void tab_saved_profiles(); static void tab_search(); }; } \ No newline at end of file diff --git a/BigBaseV2/src/services/vehicle_service.cpp b/BigBaseV2/src/services/vehicle_service.cpp index ce80a49d..d6647273 100644 --- a/BigBaseV2/src/services/vehicle_service.cpp +++ b/BigBaseV2/src/services/vehicle_service.cpp @@ -155,6 +155,28 @@ namespace big return true; } + bool vehicle_service::load_saved_profiles(bool force_update) + { + static bool busy = false, up_to_date = false; + + if (busy) return false; + + if (!safe_to_modify()) + return false; + + if (!force_update && up_to_date) return true; + + busy = true; + + g_thread_pool->push([]() + { + //api::vehicle::handling::save_profile() + + busy = false; + up_to_date = true; + }); + } + bool vehicle_service::publish_profile(const char* name, const char* description, std::string share_code) { if (this->m_publish_status == PublishStatus::SAVED) diff --git a/BigBaseV2/src/services/vehicle_service.hpp b/BigBaseV2/src/services/vehicle_service.hpp index c1a98ebc..a84e1156 100644 --- a/BigBaseV2/src/services/vehicle_service.hpp +++ b/BigBaseV2/src/services/vehicle_service.hpp @@ -123,16 +123,17 @@ namespace big std::string get_active_profile(std::uint32_t hash); bool get_by_share_code(const char* share_code); bool handling_data_to_json(CHandlingData& handling_data, nlohmann::json& out); + bool load_saved_profiles(bool force_update = false); bool publish_profile(const char* name, const char* description, std::string share_code = ""); PublishStatus publish_status(PublishStatus new_status = PublishStatus::NONE); bool restore_vehicle(); void set_active_profile(std::uint32_t hash, std::string share_code); void set_handling_profile(HandlingProfile& profile); - bool update_mine(bool force_update = false); inline static std::unordered_map m_active_profiles; inline static std::vector m_my_profiles; + inline static std::vector m_saved_profiles; inline static std::unordered_map m_handling_profiles; SearchStatus m_search_status = SearchStatus::IDLE;