feat(VehicleService): Update existing profile

This commit is contained in:
Yimura 2021-09-18 22:11:29 +02:00
parent 0106d8a02e
commit ca3618d9ca
No known key found for this signature in database
GPG Key ID: 3D8FF4397E768682
2 changed files with 45 additions and 21 deletions

View File

@ -1,9 +1,14 @@
#include "api/api.hpp" #include "api/api.hpp"
#include "fiber_pool.hpp" #include "thread_pool.hpp"
#include "vehicle_service.hpp" #include "vehicle_service.hpp"
namespace big namespace big
{ {
bool safe_to_modify()
{
return !(g_local_player == nullptr || g_local_player->m_vehicle == nullptr);
}
vehicle_service::vehicle_service() vehicle_service::vehicle_service()
{ {
g_vehicle_service = this; g_vehicle_service = this;
@ -53,7 +58,7 @@ namespace big
if (m_search_status == SearchStatus::SEARCHING) if (m_search_status == SearchStatus::SEARCHING)
return false; return false;
if (g_local_player == nullptr || g_local_player->m_vehicle == nullptr) if (!safe_to_modify())
return false; return false;
if (up_to_date == share_code) if (up_to_date == share_code)
@ -84,21 +89,9 @@ namespace big
return true; return true;
} }
bool vehicle_service::publish_profile(const char* name, const char* description) bool vehicle_service::handling_data_to_json(CHandlingData& handling_data, nlohmann::json& out)
{ {
if (this->m_publish_status == PublishStatus::SAVED) out = {
return true;
if (this->m_publish_status == PublishStatus::SAVING)
return false;
if (g_local_player == nullptr || g_local_player->m_vehicle == nullptr) return false;
this->m_publish_status = PublishStatus::SAVING;
CHandlingData handling_data = *g_local_player->m_vehicle->m_handling;
uint32_t hash = handling_data.m_model_hash;
nlohmann::json data = {
{ "centre_of_mass", { "centre_of_mass",
{ {
{ "x", handling_data.m_centre_of_mass.x }, { "x", handling_data.m_centre_of_mass.x },
@ -159,8 +152,35 @@ namespace big
{ "roll_centre_height_rear", handling_data.m_roll_centre_height_rear } { "roll_centre_height_rear", handling_data.m_roll_centre_height_rear }
}; };
if (api::vehicle::handling::create_profile(hash, name, description, data)) return true;
}
bool vehicle_service::publish_profile(const char* name, const char* description, std::string share_code)
{
if (this->m_publish_status == PublishStatus::SAVED)
return true;
if (this->m_publish_status == PublishStatus::SAVING)
return false;
if (!safe_to_modify()) return false;
this->m_publish_status = PublishStatus::SAVING;
CHandlingData handling_data = *g_local_player->m_vehicle->m_handling;
uint32_t hash = handling_data.m_model_hash;
nlohmann::json data;
this->handling_data_to_json(handling_data, data);
nlohmann::json json;
if (!share_code.empty() && api::vehicle::handling::update(hash, name, description, share_code, data))
m_publish_status = PublishStatus::SAVED; m_publish_status = PublishStatus::SAVED;
else if (api::vehicle::handling::create_profile(hash, name, description, data, json))
{
this->set_active_profile(hash, json["share_code"]);
m_publish_status = PublishStatus::SAVED;
}
else m_publish_status = PublishStatus::FAILED; else m_publish_status = PublishStatus::FAILED;
return false; return false;
@ -178,6 +198,7 @@ namespace big
{ {
if (auto it = m_handling_backup.find(g_local_player->m_vehicle->m_handling->m_model_hash); it != m_handling_backup.end()) if (auto it = m_handling_backup.find(g_local_player->m_vehicle->m_handling->m_model_hash); it != m_handling_backup.end())
{ {
if (safe_to_modify())
*g_local_player->m_vehicle->m_handling = it->second; *g_local_player->m_vehicle->m_handling = it->second;
this->m_active_profiles.erase(g_local_player->m_vehicle->m_handling->m_model_hash); this->m_active_profiles.erase(g_local_player->m_vehicle->m_handling->m_model_hash);
@ -197,6 +218,8 @@ namespace big
void vehicle_service::set_handling_profile(HandlingProfile& profile) void vehicle_service::set_handling_profile(HandlingProfile& profile)
{ {
if (!safe_to_modify())
return;
*g_local_player->m_vehicle->m_handling = profile.data; *g_local_player->m_vehicle->m_handling = profile.data;
this->set_active_profile(profile.handling_hash, profile.share_code); this->set_active_profile(profile.handling_hash, profile.share_code);
@ -210,7 +233,7 @@ namespace big
if (busy) if (busy)
return false; return false;
if (g_local_player == nullptr || g_local_player->m_vehicle == nullptr) if (!safe_to_modify())
return false; return false;
if (!force_update && up_to_date == g_local_player->m_vehicle->m_handling->m_model_hash) if (!force_update && up_to_date == g_local_player->m_vehicle->m_handling->m_model_hash)
@ -218,7 +241,7 @@ namespace big
busy = true; busy = true;
g_fiber_pool->queue_job([&] { g_thread_pool->push([&] {
nlohmann::json json; nlohmann::json json;
if (!api::vehicle::handling::get_my_handling(g_local_player->m_vehicle->m_handling->m_model_hash, json) || json == nullptr) if (!api::vehicle::handling::get_my_handling(g_local_player->m_vehicle->m_handling->m_model_hash, json) || json == nullptr)
{ {

View File

@ -122,7 +122,8 @@ namespace big
int attempt_save(); int attempt_save();
std::string get_active_profile(std::uint32_t hash); std::string get_active_profile(std::uint32_t hash);
bool get_by_share_code(const char* share_code); bool get_by_share_code(const char* share_code);
bool publish_profile(const char* name, const char* description); bool handling_data_to_json(CHandlingData& handling_data, nlohmann::json& out);
bool publish_profile(const char* name, const char* description, std::string share_code = "");
PublishStatus publish_status(PublishStatus new_status = PublishStatus::NONE); PublishStatus publish_status(PublishStatus new_status = PublishStatus::NONE);
bool restore_vehicle(); bool restore_vehicle();
void set_active_profile(std::uint32_t hash, std::string share_code); void set_active_profile(std::uint32_t hash, std::string share_code);