mirror of
https://github.com/SapphireServer/Sapphire.git
synced 2025-04-23 05:07:46 +00:00
Further cleanup of event system.
This commit is contained in:
parent
ee6bf76124
commit
24d2b0331a
12 changed files with 75 additions and 51 deletions
|
@ -2,11 +2,17 @@
|
||||||
|
|
||||||
using namespace Sapphire::Common::EventSystem;
|
using namespace Sapphire::Common::EventSystem;
|
||||||
|
|
||||||
void EventDispatcher::subscribe( const Event::DescriptorType& descriptor, SlotType&& slot )
|
void EventDispatcher::subscribe( const Event::DescriptorType& descriptor, const SlotType& slot )
|
||||||
{
|
{
|
||||||
m_observers[ descriptor ].push_back( slot );
|
m_observers[ descriptor ].push_back( slot );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void EventDispatcher::subscribe( const Event::DescriptorType& descriptor, const std::vector< SlotType >& slots )
|
||||||
|
{
|
||||||
|
for( auto& slot : slots )
|
||||||
|
subscribe( descriptor, slot );
|
||||||
|
}
|
||||||
|
|
||||||
void EventDispatcher::emit( const Event& event ) const
|
void EventDispatcher::emit( const Event& event ) const
|
||||||
{
|
{
|
||||||
auto type = event.type();
|
auto type = event.type();
|
||||||
|
@ -14,8 +20,8 @@ void EventDispatcher::emit( const Event& event ) const
|
||||||
if( m_observers.find( type ) == m_observers.end() )
|
if( m_observers.find( type ) == m_observers.end() )
|
||||||
return;
|
return;
|
||||||
|
|
||||||
auto&& observers = m_observers.at( type );
|
auto& observers = m_observers.at( type );
|
||||||
|
|
||||||
for( auto&& observer : observers )
|
for( auto& observer : observers )
|
||||||
observer( event );
|
observer( event );
|
||||||
}
|
}
|
|
@ -3,6 +3,8 @@
|
||||||
#include "Event.h"
|
#include "Event.h"
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#define BIND_EVENT( v1, v2 ) std::bind( v1, v2, _1 )
|
||||||
|
|
||||||
namespace Sapphire::Common::EventSystem
|
namespace Sapphire::Common::EventSystem
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -11,7 +13,8 @@ namespace Sapphire::Common::EventSystem
|
||||||
public:
|
public:
|
||||||
using SlotType = std::function< void( const Event& ) >;
|
using SlotType = std::function< void( const Event& ) >;
|
||||||
|
|
||||||
void subscribe( const Event::DescriptorType& descriptor, SlotType&& slot );
|
void subscribe( const Event::DescriptorType& descriptor, const SlotType& slot );
|
||||||
|
void subscribe( const Event::DescriptorType& descriptor, const std::vector< SlotType >& slots );
|
||||||
|
|
||||||
void emit( const Event& event ) const;
|
void emit( const Event& event ) const;
|
||||||
|
|
||||||
|
|
|
@ -1,11 +0,0 @@
|
||||||
#pragma once
|
|
||||||
#include "Event.h"
|
|
||||||
|
|
||||||
namespace Sapphire::Common::EventSystem
|
|
||||||
{
|
|
||||||
class EventObserver
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
virtual void handleEvent( const Event& e ) = 0;
|
|
||||||
};
|
|
||||||
}
|
|
|
@ -496,7 +496,6 @@ void Player::discover( int16_t mapId, int16_t subId )
|
||||||
offset = 320 + 4 * info->data().DiscoveryIndex;
|
offset = 320 + 4 * info->data().DiscoveryIndex;
|
||||||
|
|
||||||
uint16_t index;
|
uint16_t index;
|
||||||
uint8_t bitIndex;
|
|
||||||
uint8_t value;
|
uint8_t value;
|
||||||
|
|
||||||
Util::valueToFlagByteIndexValue( subId, value, index );
|
Util::valueToFlagByteIndexValue( subId, value, index );
|
||||||
|
|
|
@ -9,11 +9,9 @@ namespace Sapphire::Common::EventSystem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr DescriptorType descriptor = "LoginEvent";
|
static constexpr DescriptorType descriptor = "LoginEvent";
|
||||||
virtual DescriptorType type() const { return descriptor; }
|
DescriptorType type() const override { return descriptor; }
|
||||||
|
|
||||||
LoginEvent( uint64_t charId ) : characterId( charId ) {};
|
|
||||||
virtual ~LoginEvent() = default;
|
|
||||||
|
|
||||||
|
explicit LoginEvent( uint64_t charId ) : characterId( charId ) {};
|
||||||
|
|
||||||
uint64_t characterId;
|
uint64_t characterId;
|
||||||
};
|
};
|
||||||
|
@ -22,13 +20,12 @@ namespace Sapphire::Common::EventSystem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
static constexpr DescriptorType descriptor = "LogoutEvent";
|
static constexpr DescriptorType descriptor = "LogoutEvent";
|
||||||
virtual DescriptorType type() const { return descriptor; }
|
DescriptorType type() const override { return descriptor; }
|
||||||
|
|
||||||
LogoutEvent( uint64_t charId ) : characterId( charId ) {};
|
|
||||||
virtual ~LogoutEvent() = default;
|
|
||||||
|
|
||||||
|
LogoutEvent( uint64_t charId, bool disconnect ) : characterId( charId ), isDisconnect( disconnect ) {};
|
||||||
|
|
||||||
uint64_t characterId;
|
uint64_t characterId;
|
||||||
|
bool isDisconnect;
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
|
@ -271,6 +271,7 @@ void FreeCompanyMgr::onFcLogin( const Common::EventSystem::Event& e )
|
||||||
|
|
||||||
void FreeCompanyMgr::onFcLogout( const Common::EventSystem::Event& e )
|
void FreeCompanyMgr::onFcLogout( const Common::EventSystem::Event& e )
|
||||||
{
|
{
|
||||||
|
Logger::debug( "{}", __FUNCTION__ );
|
||||||
const auto& logoutEvent = static_cast< const Common::EventSystem::LogoutEvent& >( e );
|
const auto& logoutEvent = static_cast< const Common::EventSystem::LogoutEvent& >( e );
|
||||||
auto& server = Common::Service< World::WorldServer >::ref();
|
auto& server = Common::Service< World::WorldServer >::ref();
|
||||||
auto player = server.getPlayer( logoutEvent.characterId );
|
auto player = server.getPlayer( logoutEvent.characterId );
|
||||||
|
|
|
@ -5,7 +5,6 @@
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "ForwardsZone.h"
|
#include "ForwardsZone.h"
|
||||||
#include "FreeCompany/FreeCompany.h"
|
#include "FreeCompany/FreeCompany.h"
|
||||||
#include <Event/Observer.h>
|
|
||||||
#include <Event/EventDefinitions/EventDefinitions.h>
|
#include <Event/EventDefinitions/EventDefinitions.h>
|
||||||
|
|
||||||
namespace Sapphire::World::Manager
|
namespace Sapphire::World::Manager
|
||||||
|
|
|
@ -171,15 +171,27 @@ void PartyMgr::onDisband( Entity::Player& disbandingPlayer )
|
||||||
|
|
||||||
void PartyMgr::onMoveZone( Sapphire::Entity::Player &movingPlayer )
|
void PartyMgr::onMoveZone( Sapphire::Entity::Player &movingPlayer )
|
||||||
{
|
{
|
||||||
|
if( movingPlayer.getPartyId() == 0 )
|
||||||
|
return;
|
||||||
auto party = getParty( movingPlayer.getPartyId() );
|
auto party = getParty( movingPlayer.getPartyId() );
|
||||||
assert( party );
|
assert( party );
|
||||||
sendPartyUpdate( *party );
|
sendPartyUpdate( *party );
|
||||||
}
|
}
|
||||||
|
|
||||||
void PartyMgr::onMemberDisconnect( Entity::Player& disconnectingPlayer )
|
void PartyMgr::onMemberLogout( const Common::EventSystem::Event& e )
|
||||||
{
|
{
|
||||||
|
Logger::debug( "{}", __FUNCTION__ );
|
||||||
|
const auto& logoutEvent = dynamic_cast< const Common::EventSystem::LogoutEvent& >( e );
|
||||||
auto& server = Common::Service< World::WorldServer >::ref();
|
auto& server = Common::Service< World::WorldServer >::ref();
|
||||||
auto party = getParty( disconnectingPlayer.getPartyId() );
|
auto pMember = server.getPlayer( logoutEvent.characterId );
|
||||||
|
|
||||||
|
if( !pMember )
|
||||||
|
return;
|
||||||
|
|
||||||
|
if( pMember->getPartyId() == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
auto party = getParty( pMember->getPartyId() );
|
||||||
assert( party );
|
assert( party );
|
||||||
auto members = getPartyMembers( *party );
|
auto members = getPartyMembers( *party );
|
||||||
auto pLeader = getPartyLeader( *party );
|
auto pLeader = getPartyLeader( *party );
|
||||||
|
@ -203,7 +215,7 @@ void PartyMgr::onMemberDisconnect( Entity::Player& disconnectingPlayer )
|
||||||
for( const auto& member : members )
|
for( const auto& member : members )
|
||||||
{
|
{
|
||||||
// TODO: 2nd argument here makes it automatically send passing leadership message
|
// TODO: 2nd argument here makes it automatically send passing leadership message
|
||||||
server.queueForPlayer( member->getCharacterId(), { makePcPartyUpdate( disconnectingPlayer, UpdateStatus::OFFLINE_MEMBER, party->PartyCount ),
|
server.queueForPlayer( member->getCharacterId(), { makePcPartyUpdate( *pMember, UpdateStatus::OFFLINE_MEMBER, party->PartyCount ),
|
||||||
makeZonePacket< FFXIVIpcUpdateParty >( member->getId() ) } );
|
makeZonePacket< FFXIVIpcUpdateParty >( member->getId() ) } );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,6 +7,8 @@
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
|
|
||||||
|
#include <Event/EventDefinitions/EventDefinitions.h>
|
||||||
|
|
||||||
namespace Sapphire::World::Manager
|
namespace Sapphire::World::Manager
|
||||||
{
|
{
|
||||||
|
|
||||||
|
@ -56,7 +58,8 @@ namespace Sapphire::World::Manager
|
||||||
void onKick( const std::string& kickPlayerName, Entity::Player& leader );
|
void onKick( const std::string& kickPlayerName, Entity::Player& leader );
|
||||||
void onChangeLeader( const std::string& newLeaderName, Entity::Player& oldLeader );
|
void onChangeLeader( const std::string& newLeaderName, Entity::Player& oldLeader );
|
||||||
|
|
||||||
void onMemberDisconnect( Entity::Player& disconnectingPlayer );
|
void onMemberLogout( const Common::EventSystem::Event& e );
|
||||||
|
|
||||||
void onMemberRejoin( Entity::Player& joiningPlayer );
|
void onMemberRejoin( Entity::Player& joiningPlayer );
|
||||||
|
|
||||||
void onJoinBuddy( Entity::Player& buddyOwner, Party& party );
|
void onJoinBuddy( Entity::Player& buddyOwner, Party& party );
|
||||||
|
|
|
@ -355,13 +355,9 @@ void PlayerMgr::onLogin( const Common::EventSystem::Event& e )
|
||||||
|
|
||||||
void PlayerMgr::onLogout( const Common::EventSystem::Event& e )
|
void PlayerMgr::onLogout( const Common::EventSystem::Event& e )
|
||||||
{
|
{
|
||||||
|
Logger::debug( "{}", __FUNCTION__ );
|
||||||
const auto& logoutEvent = dynamic_cast< const Common::EventSystem::LogoutEvent& >( e );
|
const auto& logoutEvent = dynamic_cast< const Common::EventSystem::LogoutEvent& >( e );
|
||||||
auto player = *server().getPlayer( logoutEvent.characterId );
|
auto player = *server().getPlayer( logoutEvent.characterId );
|
||||||
|
|
||||||
auto& partyMgr = Common::Service< World::Manager::PartyMgr >::ref();
|
|
||||||
// send updates to mgrs
|
|
||||||
if( player.getPartyId() != 0 )
|
|
||||||
partyMgr.onMemberDisconnect( player );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerMgr::onDeath( Entity::Player& player )
|
void PlayerMgr::onDeath( Entity::Player& player )
|
||||||
|
@ -461,11 +457,7 @@ void PlayerMgr::onMoveZone( Sapphire::Entity::Player& player )
|
||||||
onGrandCompanyChanged( player );
|
onGrandCompanyChanged( player );
|
||||||
}
|
}
|
||||||
|
|
||||||
if( player.getPartyId() != 0 )
|
|
||||||
{
|
|
||||||
partyMgr.onMoveZone( player );
|
partyMgr.onMoveZone( player );
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void PlayerMgr::onUpdate( Entity::Player& player, uint64_t tickCount )
|
void PlayerMgr::onUpdate( Entity::Player& player, uint64_t tickCount )
|
||||||
|
|
|
@ -63,6 +63,7 @@
|
||||||
|
|
||||||
using namespace Sapphire::World;
|
using namespace Sapphire::World;
|
||||||
using namespace Sapphire::World::Manager;
|
using namespace Sapphire::World::Manager;
|
||||||
|
using namespace Sapphire::Common::EventSystem;
|
||||||
|
|
||||||
WorldServer::WorldServer( const std::string& configName ) :
|
WorldServer::WorldServer( const std::string& configName ) :
|
||||||
m_configName( configName ),
|
m_configName( configName ),
|
||||||
|
@ -312,16 +313,7 @@ void WorldServer::run( int32_t argc, char* argv[] )
|
||||||
Common::Service< ContentFinder >::set( contentFinder );
|
Common::Service< ContentFinder >::set( contentFinder );
|
||||||
Common::Service< Manager::TaskMgr >::set( taskMgr );
|
Common::Service< Manager::TaskMgr >::set( taskMgr );
|
||||||
|
|
||||||
using namespace Common;
|
setupEvents();
|
||||||
using namespace Manager;
|
|
||||||
using namespace std::placeholders;
|
|
||||||
|
|
||||||
dispatcher->subscribe( EventSystem::LoginEvent::descriptor, std::bind( &PlayerMgr::onLogin, pPlayerMgr, _1 ) );
|
|
||||||
dispatcher->subscribe( EventSystem::LoginEvent::descriptor, std::bind( &FreeCompanyMgr::onFcLogin, pFcMgr, _1 ) );
|
|
||||||
|
|
||||||
dispatcher->subscribe( EventSystem::LogoutEvent::descriptor, std::bind( &PlayerMgr::onLogout, pPlayerMgr, _1 ) );
|
|
||||||
dispatcher->subscribe( EventSystem::LogoutEvent::descriptor, std::bind( &FreeCompanyMgr::onFcLogout, pFcMgr, _1 ) );
|
|
||||||
|
|
||||||
|
|
||||||
Logger::info( "World server running on {0}:{1}", m_ip, m_port );
|
Logger::info( "World server running on {0}:{1}", m_ip, m_port );
|
||||||
|
|
||||||
|
@ -334,6 +326,35 @@ void WorldServer::run( int32_t argc, char* argv[] )
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void WorldServer::setupEvents()
|
||||||
|
{
|
||||||
|
Logger::info( "Setting up events" );
|
||||||
|
|
||||||
|
auto& playerMgr = Common::Service< Manager::PlayerMgr >::ref();
|
||||||
|
auto& partyMgr = Common::Service< Manager::PartyMgr >::ref();
|
||||||
|
auto& fcMgr = Common::Service< Manager::FreeCompanyMgr >::ref();
|
||||||
|
|
||||||
|
auto& dispatcher = Common::Service< EventDispatcher >::ref();
|
||||||
|
|
||||||
|
using namespace Common;
|
||||||
|
using namespace Manager;
|
||||||
|
using namespace std::placeholders;
|
||||||
|
|
||||||
|
dispatcher.subscribe( LoginEvent::descriptor,
|
||||||
|
{
|
||||||
|
BIND_EVENT( &PlayerMgr::onLogin, &playerMgr ),
|
||||||
|
BIND_EVENT( &FreeCompanyMgr::onFcLogin, &fcMgr )
|
||||||
|
} );
|
||||||
|
|
||||||
|
dispatcher.subscribe( LogoutEvent::descriptor,
|
||||||
|
{
|
||||||
|
BIND_EVENT( &PlayerMgr::onLogout, &playerMgr ),
|
||||||
|
BIND_EVENT( &FreeCompanyMgr::onFcLogout, &fcMgr ),
|
||||||
|
BIND_EVENT( &PartyMgr::onMemberLogout, &partyMgr )
|
||||||
|
} );
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
uint16_t WorldServer::getWorldId() const
|
uint16_t WorldServer::getWorldId() const
|
||||||
{
|
{
|
||||||
return m_worldId;
|
return m_worldId;
|
||||||
|
@ -419,7 +440,7 @@ void WorldServer::updateSessions( uint32_t currTime )
|
||||||
player.addOnlineStatus( Common::OnlineStatus::Offline );
|
player.addOnlineStatus( Common::OnlineStatus::Offline );
|
||||||
|
|
||||||
auto dispatcher = Common::Service< Common::EventSystem::EventDispatcher >::ref();
|
auto dispatcher = Common::Service< Common::EventSystem::EventDispatcher >::ref();
|
||||||
dispatcher.emit( Common::EventSystem::LogoutEvent( player.getCharacterId() ) );
|
dispatcher.emit( Common::EventSystem::LogoutEvent( player.getCharacterId(), !player.isMarkedForRemoval() ) );
|
||||||
Logger::info( "[{0}] Session removal", session->getId() );
|
Logger::info( "[{0}] Session removal", session->getId() );
|
||||||
session->close();
|
session->close();
|
||||||
sessionRemovalQueue.push( session->getId() );
|
sessionRemovalQueue.push( session->getId() );
|
||||||
|
|
|
@ -47,6 +47,8 @@ namespace Sapphire::World
|
||||||
|
|
||||||
void printBanner() const;
|
void printBanner() const;
|
||||||
|
|
||||||
|
void setupEvents();
|
||||||
|
|
||||||
bool loadSettings( int32_t argc, char* argv[] );
|
bool loadSettings( int32_t argc, char* argv[] );
|
||||||
|
|
||||||
std::string getPlayerNameFromDb( uint64_t characterId, bool forceDbLoad = false );
|
std::string getPlayerNameFromDb( uint64_t characterId, bool forceDbLoad = false );
|
||||||
|
|
Loading…
Add table
Reference in a new issue