Friends List

Ignore List
Instant Messaging
This commit is contained in:
HikikoMarmy
2025-07-17 21:45:13 +01:00
parent 3696c6f36f
commit 0ddf027fa4
18 changed files with 586 additions and 64 deletions

View File

@@ -16,7 +16,7 @@ public:
bool operator==( const RealmUser &other ) const
{
return m_sessionId == other.m_sessionId || m_accountId == other.m_accountId;
return m_accountId == other.m_accountId || sock->fd == other.sock->fd;
}
bool operator<( const RealmUser &other ) const
@@ -26,18 +26,28 @@ public:
return m_accountId < other.m_accountId;
}
bool IsFriend( const std::wstring &handle ) const
{
return std::find( m_friendList.begin(), m_friendList.end(), handle ) != m_friendList.end();
}
bool IsIgnored( const std::wstring &handle ) const
{
return std::find( m_ignoreList.begin(), m_ignoreList.end(), handle ) != m_ignoreList.end();
}
public:
sptr_socket sock; // For Realm Lobby
sptr_socket sock; // For Realm Lobby
RealmGameType m_gameType; // Champions of Norrath or Return to Arms
int64_t m_accountId; // Unique ID of the account
std::wstring m_sessionId; // Temporary Session ID
std::wstring m_username; // Username of the user
std::wstring m_chatHandle;
RealmGameType m_gameType; // Champions of Norrath or Return to Arms
int64_t m_accountId; // Unique ID of the account
std::wstring m_sessionId; // Temporary Session ID
std::wstring m_username; // Username of the user
std::wstring m_chatHandle; // Chat handle for the user, used in chat rooms
bool m_isLoggedIn; // True if the user has successfully authenticated and logged in
bool m_isHost; // True if this user is the host of a realm
int32_t m_gameId; // Unique ID of the realm
bool m_isLoggedIn; // True if the user has successfully authenticated and logged in
bool m_isHost; // True if this user is the host of a realm
int32_t m_gameId; // Unique ID of the realm
int32_t m_publicRoomId; // Used for public chat rooms
int32_t m_privateRoomId; // Used for private chat rooms
@@ -48,7 +58,10 @@ public:
int32_t m_discoveryPort;
int32_t m_characterId;
sptr_realm_character m_character;
sptr_realm_character m_character;
std::vector< std::wstring > m_friendList; // List of friends for this user
std::vector< std::wstring > m_ignoreList; // List of ignored users
};
using sptr_user = std::shared_ptr< RealmUser >;

View File

@@ -3,8 +3,9 @@
#include "ChatRoomManager.h"
#include "../Network/Event/NotifyForcedLogout.h"
#include "../Common/Constant.h"
#include "../Network/Event/NotifyFriendStatus.h"
#include "../Database/Database.h"
#include "../Common/Constant.h"
#include "../logging.h"
UserManager::UserManager()
@@ -59,10 +60,11 @@ void UserManager::RemoveUser( sptr_user user )
return;
}
Database::Get().DeleteSession( user->m_sessionId );
GameSessionManager::Get().OnDisconnectUser( user );
ChatRoomManager::Get().OnDisconnectUser( user );
NotifyFriendsOnlineStatus( user, false );
Log::Debug( "RemoveUser : [{}][{}]", user->m_username, user->m_sessionId );
std::lock_guard< std::mutex > lock( m_mutex );
@@ -71,26 +73,18 @@ void UserManager::RemoveUser( sptr_user user )
void UserManager::RemoveUser( const std::wstring &sessionId )
{
auto user = FindUserBySessionId( sessionId );
if( !user )
if( auto user = FindUserBySessionId( sessionId ) )
{
Log::Error( "RemoveUser : [{}] not found", sessionId );
return;
RemoveUser( user );
}
RemoveUser( user );
}
void UserManager::RemoveUser( const sptr_socket socket )
{
auto user = FindUserBySocket( socket );
if( !user )
if( auto user = FindUserBySocket( socket ) )
{
Log::Error( "RemoveUser : [{}] not found", socket->remote_ip );
return;
RemoveUser( user );
}
RemoveUser( user );
}
void UserManager::Disconnect( sptr_socket socket, const std::string reason )
@@ -146,37 +140,14 @@ sptr_user UserManager::FindUserBySocket( const sptr_socket &socket )
return ( it != m_users.end() ) ? *it : nullptr;
}
sptr_user UserManager::RecoverUserBySession( const std::wstring &sessionId, const sptr_socket &socket )
sptr_user UserManager::FindUserByChatHandle( const std::wstring &handle )
{
auto user = FindUserBySocket( socket );
if( user == nullptr )
std::lock_guard<std::mutex> lock( m_mutex );
auto it = std::find_if( m_users.begin(), m_users.end(), [ & ]( const sptr_user &user )
{
Log::Error( "RecoverUserBySession: User not found for socket: {}", socket->remote_ip );
return nullptr;
}
if( sessionId.empty() )
{
Log::Error( "RecoverUserBySession: Empty session ID provided." );
return nullptr;
}
const auto [account_id, character_id] = Database::Get().GetSession( sessionId, socket->remote_ip );
if( account_id < 0 )
{
Log::Error( "RecoverUserBySession: Failed to get session for account ID: {}, session ID: {}", account_id, sessionId );
return nullptr;
}
user->m_accountId = account_id;
user->m_sessionId = sessionId;
user->m_character = Database::Get().LoadCharacterData( account_id, character_id );
Log::Debug( "RecoverUserBySession: User recovered with session ID: {}, account ID: {}", sessionId, account_id );
return user;
return user->m_chatHandle == handle;
} );
return ( it != m_users.end() ) ? *it : nullptr;
}
int32_t UserManager::GetUserCount() const
@@ -189,3 +160,21 @@ std::vector<sptr_user> UserManager::GetUserList()
std::lock_guard<std::mutex> lock( m_mutex );
return m_users;
}
void UserManager::NotifyFriendsOnlineStatus( const sptr_user &user, bool onlineStatus )
{
if( !user || user->m_friendList.empty() )
{
return;
}
const auto notifyFriend = NotifyFriendStatus( user->m_chatHandle, onlineStatus );
for( const auto &friendHandle : user->m_friendList )
{
auto friendUser = FindUserByChatHandle( friendHandle );
if( friendUser && friendUser->sock )
{
friendUser->sock->send( notifyFriend );
}
}
}

View File

@@ -31,13 +31,14 @@ public:
void Disconnect( sptr_socket socket, const std::string reason );
void Disconnect( sptr_user user, const std::string reason );
sptr_user FindUserBySessionId( const std::wstring &sessionId );
sptr_user FindUserBySocket( const sptr_socket &socket );
sptr_user RecoverUserBySession( const std::wstring &sessionId, const sptr_socket& socket );
sptr_user FindUserByChatHandle( const std::wstring &handle );
int32_t GetUserCount() const;
std::vector< sptr_user > GetUserList();
void NotifyFriendsOnlineStatus( const sptr_user &user, bool onlineStatus );
private:
std::mutex m_mutex;
std::vector< sptr_user > m_users;