From 4d28bb484fe243035d2c05b944fd62197df2c369 Mon Sep 17 00:00:00 2001 From: hiker Date: Mon, 11 Jan 2016 17:08:06 +1100 Subject: [PATCH] Added host id which is used to determine local/remote players. --- src/network/network_player_profile.hpp | 9 +++++++ .../protocols/client_lobby_room_protocol.cpp | 25 +++++++++++-------- .../protocols/server_lobby_room_protocol.cpp | 10 +++++--- src/network/protocols/start_game_protocol.cpp | 19 +++++++------- src/network/stk_host.cpp | 4 +++ src/network/stk_host.hpp | 10 +++++++- 6 files changed, 53 insertions(+), 24 deletions(-) diff --git a/src/network/network_player_profile.hpp b/src/network/network_player_profile.hpp index bd5c48790..4aeee8e68 100644 --- a/src/network/network_player_profile.hpp +++ b/src/network/network_player_profile.hpp @@ -42,6 +42,9 @@ private: * peer list. */ uint8_t m_global_player_id; + /** Host id of this player. */ + uint8_t m_host_id; + /** The selected kart id. */ std::string m_kart_name; @@ -65,6 +68,12 @@ public: /** Returns the global ID of this player in this race. */ int getGlobalPlayerId() const { return m_global_player_id; } // ------------------------------------------------------------------------ + /** Sets the host id of this player. */ + void setHostId(int host_id) { m_host_id = host_id; } + // ------------------------------------------------------------------------ + /** Returns the host id of this player. */ + uint8_t getHostId() const { return m_host_id; } + // ------------------------------------------------------------------------ /** Sets the kart name for this player. */ void setKartName(const std::string &kart_name) { m_kart_name = kart_name; } // ------------------------------------------------------------------------ diff --git a/src/network/protocols/client_lobby_room_protocol.cpp b/src/network/protocols/client_lobby_room_protocol.cpp index ca8d011e9..55f2985ff 100644 --- a/src/network/protocols/client_lobby_room_protocol.cpp +++ b/src/network/protocols/client_lobby_room_protocol.cpp @@ -375,17 +375,17 @@ void ClientLobbyRoomProtocol::disconnectedPlayer(Event* event) * \param event : Event providing the information. * * Format of the data : - * Byte 0 1 2 3 7 8 - * --------------------------------------------------------- - * Size | 1 | 1 | 1 | 4 | | - * Data | 1 | 0 <= race id < 16 | 4 | priv token | playernames* | - * --------------------------------------------------------- + * Byte 0 1 2 3 7 8 + * ------------------------------------------------------------------ + * Size | 1 | 1 | 1 | 4 | 1 | | + * Data | 1 | 0 <= race id < 16 | 4 | priv token | hostid | playernames* | + * ------------------------------------------------------------------ */ void ClientLobbyRoomProtocol::connectionAccepted(Event* event) { NetworkString &data = event->data(); // At least 12 bytes should remain now - if (data.size() < 7|| data[0] != 1 || data[2] != 4) + if (data.size() < 8|| data[0] != 1 || data[2] != 4) { Log::error("ClientLobbyRoomProtocol", "A message notifying an accepted connection wasn't " @@ -406,7 +406,11 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event) else name = PlayerManager::getCurrentPlayer()->getName(); uint8_t my_player_id = data.getUInt8(1); + uint8_t my_host_id = data.getUInt8(7); + STKHost::get()->setMyHostId(my_host_id); + NetworkPlayerProfile* profile = new NetworkPlayerProfile(my_player_id, name); + profile->setHostId(my_host_id); STKHost::get()->getGameSetup()->setLocalMaster(my_player_id); m_setup->addPlayer(profile); // connection token @@ -417,21 +421,22 @@ void ClientLobbyRoomProtocol::connectionAccepted(Event* event) // Add all players // =============== - int n = 7; + int n = 8; while (n < data.size()) { if (data[n] != 1 ) Log::error("ClientLobbyRoomProtocol", "Bad format in players list."); - uint8_t race_player_id = data[n + 1]; + uint8_t player_id = data[n + 1]; irr::core::stringw name; int bytes_read = data.decodeStringW(n + 2, &name); + uint8_t host_id = data.getUInt8(n+2+bytes_read); NetworkPlayerProfile* profile2 = - new NetworkPlayerProfile(race_player_id, name); + new NetworkPlayerProfile(player_id, name); m_setup->addPlayer(profile2); - n += bytes_read+2; + n += bytes_read+3; // Inform the network lobby of all players so that the GUI can // show all currently connected players. NetworkingLobby::getInstance()->addPlayer(profile2); diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index bfc76347e..419919aa9 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -455,12 +455,14 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event) if(m_setup->getLocalMasterID()==0) m_setup->setLocalMaster(new_player_id); + int new_host_id = STKHost::get()->getPeerCount(); + // Notify everybody that there is a new player // ------------------------------------------- NetworkString message(8); // size of id -- id -- size of local id -- local id; message.ai8(LE_NEW_PLAYER_CONNECTED).ai8(1).ai8(new_player_id) - .encodeString(name_u8); + .encodeString(name_u8).addUInt8(new_host_id); ProtocolManager::getInstance()->sendMessageExcept(this, peer, message); // Now answer to the peer that just connected @@ -478,17 +480,19 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event) NetworkString message_ack(13 + players.size() * 7); // connection success -- size of token -- token message_ack.ai8(LE_CONNECTION_ACCEPTED).ai8(1).ai8(new_player_id).ai8(4) - .ai32(token); + .ai32(token).addUInt8(new_host_id); // Add all players so that this user knows (this new player is only added // to the list of players later, so the new player's info is not included) for (unsigned int i = 0; i < players.size(); i++) { message_ack.ai8(1).ai8(players[i]->getGlobalPlayerId()) - .encodeString(players[i]->getName()); + .encodeString(players[i]->getName()) + .addUInt8(players[i]->getHostId()); } sendMessage(peer, message_ack); NetworkPlayerProfile* profile = new NetworkPlayerProfile(new_player_id, name); + profile->setHostId(new_host_id); m_setup->addPlayer(profile); peer->setPlayerProfile(profile); peer->setClientServerToken(token); diff --git a/src/network/protocols/start_game_protocol.cpp b/src/network/protocols/start_game_protocol.cpp index eebc5c103..96d3fb55f 100644 --- a/src/network/protocols/start_game_protocol.cpp +++ b/src/network/protocols/start_game_protocol.cpp @@ -69,26 +69,25 @@ void StartGameProtocol::setup() // Create the kart information for the race manager: // ------------------------------------------------- std::vector players = m_game_setup->getPlayers(); + int local_player_id = 0; for (unsigned int i = 0; i < players.size(); i++) { NetworkPlayerProfile* profile = players[i]; - bool is_me = profile->getGlobalPlayerId() - == STKHost::get()->getGameSetup()->getLocalMasterID(); + bool is_local = profile->getHostId() + == STKHost::get()->getMyHostId(); RemoteKartInfo rki(profile->getGlobalPlayerId(), profile->getKartName(), profile->getName(), - /*hostid*/profile->getGlobalPlayerId(), - !is_me); + profile->getHostId(), + !is_local); rki.setPerPlayerDifficulty(profile->getPerPlayerDifficulty()); - rki.setLocalPlayerId(0); - // FIXME: for now (only one local player) the global player id - // can be used as host id. - rki.setHostId(profile->getGlobalPlayerId()); + rki.setLocalPlayerId(local_player_id); + if(is_local) local_player_id++; // Inform the race manager about the data for this kart. race_manager->setPlayerKart(i, rki); - if(is_me) + if(is_local) { PlayerProfile* profile_to_use = PlayerManager::getCurrentPlayer(); assert(profile_to_use); @@ -109,7 +108,7 @@ void StartGameProtocol::setup() race_manager->setLocalKartInfo(new_player_id, profile->getKartName()); NetworkWorld::getInstance()->setSelfKart(profile->getKartName()); - } // if is_me + } // if is_local else { StateManager::get()->createActivePlayer( NULL, NULL ); diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index d3a04d448..ca1e5733e 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -215,6 +215,9 @@ void STKHost::create() */ STKHost::STKHost(uint32_t server_id, uint32_t host_id) { + // Will be overwritten with the correct value once a connection with the + // server is made. + m_host_id = 0; init(); m_network = new Network(/*peer_count*/1, /*channel_limit*/2, @@ -236,6 +239,7 @@ STKHost::STKHost(uint32_t server_id, uint32_t host_id) STKHost::STKHost(const irr::core::stringw &server_name) { init(); + m_host_id = 0; // indicates a server host. ENetAddress addr; addr.host = STKHost::HOST_ANY; diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index c4c159459..aef734a36 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -74,6 +74,9 @@ private: /** The list of peers connected to this instance. */ std::vector m_peers; + /** Host id of this host. */ + uint8_t m_host_id; + /** Stores data about the online game to play. */ GameSetup* m_game_setup; @@ -197,7 +200,6 @@ public: // -------------------------------------------------------------------- /** Returns a const reference to the list of peers. */ const std::vector &getPeers() { return m_peers; } - // -------------------------------------------------------------------- /** Returns the number of currently connected peers. */ unsigned int getPeerCount() { return (int)m_peers.size(); } @@ -214,6 +216,12 @@ public: return m_is_registered; } // isRegistered // -------------------------------------------------------------------- + /** Sets the global host id of this host. */ + void setMyHostId(uint8_t my_host_id) { m_host_id = my_host_id; } + // -------------------------------------------------------------------- + /** Returns the host id of this host. */ + uint8_t getMyHostId() const { return m_host_id; } + // -------------------------------------------------------------------- }; // class STKHost #endif // STK_HOST_HPP