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

View File

@@ -8,8 +8,7 @@
GameSessionManager::GameSessionManager()
{
m_gameIndex = 0;
m_publicGameSessionList.clear();
m_privateGameSessionList.clear();
m_gameSessionList.clear();
}
GameSessionManager::~GameSessionManager()
@@ -18,12 +17,7 @@ GameSessionManager::~GameSessionManager()
void GameSessionManager::Process()
{
for( auto &gameSession : m_publicGameSessionList )
{
ProcessGameSession( gameSession );
}
for( auto &gameSession : m_privateGameSessionList )
for( auto &gameSession : m_gameSessionList)
{
ProcessGameSession( gameSession );
}
@@ -31,112 +25,52 @@ void GameSessionManager::Process()
void GameSessionManager::ProcessGameSession( sptr_game_session gameSession )
{
static int test = 0;
//if( 0 == onePass ) return;
for( auto &member : gameSession->m_userList )
{
if( member == nullptr )
{
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 )
{
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 >();
new_session->m_gameIndex = m_gameIndex++;
new_session->m_hostSessionId = hostSessionId;
new_session->m_type = GameSession::GameType::Public;
new_session->m_gameIndex = m_gameIndex;
new_session->m_gameLocation = L"Kelethin";
new_session->m_gameName = gameName;
new_session->m_minimumLevel = minimumLevel;
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;
}
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
for( auto &gameSession : m_privateGameSessionList )
for( auto &gameSession : m_gameSessionList )
{
if( gameSession->m_hostSessionId == hostSessionId )
{
Log::Error( "Host session id is already in use! [%S]", hostSessionId.c_str() );
return false;
}
if( gameSession->m_type != GameSession::GameType::Private )
continue;
if( gameSession->m_gameName == gameName )
{
Log::Error( "Game name is already in use! [%S]", gameName.c_str() );
@@ -146,67 +80,63 @@ bool GameSessionManager::CreatePrivateGameSession( sptr_user owner, std::wstring
auto new_session = std::make_shared< GameSession >();
new_session->m_gameIndex = -1;
new_session->m_hostSessionId = hostSessionId;
new_session->m_type = GameSession::GameType::Private;
new_session->m_gameIndex = m_gameIndex;
new_session->m_gameLocation = L"Kelethin";
new_session->m_gameName = gameName;
new_session->m_minimumLevel = minimumLevel;
new_session->m_maximumLevel = maximumLevel;
m_privateGameSessionList.push_back( new_session );
new_session->m_gameData.resize( 256 );
return false;
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( std::wstring sessionId )
bool GameSessionManager::CancelGameSession( sptr_user user )
{
for( auto it = m_publicGameSessionList.begin(); it != m_publicGameSessionList.end(); ++it )
auto gameId = user->game_id;
if( gameId < 0 )
{
if( ( *it )->m_hostSessionId == sessionId )
{
Log::Debug( "Public Game session canceled! [%S]", sessionId.c_str() );
m_publicGameSessionList.erase( it );
return true;
}
Log::Error( "Game session not found! [%d]", gameId );
return false;
}
for( auto it = m_privateGameSessionList.begin(); it != m_privateGameSessionList.end(); ++it )
auto it = std::find_if( m_gameSessionList.begin(), m_gameSessionList.end(), [ &gameId ]( sptr_game_session gameSession )
{
if( ( *it )->m_hostSessionId == sessionId )
{
Log::Debug( "Private Game session canceled! [%S]", sessionId.c_str() );
m_privateGameSessionList.erase( it );
return true;
}
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() )
{
m_gameSessionList.erase( it );
return true;
}
Log::Error( "Failed to cancel game session! [%S]", sessionId.c_str() );
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 )
{
for( auto &gameSession : m_publicGameSessionList )
if (gameId < 0) return nullptr;
for( auto &gameSession : m_gameSessionList)
{
if( gameSession->m_gameIndex == gameId )
{
@@ -217,45 +147,150 @@ sptr_game_session GameSessionManager::FindGame( const int32_t gameId )
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;
}
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;
}
return false;
}
Log::Error( "Game session is full! [%d]", gameId );
session->m_state = GameSession::GameState::Open;
return false;
user->is_ready = true;
Log::Info("Game session is open! [%d]", gameId);
return true;
}
void GameSessionManager::HandleJoinDiscovery( sptr_game_session gameSession, sptr_user joiningUser )
bool GameSessionManager::JoinGame(sptr_user user)
{
auto discoveryInfo = DiscoveryServer::Get().GetDiscoveryInfo( joiningUser->m_sessionId );
auto gameId = user->game_id;
auto session = FindGame(gameId);
if( !discoveryInfo.has_value() )
if (session == nullptr)
{
Log::Error( "Discovery info not found! [%S]", joiningUser->m_sessionId.c_str() );
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;
}
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 )
{
Log::Error( "Game session not found! [%d]", user->game_id );
return;
}
NotifyClientRequestConnect notify( "192.168.1.248", 3001 );
hostUser->m_realmSocket->send( notify );
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;
int32_t m_gameIndex;
std::vector< sptr_game_session > m_publicGameSessionList;
std::vector< sptr_game_session > m_privateGameSessionList;
std::vector< sptr_game_session > m_gameSessionList;
public:
GameSessionManager();
@@ -31,15 +30,17 @@ public:
void Process();
void ProcessGameSession( sptr_game_session gameSession );
bool CreatePublicGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel );
bool CreatePrivateGameSession( sptr_user owner, std::wstring gameName, int32_t minimumLevel, int32_t maximumLevel );
bool CancelGameSession( std::wstring sessionId );
bool CreatePublicGameSession( sptr_user user, 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( sptr_user user );
sptr_game_session FindGame( const std::wstring sessionId );
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:
void HandleJoinDiscovery( sptr_game_session gameSession, sptr_user joiningUser );
std::vector< sptr_game_session > GetPublicGameSessionList() const;
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()
{
m_state = UserState::MainMenu;
m_realmSocket = nullptr;
m_sessionId = L"";
tcp = nullptr;
udp = std::make_shared<RealmUDPSocket>();
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()
{
m_state = UserState::MainMenu;
if( m_realmSocket )
if( tcp )
{
m_realmSocket->flag.disconnected = true;
m_realmSocket.reset();
tcp->flag.disconnected = true;
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
#include "RealmCharacterData.h"
enum class DiscoveryState
{
None,
Host_Waiting,
Initial_Connect,
Negotiating,
Joining,
Joined
};
class RealmUser {
public:
RealmUser();
~RealmUser();
enum class UserState {
MainMenu,
DiscoveryPending,
JoinPending,
InGameLobby,
InGameSession,
} m_state;
public:
sptr_tcp_socket tcp; // For Realm Lobby
sptr_udp_socket udp; // For Discovery
sptr_tcp_socket m_realmSocket;
std::wstring m_sessionId;
std::wstring session_id; // Unique Session ID for the user (Generated at login)
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;

View File

@@ -21,14 +21,14 @@ std::wstring RealmUserManager::GenerateSessionId()
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" );
auto user = std::make_shared< RealmUser >();
user->m_sessionId = GenerateSessionId();
user->m_realmSocket = socket;
user->session_id = GenerateSessionId();
user->tcp = socket;
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 );
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;
}
GameSessionManager::Get().RemoveUser((*it));
Log::Debug( "RemoveUser : [%S]", user->m_sessionId.c_str() );
Log::Debug( "RemoveUser : [%S]", user->session_id.c_str() );
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 )
{
return user->m_sessionId == sessionId;
return user->session_id == sessionId;
} );
if( it == m_users.end() )
@@ -61,15 +63,30 @@ void RealmUserManager::RemoveUser( const std::wstring &sessionId )
return;
}
Log::Debug( "RemoveUser : [%S]", sessionId.c_str() );
m_users.erase( it );
RemoveUser((*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 )
{
for( auto &user : m_users )
{
if( user->m_sessionId == sessionId )
if( user->session_id == sessionId )
{
return user;
}
@@ -78,15 +95,38 @@ sptr_user RealmUserManager::GetUser( const std::wstring &sessionId )
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 )
{
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 nullptr;
}
}

View File

@@ -28,12 +28,15 @@ public:
public:
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( const std::wstring &sessionId );
void RemoveUser( const sptr_tcp_socket socket );
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:
};