Game room and user management

This commit is contained in:
HikikoMarmy
2025-02-20 02:37:46 +00:00
parent 650ec0d183
commit 9d26fcc01a
9 changed files with 390 additions and 226 deletions

View File

@@ -0,0 +1,29 @@
#include "../global_define.h"
#include "../Lobby Server/Event/NotifyClientReqConnect.h"
#include "../Lobby Server/Event/NotifyGameDiscovered.h"
GameSession::GameSession()
{
m_gameIndex = 0;
m_state = GameState::NotReady;
m_minimumLevel = 0;
m_maximumLevel = 0;
m_maximumPlayers = 0;
m_gameLocation.clear();
m_gameName.clear();
m_ownerName.clear();
m_gameData.clear();
}
GameSession::~GameSession()
{
}
sptr_user GameSession::GetHost()
{
return m_userList[ 0 ];
}

View File

@@ -1,51 +1,41 @@
#pragma once #pragma once
#include <string>
#include <array>
class GameSession { class GameSession {
public: public:
GameSession() GameSession();
~GameSession();
sptr_user GetHost();
bool JoinUser( sptr_user user );
public:
enum class GameType
{ {
m_gameIndex = 0; Public,
m_minimumLevel = 0; Private
m_maximumLevel = 0; } m_type;
m_hostSessionId = L""; enum class GameState
m_gameName = L"";
m_userList.fill( nullptr );
}
~GameSession()
{ {
NotReady,
} Open,
Started
sptr_user GetOwner() } m_state;
{
return m_userList[ 0 ];
}
sptr_user GetUser( const size_t index )
{
if( index < 0 || index >= m_userList.size() )
{
return nullptr;
}
return m_userList[ index ];
}
int32_t m_gameIndex; int32_t m_gameIndex;
std::wstring m_hostSessionId; std::wstring m_gameLocation;
std::wstring m_gameName; std::wstring m_gameName;
std::wstring m_ownerName;
std::string m_gameData;
int32_t m_maximumPlayers;
int32_t m_minimumLevel; int32_t m_minimumLevel;
int32_t m_maximumLevel; int32_t m_maximumLevel;
// User Information // User Information
std::array< sptr_user, 4 > m_userList; std::vector< sptr_user > m_userList;
}; };
typedef std::shared_ptr< GameSession > sptr_game_session; typedef std::shared_ptr< GameSession > sptr_game_session;

View File

