49 lines
1.4 KiB
C++
49 lines
1.4 KiB
C++
#include <server/systems/DisconnectSystem.h>
|
|
|
|
#include <server/components/Disconnect.h>
|
|
#include <server/components/EnetConnection.h>
|
|
#include <blitz/components/PlayerRemove.h>
|
|
|
|
#include <server/Server.h>
|
|
|
|
#include <blitz/common/Format.h>
|
|
#include <blitz/common/Log.h>
|
|
|
|
namespace blitz {
|
|
namespace server {
|
|
|
|
DisconnectSystem::DisconnectSystem(entt::registry&, EnttWorld& a_World, Server& a_Server) : m_World(a_World), m_Server(a_Server) {}
|
|
|
|
void DisconnectSystem::Update(Nz::Time elapsedTime) {
|
|
AtomicEnttWorld world = m_World;
|
|
|
|
entt::registry& registry = world->GetRegistry();
|
|
|
|
auto disconnects = registry.view<DisconnectComponent>();
|
|
|
|
// broadcast player leave
|
|
for (auto entity : disconnects) {
|
|
auto* player = registry.try_get<PlayerInfoComponent>(entity);
|
|
if (player) {
|
|
for (auto [entity, connection] : registry.view<EnetConnectionComponent>().each()) {
|
|
connection.m_Connection->SendPlayerLeave({player->m_PlayerId});
|
|
}
|
|
LogD(Format("[Server] %s left !", player->m_Pseudo.c_str()));
|
|
}
|
|
}
|
|
|
|
// close connections
|
|
registry.view<EnetConnectionComponent, DisconnectComponent>().each(
|
|
[this](auto entity, EnetConnectionComponent& connection, DisconnectComponent disconnect) {
|
|
m_Server.CloseConnection(connection.m_Connection->GetPeerId());
|
|
});
|
|
|
|
// remove the entities
|
|
for (auto entity : disconnects) {
|
|
registry.emplace<PlayerRemoveComponent>(entity);
|
|
}
|
|
}
|
|
|
|
} // namespace server
|
|
} // namespace blitz
|