From d55d5d0f3f28a1106362433fbb1be2395c3b238c Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 20 Apr 2018 19:10:40 +0800 Subject: [PATCH] Get local player id from server for GP sorting later --- src/network/network_player_profile.hpp | 28 ++++++------------------ src/network/protocols/client_lobby.cpp | 3 ++- src/network/protocols/lobby_protocol.cpp | 19 +++++----------- src/network/protocols/server_lobby.cpp | 5 +++-- 4 files changed, 18 insertions(+), 37 deletions(-) diff --git a/src/network/network_player_profile.hpp b/src/network/network_player_profile.hpp index 8feb37b3d..2b02977fe 100644 --- a/src/network/network_player_profile.hpp +++ b/src/network/network_player_profile.hpp @@ -56,19 +56,15 @@ private: /** The selected kart id. */ std::string m_kart_name; - /** The unique id of the player for this race. The number is assigned - * by the server (and it might not be the index of this player in the - * peer list. */ - uint8_t m_global_player_id; - - /** The kart id in the World class (pointer to AbstractKart). */ - uint8_t m_world_kart_id; + /** The local player id relative to each peer. */ + int m_local_player_id; public: NetworkPlayerProfile(std::shared_ptr peer, const irr::core::stringw &name, uint32_t host_id, float default_kart_color, uint32_t online_id, - PerPlayerDifficulty per_player_difficulty) + PerPlayerDifficulty per_player_difficulty, + uint8_t local_player_id) { m_peer = peer; m_player_name = name; @@ -76,20 +72,13 @@ public: m_default_kart_color = default_kart_color; m_online_id = online_id; m_per_player_difficulty = per_player_difficulty; - m_global_player_id = 0; - m_world_kart_id = 0; + m_local_player_id = local_player_id; } // ------------------------------------------------------------------------ ~NetworkPlayerProfile() {} // ------------------------------------------------------------------------ bool isLocalPlayer() const; // ------------------------------------------------------------------------ - /** Sets the global player id of this player. */ - void setGlobalPlayerId(int player_id) { m_global_player_id = player_id; } - // ------------------------------------------------------------------------ - /** Returns the global ID of this player in this race. */ - int getGlobalPlayerId() const { return m_global_player_id; } - // ------------------------------------------------------------------------ /** Returns the host id of this player. */ uint32_t getHostId() const { return m_host_id; } // ------------------------------------------------------------------------ @@ -99,11 +88,8 @@ public: /** Returns the name of the kart this player has selected. */ const std::string &getKartName() const { return m_kart_name; } // ------------------------------------------------------------------------ - /** Sets the world kart id for this player. */ - void setWorldKartID(int id) { m_world_kart_id = id; } - // ------------------------------------------------------------------------ - /** Retuens the world kart id for this player. */ - int getWorldKartID() const { return m_world_kart_id; } + /** Retuens the local player id for this player. */ + int getLocalPlayerId() const { return m_local_player_id; } // ------------------------------------------------------------------------ /** Returns the per-player difficulty. */ PerPlayerDifficulty getPerPlayerDifficulty() const diff --git a/src/network/protocols/client_lobby.cpp b/src/network/protocols/client_lobby.cpp index c93393e20..219f927f0 100644 --- a/src/network/protocols/client_lobby.cpp +++ b/src/network/protocols/client_lobby.cpp @@ -235,8 +235,9 @@ void ClientLobby::addAllPlayers(Event* event) float kart_color = data.getFloat(); uint32_t online_id = data.getUInt32(); PerPlayerDifficulty ppd = (PerPlayerDifficulty)data.getUInt8(); + uint8_t local_id = data.getUInt8(); auto player = std::make_shared(peer, player_name, - host_id, kart_color, online_id, ppd); + host_id, kart_color, online_id, ppd, local_id); std::string kart_name; data.decodeString(&kart_name); player->setKartName(kart_name); diff --git a/src/network/protocols/lobby_protocol.cpp b/src/network/protocols/lobby_protocol.cpp index 644ac035a..31ec8caa7 100644 --- a/src/network/protocols/lobby_protocol.cpp +++ b/src/network/protocols/lobby_protocol.cpp @@ -94,7 +94,6 @@ void LobbyProtocol::configRemoteKart( // Create the kart information for the race manager: // ------------------------------------------------- - int local_player_id = 0; for (unsigned int i = 0; i < players.size(); i++) { std::shared_ptr profile = players[i]; @@ -102,20 +101,20 @@ void LobbyProtocol::configRemoteKart( // All non-local players are created here. This means all players // on the server, and all non-local players on a client (the local - // karts are created in the NetworkingLobby). + // karts are created in the ClientLobby). + int local_player_id = profile->getLocalPlayerId(); if (!is_local) { - // On the server no device or player profile is needed. - StateManager::get()->createActivePlayer(NULL, NULL); + // No device or player profile is needed for remote kart. + local_player_id = + (int)(StateManager::get()->createActivePlayer(NULL, NULL)); } // Adjust the local player id so that local players have the numbers // 0 to num-1; and all other karts start with num. This way the local // players get the first ActivePlayers assigned (which have the // corresponding device associated with it). - RemoteKartInfo rki(is_local ? local_player_id - : i - local_player_id - + m_game_setup->getNumLocalPlayers(), + RemoteKartInfo rki(local_player_id, profile->getKartName(), profile->getName(), profile->getHostId(), @@ -123,12 +122,6 @@ void LobbyProtocol::configRemoteKart( rki.setGlobalPlayerId(i); rki.setDefaultKartColor(profile->getDefaultKartColor()); rki.setPerPlayerDifficulty(profile->getPerPlayerDifficulty()); - if (is_local) - { - rki.setLocalPlayerId(local_player_id); - local_player_id++; - } - // Inform the race manager about the data for this kart. race_manager->setPlayerKart(i, rki); } // for i in players diff --git a/src/network/protocols/server_lobby.cpp b/src/network/protocols/server_lobby.cpp index a8922ce6d..f303377fc 100644 --- a/src/network/protocols/server_lobby.cpp +++ b/src/network/protocols/server_lobby.cpp @@ -336,7 +336,8 @@ void ServerLobby::asynchronousUpdate() .addUInt32(player->getHostId()) .addFloat(player->getDefaultKartColor()) .addUInt32(player->getOnlineId()) - .addUInt8(player->getPerPlayerDifficulty()); + .addUInt8(player->getPerPlayerDifficulty()) + .addUInt8(player->getLocalPlayerId()); if (player->getKartName().empty()) { RandomGenerator rg; @@ -851,7 +852,7 @@ void ServerLobby::connectionRequested(Event* event) (PerPlayerDifficulty)data.getUInt8(); peer->addPlayer(std::make_shared (peer, name, peer->getHostId(), default_kart_color, online_id, - per_player_difficulty)); + per_player_difficulty, (uint8_t)i)); } bool is_banned = false;