connection/disconnection of peers is now handled by both server and clients, when players select karts, other player are notified. the storage of players on a server is fine.

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13185 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-07-11 15:35:31 +00:00
parent 44d18fff28
commit d5ab7a666d
6 changed files with 58 additions and 44 deletions

View File

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

View File

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

View File

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

View File

@ -52,6 +52,7 @@ class NetworkManager : public Singleton<NetworkManager>
// 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);

View File

@ -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.");
}
}
//-----------------------------------------------------------------------------

View File

@ -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<NetworkPlayerProfile*> 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.");