Add project files.

This commit is contained in:
k0t9i
2023-01-16 15:33:32 +04:00
parent 0f6fb75cff
commit 3c20df7683
130 changed files with 7756 additions and 0 deletions

View File

@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <map>
#include "ObjectService.h"
#include "../DTO/Drop.h"
#include "../Entities/Drop.h"
#include "../Repositories/DropRepositoryInterface.h"
namespace L2Bot::Domain::Services
{
class DropService : public ObjectService<Entities::Drop, DTO::Drop>
{
public:
DropService(Repositories::DropRepositoryInterface& repository) : ObjectService(repository)
{
}
virtual ~DropService() override = default;
};
}

View File

@@ -0,0 +1,31 @@
#pragma once
#include <cstdint>
#include <map>
#include "ObjectService.h"
#include "../DTO/Hero.h"
#include "../Entities/Hero.h"
#include "../Repositories/HeroRepositoryInterface.h"
namespace L2Bot::Domain::Services
{
class HeroService : public ObjectService<Entities::Hero, DTO::Hero>
{
public:
const DTO::ObjectState<Entities::Hero> GetHero()
{
const auto map = GetObjects();
if (map.size() == 0)
{
return DTO::ObjectState <Entities::Hero>{};
}
return map[0];
}
HeroService(Repositories::HeroRepositoryInterface& repository) : ObjectService(repository)
{
}
virtual ~HeroService() override = default;
};
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <map>
#include "ObjectService.h"
#include "../DTO/BaseItem.h"
#include "../ValueObjects/BaseItem.h"
#include "../Repositories/ItemRepositoryInterface.h"
namespace L2Bot::Domain::Services
{
class ItemService : public ObjectService<ValueObjects::BaseItem, DTO::BaseItem>
{
public:
ItemService(Repositories::ItemRepositoryInterface& repository) : ObjectService(repository)
{
}
virtual ~ItemService() override = default;
};
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <map>
#include "ObjectService.h"
#include "../DTO/NPC.h"
#include "../Entities/NPC.h"
#include "../Repositories/NPCRepositoryInterface.h"
namespace L2Bot::Domain::Services
{
class NPCService : public ObjectService<Entities::NPC, DTO::NPC>
{
public:
NPCService(Repositories::NPCRepositoryInterface& repository) : ObjectService(repository)
{
}
virtual ~NPCService() override = default;
};
}

View 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;
};
}

View File

@@ -0,0 +1,99 @@
#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 ObjectServicePtr
{
public:
ObjectServicePtr(Repositories::ObjectRepositoryInterface<U>& repository) : m_Repository(repository)
{
}ObjectServicePtr() = delete;
virtual ~ObjectServicePtr() = 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.get())) {
m_ObjectStates[kvp.first].object->UpdateFromDTO(dto.get());
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;
};
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <map>
#include "ObjectService.h"
#include "../DTO/Player.h"
#include "../Entities/Player.h"
#include "../Repositories/PlayerRepositoryInterface.h"
namespace L2Bot::Domain::Services
{
class PlayerService : public ObjectService<Entities::Player, DTO::Player>
{
public:
PlayerService(Repositories::PlayerRepositoryInterface& repository) : ObjectService(repository)
{
}
virtual ~PlayerService() override = default;
};
}

View File

@@ -0,0 +1,20 @@
#pragma once
#include <cstdint>
#include <map>
#include "ObjectService.h"
#include "../DTO/Skill.h"
#include "../ValueObjects/Skill.h"
#include "../Repositories/SkillRepositoryInterface.h"
namespace L2Bot::Domain::Services
{
class SkillService : public ObjectService<ValueObjects::Skill, DTO::Skill>
{
public:
SkillService(Repositories::SkillRepositoryInterface& repository) : ObjectService(repository)
{
}
virtual ~SkillService() override = default;
};
}