@@ -8,8 +8,7 @@
GameSessionManager::GameSessionManager() GameSessionManager::GameSessionManager()
{ {
m_gameIndex = 0; m_gameIndex = 0;
m_publicGameSessionList.clear(); m_gameSessionList.clear();
m_privateGameSessionList.clear();
} }
GameSessionManager::~GameSessionManager() GameSessionManager::~GameSessionManager()
@@ -18,12 +17,7 @@ GameSessionManager::~GameSessionManager()
void GameSessionManager::Process() void GameSessionManager::Process()
{ {
for( auto &gameSession : m_publicGameSessionList ) for( auto &gameSession : m_gameSessionList)
{
ProcessGameSession( gameSession );
}
for( auto &gameSession : m_privateGameSessionList )
{ {
ProcessGameSession( gameSession ); ProcessGameSession( gameSession );
} }
@@ -31,111 +25,51 @@ void GameSessionManager::Process()
void GameSessionManager::ProcessGameSession( sptr_game_session gameSession ) void GameSessionManager::ProcessGameSession( sptr_game_session gameSession )
{ {
static int test = 0;
//if( 0 == onePass ) return;
for( auto &member : gameSession->m_userList ) for( auto &member : gameSession->m_userList )
{ {
if( member == nullptr ) if( member == nullptr )
{ {
continue; continue;
} }
if( member->m_state == RealmUser::UserState::DiscoveryPending )
{
continue;
} }
//if( member->m_state == RealmUser::UserState::JoinPending )
if( test == 1 )
{
//HandleJoinDiscovery( gameSession, member );
//auto gameOwner = gameSession->GetOwner();
//NotifyClientRequestConnect notify( discoveryInfo->m_ip.c_str(), discoveryInfo->m_port );
//gameOwner->m_realmSocket->send( notify );
//
//member->m_state = RealmUser::UserState::InGameLobby;
//
//NotifyGameDiscovered notifyGameDiscover( discoveryInfo->m_ip.c_str(), discoveryInfo->m_port );
//member->m_realmSocket->send( notifyGameDiscover );
// Notify the host that a user has joined the game
//NotifyClientRequestConnect notify( "192.168.1.248", 47115 );
//NotifyClientRequestConnect notify( "192.168.1.248", 40820 );
//gameOwner->m_realmSocket->send( notify );
// Notify the user that the host has accepted the connection
//NotifyGameDiscovered notify2( gameOwner->discovery.ip, gameOwner->discovery.port );
//member->m_realmSocket->send( notify2 );
}
if( test == 2 )
{
//NotifyGameDiscovered msg2( "192.168.1.248", 3001 );
//member->m_realmSocket->send( msg2 );
//NotifyClientDiscovered msg1( "192.168.1.248", 3001 );
//member->m_realmSocket->send( msg1 );
NotifyClientRequestConnect notify( "127.0.0.1", 3001 );
member->m_realmSocket->send( notify );
}
}
test = 0;
} }
bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel ) bool GameSessionManager::CreatePublicGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel )
{ {
auto &hostSessionId = owner->m_sessionId;
// Check if the host session id is already in use
for( auto &gameSession : m_publicGameSessionList )
{
if( gameSession->m_hostSessionId == hostSessionId )
{
Log::Error( "Host session id is already in use! [%S]", hostSessionId.c_str() );
return false;
}
}
auto new_session = std::make_shared< GameSession >(); auto new_session = std::make_shared< GameSession >();
new_session->m_gameIndex = m_gameIndex++; new_session->m_type = GameSession::GameType::Public;
new_session->m_hostSessionId = hostSessionId; new_session->m_gameIndex = m_gameIndex;
new_session->m_gameLocation = L"Kelethin";
new_session->m_gameName = gameName; new_session->m_gameName = gameName;
new_session->m_minimumLevel = minimumLevel; new_session->m_minimumLevel = minimumLevel;
new_session->m_maximumLevel = maximumLevel; new_session->m_maximumLevel = maximumLevel;
new_session->m_userList[ 0 ] = owner; new_session->m_gameData.resize(256);
m_publicGameSessionList.push_back( new_session ); owner->is_host = true;
owner->is_ready = false;
owner->discovery_addr = "";
owner->discovery_port = 0;
owner->discovery_state = DiscoveryState::Initial_Connect;
owner->game_id = m_gameIndex;
new_session->m_userList.push_back(owner);
m_gameSessionList.push_back( new_session );
m_gameIndex++;
return true; return true;
} }
bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel ) bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel )
{ {
auto &hostSessionId = owner->m_sessionId;
// Check if the game name or host session id is already in use // Check if the game name or host session id is already in use
for( auto &gameSession : m_privateGameSessionList ) for( auto &gameSession : m_gameSessionList )
{ {
if( gameSession->m_hostSessionId == hostSessionId ) if( gameSession->m_type != GameSession::GameType::Private )
{ continue;
Log::Error( "Host session id is already in use! [%S]", hostSessionId.c_str() );
return false;
}
if( gameSession->m_gameName == gameName ) if( gameSession->m_gameName == gameName )
{ {
@@ -146,67 +80,63 @@ bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring
auto new_session = std::make_shared< GameSession >(); auto new_session = std::make_shared< GameSession >();
new_session->m_gameIndex = -1; new_session->m_type = GameSession::GameType::Private;
new_session->m_hostSessionId = hostSessionId; new_session->m_gameIndex = m_gameIndex;
new_session->m_gameLocation = L"Kelethin";
new_session->m_gameName = gameName; new_session->m_gameName = gameName;
new_session->m_minimumLevel = minimumLevel; new_session->m_minimumLevel = minimumLevel;
new_session->m_maximumLevel = maximumLevel; new_session->m_maximumLevel = maximumLevel;
m_privateGameSessionList.push_back( new_session ); new_session->m_gameData.resize( 256 );
owner->is_host = true;
owner->is_ready = false;
owner->discovery_addr = "";
owner->discovery_port = 0;
owner->discovery_state = DiscoveryState::Initial_Connect;
owner->game_id = m_gameIndex;
new_session->m_userList.push_back( owner );
m_gameSessionList.push_back( new_session );
m_gameIndex++;
return true;
}
bool GameSessionManager::CancelGameSession( sptr_user user )
{
auto gameId = user->game_id;
if( gameId < 0 )
{
Log::Error( "Game session not found! [%d]", gameId );
return false; return false;
} }
bool GameSessionManager::CancelGameSession( std::wstring sessionId ) auto it = std::find_if( m_gameSessionList.begin(), m_gameSessionList.end(), [ &gameId ]( sptr_game_session gameSession )
{ {
for( auto it = m_publicGameSessionList.begin(); it != m_publicGameSessionList.end(); ++it ) return gameSession->m_gameIndex == gameId;
} );
Log::Debug( "CancelGameSession : [%d]", gameId );
Log::Debug( "TODO: Notify all users in the game session" );
if( it != m_gameSessionList.end() )
{ {
if( ( *it )->m_hostSessionId == sessionId ) m_gameSessionList.erase( it );
{
Log::Debug( "Public Game session canceled! [%S]", sessionId.c_str() );
m_publicGameSessionList.erase( it );
return true; return true;
} }
}
for( auto it = m_privateGameSessionList.begin(); it != m_privateGameSessionList.end(); ++it )
{
if( ( *it )->m_hostSessionId == sessionId )
{
Log::Debug( "Private Game session canceled! [%S]", sessionId.c_str() );
m_privateGameSessionList.erase( it );
return true;
}
}
Log::Error( "Failed to cancel game session! [%S]", sessionId.c_str() );
return false; return false;
} }
sptr_game_session GameSessionManager::FindGame( const std::wstring sessionId )
{
for( auto &gameSession : m_publicGameSessionList )
{
if( gameSession->m_hostSessionId == sessionId )
{
return gameSession;
}
}
for( auto &gameSession : m_privateGameSessionList )
{
if( gameSession->m_hostSessionId == sessionId )
{
return gameSession;
}
}
return nullptr;
}
sptr_game_session GameSessionManager::FindGame( const int32_t gameId ) sptr_game_session GameSessionManager::FindGame( const int32_t gameId )
{ {
for( auto &gameSession : m_publicGameSessionList ) if (gameId < 0) return nullptr;
for( auto &gameSession : m_gameSessionList)
{ {
if( gameSession->m_gameIndex == gameId ) if( gameSession->m_gameIndex == gameId )
{ {
@@ -217,45 +147,150 @@ sptr_game_session GameSessionManager::FindGame( const int32_t gameId )
return nullptr; return nullptr;
} }
bool GameSessionManager::UserJoinGame( const int32_t gameId, sptr_user joiningUser ) bool GameSessionManager::SetGameOpen(sptr_user user)
{ {
auto gameSession = FindGame( gameId ); auto gameId = user->game_id;
auto session = FindGame(gameId);
if( gameSession == nullptr ) if (session == nullptr)
{ {
Log::Error("Game session not found! [%d]", gameId); Log::Error("Game session not found! [%d]", gameId);
return false; return false;
} }
for( auto &user : gameSession->m_userList ) if( session->m_state != GameSession::GameState::NotReady )
{ {
if( user == nullptr )
{
user = joiningUser;
user->m_state = RealmUser::UserState::JoinPending;
return true;
}
}
Log::Error( "Game session is full! [%d]", gameId );
return false; return false;
} }
void GameSessionManager::HandleJoinDiscovery( sptr_game_session gameSession, sptr_user joiningUser ) session->m_state = GameSession::GameState::Open;
{
auto discoveryInfo = DiscoveryServer::Get().GetDiscoveryInfo( joiningUser->m_sessionId );
if( !discoveryInfo.has_value() ) user->is_ready = true;
Log::Info("Game session is open! [%d]", gameId);
return true;
}
bool GameSessionManager::JoinGame(sptr_user user)
{ {
Log::Error( "Discovery info not found! [%S]", joiningUser->m_sessionId.c_str() ); auto gameId = user->game_id;
auto session = FindGame(gameId);
if (session == nullptr)
{
Log::Error("Game session not found! [%d]", gameId);
return false;
}
auto host = session->GetHost();
// Check that the user isn't already in the list.
auto it = std::find_if(session->m_userList.begin(), session->m_userList.end(), [&user](sptr_user member)
{
return member == user;
});
if (it != session->m_userList.end())
{
Log::Error("User is already in the game session! [%d]", gameId);
return false;
}
// Add the user to the game session.
user->is_host = false;
user->is_ready = false;
session->m_userList.push_back(user);
// Notify the host that a user is joining.
NotifyClientRequestConnect msg_a( user->discovery_addr, user->discovery_port );
host->tcp->send( msg_a );
// Update the user with their discovery address.
NotifyGameDiscovered msg_b( user->discovery_addr, user->discovery_port );
user->tcp->send( msg_b );
Log::Info("User [%S] joined game session! [%d]", user->session_id.c_str(), gameId);
return true;
}
void GameSessionManager::RemoveUser(sptr_user user)
{
if( user == nullptr )
{
Log::Error( "User is null!" );
return; return;
} }
Log::Debug( "Member Discovery: %s:%d", discoveryInfo->m_ip.c_str(), discoveryInfo->m_port ); auto session = FindGame( user->game_id );
auto hostUser = gameSession->GetOwner(); if( session == nullptr )
{
NotifyClientRequestConnect notify( "192.168.1.248", 3001 ); Log::Error( "Game session not found! [%d]", user->game_id );
hostUser->m_realmSocket->send( notify ); return;
}
auto it = std::find_if( session->m_userList.begin(), session->m_userList.end(), [ &user ]( sptr_user member )
{
return member == user;
} );
if( it != session->m_userList.end() )
{
session->m_userList.erase( it );
}
if( session->m_userList.empty() )
{
}
}
std::vector<sptr_game_session> GameSessionManager::GetPublicGameSessionList() const
{
std::vector< sptr_game_session > list;
for( auto &game : m_gameSessionList )
{
if( game->m_type == GameSession::GameType::Public )
{
list.push_back( game );
}
}
return list;
}
std::vector<sptr_game_session> GameSessionManager::GetAvailableGameSessionList() const
{
std::vector<sptr_game_session> list;
for( auto &game : m_gameSessionList)
{
if (game->m_type != GameSession::GameType::Public)
continue;
//if( game->m_state != GameSession::GameState::Open)
// continue;
list.push_back(game);
}
return list;
}
std::vector<sptr_game_session> GameSessionManager::GetPrivateGameSessionList() const
{
std::vector<sptr_game_session> list;
for( auto &game : m_gameSessionList )
{
if( game->m_type == GameSession::GameType::Private )
{
list.push_back( game );
}
}
return list;
} }

View File

@@ -8,8 +8,7 @@ private:
static inline std::mutex m_mutex; static inline std::mutex m_mutex;
int32_t m_gameIndex; int32_t m_gameIndex;
std::vector< sptr_game_session > m_publicGameSessionList; std::vector< sptr_game_session > m_gameSessionList;
std::vector< sptr_game_session > m_privateGameSessionList;
public: public:
GameSessionManager(); GameSessionManager();
@@ -31,15 +30,17 @@ public:
void Process(); void Process();
void ProcessGameSession( sptr_game_session gameSession ); void ProcessGameSession( sptr_game_session gameSession );
bool CreatePublicGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel ); bool CreatePublicGameSession( sptr_user user, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel );
bool CreatePrivateGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel ); bool CreatePrivateGameSession( sptr_user user, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel );
bool CancelGameSession( std::wstring sessionId ); bool CancelGameSession( sptr_user user );
sptr_game_session FindGame( const std::wstring sessionId );
sptr_game_session FindGame( const int32_t gameId ); sptr_game_session FindGame( const int32_t gameId );
bool UserJoinGame( const int32_t gameId, sptr_user joiningUser ); bool SetGameOpen( sptr_user user );
bool JoinGame( sptr_user user );
void RemoveUser( sptr_user user );
private: std::vector< sptr_game_session > GetPublicGameSessionList() const;
void HandleJoinDiscovery( sptr_game_session gameSession, sptr_user joiningUser ); std::vector< sptr_game_session > GetAvailableGameSessionList() const;
std::vector< sptr_game_session > GetPrivateGameSessionList() const;
}; };

13
Game/RealmCharacterData.h Normal file
View File

@@ -0,0 +1,13 @@
#pragma once
class RealmPlayerData
{
public:
char local_addr[24]; // [0x0]
char discovery_addr[24]; // [0x18]
int32_t discovery_port; // [0x30]
char player_name[24];
char player_class[24];
int32_t player_level;
};

View File

@@ -2,20 +2,54 @@
RealmUser::RealmUser() RealmUser::RealmUser()
{ {
m_state = UserState::MainMenu; tcp = nullptr;
m_realmSocket = nullptr; udp = std::make_shared<RealmUDPSocket>();
m_sessionId = L"";
session_id = L"";
local_addr = "";
discovery_addr = "";
discovery_port = 0;
discovery_state = DiscoveryState::None;
is_host = false;
is_ready = false;
game_id = 0;
player_name = "";
player_class = "";
player_level = 0;
} }
RealmUser::~RealmUser() RealmUser::~RealmUser()
{ {
m_state = UserState::MainMenu; if( tcp )
if( m_realmSocket )
{ {
m_realmSocket->flag.disconnected = true; tcp->flag.disconnected = true;
m_realmSocket.reset(); tcp.reset();
} }
m_sessionId = L""; if( udp )
{
udp->flag.disconnected = true;
udp.reset();
}
session_id = L"";
local_addr = "";
discovery_addr = "";
discovery_port = 0;
discovery_state = DiscoveryState::None;
is_host = false;
is_ready = false;
game_id = 0;
player_name = "";
player_class = "";
player_level = 0;
} }

View File

@@ -1,21 +1,40 @@
#pragma once #pragma once
#include "RealmCharacterData.h"
enum class DiscoveryState
{
None,
Host_Waiting,
Initial_Connect,
Negotiating,
Joining,
Joined
};
class RealmUser { class RealmUser {
public: public:
RealmUser(); RealmUser();
~RealmUser(); ~RealmUser();
enum class UserState { public:
MainMenu, sptr_tcp_socket tcp; // For Realm Lobby
DiscoveryPending, sptr_udp_socket udp; // For Discovery
JoinPending,
InGameLobby,
InGameSession,
} m_state;
sptr_tcp_socket m_realmSocket; std::wstring session_id; // Unique Session ID for the user (Generated at login)
std::wstring m_sessionId; bool is_host;
bool is_ready;
int32_t game_id;
std::string local_addr;
std::string discovery_addr;
int32_t discovery_port;
DiscoveryState discovery_state;
std::string player_name;
std::string player_class;
int32_t player_level;
}; };
typedef std::shared_ptr< RealmUser > sptr_user; typedef std::shared_ptr< RealmUser > sptr_user;

View File

@@ -21,14 +21,14 @@ std::wstring RealmUserManager::GenerateSessionId()
return sessionId; return sessionId;
} }
sptr_user RealmUserManager::CreateUser( sptr_tcp_socket socket, std::wstring userId, std::wstring userPw ) sptr_user RealmUserManager::CreateUser( sptr_tcp_socket socket )
{ {
Log::Debug( "ClientManager::CreateUser() - Created new user" ); Log::Debug( "ClientManager::CreateUser() - Created new user" );
auto user = std::make_shared< RealmUser >(); auto user = std::make_shared< RealmUser >();
user->m_sessionId = GenerateSessionId(); user->session_id = GenerateSessionId();
user->m_realmSocket = socket; user->tcp = socket;
m_users.push_back( user ); m_users.push_back( user );
@@ -40,11 +40,13 @@ void RealmUserManager::RemoveUser( sptr_user user )
auto it = std::find( m_users.begin(), m_users.end(), user ); auto it = std::find( m_users.begin(), m_users.end(), user );
if( it == m_users.end() ) if( it == m_users.end() )
{ {
Log::Error( "RemoveUser : [%S] not found", user->m_sessionId.c_str() ); Log::Error( "RemoveUser : [%S] not found", user->session_id.c_str() );
return; return;
} }
Log::Debug( "RemoveUser : [%S]", user->m_sessionId.c_str() ); GameSessionManager::Get().RemoveUser((*it));
Log::Debug( "RemoveUser : [%S]", user->session_id.c_str() );
m_users.erase( it ); m_users.erase( it );
} }
@@ -52,7 +54,7 @@ void RealmUserManager::RemoveUser( const std::wstring &sessionId )
{ {
auto it = std::find_if( m_users.begin(), m_users.end(), [ &sessionId ]( sptr_user user ) auto it = std::find_if( m_users.begin(), m_users.end(), [ &sessionId ]( sptr_user user )
{ {
return user->m_sessionId == sessionId; return user->session_id == sessionId;
} ); } );
if( it == m_users.end() ) if( it == m_users.end() )
@@ -61,15 +63,30 @@ void RealmUserManager::RemoveUser( const std::wstring &sessionId )
return; return;
} }
Log::Debug( "RemoveUser : [%S]", sessionId.c_str() ); RemoveUser((*it));
m_users.erase( it ); }
void RealmUserManager::RemoveUser( const sptr_tcp_socket socket )
{
auto it = std::find_if( m_users.begin(), m_users.end(), [ &socket ]( sptr_user user )
{
return user->tcp == socket;
} );
if( it == m_users.end() )
{
Log::Error( "RemoveUser : [%S] not found", socket->remote_ip.c_str() );
return;
}
RemoveUser((*it));
} }
sptr_user RealmUserManager::GetUser( const std::wstring &sessionId ) sptr_user RealmUserManager::GetUser( const std::wstring &sessionId )
{ {
for( auto &user : m_users ) for( auto &user : m_users )
{ {
if( user->m_sessionId == sessionId ) if( user->session_id == sessionId )
{ {
return user; return user;
} }
@@ -78,11 +95,34 @@ sptr_user RealmUserManager::GetUser( const std::wstring &sessionId )
return nullptr; return nullptr;
} }
sptr_user RealmUserManager::GetUser( sptr_tcp_socket socket ) sptr_user RealmUserManager::GetUser( const sptr_tcp_socket socket )
{
auto it = std::find_if(m_users.begin(), m_users.end(), [&socket](sptr_user user)
{
return user->tcp == socket;
});
if (it == m_users.end())
{
Log::Error("GetUser : [%S] not found", socket->remote_ip.c_str());
return nullptr;
}
return (*it);
}
sptr_user RealmUserManager::GetUserByAddress( const sockaddr_in* remoteAddr)
{ {
for( auto &user : m_users ) for( auto &user : m_users )
{ {
if( user->m_realmSocket == socket ) if( nullptr == user->udp )
{
continue;
}
// Compare the address
if( user->udp->remote_addr.sin_addr.s_addr == remoteAddr->sin_addr.s_addr &&
user->udp->remote_addr.sin_port == remoteAddr->sin_port )
{ {
return user; return user;
} }

View File

@@ -28,12 +28,15 @@ public:
public: public:
static std::wstring GenerateSessionId(); static std::wstring GenerateSessionId();
sptr_user CreateUser( sptr_tcp_socket socket, std::wstring userId, std::wstring userPw ); sptr_user CreateUser( sptr_tcp_socket socket );
void RemoveUser( sptr_user user ); void RemoveUser( sptr_user user );
void RemoveUser( const std::wstring &sessionId ); void RemoveUser( const std::wstring &sessionId );
void RemoveUser( const sptr_tcp_socket socket );
sptr_user GetUser( const std::wstring &sessionId ); sptr_user GetUser( const std::wstring &sessionId );
sptr_user GetUser( sptr_tcp_socket socket ); sptr_user GetUser( const sptr_tcp_socket socket );
sptr_user GetUserByAddress(const sockaddr_in *remoteAddr );
private: private:
}; };