feat: change dtos to entities
This commit is contained in:
89
L2BotDll/Services/EntityHandler.h
Normal file
89
L2BotDll/Services/EntityHandler.h
Normal file
@@ -0,0 +1,89 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <functional>
|
||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||
#include "Domain/DTO/EntityState.h"
|
||||
|
||||
using namespace L2Bot::Domain;
|
||||
|
||||
class EntityHandler
|
||||
{
|
||||
public:
|
||||
template<typename T>
|
||||
const std::map<uint32_t, DTO::EntityState*> GetEntities(const std::map<uint32_t, T> items, std::function<Entities::EntityInterface*(T)> callback)
|
||||
{
|
||||
RemoveOutdatedStates();
|
||||
|
||||
for (const auto& kvp : items)
|
||||
{
|
||||
const auto item = kvp.second;
|
||||
const auto newObject = callback(item);
|
||||
if (m_Objects.contains(newObject->GetId()))
|
||||
{
|
||||
if (!m_Objects[kvp.first]->GetEntity()->IsEqual(newObject)) {
|
||||
m_Objects[kvp.first]->GetEntity()->Update(newObject);
|
||||
m_Objects[kvp.first]->UpdateState(Enums::EntityStateEnum::updated);
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Objects[kvp.first]->UpdateState(Enums::EntityStateEnum::none);
|
||||
}
|
||||
delete newObject;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_Objects.emplace(
|
||||
newObject->GetId(),
|
||||
new DTO::EntityState{ newObject, Enums::EntityStateEnum::created }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& kvp : m_Objects)
|
||||
{
|
||||
if (!items.contains(kvp.second->GetEntity()->GetId()))
|
||||
{
|
||||
m_Objects[kvp.first]->GetEntity()->SaveState();
|
||||
kvp.second->UpdateState(Enums::EntityStateEnum::deleted);
|
||||
}
|
||||
}
|
||||
|
||||
return m_Objects;
|
||||
}
|
||||
|
||||
void Reset()
|
||||
{
|
||||
for (const auto& object : m_Objects)
|
||||
{
|
||||
delete object.second;
|
||||
}
|
||||
m_Objects.clear();
|
||||
}
|
||||
|
||||
EntityHandler() = default;
|
||||
virtual ~EntityHandler()
|
||||
{
|
||||
Reset();
|
||||
}
|
||||
private:
|
||||
void RemoveOutdatedStates()
|
||||
{
|
||||
auto it = m_Objects.begin();
|
||||
while (it != m_Objects.end())
|
||||
{
|
||||
if (it->second->GetState() == Enums::EntityStateEnum::deleted)
|
||||
{
|
||||
delete it->second;
|
||||
m_Objects.erase(it++);
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<uint32_t, DTO::EntityState*> m_Objects;
|
||||
};
|
@@ -1,16 +1,11 @@
|
||||
#pragma once
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <thread>
|
||||
#include <Windows.h>
|
||||
#include "Domain/Services/DropService.h"
|
||||
#include "Domain/Services/HeroService.h"
|
||||
#include "Domain/Services/NPCService.h"
|
||||
#include "Domain/Services/PlayerService.h"
|
||||
#include "Domain/Services/SkillService.h"
|
||||
#include "Domain/Services/EntityService.h"
|
||||
#include "Domain/Serializers/SerializableStateContainer.h"
|
||||
#include "Domain/Serializers/SerializerInterface.h"
|
||||
#include "Domain/Repositories/DropRepositoryInterface.h"
|
||||
#include "Domain/Repositories/SkillRepositoryInterface.h"
|
||||
#include "Domain/Repositories/EntityRepositoryInterface.h"
|
||||
#include "Domain/Transports/TransportInterface.h"
|
||||
|
||||
using namespace L2Bot::Domain;
|
||||
@@ -19,19 +14,19 @@ class WorldHandler
|
||||
{
|
||||
public:
|
||||
WorldHandler(
|
||||
Repositories::HeroRepositoryInterface& heroRepository,
|
||||
Repositories::DropRepositoryInterface& dropRepository,
|
||||
Repositories::NPCRepositoryInterface& npcRepository,
|
||||
Repositories::PlayerRepositoryInterface& playerRepository,
|
||||
Repositories::SkillRepositoryInterface& skillRepository,
|
||||
Repositories::EntityRepositoryInterface& heroRepository,
|
||||
Repositories::EntityRepositoryInterface& dropRepository,
|
||||
Repositories::EntityRepositoryInterface& npcRepository,
|
||||
Repositories::EntityRepositoryInterface& playerRepository,
|
||||
Repositories::EntityRepositoryInterface& skillRepository,
|
||||
const Serializers::SerializerInterface& serializer,
|
||||
Transports::TransportInterface& transport
|
||||
) :
|
||||
m_DropService(Services::DropService(dropRepository)),
|
||||
m_HeroService(Services::HeroService(heroRepository)),
|
||||
m_NPCService(Services::NPCService(npcRepository)),
|
||||
m_PlayerService(Services::PlayerService(playerRepository)),
|
||||
m_SkillService(Services::SkillService(skillRepository)),
|
||||
m_HeroService(Services::EntityService(heroRepository)),
|
||||
m_DropService(Services::EntityService(dropRepository)),
|
||||
m_NPCService(Services::EntityService(npcRepository)),
|
||||
m_PlayerService(Services::EntityService(playerRepository)),
|
||||
m_SkillService(Services::EntityService(skillRepository)),
|
||||
m_Serializer(serializer),
|
||||
m_Transport(transport)
|
||||
{
|
||||
@@ -120,11 +115,11 @@ private:
|
||||
{
|
||||
std::vector<Serializers::Serializable*> items
|
||||
{
|
||||
new Serializers::SerializableStateContainer<Entities::Hero>(m_HeroService.GetObjects(), "hero"),
|
||||
new Serializers::SerializableStateContainer<Entities::Drop>(m_DropService.GetObjects(), "drop"),
|
||||
new Serializers::SerializableStateContainer<Entities::NPC>(m_NPCService.GetObjects(), "npc"),
|
||||
new Serializers::SerializableStateContainer<Entities::Player>(m_PlayerService.GetObjects(), "player"),
|
||||
new Serializers::SerializableStateContainer<ValueObjects::Skill>(m_SkillService.GetObjects(), "skill")
|
||||
new Serializers::SerializableStateContainer(m_HeroService.GetEntities(), "hero"),
|
||||
new Serializers::SerializableStateContainer(m_DropService.GetEntities(), "drop"),
|
||||
new Serializers::SerializableStateContainer(m_NPCService.GetEntities(), "npc"),
|
||||
new Serializers::SerializableStateContainer(m_PlayerService.GetEntities(), "player"),
|
||||
new Serializers::SerializableStateContainer(m_SkillService.GetEntities(), "skill"),
|
||||
};
|
||||
|
||||
std::vector<Serializers::Node> result;
|
||||
@@ -146,19 +141,19 @@ private:
|
||||
|
||||
void Invalidate()
|
||||
{
|
||||
m_HeroService.Invalidate();
|
||||
m_DropService.Invalidate();
|
||||
m_HeroService.Invalidate();
|
||||
m_NPCService.Invalidate();
|
||||
m_PlayerService.Invalidate();
|
||||
m_SkillService.Invalidate();
|
||||
}
|
||||
|
||||
private:
|
||||
Services::DropService m_DropService;
|
||||
Services::HeroService m_HeroService;
|
||||
Services::NPCService m_NPCService;
|
||||
Services::PlayerService m_PlayerService;
|
||||
Services::SkillService m_SkillService;
|
||||
Services::EntityService m_DropService;
|
||||
Services::EntityService m_HeroService;
|
||||
Services::EntityService m_NPCService;
|
||||
Services::EntityService m_PlayerService;
|
||||
Services::EntityService m_SkillService;
|
||||
const Serializers::SerializerInterface& m_Serializer;
|
||||
Transports::TransportInterface& m_Transport;
|
||||
bool m_Stopped = false;
|
||||
|
Reference in New Issue
Block a user