From 60b61cbce20dfd5e0a30230d367478c69f1de09f Mon Sep 17 00:00:00 2001 From: Johann <76482511+Primexz@users.noreply.github.com> Date: Tue, 21 Feb 2023 11:52:05 +0100 Subject: [PATCH] feat(player_db): add player online status (#983) --- .../player_database/persistent_player.hpp | 8 ++++ .../player_database_service.cpp | 37 +++++++++++++++++++ .../player_database_service.hpp | 3 ++ src/views/network/view_player_database.cpp | 28 ++++++++++++++ 4 files changed, 76 insertions(+) diff --git a/src/services/player_database/persistent_player.hpp b/src/services/player_database/persistent_player.hpp index 1295f05c..dc2e264e 100644 --- a/src/services/player_database/persistent_player.hpp +++ b/src/services/player_database/persistent_player.hpp @@ -5,6 +5,13 @@ namespace big { + enum class PlayerOnlineStatus + { + UNKNOWN, + OFFLINE, + ONLINE + }; + struct persistent_player { std::string name; @@ -14,6 +21,7 @@ namespace big bool is_modder = false; std::unordered_set infractions; std::optional command_access_level = std::nullopt; + PlayerOnlineStatus online_state = PlayerOnlineStatus::UNKNOWN; }; static void to_json(nlohmann::json& j, const persistent_player& player) diff --git a/src/services/player_database/player_database_service.cpp b/src/services/player_database/player_database_service.cpp index 7075fbc8..0517160b 100644 --- a/src/services/player_database/player_database_service.cpp +++ b/src/services/player_database/player_database_service.cpp @@ -1,5 +1,7 @@ #include "player_database_service.hpp" #include "file_manager.hpp" +#include "util/session.hpp" +#include "pointers.hpp" namespace big { @@ -95,4 +97,39 @@ namespace big { return m_selected; } + + void player_database_service::invalidate_player_states() + { + for (auto& item : m_players) + item.second.online_state = PlayerOnlineStatus::UNKNOWN; + } + + void player_database_service::update_player_states() + { + invalidate_player_states(); + + //fetch current stat for each player.. this will need some time. + for (auto& item : m_players) + { + auto& player = item.second; + rage::rlGamerHandle player_handle(player.rockstar_id); + rage::rlSessionByGamerTaskResult result; + bool success = false; + rage::rlTaskStatus state{}; + + if (g_pointers->m_start_get_session_by_gamer_handle(0, &player_handle, 1, &result, 1, &success, &state)) + { + while (state.status == 1) + script::get_current()->yield(); + + if (state.status == 3 && success) + { + player.online_state = PlayerOnlineStatus::ONLINE; + continue; + } + } + + player.online_state = PlayerOnlineStatus::OFFLINE; + } + } } \ No newline at end of file diff --git a/src/services/player_database/player_database_service.hpp b/src/services/player_database/player_database_service.hpp index c8601dbb..2c294dad 100644 --- a/src/services/player_database/player_database_service.hpp +++ b/src/services/player_database/player_database_service.hpp @@ -25,6 +25,9 @@ namespace big void set_selected(persistent_player* selected); persistent_player* get_selected(); + + void update_player_states(); + void invalidate_player_states(); }; inline player_database_service* g_player_database_service; diff --git a/src/views/network/view_player_database.cpp b/src/views/network/view_player_database.cpp index 9f2e55c4..93c22c46 100644 --- a/src/views/network/view_player_database.cpp +++ b/src/views/network/view_player_database.cpp @@ -40,6 +40,27 @@ namespace big { ImGui::PushID(item.first); + float circle_size = 7.5f; + auto cursor_pos = ImGui::GetCursorScreenPos(); + auto plyr_state = player.online_state; + + //render status circle + ImGui::GetWindowDrawList()->AddCircleFilled( + ImVec2( + cursor_pos.x + 4.f + circle_size, + cursor_pos.y + 4.f + circle_size), + circle_size, + ImColor( + plyr_state == PlayerOnlineStatus::ONLINE ? ImVec4(0.f, 1.f, 0.f, 1.f) + : plyr_state == PlayerOnlineStatus::OFFLINE ? ImVec4(1.f, 0.f, 0.f, 1.f) + : plyr_state == PlayerOnlineStatus::UNKNOWN ? ImVec4(.5f, .5f, .5f, 1.0f) + : ImVec4(.5f, .5f, .5f, 1.0f) + )); + + //we need some padding + ImVec2 cursor = ImGui::GetCursorPos(); + ImGui::SetCursorPos(ImVec2(cursor.x + 25.f, cursor.y)); + if (components::selectable(player.name, &player == g_player_database_service->get_selected())) { g_player_database_service->set_selected(&player); @@ -169,6 +190,13 @@ namespace big g_player_database_service->save(); } + ImGui::SameLine(); + + components::button("RELOAD_PLYR_ONLINE_STATES"_T, [] + { + g_player_database_service->update_player_states(); + }); + ImGui::Separator(); components::sub_title("NEW_ENTRY"_T);