2023-03-01 21:27:15 +00:00
# include "core/data/block_join_reasons.hpp"
# include "core/data/command_access_levels.hpp"
# include "core/data/infractions.hpp"
2022-11-19 01:49:36 +00:00
# include "fiber_pool.hpp"
2023-07-14 09:02:47 +00:00
# include "gta/enums.hpp"
2022-11-19 01:49:36 +00:00
# include "pointers.hpp"
2022-12-22 11:49:34 +01:00
# include "services/api/api_service.hpp"
2023-03-01 21:27:15 +00:00
# include "services/player_database/player_database_service.hpp"
# include "services/players/player_service.hpp"
2022-11-19 01:49:36 +00:00
# include "util/session.hpp"
2023-03-01 21:27:15 +00:00
# include "views/view.hpp"
2022-11-19 01:49:36 +00:00
namespace big
{
2023-05-01 23:23:07 +02:00
char name_buf [ 32 ] ;
char search [ 64 ] ;
2023-06-23 06:43:44 +00:00
char note_buffer [ 1024 ] ;
bool notes_dirty = false ;
2023-05-01 23:23:07 +02:00
std : : shared_ptr < persistent_player > current_player ;
2023-07-14 09:02:47 +00:00
ImVec4 get_player_color ( persistent_player & player )
{
if ( player . session_type = = GSType : : Unknown )
return ImVec4 ( .5f , .5f , .5f , 1.0f ) ;
else if ( player . session_type = = GSType : : Invalid )
return ImVec4 ( 1.f , 0.f , 0.f , 1.f ) ;
else if ( ! player_database_service : : is_joinable_session ( player . session_type ) )
return ImVec4 ( 1.f , 1.f , 0.f , 1.f ) ;
else
return ImVec4 ( 0.f , 1.f , 0.f , 1.f ) ;
}
2023-05-01 23:23:07 +02:00
void draw_player_db_entry ( std : : shared_ptr < persistent_player > player , const std : : string & lower_search )
{
std : : string name = player - > name ;
std : : transform ( name . begin ( ) , name . end ( ) , name . begin ( ) , : : tolower ) ;
if ( lower_search . empty ( ) | | name . find ( lower_search ) ! = std : : string : : npos )
{
ImGui : : PushID ( player - > rockstar_id ) ;
float circle_size = 7.5f ;
auto cursor_pos = ImGui : : GetCursorScreenPos ( ) ;
//render status circle
2023-07-14 09:02:47 +00:00
ImGui : : GetWindowDrawList ( ) - > AddCircleFilled ( ImVec2 ( cursor_pos . x + 4.f + circle_size , cursor_pos . y + 4.f + circle_size ) , circle_size , ImColor ( get_player_color ( * player ) ) ) ;
2023-05-01 23:23:07 +02:00
//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 ( ) ) )
{
2023-06-23 06:43:44 +00:00
if ( notes_dirty )
{
// Ensure notes are saved
g_player_database_service - > save ( ) ;
notes_dirty = false ;
}
2023-05-01 23:23:07 +02:00
g_player_database_service - > set_selected ( player ) ;
current_player = player ;
strncpy ( name_buf , current_player - > name . data ( ) , sizeof ( name_buf ) ) ;
2023-06-23 06:43:44 +00:00
strncpy ( note_buffer , current_player - > notes . data ( ) , sizeof ( note_buffer ) ) ;
2023-05-01 23:23:07 +02:00
}
2023-07-14 09:02:47 +00:00
if ( ImGui : : IsItemHovered ( ) )
ImGui : : SetTooltip ( player_database_service : : get_session_type_str ( player - > session_type ) ) ;
2023-05-01 23:23:07 +02:00
ImGui : : PopID ( ) ;
}
}
2022-11-19 01:49:36 +00:00
void view : : player_database ( )
{
ImGui : : SetNextItemWidth ( 300.f ) ;
2023-02-01 19:46:33 +01:00
components : : input_text_with_hint ( " PLAYER " _T , " SEARCH " _T , search , sizeof ( search ) , ImGuiInputTextFlags_None ) ;
2022-11-19 01:49:36 +00:00
2023-06-23 13:44:06 +05:00
if ( ImGui : : BeginListBox ( " ###players " , { 180 , static_cast < float > ( * g_pointers - > m_gta . m_resolution_y - 400 - 38 * 4 ) } ) )
2022-11-19 01:49:36 +00:00
{
2023-05-01 23:23:07 +02:00
auto & item_arr = g_player_database_service - > get_sorted_players ( ) ;
2022-11-19 01:49:36 +00:00
if ( item_arr . size ( ) > 0 )
{
std : : string lower_search = search ;
std : : transform ( lower_search . begin ( ) , lower_search . end ( ) , lower_search . begin ( ) , tolower ) ;
2023-05-01 23:23:07 +02:00
for ( auto & player : item_arr | std : : ranges : : views : : values )
2022-11-19 01:49:36 +00:00
{
2023-07-14 09:02:47 +00:00
if ( player_database_service : : is_joinable_session ( player - > session_type ) )
2023-05-01 23:23:07 +02:00
draw_player_db_entry ( player , lower_search ) ;
}
2022-11-19 01:49:36 +00:00
2023-05-01 23:23:07 +02:00
for ( auto & player : item_arr | std : : ranges : : views : : values )
{
2023-07-14 09:02:47 +00:00
if ( ! player_database_service : : is_joinable_session ( player - > session_type ) & & player - > session_type ! = GSType : : Invalid
& & player - > session_type ! = GSType : : Unknown )
draw_player_db_entry ( player , lower_search ) ;
}
for ( auto & player : item_arr | std : : ranges : : views : : values )
{
if ( player - > session_type = = GSType : : Invalid | | player - > session_type = = GSType : : Unknown )
2023-05-01 23:23:07 +02:00
draw_player_db_entry ( player , lower_search ) ;
2022-11-19 01:49:36 +00:00
}
}
else
{
2023-02-01 19:46:33 +01:00
ImGui : : Text ( " NO_STORED_PLAYERS " _T . data ( ) ) ;
2022-11-19 01:49:36 +00:00
}
2023-06-23 13:44:06 +05:00
ImGui : : EndListBox ( ) ;
2022-11-19 01:49:36 +00:00
}
if ( auto selected = g_player_database_service - > get_selected ( ) )
{
ImGui : : SameLine ( ) ;
2023-04-14 18:54:07 +02:00
if ( ImGui : : BeginChild ( " ###selected_player " , { 500 , static_cast < float > ( * g_pointers - > m_gta . m_resolution_y - 388 - 38 * 4 ) } , false , ImGuiWindowFlags_NoBackground ) )
2022-11-19 01:49:36 +00:00
{
2023-02-01 19:46:33 +01:00
if ( ImGui : : InputText ( " NAME " _T . data ( ) , name_buf , sizeof ( name_buf ) ) )
2022-11-19 01:49:36 +00:00
{
2023-05-01 23:23:07 +02:00
current_player - > name = name_buf ;
2022-11-19 01:49:36 +00:00
}
2023-09-17 16:23:26 -04:00
if ( ImGui : : IsItemActive ( ) )
g . self . hud . typing = TYPING_TICKS ;
2022-11-19 01:49:36 +00:00
2023-06-05 15:46:20 -04:00
if ( ImGui : : InputScalar ( " RID " _T . data ( ) , ImGuiDataType_S64 , & current_player - > rockstar_id )
| | ImGui : : Checkbox ( " IS_MODDER " _T . data ( ) , & current_player - > is_modder )
2023-06-06 13:37:45 +02:00
| | ImGui : : Checkbox ( " BLOCK_JOIN " _T . data ( ) , & current_player - > block_join )
2023-07-14 09:02:47 +00:00
| | ImGui : : Checkbox ( " Track Player " , & current_player - > notify_online ) )
2023-05-01 23:23:07 +02:00
{
if ( current_player - > rockstar_id ! = selected - > rockstar_id )
g_player_database_service - > update_rockstar_id ( selected - > rockstar_id , current_player - > rockstar_id ) ;
g_player_database_service - > save ( ) ;
}
2022-11-19 01:49:36 +00:00
2023-05-01 23:23:07 +02:00
if ( ImGui : : BeginCombo ( " BLOCK_JOIN_ALERT " _T . data ( ) , block_join_reasons [ current_player - > block_join_reason ] ) )
2022-11-19 01:49:36 +00:00
{
for ( const auto & reason : block_join_reasons )
{
2023-05-01 23:23:07 +02:00
if ( ImGui : : Selectable ( reason . second , reason . first = = current_player - > block_join_reason ) )
2022-11-19 01:49:36 +00:00
{
2023-05-01 23:23:07 +02:00
current_player - > block_join_reason = reason . first ;
g_player_database_service - > save ( ) ;
2022-11-19 01:49:36 +00:00
}
2023-05-01 23:23:07 +02:00
if ( reason . first = = current_player - > block_join_reason )
2022-11-19 01:49:36 +00:00
{
ImGui : : SetItemDefaultFocus ( ) ;
}
}
ImGui : : EndCombo ( ) ;
}
if ( ImGui : : IsItemHovered ( ) )
2023-02-01 19:46:33 +01:00
ImGui : : SetTooltip ( " ONLY_AS_HOST " _T . data ( ) ) ;
2022-11-19 01:49:36 +00:00
2023-03-01 21:27:15 +00:00
if ( ImGui : : BeginCombo ( " CHAT_COMMAND_PERMISSIONS " _T . data ( ) ,
2023-05-01 23:23:07 +02:00
COMMAND_ACCESS_LEVELS [ current_player - > command_access_level . value_or ( g . session . chat_command_default_access_level ) ] ) )
2022-12-22 21:23:32 +00:00
{
for ( const auto & [ type , name ] : COMMAND_ACCESS_LEVELS )
{
2023-05-01 23:23:07 +02:00
if ( ImGui : : Selectable ( name , type = = current_player - > command_access_level . value_or ( g . session . chat_command_default_access_level ) ) )
2022-12-22 21:23:32 +00:00
{
2023-05-01 23:23:07 +02:00
current_player - > command_access_level = type ;
g_player_database_service - > save ( ) ;
2022-12-22 21:23:32 +00:00
}
2023-05-01 23:23:07 +02:00
if ( type = = current_player - > command_access_level . value_or ( g . session . chat_command_default_access_level ) )
2022-12-22 21:23:32 +00:00
{
ImGui : : SetItemDefaultFocus ( ) ;
}
}
ImGui : : EndCombo ( ) ;
}
2023-05-01 23:23:07 +02:00
if ( ! current_player - > infractions . empty ( ) )
2022-11-19 01:49:36 +00:00
{
2023-02-01 19:46:33 +01:00
ImGui : : Text ( " INFRACTIONS " _T . data ( ) ) ;
2022-11-19 01:49:36 +00:00
2023-05-01 23:23:07 +02:00
for ( auto & infraction : current_player - > infractions )
2022-11-19 01:49:36 +00:00
{
2023-10-13 00:11:37 +02:00
ImGui : : BulletText ( current_player - > get_infraction_description ( infraction ) ) ;
2022-11-19 01:49:36 +00:00
}
}
2023-06-23 06:43:44 +00:00
if ( ImGui : : InputTextMultiline ( " Notes " , note_buffer , sizeof ( note_buffer ) ) )
{
current_player - > notes = note_buffer ;
notes_dirty = true ;
}
2023-09-17 16:23:26 -04:00
if ( ImGui : : IsItemActive ( ) )
g . self . hud . typing = TYPING_TICKS ;
2023-06-23 06:43:44 +00:00
2023-07-15 20:37:20 +00:00
ImGui : : Checkbox ( " Join Redirect " , & current_player - > join_redirect ) ;
if ( ImGui : : IsItemHovered ( ) )
ImGui : : SetTooltip ( " Anyone trying to join you will join this player instead if they are active. The preference slider will control redirect priority if multiple players with join redirect are active " ) ;
if ( current_player - > join_redirect )
{
ImGui : : SliderInt ( " Preference " , & current_player - > join_redirect_preference , 1 , 10 ) ;
}
2023-03-01 21:27:15 +00:00
components : : button ( " JOIN_SESSION " _T , [ ] {
2023-05-01 23:23:07 +02:00
session : : join_by_rockstar_id ( current_player - > rockstar_id ) ;
2022-11-19 01:49:36 +00:00
} ) ;
2022-12-22 11:49:34 +01:00
static char message [ 256 ] ;
2023-02-01 19:46:33 +01:00
components : : input_text ( " INPUT_MSG " _T , message , sizeof ( message ) ) ;
if ( components : : button ( " SEND_MSG " _T ) )
2022-12-22 11:49:34 +01:00
{
2023-03-01 21:27:15 +00:00
g_thread_pool - > push ( [ selected ] {
2022-12-22 11:49:34 +01:00
if ( g_api_service - > send_socialclub_message ( selected - > rockstar_id , message ) )
{
2023-06-05 15:46:20 -04:00
g_notification_service - > push_success ( " SCAPI " _T . data ( ) , " MSG_SENT_SUCCESS " _T . data ( ) ) ;
2022-12-22 11:49:34 +01:00
return ;
}
2023-02-01 19:46:33 +01:00
g_notification_service - > push_error ( " SCAPI " _T . data ( ) , " MSG_SENT_FAIL " _T . data ( ) ) ;
2022-12-22 11:49:34 +01:00
} ) ;
} ;
2023-07-15 20:37:20 +00:00
ImGui : : Text ( " Session Type: %s " , player_database_service : : get_session_type_str ( selected - > session_type ) ) ;
if ( selected - > session_type ! = GSType : : Invalid & & selected - > session_type ! = GSType : : Unknown )
{
ImGui : : Text ( " Is Host Of Session: %s " , selected - > is_host_of_session ? " Yes " : " No " ) ;
ImGui : : Text ( " Is Spectating: %s " , selected - > is_spectating ? " Yes " : " No " ) ;
ImGui : : Text ( " In Job Lobby: %s " , selected - > transition_session_id ! = - 1 ? " Yes " : " No " ) ;
2023-09-08 06:46:34 -03:00
ImGui : : Text ( " Is Host Of Job Lobby: %s " , selected - > is_host_of_transition_session ? " Yes " : " No " ) ;
2023-07-15 20:37:20 +00:00
ImGui : : Text ( " Current Mission Type: %s " , player_database_service : : get_game_mode_str ( selected - > game_mode ) ) ;
if ( selected - > game_mode ! = GameMode : : None & & player_database_service : : can_fetch_name ( selected - > game_mode ) )
{
ImGui : : Text ( " Current Mission Name: %s " , selected - > game_mode_name . c_str ( ) ) ;
if ( ( selected - > game_mode_name = = " Unknown " | | selected - > game_mode_name . empty ( ) )
& & ! selected - > game_mode_id . empty ( ) )
{
ImGui : : SameLine ( ) ;
components : : button ( " Fetch " , [ ] {
current_player - > game_mode_name =
player_database_service : : get_name_by_content_id ( current_player - > game_mode_id ) ;
} ) ;
}
}
}
2023-02-01 19:46:33 +01:00
if ( ImGui : : Button ( " SAVE " _T . data ( ) ) )
2022-11-19 01:49:36 +00:00
{
2023-05-01 23:23:07 +02:00
if ( current_player - > rockstar_id ! = selected - > rockstar_id )
g_player_database_service - > update_rockstar_id ( selected - > rockstar_id , current_player - > rockstar_id ) ;
2022-11-19 01:49:36 +00:00
2023-05-01 23:23:07 +02:00
selected = current_player ;
2022-11-19 01:49:36 +00:00
g_player_database_service - > save ( ) ;
}
ImGui : : SameLine ( ) ;
2023-02-01 19:46:33 +01:00
if ( ImGui : : Button ( " REMOVE " _T . data ( ) ) )
2022-11-19 01:49:36 +00:00
{
g_player_database_service - > remove_rockstar_id ( selected - > rockstar_id ) ;
}
}
ImGui : : EndChild ( ) ;
}
2023-02-01 19:46:33 +01:00
if ( ImGui : : Button ( " REMOVE_ALL " _T . data ( ) ) )
2022-11-19 01:49:36 +00:00
{
2023-07-14 09:02:47 +00:00
ImGui : : OpenPopup ( " ##removeall " ) ;
}
if ( ImGui : : BeginPopupModal ( " ##removeall " ) )
{
ImGui : : Text ( " Are you sure? " ) ;
if ( ImGui : : Button ( " Yes " ) )
{
g_player_database_service - > set_selected ( nullptr ) ;
g_player_database_service - > get_players ( ) . clear ( ) ;
g_player_database_service - > get_sorted_players ( ) . clear ( ) ;
g_player_database_service - > save ( ) ;
ImGui : : CloseCurrentPopup ( ) ;
}
ImGui : : SameLine ( ) ;
if ( ImGui : : Button ( " No " ) )
{
ImGui : : CloseCurrentPopup ( ) ;
}
ImGui : : EndPopup ( ) ;
2022-11-19 01:49:36 +00:00
}
2023-02-21 11:52:05 +01:00
ImGui : : SameLine ( ) ;
2023-03-01 21:27:15 +00:00
components : : button ( " RELOAD_PLYR_ONLINE_STATES " _T , [ ] {
2023-02-21 11:52:05 +01:00
g_player_database_service - > update_player_states ( ) ;
} ) ;
2023-07-14 09:02:47 +00:00
if ( ImGui : : TreeNode ( " Player Tracking " ) )
{
if ( components : : command_checkbox < " player_db_auto_update_states " > ( " Enable " ) )
g_player_database_service - > start_update_loop ( ) ;
2023-09-21 20:08:23 +02:00
2023-07-14 09:02:47 +00:00
ImGui : : Checkbox ( " Notify When Online " , & g . player_db . notify_when_online ) ;
ImGui : : Checkbox ( " Notify When Joinable " , & g . player_db . notify_when_joinable ) ;
ImGui : : Checkbox ( " Notify When Unjoinable " , & g . player_db . notify_when_unjoinable ) ;
ImGui : : Checkbox ( " Notify When Offline " , & g . player_db . notify_when_offline ) ;
ImGui : : Checkbox ( " Notify On Session Type Change " , & g . player_db . notify_on_session_type_change ) ;
ImGui : : Checkbox ( " Notify On Session Change " , & g . player_db . notify_on_session_change ) ;
2023-07-15 20:37:20 +00:00
ImGui : : Checkbox ( " Notify On Spectator Change " , & g . player_db . notify_on_spectator_change ) ;
ImGui : : Checkbox ( " Notify On Become Host " , & g . player_db . notify_on_become_host ) ;
ImGui : : Checkbox ( " Notify On Job Lobby Change " , & g . player_db . notify_on_transition_change ) ;
ImGui : : Checkbox ( " Notify On Mission Change " , & g . player_db . notify_on_mission_change ) ;
2023-07-14 09:02:47 +00:00
ImGui : : TreePop ( ) ;
}
2023-05-01 23:23:07 +02:00
2022-11-19 01:49:36 +00:00
ImGui : : Separator ( ) ;
2023-02-01 19:46:33 +01:00
components : : sub_title ( " NEW_ENTRY " _T ) ;
2022-11-19 01:49:36 +00:00
static char new_name [ 64 ] ;
static int64_t new_rockstar_id ;
2023-02-01 19:46:33 +01:00
components : : input_text ( " NAME " _T , new_name , sizeof ( new_name ) ) ;
ImGui : : InputScalar ( " RID " _T . data ( ) , ImGuiDataType_S64 , & new_rockstar_id ) ;
2022-11-19 01:49:36 +00:00
2023-02-01 19:46:33 +01:00
if ( ImGui : : Button ( " ADD " _T . data ( ) ) )
2022-11-19 01:49:36 +00:00
{
2023-05-01 23:23:07 +02:00
current_player = g_player_database_service - > add_player ( new_rockstar_id , new_name ) ;
2022-11-19 01:49:36 +00:00
g_player_database_service - > save ( ) ;
}
2023-07-02 22:55:06 +02:00
ImGui : : SameLine ( ) ;
if ( ImGui : : Button ( " SEARCH " _T . data ( ) ) )
{
2023-07-14 09:02:47 +00:00
g_thread_pool - > push ( [ ] {
2023-07-02 22:55:06 +02:00
if ( ! g_api_service - > get_rid_from_username ( new_name , * ( uint64_t * ) & new_rockstar_id ) )
{
g_notification_service - > push_error ( " New Player DB Entry " , std : : format ( " No user '{}' called could be found. " , new_name ) ) ;
new_rockstar_id = 0 ;
}
} ) ;
}
if ( ImGui : : IsItemHovered ( ) )
{
ImGui : : SetTooltip ( " Do you know only the name of someone and not their Rockstar ID? Just fill in the username and click \" search \" . " ) ;
}
2022-11-19 01:49:36 +00:00
}
}