Skip to content

Commit

Permalink
feat: party leader hosts all actors
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed May 29, 2022
1 parent 7c6a9ed commit 0bb89bc
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 27 deletions.
65 changes: 40 additions & 25 deletions Code/server/Services/CharacterService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ void CharacterService::OnAssignCharacterRequest(const PacketEvent<AssignCharacte
if (!isCustom)
{
// Look for the character
auto view = m_world.view<FormIdComponent, ActorValuesComponent, CharacterComponent, MovementComponent, CellIdComponent>();
auto view = m_world.view<FormIdComponent, ActorValuesComponent, CharacterComponent, MovementComponent, CellIdComponent, OwnerComponent>();

const auto itor = std::find_if(std::begin(view), std::end(view), [view, refId](auto entity)
{
Expand All @@ -206,6 +206,19 @@ void CharacterService::OnAssignCharacterRequest(const PacketEvent<AssignCharacte
auto& characterComponent = view.get<CharacterComponent>(*itor);
auto& movementComponent = view.get<MovementComponent>(*itor);
auto& cellIdComponent = view.get<CellIdComponent>(*itor);
auto& ownerComponent = view.get<OwnerComponent>(*itor);

auto& partyService = m_world.GetPartyService();

if (partyService.IsPlayerInParty(acMessage.pPlayer) && partyService.IsPlayerLeader(acMessage.pPlayer))
{
PartyService::Party* pParty = partyService.GetPlayerParty(acMessage.pPlayer);
Player* pOwningPlayer = view.get<OwnerComponent>(*itor).GetOwner();

// Transfer ownership if owning player is in the same party as the owner
if (std::find(pParty->Members.begin(), pParty->Members.end(), pOwningPlayer) != pParty->Members.end())
TransferOwnership(acMessage.pPlayer, World::ToInteger(*itor));
}

AssignCharacterResponse response;
response.Cookie = message.Cookie;
Expand Down Expand Up @@ -334,30 +347,7 @@ void CharacterService::OnCharacterRemoveEvent(const CharacterRemoveEvent& acEven

void CharacterService::OnOwnershipClaimRequest(const PacketEvent<RequestOwnershipClaim>& acMessage) const noexcept
{
auto& message = acMessage.Packet;

//const OwnerView<CharacterComponent, CellIdComponent> view(m_world, acMessage.GetSender());
auto view = m_world.view<OwnerComponent>();
const auto it = view.find(static_cast<entt::entity>(message.ServerId));
if (it == view.end())
{
spdlog::warn("Client {:X} requested ownership of an entity that doesn't exist ({:X})!", acMessage.pPlayer->GetConnectionId(), message.ServerId);
return;
}

auto& characterOwnerComponent = view.get<OwnerComponent>(*it);

if (characterOwnerComponent.GetOwner() != acMessage.pPlayer)
{
NotifyRelinquishControl notify;
notify.ServerId = message.ServerId;
characterOwnerComponent.pOwner->Send(notify);
}

characterOwnerComponent.SetOwner(acMessage.pPlayer);
characterOwnerComponent.InvalidOwners.clear();

spdlog::info("\tOwnership claimed {:X}", message.ServerId);
TransferOwnership(acMessage.pPlayer, acMessage.Packet.ServerId);
}

void CharacterService::OnCharacterSpawned(const CharacterSpawnedEvent& acEvent) const noexcept
Expand Down Expand Up @@ -699,6 +689,31 @@ void CharacterService::CreateCharacter(const PacketEvent<AssignCharacterRequest>
dispatcher.trigger(CharacterSpawnedEvent(cEntity));
}

void CharacterService::TransferOwnership(Player* apPlayer, const uint32_t acServerId) const noexcept
{
//const OwnerView<CharacterComponent, CellIdComponent> view(m_world, acMessage.GetSender());
auto view = m_world.view<OwnerComponent>();
const auto it = view.find(static_cast<entt::entity>(acServerId));
if (it == view.end())
{
spdlog::warn("Client {:X} requested ownership of an entity that doesn't exist ({:X})!", apPlayer->GetConnectionId(), acServerId);
return;
}

auto& characterOwnerComponent = view.get<OwnerComponent>(*it);

if (characterOwnerComponent.GetOwner() != apPlayer)
{
NotifyRelinquishControl notify;
notify.ServerId = acServerId;
characterOwnerComponent.pOwner->Send(notify);
}

characterOwnerComponent.SetOwner(apPlayer);
characterOwnerComponent.InvalidOwners.clear();

spdlog::info("\tOwnership claimed {:X}", acServerId);
}

void CharacterService::ProcessFactionsChanges() const noexcept
{
Expand Down
1 change: 1 addition & 0 deletions Code/server/Services/CharacterService.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ struct CharacterService
void OnSubtitleRequest(const PacketEvent<SubtitleRequest>& acMessage) const noexcept;

void CreateCharacter(const PacketEvent<AssignCharacterRequest>& acMessage) const noexcept;
void TransferOwnership(Player* apPlayer, const uint32_t acServerId) const noexcept;

void ProcessFactionsChanges() const noexcept;
void ProcessMovementChanges() const noexcept;
Expand Down
28 changes: 28 additions & 0 deletions Code/server/Services/PartyService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,34 @@ const PartyService::Party* PartyService::GetById(uint32_t aId) const noexcept
return nullptr;
}

bool PartyService::IsPlayerInParty(Player* const apPlayer) const noexcept
{
return apPlayer->GetParty().JoinedPartyId.has_value();
}

bool PartyService::IsPlayerLeader(Player* const apPlayer) noexcept
{
auto& inviterPartyComponent = apPlayer->GetParty();
if (inviterPartyComponent.JoinedPartyId)
{
Party& party = m_parties[*inviterPartyComponent.JoinedPartyId];
return party.LeaderPlayerId == apPlayer->GetId();
}

return false;
}

PartyService::Party* PartyService::GetPlayerParty(Player* const apPlayer) noexcept
{
auto& inviterPartyComponent = apPlayer->GetParty();
if (inviterPartyComponent.JoinedPartyId)
{
return &m_parties[*inviterPartyComponent.JoinedPartyId];
}

return nullptr;
}

void PartyService::OnUpdate(const UpdateEvent& acEvent) noexcept
{
const auto cCurrentTick = GameServer::Get()->GetTick();
Expand Down
3 changes: 3 additions & 0 deletions Code/server/Services/PartyService.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ struct PartyService
TP_NOCOPYMOVE(PartyService);

const Party* GetById(uint32_t aId) const noexcept;
bool IsPlayerInParty(Player* const apPlayer) const noexcept;
bool IsPlayerLeader(Player* const apPlayer) noexcept;
Party* GetPlayerParty(Player* const apPlayer) noexcept;

protected:

Expand Down
2 changes: 0 additions & 2 deletions Code/server/World.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
#include <Components.h>

#include <Services/CharacterService.h>
#include <Services/PlayerService.h>
#include <Services/ObjectService.h>
#include <Services/QuestService.h>
#include <Services/ServerListService.h>
#include <Services/PartyService.h>
#include <Services/ActorValueService.h>
#include <Services/AdminService.h>
#include <Services/InventoryService.h>
Expand Down
3 changes: 3 additions & 0 deletions Code/server/World.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "Services/AdminService.h"

#include <Services/PlayerService.h>
#include <Services/PartyService.h>
#include <Services/CharacterService.h>
#include <Services/CalendarService.h>
#include <Services/QuestService.h>
Expand All @@ -24,6 +25,8 @@ struct World : entt::registry
const CharacterService& GetCharacterService() const noexcept { return ctx().at<const CharacterService>(); }
PlayerService& GetPlayerService() noexcept { return ctx().at<PlayerService>(); }
const PlayerService& GetPlayerService() const noexcept { return ctx().at<const PlayerService>(); }
PartyService& GetPartyService() noexcept { return ctx().at<PartyService>(); }
const PartyService& GetPartyService() const noexcept { return ctx().at<const PartyService>(); }
CalendarService& GetCalendarService() noexcept { return ctx().at<CalendarService>(); }
const CalendarService& GetCalendarService() const noexcept { return ctx().at<const CalendarService>(); }
QuestService& GetQuestService() noexcept { return ctx().at<QuestService>(); }
Expand Down

0 comments on commit 0bb89bc

Please sign in to comment.