Chat Room Stubs

This commit is contained in:
HikikoMarmy
2025-04-24 11:19:52 +01:00
parent 5f8becc229
commit 375cc9ad89
4 changed files with 212 additions and 0 deletions

15
Game/ChatRoom.cpp Normal file
View File

@@ -0,0 +1,15 @@
#include "../global_define.h"
ChatRoomSession::ChatRoomSession()
{
m_type = GameType::Public;
m_gameIndex = 0;
m_gameName.clear();
}
ChatRoomSession::~ChatRoomSession()
{
m_type = GameType::Public;
m_gameIndex = 0;
m_gameName.clear();
}

140
Game/ChatRoomManager.cpp Normal file
View File

@@ -0,0 +1,140 @@
#include "../global_define.h"
#include "ChatRoomManager.h"
ChatRoomManager::ChatRoomManager()
{
m_chatIndex = 0;
m_chatSessionList.clear();
}
ChatRoomManager::~ChatRoomManager()
{
}
void ChatRoomManager::OnDisconnectUser( sptr_user user )
{
if( !user || user->m_gameId < 0 )
return;
const auto gameId = user->m_gameId;
auto session = FindRoom( gameId );
if( !session )
return;
//auto owner = session->m_owner.lock();
//if( !owner )
//{
// Log::Error( "Game session owner not found! [%d]", gameId );
// ForceTerminateChat( gameId );
// return;
//}
//
//if( owner->m_sessionId == user->m_sessionId )
//{
// Log::Info( "Game session owner disconnected! [%d]", gameId );
// ForceTerminateChat( gameId );
//}
}
bool ChatRoomManager::CreatePublicChatSession( sptr_user owner, std::wstring gameName )
{
auto new_session = std::make_shared< ChatRoomSession >();
new_session->m_type = ChatRoomSession::GameType::Public;
new_session->m_gameIndex = m_chatIndex;
new_session->m_gameName = L"";
std::lock_guard< std::mutex > lock( m_mutex );
m_chatSessionList.push_back( new_session );
m_chatIndex++;
return true;
}
bool ChatRoomManager::CreatePrivateChatSession( sptr_user owner, std::wstring roomName )
{
for( auto &chatSession : m_chatSessionList )
{
if( chatSession->m_type != ChatRoomSession::GameType::Private )
continue;
if( chatSession->m_gameName == roomName )
{
Log::Error( "Game name is already in use! [%S]", roomName.c_str() );
return false;
}
}
auto new_session = std::make_shared< ChatRoomSession >();
new_session->m_type = ChatRoomSession::GameType::Private;
new_session->m_gameIndex = m_chatIndex;
new_session->m_gameName = roomName;
std::lock_guard< std::mutex > lock( m_mutex );
m_chatSessionList.push_back( new_session );
m_chatIndex++;
return true;
}
bool ChatRoomManager::ForceTerminateChat( const int32_t gameId )
{
if( gameId < 0 )
{
return false;
}
std::lock_guard< std::mutex > lock( m_mutex );
auto it = std::find_if( m_chatSessionList.begin(), m_chatSessionList.end(), [ &gameId ]( sptr_chat_room_session chatSession )
{
return chatSession->m_gameIndex == gameId;
} );
if( it != m_chatSessionList.end() )
{
m_chatSessionList.erase( it );
return true;
}
return false;
}
sptr_chat_room_session ChatRoomManager::FindRoom( const int32_t gameId )
{
if( gameId < 0 ) return nullptr;
std::lock_guard< std::mutex > lock( m_mutex );
for( auto &chatSession : m_chatSessionList )
{
if( chatSession->m_gameIndex == gameId )
{
return chatSession;
}
}
return nullptr;
}
sptr_chat_room_session ChatRoomManager::FindRoom( const std::wstring &gameName )
{
if( gameName.empty() ) return nullptr;
std::lock_guard< std::mutex > lock( m_mutex );
for( auto &chatSession : m_chatSessionList )
{
if( chatSession->m_gameName == gameName )
{
return chatSession;
}
}
return nullptr;
}

37
Game/ChatRoomManager.h Normal file
View File

@@ -0,0 +1,37 @@
#pragma once
#include "ChatRoomSession.h"
class ChatRoomManager {
private:
static inline std::unique_ptr< ChatRoomManager > m_instance;
static inline std::mutex m_mutex;
int32_t m_chatIndex;
std::vector< sptr_chat_room_session > m_chatSessionList;
public:
ChatRoomManager();
~ChatRoomManager();
ChatRoomManager( const ChatRoomManager & ) = delete;
ChatRoomManager &operator=( const ChatRoomManager & ) = delete;
static ChatRoomManager &Get()
{
std::lock_guard< std::mutex > lock( m_mutex );
if( m_instance == nullptr )
{
m_instance.reset( new ChatRoomManager() );
}
return *m_instance;
}
void OnDisconnectUser( sptr_user user );
bool CreatePublicChatSession( sptr_user user, std::wstring roomName );
bool CreatePrivateChatSession( sptr_user user, std::wstring roomName );
bool ForceTerminateChat( const int32_t gameId );
sptr_chat_room_session FindRoom( const int32_t gameId );
sptr_chat_room_session FindRoom( const std::wstring &roomName );
};

20
Game/ChatRoomSession.h Normal file
View File

@@ -0,0 +1,20 @@
#pragma once
class ChatRoomSession {
public:
ChatRoomSession();
~ChatRoomSession();
enum class GameType
{
Public,
Private
} m_type;
int32_t m_gameIndex;
std::wstring m_gameName;
std::weak_ptr< RealmUser > m_owner;
};
typedef std::shared_ptr< ChatRoomSession > sptr_chat_room_session;