refactor: move event disptacher into service locator

This commit is contained in:
k0t9i
2023-10-16 23:38:01 +04:00
parent b948273172
commit ede55a870e
15 changed files with 160 additions and 94 deletions

View File

@@ -7,8 +7,8 @@
#include "../Factories/AbnormalEffectFactory.h"
#include "../../../Events/AbnormalEffectChangedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -27,12 +27,6 @@ namespace Interlude
AbnormalEffectRepository(const AbnormalEffectFactory& factory) :
m_Factory(factory)
{
EventDispatcher::GetInstance().Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
OnEffectToggled(evt);
});
EventDispatcher::GetInstance().Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
}
AbnormalEffectRepository() = delete;
@@ -41,6 +35,16 @@ namespace Interlude
Reset();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
OnEffectToggled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
}
void Reset() override
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);

View File

@@ -4,7 +4,7 @@
#include <shared_mutex>
#include "../Factories/ChatMessageFactory.h"
#include "../../../Events/ChatMessageCreatedEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -20,18 +20,22 @@ namespace Interlude
return m_Messages;
}
void Reset()
void Reset() override
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
m_Messages.clear();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ChatMessageCreatedEvent::name, [this](const Event& evt) {
OnMessageCreated(evt);
});
}
ChatMessageRepository(const ChatMessageFactory& factory) :
m_Factory(factory)
{
EventDispatcher::GetInstance().Subscribe(ChatMessageCreatedEvent::name, [this](const Event& evt) {
OnMessageCreated(evt);
});
}
void OnMessageCreated(const Event& evt)

View File

@@ -3,10 +3,10 @@
#include <shared_mutex>
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/HeroFactory.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Events/HeroCreatedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -25,7 +25,7 @@ namespace Interlude
if (hero) {
if (!m_Hero) {
m_Hero = m_Factory.Create(hero);
EventDispatcher::GetInstance().Dispatch(HeroCreatedEvent{});
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(HeroCreatedEvent{});
}
else
{
@@ -35,7 +35,7 @@ namespace Interlude
}
else if (m_Hero) {
m_Hero = nullptr;
EventDispatcher::GetInstance().Dispatch(HeroDeletedEvent{});
ServiceLocator::GetInstance().GetEventDispatcher()->Dispatch(HeroDeletedEvent{});
}
return result;

View File

@@ -12,7 +12,7 @@
#include "../../../Events/HeroDeletedEvent.h"
#include "../../../Events/ItemAutousedEvent.h"
#include "../../../Events/OnEndItemListEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -44,24 +44,6 @@ namespace Interlude
m_NetworkHandler(networkHandler),
m_Factory(factory)
{
EventDispatcher::GetInstance().Subscribe(ItemCreatedEvent::name, [this](const Event& evt) {
OnItemCreated(evt);
});
EventDispatcher::GetInstance().Subscribe(ItemUpdatedEvent::name, [this](const Event& evt) {
OnItemUpdated(evt);
});
EventDispatcher::GetInstance().Subscribe(ItemDeletedEvent::name, [this](const Event& evt) {
OnItemDeleted(evt);
});
EventDispatcher::GetInstance().Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
EventDispatcher::GetInstance().Subscribe(ItemAutousedEvent::name, [this](const Event& evt) {
OnItemAutoused(evt);
});
EventDispatcher::GetInstance().Subscribe(OnEndItemListEvent::name, [this](const Event& evt) {
OnEndItemList(evt);
});
}
void OnEndItemList(const Event& evt)
@@ -198,6 +180,28 @@ namespace Interlude
m_NetworkHandler.RequestItemList();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemCreatedEvent::name, [this](const Event& evt) {
OnItemCreated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemUpdatedEvent::name, [this](const Event& evt) {
OnItemUpdated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemDeletedEvent::name, [this](const Event& evt) {
OnItemDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(ItemAutousedEvent::name, [this](const Event& evt) {
OnItemAutoused(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(OnEndItemListEvent::name, [this](const Event& evt) {
OnEndItemList(evt);
});
}
private:
const ItemFactory& m_Factory;
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::BaseItem>> m_Items;

View File

@@ -5,10 +5,10 @@
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "Domain/Repositories/EntityRepositoryInterface.h"
#include "../Factories/NPCFactory.h"
#include "../../../Events/EventDispatcher.h"
#include "../../../Events/SpoiledEvent.h"
#include "../../../Events/CreatureDiedEvent.h"
#include "../../GameStructs/FindObjectsTrait.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -55,17 +55,21 @@ namespace Interlude
m_Npcs.clear();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SpoiledEvent::name, [this](const Event& evt) {
OnSpoiled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(CreatureDiedEvent::name, [this](const Event& evt) {
OnCreatureDied(evt);
});
}
NPCRepository(const NetworkHandlerWrapper& networkHandler, const NPCFactory& factory, const uint16_t radius) :
m_NetworkHandler(networkHandler),
m_Factory(factory),
m_Radius(radius)
{
EventDispatcher::GetInstance().Subscribe(SpoiledEvent::name, [this](const Event& evt) {
OnSpoiled(evt);
});
EventDispatcher::GetInstance().Subscribe(CreatureDiedEvent::name, [this](const Event& evt) {
OnCreatureDied(evt);
});
}
NPCRepository() = delete;

View File

@@ -11,9 +11,9 @@
#include "../../../Events/AbnormalEffectChangedEvent.h"
#include "../../../Events/HeroDeletedEvent.h"
#include "../../../Events/GameEngineTickedEvent.h"
#include "../../../Events/EventDispatcher.h"
#include "../GameStructs/NetworkHandlerWrapper.h"
#include "../../../Common/TimerMap.h"
#include "../../../Services/ServiceLocator.h"
using namespace L2Bot::Domain;
@@ -34,24 +34,7 @@ namespace Interlude
m_NetworkHandler(networkHandler),
m_Factory(factory)
{
EventDispatcher::GetInstance().Subscribe(SkillCreatedEvent::name, [this](const Event& evt) {
OnSkillCreated(evt);
});
EventDispatcher::GetInstance().Subscribe(SkillUsedEvent::name, [this](const Event& evt) {
OnSkillUsed(evt);
});
EventDispatcher::GetInstance().Subscribe(SkillCancelledEvent::name, [this](const Event& evt) {
OnSkillCancelled(evt);
});
EventDispatcher::GetInstance().Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
OnSkillToggled(evt);
});
EventDispatcher::GetInstance().Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
EventDispatcher::GetInstance().Subscribe(GameEngineTickedEvent::name, [this](const Event& evt) {
OnGameEngineTicked(evt);
});
}
SkillRepository() = delete;
@@ -68,6 +51,28 @@ namespace Interlude
m_NewSkills.clear();
}
void Init() override
{
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillCreatedEvent::name, [this](const Event& evt) {
OnSkillCreated(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillUsedEvent::name, [this](const Event& evt) {
OnSkillUsed(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(SkillCancelledEvent::name, [this](const Event& evt) {
OnSkillCancelled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
OnSkillToggled(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(HeroDeletedEvent::name, [this](const Event& evt) {
OnHeroDeleted(evt);
});
ServiceLocator::GetInstance().GetEventDispatcher()->Subscribe(GameEngineTickedEvent::name, [this](const Event& evt) {
OnGameEngineTicked(evt);
});
}
void OnGameEngineTicked(const Event& evt)
{
std::shared_lock<std::shared_timed_mutex>(m_Mutex);