eqemu-server/world/dynamic_zone.cpp
hg 55155ff800
[Expeditions] Move expedition code into DynamicZone (#4672)
This removes the separate Expedition class and moves lockout code and
/dz command handlers into DynamicZone classes. It also refactors some
code to reduce bloat and some database usage.

This completes the effort of moving everything to DynamicZone that
started when implementing shared tasks. It also makes sense to do this
since expeditions are just dynamic zones internally despite dzs being
used for other types. Expedition specific things are just handled with
dz type checks.

Functionally nothing should change. This is mainly internal refactoring
and moving code around along with some bug fixes and reduced database
usage.

Main changes:

 - The `expeditions` database table has been removed

 - Expeditions no longer use a separate id, the expedition id is just the dz id

 - Expedition lock state and replay timer option were moved to the
   `dynamic_zones` table

 - Expeditions no longer have a separate cache from dynamic zones

 - Expedition creation no longer has every zone query the database to cache it

 - Expedition internal lockouts are now stored on DynamicZone

 - The `expedition_lockouts` table has been renamed to `dynamic_zone_lockouts`

 - Fixed a small bug with the UpdateLockoutDuration api where the
   internal lockout would get the time added twice in memory in the
   initiating zone (this api is likely rarely used)

 - Fixed an issue where use of the group/raid DoesAnyMemberHaveExpeditionLockout
   api would query once for every out of zone character.

   - This api now checks all members in the current zone first and only
     performs a single bulk query for out of zone members if that check
     is exhausted

 - Deprecated the max_check_count param of DoesAnyMemberHaveExpeditionLockout,
   the quest api still exists to avoid api break but a passed arg has no effect
2025-02-15 18:40:35 -06:00

270 lines
7.8 KiB
C++

#include "dynamic_zone.h"
#include "cliententry.h"
#include "clientlist.h"
#include "dynamic_zone_manager.h"
#include "worlddb.h"
#include "zonelist.h"
#include "zoneserver.h"
#include "../common/eqemu_logsys.h"
#include "../common/repositories/instance_list_repository.h"
extern ClientList client_list;
extern ZSList zoneserver_list;
Database& DynamicZone::GetDatabase()
{
return database;
}
bool DynamicZone::SendServerPacket(ServerPacket* packet)
{
return zoneserver_list.SendPacket(packet);
}
DynamicZone* DynamicZone::FindDynamicZoneByID(uint32_t dz_id)
{
auto dz = dynamic_zone_manager.dynamic_zone_cache.find(dz_id);
if (dz != dynamic_zone_manager.dynamic_zone_cache.end())
{
return dz->second.get();
}
return nullptr;
}
void DynamicZone::ChooseNewLeader()
{
if (m_members.empty() || !m_choose_leader_cooldown_timer.Check())
{
m_choose_leader_needed = true;
return;
}
auto it = std::find_if(m_members.begin(), m_members.end(), [&](const DynamicZoneMember& member) {
if (member.id != GetLeaderID() && member.IsOnline()) {
auto member_cle = client_list.FindCLEByCharacterID(member.id);
return (member_cle && member_cle->GetOnline() == CLE_Status::InZone);
}
return false;
});
if (it == m_members.end())
{
// no online members found, fallback to choosing any member
it = std::find_if(m_members.begin(), m_members.end(),
[&](const DynamicZoneMember& member) { return member.id != GetLeaderID(); });
}
if (it != m_members.end() && SetNewLeader(it->id))
{
m_choose_leader_needed = false;
}
}
bool DynamicZone::SetNewLeader(uint32_t member_id)
{
auto new_leader = GetMemberData(member_id);
if (!new_leader.IsValid())
{
return false;
}
LogDynamicZonesDetail("Replacing dz [{}] leader [{}] with [{}]", GetID(), GetLeaderName(), new_leader.name);
SetLeader(new_leader, true);
SendZonesLeaderChanged();
return true;
}
void DynamicZone::CheckLeader()
{
if (m_choose_leader_needed)
{
ChooseNewLeader();
}
}
DynamicZoneStatus DynamicZone::Process()
{
DynamicZoneStatus status = DynamicZoneStatus::Normal;
// force expire if no members
if (!HasMembers() || IsExpired())
{
status = DynamicZoneStatus::Expired;
auto dz_zoneserver = zoneserver_list.FindByInstanceID(GetInstanceID());
if (!dz_zoneserver || dz_zoneserver->NumPlayers() == 0) // no clients inside dz
{
status = DynamicZoneStatus::ExpiredEmpty;
if (!HasMembers() && !m_is_pending_early_shutdown && RuleB(DynamicZone, EmptyShutdownEnabled))
{
SetSecondsRemaining(RuleI(DynamicZone, EmptyShutdownDelaySeconds));
m_is_pending_early_shutdown = true;
}
}
}
if (GetType() == DynamicZoneType::Expedition && status != DynamicZoneStatus::ExpiredEmpty)
{
CheckExpireWarning();
CheckLeader();
}
return status;
}
void DynamicZone::SendZonesDynamicZoneDeleted()
{
uint32_t pack_size = sizeof(ServerDzID_Struct);
auto pack = std::make_unique<ServerPacket>(ServerOP_DzDeleted, pack_size);
auto buf = reinterpret_cast<ServerDzID_Struct*>(pack->pBuffer);
buf->dz_id = GetID();
zoneserver_list.SendPacket(pack.get());
}
void DynamicZone::SetSecondsRemaining(uint32_t seconds_remaining)
{
auto now = std::chrono::system_clock::now();
auto new_remaining = std::chrono::seconds(seconds_remaining);
auto current_remaining = m_expire_time - now;
if (current_remaining > new_remaining) // reduce only
{
LogDynamicZonesDetail("Updating dynamic zone [{}] instance [{}] seconds remaining to [{}]s",
GetID(), GetInstanceID(), seconds_remaining);
// preserve original start time and adjust duration instead
m_expire_time = now + new_remaining;
m_duration = std::chrono::duration_cast<std::chrono::seconds>(m_expire_time - m_start_time);
InstanceListRepository::UpdateDuration(database,
GetInstanceID(), static_cast<uint32_t>(m_duration.count()));
SendZonesDurationUpdate(); // update zone caches and actual instance's timer
}
}
void DynamicZone::SendZonesDurationUpdate()
{
constexpr uint32_t packsize = sizeof(ServerDzSetDuration_Struct);
auto pack = std::make_unique<ServerPacket>(ServerOP_DzDurationUpdate, packsize);
auto packbuf = reinterpret_cast<ServerDzSetDuration_Struct*>(pack->pBuffer);
packbuf->dz_id = GetID();
packbuf->seconds = static_cast<uint32_t>(m_duration.count());
zoneserver_list.SendPacket(pack.get());
}
void DynamicZone::SendZonesLeaderChanged()
{
uint32_t pack_size = sizeof(ServerDzLeaderID_Struct);
auto pack = std::make_unique<ServerPacket>(ServerOP_DzLeaderChanged, pack_size);
auto buf = reinterpret_cast<ServerDzLeaderID_Struct*>(pack->pBuffer);
buf->dz_id = GetID();
buf->leader_id = GetLeaderID();
zoneserver_list.SendPacket(pack.get());
}
void DynamicZone::ProcessMemberAddRemove(const DynamicZoneMember& member, bool removed)
{
DynamicZoneBase::ProcessMemberAddRemove(member, removed);
if (GetType() == DynamicZoneType::Expedition && removed && member.id == GetLeaderID())
{
ChooseNewLeader();
}
}
bool DynamicZone::ProcessMemberStatusChange(uint32_t character_id, DynamicZoneMemberStatus status)
{
bool changed = DynamicZoneBase::SetInternalMemberStatus(character_id, status);
if (changed && GetType() == DynamicZoneType::Expedition)
{
// any member status update will trigger a leader fix if leader was offline
if (GetLeader().status == DynamicZoneMemberStatus::Offline && GetMemberCount() > 1)
{
ChooseNewLeader();
}
}
return changed;
}
void DynamicZone::SendZonesExpireWarning(uint32_t minutes_remaining)
{
uint32_t pack_size = sizeof(ServerDzExpireWarning_Struct);
auto pack = std::make_unique<ServerPacket>(ServerOP_DzExpireWarning, pack_size);
auto buf = reinterpret_cast<ServerDzExpireWarning_Struct*>(pack->pBuffer);
buf->dz_id = GetID();
buf->minutes_remaining = minutes_remaining;
zoneserver_list.SendPacket(pack.get());
}
void DynamicZone::SendZoneMemberStatuses(uint16_t zone_id, uint16_t instance_id)
{
uint32_t members_count = static_cast<uint32_t>(m_members.size());
uint32_t entries_size = sizeof(ServerDzMemberStatusEntry_Struct) * members_count;
uint32_t pack_size = sizeof(ServerDzMemberStatuses_Struct) + entries_size;
auto pack = std::make_unique<ServerPacket>(ServerOP_DzGetMemberStatuses, pack_size);
auto buf = reinterpret_cast<ServerDzMemberStatuses_Struct*>(pack->pBuffer);
buf->dz_id = GetID();
buf->count = members_count;
for (int i = 0; i < m_members.size(); ++i)
{
buf->entries[i].character_id = m_members[i].id;
buf->entries[i].online_status = static_cast<uint8_t>(m_members[i].status);
}
zoneserver_list.SendPacket(zone_id, instance_id, pack.get());
}
void DynamicZone::CacheMemberStatuses()
{
if (m_has_member_statuses || m_members.empty())
{
return;
}
// called when a new dz is cached to fill member statuses
std::string zone_name;
std::vector<ClientListEntry*> all_clients;
all_clients.reserve(client_list.GetClientCount());
client_list.GetClients(zone_name.c_str(), all_clients);
for (const auto& member : m_members)
{
auto it = std::find_if(all_clients.begin(), all_clients.end(),
[&](const ClientListEntry* cle) { return (cle && cle->CharID() == member.id); });
auto status = DynamicZoneMemberStatus::Offline;
if (it != all_clients.end())
{
status = DynamicZoneMemberStatus::Online;
if (IsSameDz((*it)->zone(), (*it)->instance()))
{
status = DynamicZoneMemberStatus::InDynamicZone;
}
}
SetInternalMemberStatus(member.id, status);
}
m_has_member_statuses = true;
}
void DynamicZone::CheckExpireWarning()
{
if (m_warning_cooldown_timer.Check(false))
{
using namespace std::chrono_literals;
auto remaining = GetDurationRemaining();
if ((remaining > 14min && remaining < 15min) ||
(remaining > 4min && remaining < 5min) ||
(remaining > 0min && remaining < 1min))
{
int minutes = std::chrono::duration_cast<std::chrono::minutes>(remaining).count() + 1;
SendZonesExpireWarning(minutes);
m_warning_cooldown_timer.Start(120000); // 2 minute cooldown after a warning
}
}
}