mirror of
https://github.com/EQEmu/Server.git
synced 2026-05-22 08:08:25 +00:00
[Expeditions] Refactor expedition caching (#1315)
Add common expedition base class Use repository for zone and world expedition caching World now stores members and leader as Member objects instead of ids This improves readability of the caching methods and lets world cache expedition dzs and members like zone. World also now caches expeditions as unique_ptr which will be necessary for future dz callback lambdas that capture 'this' so addresses don't change on cache vector resizes.
This commit is contained in:
+71
-36
@@ -21,7 +21,9 @@
|
||||
#include "expedition_state.h"
|
||||
#include "expedition.h"
|
||||
#include "expedition_database.h"
|
||||
#include "worlddb.h"
|
||||
#include "../common/eqemu_logsys.h"
|
||||
#include "../common/repositories/expedition_members_repository.h"
|
||||
#include <algorithm>
|
||||
|
||||
ExpeditionState expedition_state;
|
||||
@@ -29,66 +31,99 @@ ExpeditionState expedition_state;
|
||||
Expedition* ExpeditionState::GetExpedition(uint32_t expedition_id)
|
||||
{
|
||||
auto it = std::find_if(m_expeditions.begin(), m_expeditions.end(),
|
||||
[&](const Expedition& expedition) { return expedition.GetID() == expedition_id; });
|
||||
[&](const std::unique_ptr<Expedition>& expedition) {
|
||||
return expedition->GetID() == expedition_id;
|
||||
});
|
||||
|
||||
return (it != m_expeditions.end()) ? &(*it) : nullptr;
|
||||
return (it != m_expeditions.end()) ? it->get() : nullptr;
|
||||
}
|
||||
|
||||
Expedition* ExpeditionState::GetExpeditionByDynamicZoneID(uint32_t dz_id)
|
||||
{
|
||||
auto it = std::find_if(m_expeditions.begin(), m_expeditions.end(),
|
||||
[&](Expedition& expedition) { return expedition.GetDynamicZone().GetID() == dz_id; });
|
||||
[&](const std::unique_ptr<Expedition>& expedition) {
|
||||
return expedition->GetDynamicZone().GetID() == dz_id;
|
||||
});
|
||||
|
||||
return (it != m_expeditions.end()) ? &(*it) : nullptr;
|
||||
return (it != m_expeditions.end()) ? it->get() : nullptr;
|
||||
}
|
||||
|
||||
void ExpeditionState::LoadActiveExpeditions()
|
||||
void ExpeditionState::CacheFromDatabase(uint32_t expedition_id)
|
||||
{
|
||||
BenchTimer benchmark;
|
||||
|
||||
m_expeditions = ExpeditionDatabase::LoadExpeditions();
|
||||
|
||||
auto elapsed = benchmark.elapsed();
|
||||
LogExpeditions("World caching [{}] expeditions took [{}s]", m_expeditions.size(), elapsed);
|
||||
}
|
||||
|
||||
void ExpeditionState::AddExpedition(uint32_t expedition_id)
|
||||
{
|
||||
if (expedition_id == 0)
|
||||
if (expedition_id == 0 || GetExpedition(expedition_id))
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
auto expedition = ExpeditionDatabase::LoadExpedition(expedition_id);
|
||||
auto expedition = ExpeditionsRepository::GetWithLeaderName(database, expedition_id);
|
||||
CacheExpeditions({ std::move(expedition) });
|
||||
}
|
||||
|
||||
if (expedition.IsValid())
|
||||
void ExpeditionState::CacheAllFromDatabase()
|
||||
{
|
||||
BenchTimer benchmark;
|
||||
|
||||
auto expeditions = ExpeditionsRepository::GetAllWithLeaderName(database);
|
||||
m_expeditions.clear();
|
||||
m_expeditions.reserve(expeditions.size());
|
||||
|
||||
CacheExpeditions(std::move(expeditions));
|
||||
|
||||
LogExpeditions("Caching [{}] expedition(s) took [{}s]", m_expeditions.size(), benchmark.elapsed());
|
||||
}
|
||||
|
||||
void ExpeditionState::CacheExpeditions(
|
||||
std::vector<ExpeditionsRepository::ExpeditionWithLeader>&& expedition_entries)
|
||||
{
|
||||
// bulk load expedition dzs and members before caching
|
||||
std::vector<uint32_t> expedition_ids;
|
||||
std::vector<uint32_t> dynamic_zone_ids;
|
||||
for (const auto& entry : expedition_entries)
|
||||
{
|
||||
auto existing_expedition = GetExpedition(expedition_id);
|
||||
if (!existing_expedition)
|
||||
expedition_ids.emplace_back(entry.id);
|
||||
dynamic_zone_ids.emplace_back(entry.dynamic_zone_id);
|
||||
}
|
||||
|
||||
auto dynamic_zones = DynamicZonesRepository::GetWithInstance(database, dynamic_zone_ids);
|
||||
auto expedition_members = ExpeditionMembersRepository::GetWithNames(database, expedition_ids);
|
||||
|
||||
for (auto& entry : expedition_entries)
|
||||
{
|
||||
auto expedition = std::make_unique<Expedition>();
|
||||
expedition->LoadRepositoryResult(std::move(entry));
|
||||
|
||||
auto dz_entry_iter = std::find_if(dynamic_zones.begin(), dynamic_zones.end(),
|
||||
[&](const DynamicZonesRepository::DynamicZoneInstance& dz_entry) {
|
||||
return dz_entry.id == entry.dynamic_zone_id;
|
||||
});
|
||||
|
||||
if (dz_entry_iter != dynamic_zones.end())
|
||||
{
|
||||
m_expeditions.emplace_back(expedition);
|
||||
expedition->SetDynamicZone(std::move(*dz_entry_iter));
|
||||
}
|
||||
|
||||
for (auto& member : expedition_members)
|
||||
{
|
||||
if (member.expedition_id == expedition->GetID())
|
||||
{
|
||||
expedition->AddMemberFromRepositoryResult(std::move(member));
|
||||
}
|
||||
}
|
||||
|
||||
m_expeditions.emplace_back(std::move(expedition));
|
||||
}
|
||||
}
|
||||
|
||||
void ExpeditionState::RemoveExpedition(uint32_t expedition_id)
|
||||
{
|
||||
m_expeditions.erase(std::remove_if(m_expeditions.begin(), m_expeditions.end(),
|
||||
[&](const Expedition& expedition) {
|
||||
return expedition.GetID() == expedition_id;
|
||||
}
|
||||
), m_expeditions.end());
|
||||
}
|
||||
|
||||
void ExpeditionState::MemberChange(uint32_t expedition_id, uint32_t character_id, bool remove)
|
||||
void ExpeditionState::MemberChange(
|
||||
uint32_t expedition_id, const ExpeditionMember& member, bool remove)
|
||||
{
|
||||
auto expedition = GetExpedition(expedition_id);
|
||||
if (expedition)
|
||||
{
|
||||
if (remove) {
|
||||
expedition->RemoveMember(character_id);
|
||||
expedition->RemoveMember(member.char_id);
|
||||
} else {
|
||||
expedition->AddMember(character_id);
|
||||
expedition->AddInternalMember(member);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,7 +133,7 @@ void ExpeditionState::RemoveAllMembers(uint32_t expedition_id)
|
||||
auto expedition = GetExpedition(expedition_id);
|
||||
if (expedition)
|
||||
{
|
||||
expedition->RemoveAllMembers();
|
||||
expedition->ClearInternalMembers();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -113,10 +148,10 @@ void ExpeditionState::Process()
|
||||
|
||||
for (auto it = m_expeditions.begin(); it != m_expeditions.end();)
|
||||
{
|
||||
bool is_deleted = it->Process();
|
||||
bool is_deleted = (*it)->Process();
|
||||
if (is_deleted)
|
||||
{
|
||||
expedition_ids.emplace_back(it->GetID());
|
||||
expedition_ids.emplace_back((*it)->GetID());
|
||||
}
|
||||
it = is_deleted ? m_expeditions.erase(it) : it + 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user