feat(PlayerList): Further simplified how selected player is handled

This commit is contained in:
Yimura 2022-02-02 01:38:35 +01:00
parent b5c12a12bf
commit 0cff76852a
8 changed files with 71 additions and 36 deletions

View File

@ -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);

View File

@ -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(

View File

@ -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

View File

@ -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

View File

@ -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;
}
}

View File

@ -18,7 +18,9 @@ namespace big
std::unique_ptr<player>& 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;
}
}

View File

@ -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<player> plyr = std::make_unique<player>(net_game_player);
m_players.erase(plyr->to_lowercase_identifier());
}
void player_service::set_selected(player* plyr)
{
m_selected_player = plyr;
}
}

View File

@ -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<std::string, std::unique_ptr<player>> 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<player>& 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{};