Add project files.
This commit is contained in:
100
L2BotCore/Domain/Services/ObjectService.h
Normal file
100
L2BotCore/Domain/Services/ObjectService.h
Normal file
@@ -0,0 +1,100 @@
|
||||
#pragma once
|
||||
#include <cstdint>
|
||||
#include <map>
|
||||
#include <vector>
|
||||
#include <math.h>
|
||||
#include "../DTO/ObjectState.h"
|
||||
#include "../Entities/Hero.h"
|
||||
#include "../Repositories/ObjectRepositoryInterface.h"
|
||||
|
||||
namespace L2Bot::Domain::Services
|
||||
{
|
||||
template <typename T, typename U>
|
||||
class ObjectService
|
||||
{
|
||||
public:
|
||||
ObjectService(Repositories::ObjectRepositoryInterface<U>& repository) : m_Repository(repository)
|
||||
{
|
||||
|
||||
}
|
||||
ObjectService() = delete;
|
||||
virtual ~ObjectService() = default;
|
||||
|
||||
virtual const std::vector<DTO::ObjectState<T>> GetObjects()
|
||||
{
|
||||
UpdateObjectsFromRepository();
|
||||
|
||||
std::vector<DTO::ObjectState<T>> objects;
|
||||
|
||||
for (const auto& kvp : m_ObjectStates) {
|
||||
objects.push_back(kvp.second);
|
||||
}
|
||||
|
||||
return objects;
|
||||
}
|
||||
|
||||
void Invalidate()
|
||||
{
|
||||
m_ObjectStates.clear();
|
||||
}
|
||||
|
||||
protected:
|
||||
virtual void UpdateObjectsFromRepository()
|
||||
{
|
||||
auto objects = m_Repository.GetObjects();
|
||||
|
||||
RemoveOutdatedStates();
|
||||
|
||||
|
||||
for (const auto& kvp : objects)
|
||||
{
|
||||
const auto& dto = kvp.second;
|
||||
if (m_ObjectStates.contains(kvp.first))
|
||||
{
|
||||
if (!m_ObjectStates[kvp.first].object.IsEqual(&dto)) {
|
||||
m_ObjectStates[kvp.first].object.UpdateFromDTO(&dto);
|
||||
m_ObjectStates[kvp.first].state = Enums::ObjectStateEnum::updated;
|
||||
}
|
||||
else
|
||||
{
|
||||
m_ObjectStates[kvp.first].state = Enums::ObjectStateEnum::none;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
m_ObjectStates.emplace(kvp.first, DTO::ObjectState<T>{ T::CreateFromDTO(dto), Enums::ObjectStateEnum::created });
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& kvp : m_ObjectStates)
|
||||
{
|
||||
if (!objects.contains(kvp.second.object.GetId()))
|
||||
{
|
||||
m_ObjectStates[kvp.first].object.SaveState();
|
||||
kvp.second.state = Enums::ObjectStateEnum::deleted;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
void RemoveOutdatedStates()
|
||||
{
|
||||
auto it = m_ObjectStates.begin();
|
||||
while (it != m_ObjectStates.end())
|
||||
{
|
||||
if (it->second.state == Enums::ObjectStateEnum::deleted)
|
||||
{
|
||||
m_ObjectStates.erase(it++);
|
||||
}
|
||||
else
|
||||
{
|
||||
it++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
Repositories::ObjectRepositoryInterface<U>& m_Repository;
|
||||
std::map<uint32_t, DTO::ObjectState<T>> m_ObjectStates;
|
||||
};
|
||||
}
|
Reference in New Issue
Block a user