Adding the base class that will contain all information about all players, correcting events passing inside the protocols manager

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/networking@13081 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-07-04 20:36:50 +00:00
parent 2a0a9a3617
commit 01ceeaa23a
5 changed files with 181 additions and 4 deletions

View File

@ -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

View File

@ -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];
}
}
}
//-----------------------------------------------------------------------------

View File

@ -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 <vector>
#include <string>
/*! \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<PlayerProfile> m_players; //!< Information about players
PlayerProfile m_self_profile; //!< Information about self
};
#endif // GAME_SETUP_HPP

View File

@ -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

View File

@ -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()