diff --git a/src/network/game_setup.cpp b/src/network/game_setup.cpp index 526583205..9ef9f07d3 100644 --- a/src/network/game_setup.cpp +++ b/src/network/game_setup.cpp @@ -37,8 +37,8 @@ GameSetup::~GameSetup() void GameSetup::addPlayer(NetworkPlayerProfile* profile) { m_players.push_back(profile); - Log::verbose("GameSetup", "New player in the game setup. Global id : %d," - "Race id : %d.", profile->race_id, profile->user_profile->getUserID()); + Log::verbose("GameSetup", "New player in the game setup. Global id : %u, " + "Race id : %d.", profile->user_profile->getUserID(), profile->race_id); } //----------------------------------------------------------------------------- @@ -51,7 +51,8 @@ bool GameSetup::removePlayer(uint32_t id) { delete m_players[i]; m_players.erase(m_players.begin()+i, m_players.begin()+i+1); - Log::verbose("GameSetup", "Removed a player from the game setup."); + Log::verbose("GameSetup", "Removed a player from the game setup. " + "Remains %u.", m_players.size()); return true; } } @@ -66,8 +67,10 @@ bool GameSetup::removePlayer(uint8_t id) { if (m_players[i]->race_id == id) // check the given id { + delete m_players[i]; m_players.erase(m_players.begin()+i, m_players.begin()+i+1); - Log::verbose("GameSetup", "Removed a player from the game setup."); + Log::verbose("GameSetup", "Removed a player from the game setup. " + "Remains %u.", m_players.size()); return true; } } @@ -84,7 +87,8 @@ void GameSetup::setPlayerKart(uint8_t id, std::string kart_name) if (m_players[i]->race_id == id) { m_players[i]->kart_name = kart_name; - Log::info("GameSetup::setPlayerKart", "Player %d took kart %s", id, kart_name.c_str()); + Log::info("GameSetup::setPlayerKart", "Player %d took kart %s", + id, kart_name.c_str()); found = true; } } diff --git a/src/network/game_setup.hpp b/src/network/game_setup.hpp index 16dfab8b8..3d1a3d481 100644 --- a/src/network/game_setup.hpp +++ b/src/network/game_setup.hpp @@ -60,7 +60,7 @@ class GameSetup int getPlayerCount() { return m_players.size(); } const NetworkPlayerProfile* getProfile(uint32_t id); //!< Get a profile by database id const NetworkPlayerProfile* getProfile(uint8_t id); //!< Get the profile by the lobby id - + bool isKartAvailable(std::string kart_name); bool isKartAllowed(std::string kart_name) {return true; } diff --git a/src/network/network_manager.cpp b/src/network/network_manager.cpp index 1a82cc483..2a12fbfc9 100644 --- a/src/network/network_manager.cpp +++ b/src/network/network_manager.cpp @@ -110,36 +110,6 @@ void NetworkManager::notifyEvent(Event* event) // notify for the event now. ProtocolManager::getInstance()->notifyEvent(event); - if (event->type == EVENT_TYPE_DISCONNECTED) - { - Log::debug("NetworkManager", "Disconnected host: %i.%i.%i.%i:%i", - peer->getAddress()>>24&0xff, - peer->getAddress()>>16&0xff, - peer->getAddress()>>8&0xff, - peer->getAddress()&0xff, - peer->getPort()); - // remove the peer: - bool removed = false; - for (unsigned int i = 0; i < m_peers.size(); i++) - { - if (m_peers[i]->isSamePeer(peer) && !removed) // remove only one - { - delete m_peers[i]; - m_peers.erase(m_peers.begin()+i, m_peers.begin()+i+1); - Log::verbose("NetworkManager", "The peer has been removed from the Network Manager."); - removed = true; - } - else if (m_peers[i]->isSamePeer(peer)) - { - Log::fatal("NetworkManager", "Multiple peers match the disconnected one."); - } - } - if (!removed) - Log::warn("NetworkManager", "The peer that has been disconnected was not registered by the Network Manager."); - - Log::info("NetworkManager", "Somebody is now disconnected. There are now %lu peers.", m_peers.size()); - } - delete event; // in this case only, the event has been copied by the protocol manager } @@ -189,6 +159,37 @@ void NetworkManager::setPublicAddress(TransportAddress addr) { m_public_address = addr; } +//----------------------------------------------------------------------------- + +void NetworkManager::removePeer(STKPeer* peer) +{ + Log::debug("NetworkManager", "Disconnected host: %i.%i.%i.%i:%i", + peer->getAddress()>>24&0xff, + peer->getAddress()>>16&0xff, + peer->getAddress()>>8&0xff, + peer->getAddress()&0xff, + peer->getPort()); + // remove the peer: + bool removed = false; + for (unsigned int i = 0; i < m_peers.size(); i++) + { + if (m_peers[i]->isSamePeer(peer) && !removed) // remove only one + { + delete m_peers[i]; + m_peers.erase(m_peers.begin()+i, m_peers.begin()+i+1); + Log::verbose("NetworkManager", "The peer has been removed from the Network Manager."); + removed = true; + } + else if (m_peers[i]->isSamePeer(peer)) + { + Log::fatal("NetworkManager", "Multiple peers match the disconnected one."); + } + } + if (!removed) + Log::warn("NetworkManager", "The peer that has been disconnected was not registered by the Network Manager."); + + Log::info("NetworkManager", "Somebody is now disconnected. There are now %lu peers.", m_peers.size()); +} //----------------------------------------------------------------------------- diff --git a/src/network/network_manager.hpp b/src/network/network_manager.hpp index 1e3673f9c..cc13ae665 100644 --- a/src/network/network_manager.hpp +++ b/src/network/network_manager.hpp @@ -52,6 +52,7 @@ class NetworkManager : public Singleton // raw data management void setLogin(std::string username, std::string password); void setPublicAddress(TransportAddress addr); + void removePeer(STKPeer* peer); // getters virtual bool peerExists(TransportAddress peer); diff --git a/src/network/protocols/client_lobby_room_protocol.cpp b/src/network/protocols/client_lobby_room_protocol.cpp index 31151c2fc..e0c85d345 100644 --- a/src/network/protocols/client_lobby_room_protocol.cpp +++ b/src/network/protocols/client_lobby_room_protocol.cpp @@ -137,21 +137,25 @@ void ClientLobbyRoomProtocol::newPlayer(Event* event) } uint32_t global_id = event->data.gui32(1); - - NetworkPlayerProfile* profile = new NetworkPlayerProfile(); - profile->kart_name = ""; - profile->race_id = event->data.gui8(6); + uint8_t race_id = event->data.gui8(6); if (global_id == CurrentOnlineUser::get()->getUserID()) { Log::error("ClientLobbyRoomProtocol", "The server notified me that i'm a new player in the room (not normal)."); } - else + else if (m_setup->getProfile(race_id) == NULL || m_setup->getProfile(global_id) == NULL) { Log::verbose("ClientLobbyRoomProtocol", "New player connected."); + NetworkPlayerProfile* profile = new NetworkPlayerProfile(); + profile->kart_name = ""; + profile->race_id = race_id; profile->user_profile = new OnlineUser(global_id); m_setup->addPlayer(profile); } + else + { + Log::error("ClientLobbyRoomProtocol", "One of the player notified in the list is myself."); + } } //----------------------------------------------------------------------------- diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index dcefcb20b..61a42b763 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -169,7 +169,9 @@ void ServerLobbyRoomProtocol::kartDisconnected(Event* event) NetworkString msg; msg.ai8(0x02).ai8(1).ai8(peer->getPlayerProfile()->race_id); m_listener->sendMessage(this, msg); - Log::info("ServerLobbyRoomProtocol", "Player disconnected."); + Log::info("ServerLobbyRoomProtocol", "Player disconnected : id %d", + peer->getPlayerProfile()->race_id); + m_setup->removePlayer(peer->getPlayerProfile()->race_id); } else Log::info("ServerLobbyRoomProtocol", "The DC peer wasn't registered."); @@ -225,7 +227,9 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event) std::vector players = m_setup->getPlayers(); for (unsigned int i = 0; i < players.size(); i++) { - message_ack.ai8(1).ai8(players[i]->race_id).ai8(4).ai32(players[i]->user_profile->getUserID()); + // do not make a duplicate of the player + if (players[i]->race_id != m_next_id && players[i]->user_profile->getUserID() != player_id) + message_ack.ai8(1).ai8(players[i]->race_id).ai8(4).ai32(players[i]->user_profile->getUserID()); } m_listener->sendMessage(this, peer, message_ack); @@ -234,7 +238,7 @@ void ServerLobbyRoomProtocol::connectionRequested(Event* event) NetworkPlayerProfile* profile = new NetworkPlayerProfile(); profile->race_id = m_next_id; profile->kart_name = ""; - profile->user_profile = new OnlineUser("Unnamed Player"); + profile->user_profile = new OnlineUser(player_id); m_setup->addPlayer(profile); peer->setPlayerProfile(profile); Log::verbose("ServerLobbyRoomProtocol", "New player.");