69 lines
2.3 KiB
C++
69 lines
2.3 KiB
C++
#include <server/handlers/PlayerLoginHandler.h>
|
|
|
|
#include <Nazara/Core/Time.hpp>
|
|
#include <blitz/components/PlayerInfo.h>
|
|
#include <server/components/EnetConnection.h>
|
|
#include <server/components/KeepAliveSession.h>
|
|
#include <server/components/ServerIdCounter.h>
|
|
|
|
namespace blitz {
|
|
namespace server {
|
|
|
|
PlayerLoginHandler::PlayerLoginHandler(network::EnetConnection& a_Connection, EnttWorld& a_World) :
|
|
protocol::PacketHandler(a_Connection, a_World) {
|
|
m_Slot.Connect(a_Connection.OnPlayerLogin,
|
|
[this](const protocol::data::PlayerLogin& a_PlayerLogin) { Handle(m_Connection.GetPeerId(), a_PlayerLogin); });
|
|
}
|
|
|
|
PlayerLoginHandler::~PlayerLoginHandler() {}
|
|
|
|
void PlayerLoginHandler::Handle(std::uint16_t a_PeerId, const protocol::data::PlayerLogin& a_PlayerLogin) {
|
|
auto world = m_World.load();
|
|
|
|
std::vector<PlayerInfoComponent> players;
|
|
|
|
for (auto [entity, playerInfo] : world->GetRegistry().view<PlayerInfoComponent>().each()) {
|
|
players.push_back(playerInfo);
|
|
}
|
|
|
|
assert(world->GetRegistry().view<ServerIdCounterComponent>().size() == 1 && "There should be only one counter !");
|
|
|
|
auto& serverCounter =
|
|
world->GetRegistry().get<ServerIdCounterComponent>(world->GetRegistry().view<ServerIdCounterComponent>().front());
|
|
|
|
EntityID newEntityId = serverCounter.m_NextEntityId++;
|
|
|
|
PlayerInfoComponent newPlayer;
|
|
|
|
for (auto [entity, connection] : world->GetRegistry().view<EnetConnectionComponent>().each()) {
|
|
if (connection.m_Connection->GetPeerId() == m_Connection.GetPeerId()) {
|
|
// players should not try to log in twice
|
|
auto previousPlayerInfo = world->GetRegistry().try_get<PlayerInfoComponent>(entity);
|
|
if (previousPlayerInfo)
|
|
return;
|
|
|
|
newPlayer = world->GetRegistry().emplace<PlayerInfoComponent>(entity, newEntityId, a_PlayerLogin.m_PlayerName);
|
|
|
|
world->GetRegistry().emplace<KeepAliveSessionComponent>(
|
|
entity, m_Connection.GetPeerId(), 69, Nz::GetElapsedMilliseconds(), true);
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
// send logging success
|
|
m_Connection.SendLoggingSuccess({newEntityId});
|
|
|
|
// send player list
|
|
if (!players.empty())
|
|
m_Connection.SendPlayerList({players});
|
|
|
|
// broadcast player join
|
|
for (auto [entity, connection] : world->GetRegistry().view<EnetConnectionComponent>().each()) {
|
|
connection.m_Connection->SendPlayerJoin({newPlayer});
|
|
}
|
|
}
|
|
|
|
} // namespace server
|
|
} // namespace blitz
|