diff --git a/sources.cmake b/sources.cmake index 272efb21f..6ec5cfaf5 100644 --- a/sources.cmake +++ b/sources.cmake @@ -137,6 +137,7 @@ src/modes/world_status.cpp src/modes/world_with_rank.cpp src/network/client_network_manager.cpp src/network/event.cpp +src/network/game_setup.cpp src/network/http_functions.cpp src/network/network_interface.cpp src/network/network_manager.cpp @@ -399,6 +400,7 @@ src/modes/world_status.hpp src/modes/world_with_rank.hpp src/network/client_network_manager.hpp src/network/event.hpp +src/network/game_setup.hpp src/network/http_functions.hpp src/network/network_interface.hpp src/network/network_manager.hpp diff --git a/src/network/game_setup.cpp b/src/network/game_setup.cpp new file mode 100644 index 000000000..feee1e5d1 --- /dev/null +++ b/src/network/game_setup.cpp @@ -0,0 +1,97 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2013 SuperTuxKart-Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +#include "network/game_setup.hpp" + +#include "utils/log.hpp" + +//----------------------------------------------------------------------------- + +GameSetup::GameSetup() +{ +} + +//----------------------------------------------------------------------------- + +GameSetup::~GameSetup() +{ +} + +//----------------------------------------------------------------------------- + +void GameSetup::addPlayer(PlayerProfile profile) +{ + m_players.push_back(profile); +} + +//----------------------------------------------------------------------------- + +void GameSetup::removePlayer(uint32_t id) +{ + for (unsigned int i = 0; i < m_players.size(); i++) + { + if (m_players[i].user_profile.getUserID() == id) + { + m_players.erase(m_players.begin()+i, m_players.begin()+i+1); + Log::verbose("GameSetup", "Removed a player from the game setup."); + return; + } + } +} + +//----------------------------------------------------------------------------- + +void GameSetup::removePlayer(uint8_t id) +{ + for (unsigned int i = 0; i < m_players.size(); i++) + { + if (m_players[i].race_id == id) // check the given id + { + m_players.erase(m_players.begin()+i, m_players.begin()+i+1); + Log::verbose("GameSetup", "Removed a player from the game setup."); + return; + } + } +} + +//----------------------------------------------------------------------------- + +const PlayerProfile* GameSetup::getProfile(uint32_t id) +{ + for (unsigned int i = 0; i < m_players.size(); i++) + { + if (m_players[i].user_profile.getUserID() == id) + { + return &m_players[i]; + } + } +} + +//----------------------------------------------------------------------------- + +const PlayerProfile* GameSetup::getProfile(uint8_t id) +{ + for (unsigned int i = 0; i < m_players.size(); i++) + { + if (m_players[i].race_id == id) + { + return &m_players[i]; + } + } +} +//----------------------------------------------------------------------------- diff --git a/src/network/game_setup.hpp b/src/network/game_setup.hpp new file mode 100644 index 000000000..0cd8a010a --- /dev/null +++ b/src/network/game_setup.hpp @@ -0,0 +1,66 @@ +// +// SuperTuxKart - a fun racing game with go-kart +// Copyright (C) 2013 SuperTuxKart-Team +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 3 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + +/*! \file game_setup.hpp + */ + +#ifndef GAME_SETUP_HPP +#define GAME_SETUP_HPP + +#include "online/online_user.hpp" + +#include +#include + +/*! \class PlayerProfile + * \brief Contains the profile of a player. + */ +class PlayerProfile +{ + public: + PlayerProfile() : user_profile("") {} + ~PlayerProfile() {} + + uint8_t race_id; //!< The id of the player for the race + std::string kart_name; //!< The selected kart. + OnlineUser user_profile; //!< Pointer to the lobby profile +}; + +/*! \class GameSetup + * \brief Used to store the needed data about the players that join a game. + * This class stores all the possible information about players in a lobby. + */ +class GameSetup +{ + public: + GameSetup(); + virtual ~GameSetup(); + + void addPlayer(PlayerProfile profile); //!< Add a player. + void removePlayer(uint32_t id); //!< Remove a player by id. + void removePlayer(uint8_t id); //!< Remove a player by local id. + + const PlayerProfile* getProfile(uint32_t id); //!< Get a profile by database id + const PlayerProfile* getProfile(uint8_t id); //!< Get the profile by the lobby id + + protected: + std::vector m_players; //!< Information about players + PlayerProfile m_self_profile; //!< Information about self +}; + +#endif // GAME_SETUP_HPP diff --git a/src/network/protocol_manager.cpp b/src/network/protocol_manager.cpp index 4b6190814..c9094acb4 100644 --- a/src/network/protocol_manager.cpp +++ b/src/network/protocol_manager.cpp @@ -202,9 +202,12 @@ void ProtocolManager::update() Event* event = m_events_to_process.back(); PROTOCOL_TYPE searchedProtocol = PROTOCOL_NONE; - if (event->data.size() > 0) - searchedProtocol = (PROTOCOL_TYPE)(event->data[0]); - event->removeFront(1); // remove the first byte which indicates the protocol + if (event->type == EVENT_TYPE_MESSAGE) + { + if (event->data.size() > 0) + searchedProtocol = (PROTOCOL_TYPE)(event->data[0]); + event->removeFront(1); // remove the first byte which indicates the protocol + } for (unsigned int i = 0; i < m_protocols.size() ; i++) { if (m_protocols[i].protocol->getProtocolType() == searchedProtocol || event->type != EVENT_TYPE_MESSAGE) // pass data to protocols even when paused diff --git a/src/network/protocols/lobby_room_protocol.cpp b/src/network/protocols/lobby_room_protocol.cpp index cc39b340e..c62528e63 100644 --- a/src/network/protocols/lobby_room_protocol.cpp +++ b/src/network/protocols/lobby_room_protocol.cpp @@ -30,10 +30,19 @@ LobbyRoomProtocol::~LobbyRoomProtocol() void LobbyRoomProtocol::notifyEvent(Event* event) { + Log::setLogLevel(1); if (event->type == EVENT_TYPE_MESSAGE) { - Log::info("LobbyRoomProtocol", "Message from %u : \"%s\"\n", event->peer->getAddress(), event->data.c_str()); + + Log::verbose("LobbyRoomProtocol", "Message from %u : \"%s\"", event->peer->getAddress(), event->data.c_str()); } + if (event->type == EVENT_TYPE_CONNECTED) + { + Log::verbose("LobbyRoomProtocol", "New player."); + // add the player to the game setup + + } + Log::setLogLevel(3); } void LobbyRoomProtocol::setup()