feat(player_db): add player online status (#983)

This commit is contained in:
Johann 2023-02-21 11:52:05 +01:00 committed by GitHub
parent c798bbe7c9
commit 60b61cbce2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 76 additions and 0 deletions

View File

@ -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<int> infractions;
std::optional<CommandAccessLevel> command_access_level = std::nullopt;
PlayerOnlineStatus online_state = PlayerOnlineStatus::UNKNOWN;
};
static void to_json(nlohmann::json& j, const persistent_player& player)

View File

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

View File

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

View File

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