From 2dada03bae4075488c6acbae9641b731497d826d Mon Sep 17 00:00:00 2001 From: Benau Date: Thu, 19 Apr 2018 16:08:56 +0800 Subject: [PATCH] Save available karts and tracks from clients in STKPeer So if this peer disconnects later in lobby it won't affect current players --- src/network/protocols/server_lobby.cpp | 39 +++++++++++++++++--------- src/network/stk_peer.hpp | 38 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 14 deletions(-) diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index aa8d0c4e1..729973a8b 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -588,6 +588,24 @@ void ServerLobby::startSelection(const Event *event) // a new screen, which must be donefrom the main thread. ns->setSynchronous(true); ns->addUInt8(LE_START_SELECTION); + + // Remove karts / tracks from server that are not supported on all clients + std::set karts_erase, tracks_erase; + auto peers = STKHost::get()->getPeers(); + for (auto peer : peers) + { + peer->eraseServerKarts(m_available_kts.first, karts_erase); + peer->eraseServerTracks(m_available_kts.second, tracks_erase); + } + for (const std::string& kart_erase : karts_erase) + { + m_available_kts.first.erase(kart_erase); + } + for (const std::string& track_erase : tracks_erase) + { + m_available_kts.second.erase(track_erase); + } + const auto& all_k = m_available_kts.first; const auto& all_t = m_available_kts.second; ns->addUInt16((uint16_t)all_k.size()).addUInt16((uint16_t)all_t.size()); @@ -888,9 +906,8 @@ void ServerLobby::connectionRequested(Event* event) client_tracks.insert(track); } - // Remove karts/tracks from server that are not supported on the new client - // so that in the end the server has a list of all karts/tracks available - // on all clients + // Drop this player if he doesn't have at least 1 kart / track the same + // as server std::set karts_erase, tracks_erase; for (const std::string& server_kart : m_available_kts.first) { @@ -907,8 +924,6 @@ void ServerLobby::connectionRequested(Event* event) } } - // Drop this player if he doesn't have at least 1 kart / track the same - // from server if (karts_erase.size() == m_available_kts.first.size() || tracks_erase.size() == m_available_kts.second.size()) { @@ -923,14 +938,9 @@ void ServerLobby::connectionRequested(Event* event) return; } - for (const std::string& kart_erase : karts_erase) - { - m_available_kts.first.erase(kart_erase); - } - for (const std::string& track_erase : tracks_erase) - { - m_available_kts.second.erase(track_erase); - } + // Save available karts and tracks from clients in STKPeer so if this peer + // disconnects later in lobby it won't affect current players + peer->setAvailableKartsTracks(client_karts, client_tracks); if (!peer->isClientServerTokenSet()) { @@ -945,6 +955,7 @@ void ServerLobby::connectionRequested(Event* event) peer->setClientServerToken(token); } + // send a message to the one that asked to connect NetworkString* message_ack = getNetworkString(4); message_ack->setSynchronous(true); @@ -1072,7 +1083,7 @@ void ServerLobby::kartSelectionRequested(Event* event) { peer->getPlayerProfiles()[i]->setKartName(kart); } - Log::verbose("ServerLobby", "Player %d from peer %d chose %s", i, + Log::debug("ServerLobby", "Player %d from peer %d chose %s", i, peer->getHostId(), kart.c_str()); } } // kartSelectionRequested diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index c381c0f60..9c6793372 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -31,6 +31,7 @@ #include #include +#include #include class NetworkPlayerProfile; @@ -72,6 +73,9 @@ protected: float m_connected_time; + /** Available karts and tracks from this peer */ + std::pair, std::set > m_available_kts; + public: STKPeer(ENetPeer *enet_peer, STKHost* host, uint32_t host_id); ~STKPeer() {} @@ -126,6 +130,40 @@ public: // ------------------------------------------------------------------------ float getConnectedTime() const { return m_connected_time; } // ------------------------------------------------------------------------ + void setAvailableKartsTracks(std::set& k, + std::set& t) + { m_available_kts = std::make_pair(std::move(k), std::move(t)); } + // ------------------------------------------------------------------------ + void eraseServerKarts(const std::set& server_karts, + std::set& karts_erase) + { + if (m_available_kts.first.empty()) + return; + for (const std::string& server_kart : server_karts) + { + if (m_available_kts.first.find(server_kart) == + m_available_kts.first.end()) + { + karts_erase.insert(server_kart); + } + } + } + // ------------------------------------------------------------------------ + void eraseServerTracks(const std::set& server_tracks, + std::set& tracks_erase) + { + if (m_available_kts.second.empty()) + return; + for (const std::string& server_track : server_tracks) + { + if (m_available_kts.second.find(server_track) == + m_available_kts.second.end()) + { + tracks_erase.insert(server_track); + } + } + } + // ------------------------------------------------------------------------ uint32_t getPing() const; }; // STKPeer