From 8a79bbadfd27708d16891dec50e39dcba13446f2 Mon Sep 17 00:00:00 2001 From: hilnius Date: Mon, 29 Jul 2013 15:27:25 +0000 Subject: [PATCH] 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 --- .../protocols/client_lobby_room_protocol.cpp | 8 +++++ .../protocols/client_lobby_room_protocol.hpp | 2 +- .../protocols/server_lobby_room_protocol.cpp | 1 + src/network/stk_peer.cpp | 1 + src/states_screens/main_menu_screen.cpp | 2 +- src/states_screens/network_kart_selection.cpp | 29 +++++++++++++++---- 6 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/network/protocols/client_lobby_room_protocol.cpp b/src/network/protocols/client_lobby_room_protocol.cpp index cd68a5854..8c726fea7 100644 --- a/src/network/protocols/client_lobby_room_protocol.cpp +++ b/src/network/protocols/client_lobby_room_protocol.cpp @@ -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 diff --git a/src/network/protocols/client_lobby_room_protocol.hpp b/src/network/protocols/client_lobby_room_protocol.hpp index 4dde327a9..5866e4ff0 100644 --- a/src/network/protocols/client_lobby_room_protocol.hpp +++ b/src/network/protocols/client_lobby_room_protocol.hpp @@ -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); diff --git a/src/network/protocols/server_lobby_room_protocol.cpp b/src/network/protocols/server_lobby_room_protocol.cpp index 494855c4c..c5b236347 100644 --- a/src/network/protocols/server_lobby_room_protocol.cpp +++ b/src/network/protocols/server_lobby_room_protocol.cpp @@ -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."); diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index 9ee05a91d..fbc3607be 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -85,6 +85,7 @@ bool STKPeer::connectToHost(STKHost* localhost, TransportAddress host, void STKPeer::disconnect() { enet_peer_disconnect(m_peer, 0); + NetworkManager::getInstance()->removePeer(this); } //----------------------------------------------------------------------------- diff --git a/src/states_screens/main_menu_screen.cpp b/src/states_screens/main_menu_screen.cpp index a32836c6f..1e934d1b0 100644 --- a/src/states_screens/main_menu_screen.cpp +++ b/src/states_screens/main_menu_screen.cpp @@ -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 ); diff --git a/src/states_screens/network_kart_selection.cpp b/src/states_screens/network_kart_selection.cpp index 0df55bf6e..21df426c9 100644 --- a/src/states_screens/network_kart_selection.cpp +++ b/src/states_screens/network_kart_selection.cpp @@ -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("kartgroups"); @@ -25,6 +30,8 @@ void NetworkKartSelectionScreen::init() // change the back button image (because it makes the game quit) IconButtonWidget* back_button = getWidget("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( + 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