#include #include #include #include #include #include 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 players; for (auto [entity, playerInfo] : world->GetRegistry().view().each()) { players.push_back(playerInfo); } assert(world->GetRegistry().view().size() == 1 && "There should be only one counter !"); auto& serverCounter = world->GetRegistry().get(world->GetRegistry().view().front()); EntityID newEntityId = serverCounter.m_NextEntityId++; PlayerInfoComponent newPlayer; for (auto [entity, connection] : world->GetRegistry().view().each()) { if (connection.m_Connection->GetPeerId() == m_Connection.GetPeerId()) { // players should not try to log in twice auto previousPlayerInfo = world->GetRegistry().try_get(entity); if (previousPlayerInfo) return; newPlayer = world->GetRegistry().emplace(entity, newEntityId, a_PlayerLogin.m_PlayerName); world->GetRegistry().emplace( 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().each()) { connection.m_Connection->SendPlayerJoin({newPlayer}); } } } // namespace server } // namespace blitz