From 54375ab6828a676c0eb68917ff3fbb32891440a1 Mon Sep 17 00:00:00 2001 From: Benau Date: Tue, 16 Apr 2019 10:29:46 +0800 Subject: [PATCH] Split decode and encode players list for later usage --- src/network/protocols/client_lobby.cpp | 59 ++++++++++-------- src/network/protocols/client_lobby.hpp | 4 ++ src/network/protocols/server_lobby.cpp | 82 +++++++++++++++++--------- src/network/protocols/server_lobby.hpp | 3 + 4 files changed, 95 insertions(+), 53 deletions(-) diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index 76670b7f2..75b88693c 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -260,31 +260,9 @@ void ClientLobby::addAllPlayers(Event* event) peer->cleanPlayerProfiles(); m_server_send_live_load_world = data.getUInt8() == 1; - std::vector > players; - unsigned player_count = data.getUInt8(); - bool is_specator = true; - for (unsigned i = 0; i < player_count; i++) - { - core::stringw player_name; - data.decodeStringW(&player_name); - uint32_t host_id = data.getUInt32(); - float kart_color = data.getFloat(); - uint32_t online_id = data.getUInt32(); - PerPlayerDifficulty ppd = (PerPlayerDifficulty)data.getUInt8(); - uint8_t local_id = data.getUInt8(); - KartTeam team = (KartTeam)data.getUInt8(); - std::string country_id; - data.decodeString(&country_id); - if (host_id == STKHost::get()->getMyHostId()) - is_specator = false; - auto player = std::make_shared(peer, player_name, - host_id, kart_color, online_id, ppd, local_id, team, country_id); - std::string kart_name; - data.decodeString(&kart_name); - player->setKartName(kart_name); - players.push_back(player); - } + std::vector > players = + decodePlayers(data, peer, &is_specator); setSpectator(is_specator); uint32_t random_seed = data.getUInt32(); @@ -331,6 +309,39 @@ void ClientLobby::addAllPlayers(Event* event) input_manager->getDeviceManager()->setAssignMode(ASSIGN); } // addAllPlayers +//----------------------------------------------------------------------------- +/* Get list of players from server and see if we are spectating it. */ +std::vector > + ClientLobby::decodePlayers(const BareNetworkString& data, + std::shared_ptr peer, + bool* is_specator) const +{ + std::vector > players; + unsigned player_count = data.getUInt8(); + for (unsigned i = 0; i < player_count; i++) + { + core::stringw player_name; + data.decodeStringW(&player_name); + uint32_t host_id = data.getUInt32(); + float kart_color = data.getFloat(); + uint32_t online_id = data.getUInt32(); + PerPlayerDifficulty ppd = (PerPlayerDifficulty)data.getUInt8(); + uint8_t local_id = data.getUInt8(); + KartTeam team = (KartTeam)data.getUInt8(); + std::string country_id; + data.decodeString(&country_id); + if (is_specator && host_id == STKHost::get()->getMyHostId()) + *is_specator = false; + auto player = std::make_shared(peer, player_name, + host_id, kart_color, online_id, ppd, local_id, team, country_id); + std::string kart_name; + data.decodeString(&kart_name); + player->setKartName(kart_name); + players.push_back(player); + } + return players; +} // decodePlayers + //----------------------------------------------------------------------------- void ClientLobby::update(int ticks) { diff --git a/src/network/protocols/client_lobby.hpp b/src/network/protocols/client_lobby.hpp index 5b59a1b77..2bf11b6b6 100644 --- a/src/network/protocols/client_lobby.hpp +++ b/src/network/protocols/client_lobby.hpp @@ -130,6 +130,10 @@ private: void liveJoinAcknowledged(Event* event); void handleKartInfo(Event* event); void finishLiveJoin(); + std::vector > + decodePlayers(const BareNetworkString& data, + std::shared_ptr peer = nullptr, + bool* is_specator = NULL) const; public: ClientLobby(const TransportAddress& a, std::shared_ptr s); virtual ~ClientLobby(); diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index 22a05758e..e22b1bbf2 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -636,6 +636,19 @@ void ServerLobby::asynchronousUpdate() // Add placeholder players for live join addLiveJoinPlaceholder(players); + // If player chose random / hasn't chose any kart + for (unsigned i = 0; i < players.size(); i++) + { + if (players[i]->getKartName().empty()) + { + RandomGenerator rg; + std::set::iterator it = + m_available_kts.first.begin(); + std::advance(it, + rg.get((int)m_available_kts.first.size())); + players[i]->setKartName(*it); + } + } NetworkString* load_world_message = getLoadWorldMessage(players, false/*live_join*/); @@ -663,6 +676,27 @@ void ServerLobby::asynchronousUpdate() } // asynchronousUpdate +//----------------------------------------------------------------------------- +void ServerLobby::encodePlayers(BareNetworkString* bns, + std::vector >& players) const +{ + bns->addUInt8((uint8_t)players.size()); + for (unsigned i = 0; i < players.size(); i++) + { + std::shared_ptr& player = players[i]; + bns->encodeString(player->getName()) + .addUInt32(player->getHostId()) + .addFloat(player->getDefaultKartColor()) + .addUInt32(player->getOnlineId()) + .addUInt8(player->getPerPlayerDifficulty()) + .addUInt8(player->getLocalPlayerId()) + .addUInt8( + race_manager->teamEnabled() ? player->getTeam() : KART_TEAM_NONE) + .encodeString(player->getCountryId()); + bns->encodeString(player->getKartName()); + } +} // encodePlayers + //----------------------------------------------------------------------------- NetworkString* ServerLobby::getLoadWorldMessage( std::vector >& players, @@ -673,29 +707,8 @@ NetworkString* ServerLobby::getLoadWorldMessage( load_world_message->addUInt8(LE_LOAD_WORLD); load_world_message->addUInt32(m_winner_peer_id); m_default_vote->encode(load_world_message); - load_world_message->addUInt8(live_join ? 1 : 0). - addUInt8((uint8_t)players.size()); - for (unsigned i = 0; i < players.size(); i++) - { - std::shared_ptr& player = players[i]; - load_world_message->encodeString(player->getName()) - .addUInt32(player->getHostId()) - .addFloat(player->getDefaultKartColor()) - .addUInt32(player->getOnlineId()) - .addUInt8(player->getPerPlayerDifficulty()) - .addUInt8(player->getLocalPlayerId()) - .addUInt8( - race_manager->teamEnabled() ? player->getTeam() : KART_TEAM_NONE) - .encodeString(player->getCountryId()); - if (player->getKartName().empty()) - { - RandomGenerator rg; - std::set::iterator it = m_available_kts.first.begin(); - std::advance(it, rg.get((int)m_available_kts.first.size())); - player->setKartName(*it); - } - load_world_message->encodeString(player->getKartName()); - } + load_world_message->addUInt8(live_join ? 1 : 0); + encodePlayers(load_world_message, players); load_world_message->addUInt32(m_item_seed); if (race_manager->isBattleMode()) { @@ -840,6 +853,21 @@ void ServerLobby::liveJoinRequest(Event* event) peer->getAddress().toString().c_str()); } + std::vector > players = + getLivePlayers(); + NetworkString* load_world_message = getLoadWorldMessage(players, + true/*live_join*/); + peer->sendPacket(load_world_message, true/*reliable*/); + delete load_world_message; + peer->updateLastActivity(); +} // liveJoinRequest + +//----------------------------------------------------------------------------- +/** Get a list of current ingame players for live join or spectate. + */ +std::vector > + ServerLobby::getLivePlayers() const +{ std::vector > players; for (unsigned i = 0; i < race_manager->getNumPlayers(); i++) { @@ -869,12 +897,8 @@ void ServerLobby::liveJoinRequest(Event* event) } players.push_back(player); } - NetworkString* load_world_message = getLoadWorldMessage(players, - true/*live_join*/); - peer->sendPacket(load_world_message, true/*reliable*/); - delete load_world_message; - peer->updateLastActivity(); -} // liveJoinRequest + return players; +} // getLivePlayers //----------------------------------------------------------------------------- /** Decide where to put the live join player depends on his team and game mode. diff --git a/src/network/protocols/server_lobby.hpp b/src/network/protocols/server_lobby.hpp index d33dbb28b..35741c5ad 100644 --- a/src/network/protocols/server_lobby.hpp +++ b/src/network/protocols/server_lobby.hpp @@ -283,6 +283,9 @@ private: NetworkString* getLoadWorldMessage( std::vector >& players, bool live_join) const; + void encodePlayers(BareNetworkString* bns, + std::vector >& players) const; + std::vector > getLivePlayers() const; void setPlayerKarts(const NetworkString& ns, STKPeer* peer) const; void liveJoinRequest(Event* event); void rejectLiveJoin(STKPeer* peer, BackLobbyReason blr);