refactor: remove unnecessary classes
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <shared_mutex>
|
||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||
@@ -9,7 +9,6 @@
|
||||
#include "../../../Events/HeroDeletedEvent.h"
|
||||
#include "../../../Events/EventDispatcher.h"
|
||||
#include "../GameStructs/NetworkHandlerWrapper.h"
|
||||
#include "../../../Services/EntityFinder.h"
|
||||
|
||||
using namespace L2Bot::Domain;
|
||||
|
||||
@@ -18,27 +17,15 @@ namespace Interlude
|
||||
class AbnormalEffectRepository : public Repositories::EntityRepositoryInterface
|
||||
{
|
||||
public:
|
||||
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
|
||||
const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() override
|
||||
{
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
const auto objects = m_EntityFinder.FindEntities<std::shared_ptr<Entities::AbnormalEffect>>(m_Effects, [this](std::shared_ptr<Entities::AbnormalEffect> item) {
|
||||
return std::make_shared<Entities::AbnormalEffect>(item.get());
|
||||
});
|
||||
|
||||
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
|
||||
|
||||
for (const auto kvp : objects)
|
||||
{
|
||||
result.push_back(kvp.second);
|
||||
}
|
||||
|
||||
return result;
|
||||
return m_Effects;
|
||||
}
|
||||
|
||||
AbnormalEffectRepository(const AbnormalEffectFactory& factory, EntityFinder& finder) :
|
||||
m_Factory(factory),
|
||||
m_EntityFinder(finder)
|
||||
AbnormalEffectRepository(const AbnormalEffectFactory& factory) :
|
||||
m_Factory(factory)
|
||||
{
|
||||
EventDispatcher::GetInstance().Subscribe(AbnormalEffectChangedEvent::name, [this](const Event& evt) {
|
||||
OnEffectToggled(evt);
|
||||
@@ -75,40 +62,48 @@ namespace Interlude
|
||||
if (evt.GetName() == AbnormalEffectChangedEvent::name)
|
||||
{
|
||||
const auto casted = static_cast<const AbnormalEffectChangedEvent&>(evt);
|
||||
const auto skillInfo = casted.GetSkillInfo();
|
||||
|
||||
const auto &actualIds = Create(casted.GetSkillInfo());
|
||||
Delete(actualIds);
|
||||
}
|
||||
}
|
||||
|
||||
std::map<uint32_t, int32_t> ids;
|
||||
for (size_t i = 0; i < skillInfo.size(); i += 3)
|
||||
private:
|
||||
const std::unordered_map<uint32_t, int32_t> Create(const std::vector<int32_t> &skillInfo)
|
||||
{
|
||||
std::unordered_map<uint32_t, int32_t> ids;
|
||||
|
||||
for (size_t i = 0; i < skillInfo.size(); i += 3)
|
||||
{
|
||||
const auto effectId = skillInfo[i];
|
||||
const auto level = skillInfo[i + 1];
|
||||
|
||||
m_Effects[effectId] = m_Factory.Create(effectId, level);
|
||||
|
||||
ids[effectId] = effectId;
|
||||
}
|
||||
|
||||
return ids;
|
||||
}
|
||||
|
||||
void Delete(const std::unordered_map<uint32_t, int32_t> &actualIds)
|
||||
{
|
||||
for (auto it = m_Effects.begin(); it != m_Effects.end();)
|
||||
{
|
||||
if (actualIds.find(it->second->GetId()) == actualIds.end())
|
||||
{
|
||||
const auto effectId = skillInfo[i];
|
||||
const auto level = skillInfo[i + 1];
|
||||
|
||||
auto effect = m_Factory.Create(effectId, level);
|
||||
m_Effects.emplace(effect->GetId(), effect);
|
||||
|
||||
ids[effectId] = effectId;
|
||||
it = m_Effects.erase(it);
|
||||
}
|
||||
|
||||
for (auto it = m_Effects.begin(); it != m_Effects.end();)
|
||||
else
|
||||
{
|
||||
const auto& effect = it->second;
|
||||
|
||||
if (ids.find(effect->GetId()) == ids.end())
|
||||
{
|
||||
it = m_Effects.erase(it);
|
||||
}
|
||||
else
|
||||
{
|
||||
++it;
|
||||
}
|
||||
++it;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const AbnormalEffectFactory& m_Factory;
|
||||
std::map<uint32_t, std::shared_ptr<Entities::AbnormalEffect>> m_Effects;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<Entities::EntityInterface>> m_Effects;
|
||||
std::shared_timed_mutex m_Mutex;
|
||||
EntityFinder& m_EntityFinder;
|
||||
};
|
||||
}
|
@@ -2,7 +2,6 @@
|
||||
|
||||
#include <vector>
|
||||
#include <shared_mutex>
|
||||
#include "Domain/Repositories/ChatMessageRepositoryInterface.h"
|
||||
#include "../Factories/ChatMessageFactory.h"
|
||||
#include "../../../Events/ChatMessageCreatedEvent.h"
|
||||
#include "../../../Events/EventDispatcher.h"
|
||||
@@ -11,17 +10,20 @@ using namespace L2Bot::Domain;
|
||||
|
||||
namespace Interlude
|
||||
{
|
||||
class ChatMessageRepository : public Repositories::ChatMessageRepositoryInterface
|
||||
class ChatMessageRepository : public Repositories::EntityRepositoryInterface
|
||||
{
|
||||
public:
|
||||
const std::vector<ValueObjects::ChatMessage> GetMessages() override
|
||||
const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() override
|
||||
{
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
const auto result = m_Messages;
|
||||
m_Messages.clear();
|
||||
return m_Messages;
|
||||
}
|
||||
|
||||
return result;
|
||||
void Reset()
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
m_Messages.clear();
|
||||
}
|
||||
|
||||
ChatMessageRepository(const ChatMessageFactory& factory) :
|
||||
@@ -39,7 +41,8 @@ namespace Interlude
|
||||
{
|
||||
const auto casted = static_cast<const ChatMessageCreatedEvent&>(evt);
|
||||
|
||||
m_Messages.push_back(m_Factory.Create(casted.GetChatMessage()));
|
||||
const auto message = m_Factory.Create(casted.GetChatMessage());
|
||||
m_Messages[message->GetId()] = message;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -48,7 +51,7 @@ namespace Interlude
|
||||
|
||||
private:
|
||||
const ChatMessageFactory& m_Factory;
|
||||
std::vector<ValueObjects::ChatMessage> m_Messages;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<Entities::EntityInterface>> m_Messages;
|
||||
std::shared_timed_mutex m_Mutex;
|
||||
};
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <memory>
|
||||
#include <shared_mutex>
|
||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||
@@ -8,7 +8,6 @@
|
||||
#include "../Factories/DropFactory.h"
|
||||
#include "../../GameStructs/FindObjectsTrait.h"
|
||||
#include "../GameStructs/NetworkHandlerWrapper.h"
|
||||
#include "../../../Services/EntityFinder.h"
|
||||
|
||||
using namespace L2Bot::Domain;
|
||||
|
||||
@@ -17,22 +16,25 @@ namespace Interlude
|
||||
class DropRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
||||
{
|
||||
public:
|
||||
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
|
||||
const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() override
|
||||
{
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
const std::map<uint32_t, Item*> items = FindAllObjects<Item*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||
const auto allItems = FindAllObjects<Item*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||
return m_NetworkHandler.GetNextItem(radius, prevId);
|
||||
});
|
||||
const auto objects = m_EntityFinder.FindEntities<Item*>(items, [this](Item* item) {
|
||||
return m_Factory.Create(item);
|
||||
});
|
||||
|
||||
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
|
||||
|
||||
for (const auto kvp : objects)
|
||||
{
|
||||
result.push_back(kvp.second);
|
||||
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> result;
|
||||
for (const auto kvp : allItems) {
|
||||
const auto item = kvp.second;
|
||||
if (m_Drops.find(item->objectId) == m_Drops.end()) {
|
||||
m_Drops[item->objectId] = m_Factory.Create(item);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Factory.Update(m_Drops[item->objectId], item);
|
||||
}
|
||||
result[item->objectId] = m_Drops[item->objectId];
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -41,14 +43,13 @@ namespace Interlude
|
||||
void Reset() override
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
m_EntityFinder.Reset();
|
||||
m_Drops.clear();
|
||||
}
|
||||
|
||||
DropRepository(const NetworkHandlerWrapper& networkHandler, const DropFactory& factory, EntityFinder& finder, const uint16_t radius) :
|
||||
DropRepository(const NetworkHandlerWrapper& networkHandler, const DropFactory& factory, const uint16_t radius) :
|
||||
m_NetworkHandler(networkHandler),
|
||||
m_Factory(factory),
|
||||
m_Radius(radius),
|
||||
m_EntityFinder(finder)
|
||||
m_Radius(radius)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -60,7 +61,7 @@ namespace Interlude
|
||||
const NetworkHandlerWrapper& m_NetworkHandler;
|
||||
const DropFactory& m_Factory;
|
||||
const uint16_t m_Radius;
|
||||
EntityFinder& m_EntityFinder;
|
||||
std::shared_timed_mutex m_Mutex;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<Entities::Drop>> m_Drops;
|
||||
};
|
||||
}
|
||||
|
@@ -7,7 +7,6 @@
|
||||
#include "../../../Events/HeroCreatedEvent.h"
|
||||
#include "../../../Events/HeroDeletedEvent.h"
|
||||
#include "../GameStructs/NetworkHandlerWrapper.h"
|
||||
#include "../../../Services/EntityFinder.h"
|
||||
|
||||
using namespace L2Bot::Domain;
|
||||
|
||||
@@ -16,39 +15,28 @@ namespace Interlude
|
||||
class HeroRepository : public Repositories::EntityRepositoryInterface
|
||||
{
|
||||
public:
|
||||
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
|
||||
const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() override
|
||||
{
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
auto hero = m_NetworkHandler.GetHero();
|
||||
const auto hero = m_NetworkHandler.GetHero();
|
||||
|
||||
if (hero != nullptr && m_PrevHero == nullptr)
|
||||
{
|
||||
EventDispatcher::GetInstance().Dispatch(HeroCreatedEvent{});
|
||||
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> result;
|
||||
if (hero) {
|
||||
if (!m_Hero) {
|
||||
m_Hero = m_Factory.Create(hero);
|
||||
EventDispatcher::GetInstance().Dispatch(HeroCreatedEvent{});
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Factory.Update(m_Hero, hero);
|
||||
}
|
||||
result[hero->objectId] = m_Hero;
|
||||
}
|
||||
if (hero == nullptr && m_PrevHero != nullptr)
|
||||
{
|
||||
else {
|
||||
m_Hero = nullptr;
|
||||
EventDispatcher::GetInstance().Dispatch(HeroDeletedEvent{});
|
||||
}
|
||||
m_PrevHero = hero;
|
||||
|
||||
std::map<uint32_t, User*> items;
|
||||
|
||||
if (hero)
|
||||
{
|
||||
items.emplace(hero->objectId, hero);
|
||||
}
|
||||
|
||||
const auto objects = m_EntityFinder.FindEntities<User*>(items, [this](User* item) {
|
||||
return m_Factory.Create(item);
|
||||
});
|
||||
|
||||
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
|
||||
|
||||
for (const auto kvp : objects)
|
||||
{
|
||||
result.push_back(kvp.second);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
@@ -56,13 +44,12 @@ namespace Interlude
|
||||
void Reset() override
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
m_EntityFinder.Reset();
|
||||
m_Hero = nullptr;
|
||||
}
|
||||
|
||||
HeroRepository(const NetworkHandlerWrapper& networkHandler, const HeroFactory& factory, EntityFinder& finder) :
|
||||
HeroRepository(const NetworkHandlerWrapper& networkHandler, const HeroFactory& factory) :
|
||||
m_NetworkHandler(networkHandler),
|
||||
m_Factory(factory),
|
||||
m_EntityFinder(finder)
|
||||
m_Factory(factory)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -73,8 +60,6 @@ namespace Interlude
|
||||
private:
|
||||
const HeroFactory& m_Factory;
|
||||
const NetworkHandlerWrapper& m_NetworkHandler;
|
||||
User* m_PrevHero = nullptr;
|
||||
EntityFinder& m_EntityFinder;
|
||||
std::shared_timed_mutex m_Mutex;
|
||||
std::shared_ptr<Entities::Hero> m_Hero;
|
||||
};
|
||||
}
|
@@ -1,12 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <shared_mutex>
|
||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||
#include "../Factories/ItemFactory.h"
|
||||
#include "../GameStructs/NetworkHandlerWrapper.h"
|
||||
#include "../../../Services/EntityFinder.h"
|
||||
#include "../../../Events/ItemCreatedEvent.h"
|
||||
#include "../../../Events/ItemUpdatedEvent.h"
|
||||
#include "../../../Events/ItemDeletedEvent.h"
|
||||
@@ -22,21 +21,11 @@ namespace Interlude
|
||||
class ItemRepository : public Repositories::EntityRepositoryInterface
|
||||
{
|
||||
public:
|
||||
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
|
||||
const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() override
|
||||
{
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
const auto objects = m_EntityFinder.FindEntities<std::shared_ptr<Entities::BaseItem>>(m_Items, [this](std::shared_ptr<Entities::BaseItem> item) {
|
||||
return m_Factory.Copy(item);
|
||||
});
|
||||
|
||||
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
|
||||
|
||||
for (const auto kvp : objects)
|
||||
{
|
||||
result.push_back(kvp.second);
|
||||
}
|
||||
|
||||
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> result(m_Items.begin(), m_Items.end());
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -51,10 +40,9 @@ namespace Interlude
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
ItemRepository(const NetworkHandlerWrapper& networkHandler, const ItemFactory& factory, EntityFinder& finder) :
|
||||
ItemRepository(const NetworkHandlerWrapper& networkHandler, const ItemFactory& factory) :
|
||||
m_NetworkHandler(networkHandler),
|
||||
m_Factory(factory),
|
||||
m_EntityFinder(finder)
|
||||
m_Factory(factory)
|
||||
{
|
||||
EventDispatcher::GetInstance().Subscribe(ItemCreatedEvent::name, [this](const Event& evt) {
|
||||
OnItemCreated(evt);
|
||||
@@ -121,10 +109,13 @@ namespace Interlude
|
||||
{
|
||||
if (kvp.second->GetItemId() == itemId)
|
||||
{
|
||||
auto ptr = dynamic_cast<Entities::EtcItem*>(kvp.second.get());
|
||||
if (ptr)
|
||||
auto casted = std::dynamic_pointer_cast<Entities::EtcItem>(kvp.second);
|
||||
if (isEnabled) {
|
||||
casted->StartAutouse();
|
||||
}
|
||||
else
|
||||
{
|
||||
ptr->Autouse(isEnabled);
|
||||
casted->StopAutouse();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -145,15 +136,18 @@ namespace Interlude
|
||||
const auto casted = static_cast<const ItemCreatedEvent&>(evt);
|
||||
const auto& data = casted.GetItemData();
|
||||
|
||||
auto item = m_Factory.Create(data);
|
||||
|
||||
if (m_Items.find(data.objectId) == m_Items.end())
|
||||
{
|
||||
m_Items.emplace(data.objectId, item);
|
||||
auto item = m_Factory.Create(data);
|
||||
if (item) {
|
||||
m_Items[data.objectId] = item;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// When equip/unequip accessories
|
||||
m_Items[data.objectId]->Update(item.get());
|
||||
m_Factory.Update(m_Items[data.objectId], data);
|
||||
}
|
||||
m_NewItems[data.objectId] = data.objectId;
|
||||
}
|
||||
@@ -173,8 +167,7 @@ namespace Interlude
|
||||
return;
|
||||
}
|
||||
|
||||
auto item = m_Factory.Create(data);
|
||||
m_Items[data.objectId]->Update(item.get());
|
||||
m_Factory.Update(m_Items[data.objectId], data);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -207,12 +200,11 @@ namespace Interlude
|
||||
|
||||
private:
|
||||
const ItemFactory& m_Factory;
|
||||
std::map<uint32_t, std::shared_ptr<Entities::BaseItem>> m_Items;
|
||||
std::map<uint32_t, uint32_t> m_NewItems;
|
||||
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::BaseItem>> m_Items;
|
||||
std::unordered_map<std::uint32_t, std::uint32_t> m_NewItems;
|
||||
bool m_IsNewCycle = true;
|
||||
uint32_t m_UsedSkillId = 0;
|
||||
std::uint32_t m_UsedSkillId = 0;
|
||||
const NetworkHandlerWrapper& m_NetworkHandler;
|
||||
std::shared_timed_mutex m_Mutex;
|
||||
EntityFinder& m_EntityFinder;
|
||||
};
|
||||
}
|
@@ -9,7 +9,6 @@
|
||||
#include "../../../Events/SpoiledEvent.h"
|
||||
#include "../../../Events/CreatureDiedEvent.h"
|
||||
#include "../../GameStructs/FindObjectsTrait.h"
|
||||
#include "../../../Services/EntityFinder.h"
|
||||
|
||||
using namespace L2Bot::Domain;
|
||||
|
||||
@@ -18,33 +17,33 @@ namespace Interlude
|
||||
class NPCRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
||||
{
|
||||
public:
|
||||
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
|
||||
const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() override
|
||||
{
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
const auto creatures = FindAllObjects<User*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||
const auto allCreatures = FindAllObjects<User*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||
return m_NetworkHandler.GetNextCreature(radius, prevId);
|
||||
});
|
||||
|
||||
std::map<uint32_t, User*> items;
|
||||
for (const auto& kvp : creatures)
|
||||
{
|
||||
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> result;
|
||||
for (const auto kvp : allCreatures) {
|
||||
const auto creature = kvp.second;
|
||||
if (creature->userType == L2::UserType::NPC) {
|
||||
items.emplace(creature->objectId, creature);
|
||||
if (creature->userType != L2::UserType::NPC) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const auto objects = m_EntityFinder.FindEntities<User*>(items, [this](User* item) {
|
||||
const auto spoilState = m_Spoiled.find(item->objectId) == m_Spoiled.end() ? Enums::SpoilStateEnum::none : m_Spoiled.at(item->objectId);
|
||||
return m_Factory.Create(item, spoilState);
|
||||
});
|
||||
if (m_Npcs.find(creature->objectId) == m_Npcs.end()) {
|
||||
m_Npcs[creature->objectId] = m_Factory.Create(creature);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Factory.Update(m_Npcs[creature->objectId], creature);
|
||||
}
|
||||
|
||||
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
|
||||
const auto spoilState = m_Spoiled.find(creature->objectId) == m_Spoiled.end() ? Enums::SpoilStateEnum::none : m_Spoiled[creature->objectId];
|
||||
m_Npcs[creature->objectId]->UpdateSpoilState(spoilState);
|
||||
|
||||
for (const auto kvp : objects)
|
||||
{
|
||||
result.push_back(kvp.second);
|
||||
result[creature->objectId] = m_Npcs[creature->objectId];
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -53,14 +52,13 @@ namespace Interlude
|
||||
void Reset() override
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
m_EntityFinder.Reset();
|
||||
m_Npcs.clear();
|
||||
}
|
||||
|
||||
NPCRepository(const NetworkHandlerWrapper& networkHandler, const NPCFactory& factory, EntityFinder& finder, const uint16_t radius) :
|
||||
NPCRepository(const NetworkHandlerWrapper& networkHandler, const NPCFactory& factory, const uint16_t radius) :
|
||||
m_NetworkHandler(networkHandler),
|
||||
m_Factory(factory),
|
||||
m_Radius(radius),
|
||||
m_EntityFinder(finder)
|
||||
m_Radius(radius)
|
||||
{
|
||||
EventDispatcher::GetInstance().Subscribe(SpoiledEvent::name, [this](const Event& evt) {
|
||||
OnSpoiled(evt);
|
||||
@@ -75,6 +73,7 @@ namespace Interlude
|
||||
|
||||
void OnSpoiled(const Event& evt)
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
if (evt.GetName() == SpoiledEvent::name)
|
||||
{
|
||||
const auto casted = static_cast<const SpoiledEvent&>(evt);
|
||||
@@ -92,6 +91,7 @@ namespace Interlude
|
||||
|
||||
void OnCreatureDied(const Event& evt)
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
if (evt.GetName() == CreatureDiedEvent::name)
|
||||
{
|
||||
const auto casted = static_cast<const CreatureDiedEvent&>(evt);
|
||||
@@ -115,7 +115,7 @@ namespace Interlude
|
||||
std::map<uint32_t, Enums::SpoilStateEnum> m_Spoiled;
|
||||
const NetworkHandlerWrapper& m_NetworkHandler;
|
||||
const uint16_t m_Radius = 0;
|
||||
EntityFinder& m_EntityFinder;
|
||||
std::shared_timed_mutex m_Mutex;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<Entities::NPC>> m_Npcs;
|
||||
};
|
||||
}
|
@@ -1,11 +1,9 @@
|
||||
#pragma once
|
||||
#include <map>
|
||||
#include <shared_mutex>
|
||||
#include <unordered_map>
|
||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||
#include "../Factories/PlayerFactory.h"
|
||||
#include "../../GameStructs/FindObjectsTrait.h"
|
||||
#include "../GameStructs/NetworkHandlerWrapper.h"
|
||||
#include "../../../Services/EntityFinder.h"
|
||||
|
||||
using namespace L2Bot::Domain;
|
||||
|
||||
@@ -14,32 +12,29 @@ namespace Interlude
|
||||
class PlayerRepository : public Repositories::EntityRepositoryInterface, public FindObjectsTrait
|
||||
{
|
||||
public:
|
||||
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
|
||||
const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() override
|
||||
{
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
const auto creatures = FindAllObjects<User*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||
const auto allCreatures = FindAllObjects<User*>(m_Radius, [this](float_t radius, int32_t prevId) {
|
||||
return m_NetworkHandler.GetNextCreature(radius, prevId);
|
||||
});
|
||||
|
||||
std::map<uint32_t, User*> items;
|
||||
for (const auto& kvp : creatures)
|
||||
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> result;
|
||||
for (const auto& kvp : allCreatures)
|
||||
{
|
||||
const auto creature = kvp.second;
|
||||
if (creature->userType == L2::UserType::USER && creature->lvl == 0) {
|
||||
items.emplace(creature->objectId, creature);
|
||||
const auto &creature = kvp.second;
|
||||
if (creature->userType != L2::UserType::USER || creature->lvl != 0) {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
const auto objects = m_EntityFinder.FindEntities<User*>(items, [this](User* item) {
|
||||
return m_Factory.Create(item);
|
||||
});
|
||||
if (m_Players.find(creature->objectId) == m_Players.end()) {
|
||||
m_Players[creature->objectId] = m_Factory.Create(creature);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Factory.Update(m_Players[creature->objectId], creature);
|
||||
}
|
||||
|
||||
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
|
||||
|
||||
for (const auto kvp : objects)
|
||||
{
|
||||
result.push_back(kvp.second);
|
||||
result[creature->objectId] = m_Players[creature->objectId];
|
||||
}
|
||||
|
||||
return result;
|
||||
@@ -48,14 +43,13 @@ namespace Interlude
|
||||
void Reset() override
|
||||
{
|
||||
std::shared_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
m_EntityFinder.Reset();
|
||||
m_Players.clear();
|
||||
}
|
||||
|
||||
PlayerRepository(const NetworkHandlerWrapper& networkHandler, const PlayerFactory& factory, EntityFinder& finder, const uint16_t radius) :
|
||||
PlayerRepository(const NetworkHandlerWrapper& networkHandler, const PlayerFactory& factory, const uint16_t radius) :
|
||||
m_NetworkHandler(networkHandler),
|
||||
m_Factory(factory),
|
||||
m_Radius(radius),
|
||||
m_EntityFinder(finder)
|
||||
m_Radius(radius)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -67,7 +61,6 @@ namespace Interlude
|
||||
const PlayerFactory& m_Factory;
|
||||
const NetworkHandlerWrapper& m_NetworkHandler;
|
||||
const uint16_t m_Radius;
|
||||
EntityFinder& m_EntityFinder;
|
||||
std::shared_timed_mutex m_Mutex;
|
||||
std::unordered_map<uint32_t, std::shared_ptr<Entities::Player>> m_Players;
|
||||
};
|
||||
}
|
@@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <unordered_map>
|
||||
#include <chrono>
|
||||
#include <shared_mutex>
|
||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||
@@ -14,7 +14,6 @@
|
||||
#include "../../../Events/EventDispatcher.h"
|
||||
#include "../GameStructs/NetworkHandlerWrapper.h"
|
||||
#include "../../../Common/TimerMap.h"
|
||||
#include "../../../Services/EntityFinder.h"
|
||||
|
||||
using namespace L2Bot::Domain;
|
||||
|
||||
@@ -23,28 +22,17 @@ namespace Interlude
|
||||
class SkillRepository : public Repositories::EntityRepositoryInterface
|
||||
{
|
||||
public:
|
||||
const std::vector<std::shared_ptr<DTO::EntityState>> GetEntities() override
|
||||
const std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> GetEntities() override
|
||||
{
|
||||
std::unique_lock<std::shared_timed_mutex>(m_Mutex);
|
||||
|
||||
const auto objects = m_EntityFinder.FindEntities<std::shared_ptr<Entities::Skill>>(m_Skills, [this](std::shared_ptr<Entities::Skill> item) {
|
||||
return std::make_shared<Entities::Skill>(item.get());
|
||||
});
|
||||
|
||||
auto result = std::vector<std::shared_ptr<DTO::EntityState>>();
|
||||
|
||||
for (const auto kvp : objects)
|
||||
{
|
||||
result.push_back(kvp.second);
|
||||
}
|
||||
|
||||
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::EntityInterface>> result(m_Skills.begin(), m_Skills.end());
|
||||
return result;
|
||||
}
|
||||
|
||||
SkillRepository(const NetworkHandlerWrapper& networkHandler, const SkillFactory& factory, EntityFinder& finder) :
|
||||
SkillRepository(const NetworkHandlerWrapper& networkHandler, const SkillFactory& factory) :
|
||||
m_NetworkHandler(networkHandler),
|
||||
m_Factory(factory),
|
||||
m_EntityFinder(finder)
|
||||
m_Factory(factory)
|
||||
{
|
||||
EventDispatcher::GetInstance().Subscribe(SkillCreatedEvent::name, [this](const Event& evt) {
|
||||
OnSkillCreated(evt);
|
||||
@@ -130,17 +118,17 @@ namespace Interlude
|
||||
|
||||
if (m_Skills.find(skillId) == m_Skills.end())
|
||||
{
|
||||
auto skill = m_Factory.Create(
|
||||
m_Skills[skillId] = m_Factory.Create(
|
||||
skillInfo[2],
|
||||
skillInfo[1],
|
||||
skillInfo[0]
|
||||
);
|
||||
m_Skills[skill->GetId()] = skill;
|
||||
}
|
||||
else
|
||||
{
|
||||
auto skill = m_Skills[skillId];
|
||||
m_Factory.Update(
|
||||
m_Skills[skillId],
|
||||
skill,
|
||||
skillInfo[2],
|
||||
skillInfo[1],
|
||||
skillInfo[0]
|
||||
@@ -198,10 +186,8 @@ namespace Interlude
|
||||
return;
|
||||
}
|
||||
|
||||
const auto& skill = m_Skills[m_UsedSkillId];
|
||||
|
||||
skill->StopCasting();
|
||||
m_CastingTimers.StopTimer(skill->GetId());
|
||||
m_Skills[m_UsedSkillId]->StopCasting();
|
||||
m_CastingTimers.StopTimer(m_UsedSkillId);
|
||||
|
||||
m_UsedSkillId = 0;
|
||||
}
|
||||
@@ -215,7 +201,7 @@ namespace Interlude
|
||||
const auto casted = static_cast<const AbnormalEffectChangedEvent&>(evt);
|
||||
const auto skillInfo = casted.GetSkillInfo();
|
||||
|
||||
std::map<uint32_t, int32_t> ids;
|
||||
std::unordered_map<uint32_t, int32_t> ids;
|
||||
|
||||
for (size_t i = 0; i < skillInfo.size(); i += 3)
|
||||
{
|
||||
@@ -244,14 +230,13 @@ namespace Interlude
|
||||
|
||||
private:
|
||||
const SkillFactory& m_Factory;
|
||||
std::map<uint32_t, std::shared_ptr<Entities::Skill>> m_Skills;
|
||||
std::map<uint32_t, uint32_t> m_NewSkills;
|
||||
std::unordered_map<std::uint32_t, std::shared_ptr<Entities::Skill>> m_Skills;
|
||||
std::unordered_map<std::uint32_t, std::uint32_t> m_NewSkills;
|
||||
bool m_IsNewCycle = true;
|
||||
uint32_t m_UsedSkillId = 0;
|
||||
const NetworkHandlerWrapper& m_NetworkHandler;
|
||||
TimerMap m_ReloadingTimers;
|
||||
TimerMap m_CastingTimers;
|
||||
std::shared_timed_mutex m_Mutex;
|
||||
EntityFinder& m_EntityFinder;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user