better control of events to leave the server properly if the user is in the kart selection phase and decides to leave

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13382 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-07-29 15:27:25 +00:00
parent ad18d9299d
commit 8a79bbadfd
6 changed files with 36 additions and 7 deletions

View File

@ -58,6 +58,14 @@ void ClientLobbyRoomProtocol::requestKartSelection(std::string kart_name)
//-----------------------------------------------------------------------------
void ClientLobbyRoomProtocol::leave()
{
assert(NetworkManager::getInstance()->getPeerCount() == 1);
NetworkManager::getInstance()->getPeers()[0]->disconnect(); // just dc
}
//-----------------------------------------------------------------------------
void ClientLobbyRoomProtocol::notifyEvent(Event* event)
{
assert(m_setup); // assert that the setup exists

View File

@ -11,13 +11,13 @@ class ClientLobbyRoomProtocol : public LobbyRoomProtocol
void requestKartSelection(std::string kart_name);
void sendMessage(std::string message);
void leave();
virtual void notifyEvent(Event* event);
virtual void setup();
virtual void update();
virtual void asynchronousUpdate() {}
protected:
void newPlayer(Event* event);
void disconnectedPlayer(Event* event);

View File

@ -213,6 +213,7 @@ void ServerLobbyRoomProtocol::kartDisconnected(Event* event)
Log::info("ServerLobbyRoomProtocol", "Player disconnected : id %d",
peer->getPlayerProfile()->race_id);
m_setup->removePlayer(peer->getPlayerProfile()->race_id);
NetworkManager::getInstance()->removePeer(peer);
}
else
Log::info("ServerLobbyRoomProtocol", "The DC peer wasn't registered.");

View File

@ -85,6 +85,7 @@ bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host,
void STKPeer::disconnect()
{
enet_peer_disconnect(m_peer, 0);
NetworkManager::getInstance()->removePeer(this);
}
//-----------------------------------------------------------------------------

View File

@ -279,7 +279,7 @@ void MainMenuScreen::eventCallback(Widget* widget, const std::string& name,
}
else if (selection == "multiplayer")
{
KartSelectionScreen* s = OfflineKartSelectionScreen::getInstance();
KartSelectionScreen* s = NetworkKartSelectionScreen::getInstance();
s->setMultiplayer(true);
s->setFromOverworld(false);
StateManager::get()->pushScreen( s );

View File

@ -1,5 +1,9 @@
#include "states_screens/network_kart_selection.hpp"
#include "network/protocol_manager.hpp"
#include "network/protocols/client_lobby_room_protocol.hpp"
#include "states_screens/state_manager.hpp"
using namespace GUIEngine;
DEFINE_SCREEN_SINGLETON( NetworkKartSelectionScreen );
@ -15,6 +19,7 @@ NetworkKartSelectionScreen::~NetworkKartSelectionScreen()
void NetworkKartSelectionScreen::init()
{
m_multiplayer = false;
KartSelectionScreen::init();
RibbonWidget* tabs = getWidget<RibbonWidget>("kartgroups");
@ -25,6 +30,8 @@ void NetworkKartSelectionScreen::init()
// change the back button image (because it makes the game quit)
IconButtonWidget* back_button = getWidget<IconButtonWidget>("back");
back_button->setImage("gui/main_quit.png");
m_multiplayer = false;
}
/**
@ -33,12 +40,24 @@ void NetworkKartSelectionScreen::init()
void NetworkKartSelectionScreen::eventCallback(GUIEngine::Widget* widget, const std::string& name,
const int playerID)
{
if (name != "karts")
if (name == "karts")
{
}
else if (name == "back")
{
// first do the back action
KartSelectionScreen::eventCallback(widget, name, playerID);
// then remove the lobby screen (you left the server)
StateManager::get()->popMenu();
// and notify the server that you left
ClientLobbyRoomProtocol* protocol = static_cast<ClientLobbyRoomProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
if (protocol)
protocol->leave();
}
else // name != karts
{
KartSelectionScreen::eventCallback(widget, name, playerID);
}
else // name = karts
{
// must quit the server here
}
} // eventCallback