Skip to content

Commit

Permalink
fix: destroy party on disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
RobbeBryssinck committed May 4, 2022
1 parent 086c80f commit 3944b58
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 16 deletions.
33 changes: 19 additions & 14 deletions Code/client/Services/Generic/PartyService.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
#include <TiltedOnlinePCH.h>


#include <imgui.h>
#include <Events/UpdateEvent.h>

#include <Services/PartyService.h>

#include <Services/ImguiService.h>
#include <Services/TransportService.h>

#include <Events/UpdateEvent.h>
#include <Events/DisconnectedEvent.h>

#include <Messages/NotifyPlayerList.h>
#include <Messages/NotifyPartyInfo.h>
#include <Messages/NotifyPartyInvite.h>
Expand All @@ -24,6 +22,7 @@ PartyService::PartyService(entt::dispatcher& aDispatcher, TransportService& aTra
: m_transportService(aTransportService)
{
m_updateConnection = aDispatcher.sink<UpdateEvent>().connect<&PartyService::OnUpdate>(this);
m_disconnectConnection = aDispatcher.sink<DisconnectedEvent>().connect<&PartyService::OnDisconnected>(this);

m_playerListConnection = aDispatcher.sink<NotifyPlayerList>().connect<&PartyService::OnPlayerList>(this);
m_partyInfoConnection = aDispatcher.sink<NotifyPartyInfo>().connect<&PartyService::OnPartyInfo>(this);
Expand All @@ -45,16 +44,17 @@ void PartyService::OnUpdate(const UpdateEvent& acEvent) noexcept
while (itor != std::end(m_invitations))
{
if (itor->second < cCurrentTick)
{
itor = m_invitations.erase(itor);
}
else
{
++itor;
}
}
}

void PartyService::OnDisconnected(const DisconnectedEvent& acEvent) noexcept
{
DestroyParty();
}

void PartyService::OnPlayerList(const NotifyPlayerList& acPlayerList) noexcept
{
m_players = acPlayerList.Players;
Expand All @@ -80,10 +80,7 @@ void PartyService::OnPartyInvite(const NotifyPartyInvite& acPartyInvite) noexcep
void PartyService::OnPartyLeft(const NotifyPartyLeft& acPartyLeft) noexcept
{
spdlog::debug("[PartyService]: Left party");
m_inParty = false;
m_isLeader = false;
m_leaderPlayerId = -1;
m_partyMembers.clear();
DestroyParty();
}

void PartyService::OnPartyJoined(const NotifyPartyJoined& acPartyJoined) noexcept
Expand All @@ -95,3 +92,11 @@ void PartyService::OnPartyJoined(const NotifyPartyJoined& acPartyJoined) noexcep
m_leaderPlayerId = acPartyJoined.LeaderPlayerId;
m_partyMembers = acPartyJoined.PlayerIds;
}

void PartyService::DestroyParty() noexcept
{
m_inParty = false;
m_isLeader = false;
m_leaderPlayerId = -1;
m_partyMembers.clear();
}
10 changes: 8 additions & 2 deletions Code/client/Services/PartyService.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

struct ImguiService;
struct TransportService;
struct UpdateEvent;
struct DisconnectedEvent;
struct NotifyPlayerList;
struct NotifyPartyInfo;
struct NotifyPartyInvite;
struct UpdateEvent;
struct NotifyPartyJoined;
struct NotifyPartyLeft;

Expand Down Expand Up @@ -47,14 +48,18 @@ struct PartyService

protected:

void OnUpdate(const UpdateEvent& acPlayerList) noexcept;
void OnUpdate(const UpdateEvent& acEvent) noexcept;
void OnDisconnected(const DisconnectedEvent& acEvent) noexcept;
void OnPlayerList(const NotifyPlayerList& acPlayerList) noexcept;
void OnPartyInfo(const NotifyPartyInfo& acPartyInfo) noexcept;
void OnPartyInvite(const NotifyPartyInvite& acPartyInvite) noexcept;
void OnPartyJoined(const NotifyPartyJoined& acPartyJoined) noexcept;
void OnPartyLeft(const NotifyPartyLeft& acPartyLeft) noexcept;

private:

void DestroyParty() noexcept;

Map<uint32_t, String> m_players;
Map<uint32_t, uint64_t> m_invitations;
uint64_t m_nextUpdate{0};
Expand All @@ -67,6 +72,7 @@ struct PartyService
TransportService& m_transportService;

entt::scoped_connection m_updateConnection;
entt::scoped_connection m_disconnectConnection;
entt::scoped_connection m_playerListConnection;
entt::scoped_connection m_partyInfoConnection;
entt::scoped_connection m_partyInviteConnection;
Expand Down

0 comments on commit 3944b58

Please sign in to comment.