From 0cff76852a1817c41a8ae2e5da45a01abbd87501 Mon Sep 17 00:00:00 2001 From: Yimura Date: Wed, 2 Feb 2022 01:38:35 +0100 Subject: [PATCH] feat(PlayerList): Further simplified how selected player is handled --- .../src/backend/looped/player/spectate.cpp | 4 +- .../src/gui/window/player/player_info.cpp | 24 +++++++---- .../src/gui/window/player/player_teleport.cpp | 6 +-- .../src/gui/window/player/player_toxic.cpp | 4 +- BigBaseV2/src/gui/window/window_player.cpp | 15 +------ BigBaseV2/src/gui/window/window_users.cpp | 4 +- BigBaseV2/src/services/player_service.cpp | 40 ++++++++++++++++--- BigBaseV2/src/services/player_service.hpp | 10 ++++- 8 files changed, 71 insertions(+), 36 deletions(-) diff --git a/BigBaseV2/src/backend/looped/player/spectate.cpp b/BigBaseV2/src/backend/looped/player/spectate.cpp index 696dba4a..0a45fad9 100644 --- a/BigBaseV2/src/backend/looped/player/spectate.cpp +++ b/BigBaseV2/src/backend/looped/player/spectate.cpp @@ -9,7 +9,7 @@ namespace big void looped::player_specate() { - if (g_player_service->m_selected_player == nullptr || !g_player_service->m_selected_player->is_valid() || !g.player.spectating) + if (!g_player_service->get_selected()->is_valid() || !g.player.spectating) { if (g.player.spectating) g.player.spectating = false; @@ -24,7 +24,7 @@ namespace big return; } - Ped target = PLAYER::GET_PLAYER_PED_SCRIPT_INDEX(g_player_service->m_selected_player->id()); + Ped target = PLAYER::GET_PLAYER_PED_SCRIPT_INDEX(g_player_service->get_selected()->id()); NETWORK::NETWORK_SET_IN_SPECTATOR_MODE(true, target); HUD::SET_MINIMAP_IN_SPECTATOR_MODE(true, target); diff --git a/BigBaseV2/src/gui/window/player/player_info.cpp b/BigBaseV2/src/gui/window/player/player_info.cpp index e1d7d20d..4de38ccf 100644 --- a/BigBaseV2/src/gui/window/player/player_info.cpp +++ b/BigBaseV2/src/gui/window/player/player_info.cpp @@ -11,25 +11,33 @@ namespace big ImGui::Separator(); - ImGui::Text("Player ID: %d", g_player_service->m_selected_player->id()); + ImGui::Text("Player ID: %d", g_player_service->get_selected()->id()); - ImGui::Text("Session Host: %s", g_player_service->m_selected_player->is_host() ? "Yes" : "No"); + ImGui::Text("Session Host: %s", g_player_service->get_selected()->is_host() ? "Yes" : "No"); ImGui::Separator(); - if (CPlayerInfo* player_info = g_player_service->m_selected_player->get_player_info(); player_info != nullptr) + if (CPlayerInfo* player_info = g_player_service->get_selected()->get_player_info(); player_info != nullptr) { ImGui::Text("Wanted Level: %d", player_info->m_wanted_level); - ImGui::Text("Player God Mode: %s", misc::has_bit_set((int*)&player_info->m_ped->m_damage_bits, 8) ? "Yes" : "No"); - ImGui::Text("Vehicle God Mode: %s", - player_info->m_ped->m_vehicle == nullptr ? "No vehicle detected" : - misc::has_bit_set((int*)&player_info->m_ped->m_vehicle->m_damage_bits, 8) ? "Yes" : "No" + } + + if (CPed* ped = g_player_service->get_selected()->get_ped(); ped != nullptr) + { + ImGui::Text("Player God Mode: %s", + misc::has_bit_set((int*)&ped->m_damage_bits, 8) ? "Yes" : "No" ); } + CAutomobile* vehicle = g_player_service->get_selected()->get_current_vehicle(); + ImGui::Text("Vehicle God Mode: %s", + vehicle == nullptr ? "No vehicle detected" : + misc::has_bit_set((int*)&vehicle->m_damage_bits, 8) ? "Yes" : "No" + ); + ImGui::Separator(); - if (rage::netPlayerData* net_player_data = g_player_service->m_selected_player->get_net_data(); net_player_data != nullptr) + if (rage::netPlayerData* net_player_data = g_player_service->get_selected()->get_net_data(); net_player_data != nullptr) { ImGui::Text("Rockstar ID: %d", net_player_data->m_rockstar_id); ImGui::Text( diff --git a/BigBaseV2/src/gui/window/player/player_teleport.cpp b/BigBaseV2/src/gui/window/player/player_teleport.cpp index 8cace2ad..a1f2441a 100644 --- a/BigBaseV2/src/gui/window/player/player_teleport.cpp +++ b/BigBaseV2/src/gui/window/player/player_teleport.cpp @@ -12,7 +12,7 @@ namespace big { QUEUE_JOB_BEGIN_CLAUSE() { - teleport::to_player(g_player_service->m_selected_player->id()); + teleport::to_player(g_player_service->get_selected()->id()); }QUEUE_JOB_END_CLAUSE } @@ -21,7 +21,7 @@ namespace big { QUEUE_JOB_BEGIN_CLAUSE() { - teleport::bring_player(g_player_service->m_selected_player->id()); + teleport::bring_player(g_player_service->get_selected()->id()); }QUEUE_JOB_END_CLAUSE } @@ -29,7 +29,7 @@ namespace big { QUEUE_JOB_BEGIN_CLAUSE() { - Vehicle veh = PED::GET_VEHICLE_PED_IS_IN(PLAYER::GET_PLAYER_PED_SCRIPT_INDEX(g_player_service->m_selected_player->id()), false); + Vehicle veh = PED::GET_VEHICLE_PED_IS_IN(PLAYER::GET_PLAYER_PED_SCRIPT_INDEX(g_player_service->get_selected()->id()), false); teleport::into_vehicle(veh); }QUEUE_JOB_END_CLAUSE diff --git a/BigBaseV2/src/gui/window/player/player_toxic.cpp b/BigBaseV2/src/gui/window/player/player_toxic.cpp index 01810075..a374ec4d 100644 --- a/BigBaseV2/src/gui/window/player/player_toxic.cpp +++ b/BigBaseV2/src/gui/window/player/player_toxic.cpp @@ -17,8 +17,8 @@ namespace big QUEUE_JOB_BEGIN_CLAUSE() { toxic::blame_explode_player( - g_player_service->m_selected_player->id(), - g_player_service->m_selected_player->id(), + g_player_service->get_selected()->id(), + g_player_service->get_selected()->id(), eExplosionType::PLANE, 1000, false, true, 0.f ); }QUEUE_JOB_END_CLAUSE diff --git a/BigBaseV2/src/gui/window/window_player.cpp b/BigBaseV2/src/gui/window/window_player.cpp index f7f538c4..54c13c50 100644 --- a/BigBaseV2/src/gui/window/window_player.cpp +++ b/BigBaseV2/src/gui/window/window_player.cpp @@ -6,21 +6,13 @@ namespace big { void window::w_player() { - if (g_player_service->m_selected_player == nullptr || !g_player_service->m_selected_player->is_valid()) - { - g.window.player = false; - - return; - } - else g.window.player = true; - char title[64]; strcpy(title, "Player Options: "); - strcat(title, g_player_service->m_selected_player->get_name()); + strcat(title, g_player_service->get_selected()->get_name()); strcat(title, "###player_options"); ImGui::SetNextWindowSize({ 350.f, 300.f }, ImGuiCond_FirstUseEver); - if (g.window.player && ImGui::Begin(title, &g.window.player)) + if (g.window.player && g_player_service->get_selected()->is_valid() && ImGui::Begin(title, &g.window.player)) { ImGui::BeginTabBar("tabbar_player"); tab_player::tab_info(); @@ -30,8 +22,5 @@ namespace big ImGui::End(); } - - if (!g.window.player) - g_player_service->m_selected_player = nullptr; } } \ No newline at end of file diff --git a/BigBaseV2/src/gui/window/window_users.cpp b/BigBaseV2/src/gui/window/window_users.cpp index 54745943..666ace37 100644 --- a/BigBaseV2/src/gui/window/window_users.cpp +++ b/BigBaseV2/src/gui/window/window_users.cpp @@ -18,7 +18,9 @@ namespace big std::unique_ptr& plyr = item.second; if (ImGui::Button(plyr->get_name(), { ImGui::GetWindowSize().x - 15.f, 0.f })) { - g_player_service->m_selected_player = plyr.get(); + g_player_service->set_selected(plyr.get()); + + g.window.player = true; } } diff --git a/BigBaseV2/src/services/player_service.cpp b/BigBaseV2/src/services/player_service.cpp index 15152329..82182463 100644 --- a/BigBaseV2/src/services/player_service.cpp +++ b/BigBaseV2/src/services/player_service.cpp @@ -20,12 +20,12 @@ namespace big const char* player::get_name() { - return m_net_game_player->get_name(); + return m_net_game_player == nullptr ? "" : m_net_game_player->get_name(); } rage::netPlayerData* player::get_net_data() { - return m_net_game_player->get_net_data(); + return m_net_game_player == nullptr ? false : m_net_game_player->get_net_data(); } CNetGamePlayer* player::get_net_game_player() @@ -50,12 +50,12 @@ namespace big uint8_t player::id() { - return m_net_game_player->m_player_id; + return m_net_game_player == nullptr ? -1 : m_net_game_player->m_player_id; } bool player::is_host() { - return m_net_game_player->is_host(); + return m_net_game_player == nullptr ? false : m_net_game_player->is_host(); } bool player::is_valid() @@ -63,6 +63,11 @@ namespace big return m_net_game_player == nullptr ? false : m_net_game_player->is_valid(); } + bool player::equals(CNetGamePlayer* net_game_player) + { + return net_game_player == m_net_game_player; + } + std::string player::to_lowercase_identifier() { std::string lower = this->get_name(); @@ -89,19 +94,38 @@ namespace big } } + m_dummy_player = new player(nullptr); + g_player_service = this; } player_service::~player_service() { g_player_service = nullptr; + + delete m_dummy_player; } void player_service::do_cleanup() { + m_selected_player = nullptr; m_players.clear(); } + player* player_service::get_by_name(std::string name) + { + std::transform(name.begin(), name.end(), name.begin(), ::tolower); + + if (auto it = m_players.find(name); it != m_players.end()) + return it->second.get(); + return nullptr; + } + + player* player_service::get_selected() + { + return m_selected_player == nullptr ? m_dummy_player : m_selected_player; + } + void player_service::player_join(CNetGamePlayer* net_game_player) { if (net_game_player == nullptr) return; @@ -118,9 +142,15 @@ namespace big void player_service::player_leave(CNetGamePlayer* net_game_player) { if (net_game_player == nullptr) return; + if (m_selected_player && m_selected_player->equals(net_game_player)) + m_selected_player = nullptr; std::unique_ptr plyr = std::make_unique(net_game_player); - m_players.erase(plyr->to_lowercase_identifier()); } + + void player_service::set_selected(player* plyr) + { + m_selected_player = plyr; + } } \ No newline at end of file diff --git a/BigBaseV2/src/services/player_service.hpp b/BigBaseV2/src/services/player_service.hpp index 28deb798..6dea4b5a 100644 --- a/BigBaseV2/src/services/player_service.hpp +++ b/BigBaseV2/src/services/player_service.hpp @@ -27,6 +27,8 @@ namespace big bool is_valid(); protected: + bool equals(CNetGamePlayer* net_game_player); + std::string to_lowercase_identifier(); }; @@ -34,20 +36,24 @@ namespace big typedef std::map> player_list; class player_service final { + player* m_dummy_player{}; + player* m_selected_player = nullptr; public: player_list m_players; - player* m_selected_player = nullptr; player_service(); virtual ~player_service(); void do_cleanup(); - std::unique_ptr& get_by_name(std::string name); + player* get_by_name(std::string name); + player* get_selected(); void player_join(CNetGamePlayer* net_game_player); void player_leave(CNetGamePlayer* net_game_player); + void set_selected(player* plyr); + }; inline player_service* g_player_service{};