Use singleton for lobby protocols.

This commit is contained in:
hiker 2016-11-18 09:08:37 +11:00
parent ba600f40f2
commit 6b1563321f
14 changed files with 110 additions and 50 deletions

View File

@ -1,5 +1,5 @@
# Modify this file to change the last-modified date when you add/remove a file. # Modify this file to change the last-modified date when you add/remove a file.
# This will then trigger a new cmake run automatically. # This will then trigger a new cmake run automatically.
file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp") file(GLOB_RECURSE STK_HEADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.hpp")
file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp") file(GLOB_RECURSE STK_SOURCES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "src/*.cpp")
file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*") file(GLOB_RECURSE STK_SHADERS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "data/shaders/*")

View File

@ -21,7 +21,6 @@
#include "main_loop.hpp" #include "main_loop.hpp"
#include "network/network_config.hpp" #include "network/network_config.hpp"
#include "network/network_player_profile.hpp" #include "network/network_player_profile.hpp"
#include "network/protocol_manager.hpp"
#include "network/stk_host.hpp" #include "network/stk_host.hpp"
#include "network/protocols/client_lobby_room_protocol.hpp" #include "network/protocols/client_lobby_room_protocol.hpp"
#include "network/protocols/server_lobby_room_protocol.hpp" #include "network/protocols/server_lobby_room_protocol.hpp"
@ -79,27 +78,23 @@ void* NetworkConsole::mainLoop(void* data)
} }
else if (str == "start" && NetworkConfig::get()->isServer()) else if (str == "start" && NetworkConfig::get()->isServer())
{ {
ServerLobbyRoomProtocol* protocol = ServerLobbyRoomProtocol* protocol =
static_cast<ServerLobbyRoomProtocol*> dynamic_cast<ServerLobbyRoomProtocol*>(LobbyRoomProtocol::get());
(ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
assert(protocol);
protocol->startGame(); protocol->startGame();
} }
else if (str == "selection" && NetworkConfig::get()->isServer()) else if (str == "selection" && NetworkConfig::get()->isServer())
{ {
ServerLobbyRoomProtocol* protocol = ServerLobbyRoomProtocol* protocol =
static_cast<ServerLobbyRoomProtocol*> dynamic_cast<ServerLobbyRoomProtocol*>(LobbyRoomProtocol::get());
(ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
assert(protocol);
protocol->startSelection(); protocol->startSelection();
} }
else if (str == "select" && NetworkConfig::get()->isClient()) else if (str == "select" && NetworkConfig::get()->isClient())
{ {
std::string str2; std::string str2;
getline(std::cin, str2); getline(std::cin, str2);
Protocol* protocol = ServerLobbyRoomProtocol* protocol =
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM); dynamic_cast<ServerLobbyRoomProtocol*>(LobbyRoomProtocol::get());
ClientLobbyRoomProtocol* clrp = static_cast<ClientLobbyRoomProtocol*>(protocol); ClientLobbyRoomProtocol* clrp = dynamic_cast<ClientLobbyRoomProtocol*>(protocol);
std::vector<NetworkPlayerProfile*> players = std::vector<NetworkPlayerProfile*> players =
STKHost::get()->getMyPlayerProfiles(); STKHost::get()->getMyPlayerProfiles();
// For now send a vote for each local player // For now send a vote for each local player
@ -114,10 +109,9 @@ void* NetworkConsole::mainLoop(void* data)
std::cout << "Vote for ? (track/laps/reversed/major/minor/race#) :"; std::cout << "Vote for ? (track/laps/reversed/major/minor/race#) :";
std::string str2; std::string str2;
getline(std::cin, str2); getline(std::cin, str2);
Protocol* protocol = ProtocolManager::getInstance() LobbyRoomProtocol* protocol = LobbyRoomProtocol::get();
->getProtocol(PROTOCOL_LOBBY_ROOM); ClientLobbyRoomProtocol* clrp =
ClientLobbyRoomProtocol* clrp = dynamic_cast<ClientLobbyRoomProtocol*>(protocol);
static_cast<ClientLobbyRoomProtocol*>(protocol);
std::vector<NetworkPlayerProfile*> players = std::vector<NetworkPlayerProfile*> players =
STKHost::get()->getMyPlayerProfiles(); STKHost::get()->getMyPlayerProfiles();
if (str2 == "track") if (str2 == "track")

View File

@ -118,8 +118,7 @@ void Protocol::findAndTerminateProtocol(ProtocolType type)
if (protocol) if (protocol)
protocol->requestTerminate(); protocol->requestTerminate();
else else
Log::error("ClientLobbyRoomProtocol", Log::error("Protocol", "No protocol %d registered.", type);
"No protocol %d registered.", type);
} // findAndTerminateProtocol } // findAndTerminateProtocol
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------

View File

@ -35,11 +35,12 @@
#include "states_screens/state_manager.hpp" #include "states_screens/state_manager.hpp"
#include "utils/log.hpp" #include "utils/log.hpp"
ClientLobbyRoomProtocol:: // ============================================================================
ClientLobbyRoomProtocol(const TransportAddress& server_address) ClientLobbyRoomProtocol::ClientLobbyRoomProtocol()
: LobbyRoomProtocol(NULL) : LobbyRoomProtocol(NULL)
{ {
m_server_address.copy(server_address);
m_server_address.clear();
m_server = NULL; m_server = NULL;
setHandleDisconnections(true); setHandleDisconnections(true);
} // ClientLobbyRoomProtocol } // ClientLobbyRoomProtocol
@ -50,6 +51,13 @@ ClientLobbyRoomProtocol::~ClientLobbyRoomProtocol()
{ {
} // ClientLobbyRoomProtocol } // ClientLobbyRoomProtocol
//-----------------------------------------------------------------------------
/** Sets the address of the server.
*/
void ClientLobbyRoomProtocol::setAddress(const TransportAddress &address)
{
m_server_address.copy(address);
} // setAddress
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void ClientLobbyRoomProtocol::setup() void ClientLobbyRoomProtocol::setup()
@ -578,7 +586,8 @@ void ClientLobbyRoomProtocol::kartSelectionUpdate(Event* event)
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/*! \brief Called when the race needs to be started. /*! \brief Called when the server broadcasts the race start.
race needs to be started.
* \param event : Event providing the information (no additional information * \param event : Event providing the information (no additional information
* in this case). * in this case).
*/ */

View File

@ -50,11 +50,12 @@ private:
STATE m_state; STATE m_state;
public: public:
ClientLobbyRoomProtocol(const TransportAddress& server_address); ClientLobbyRoomProtocol();
virtual ~ClientLobbyRoomProtocol(); virtual ~ClientLobbyRoomProtocol();
void requestKartSelection(uint8_t player_id, virtual void requestKartSelection(uint8_t player_id,
const std::string &kart_name); const std::string &kart_name) OVERRIDE;
void setAddress(const TransportAddress &address);
void voteMajor(uint8_t player_id, uint32_t major); void voteMajor(uint8_t player_id, uint32_t major);
void voteRaceCount(uint8_t player_id, uint8_t count); void voteRaceCount(uint8_t player_id, uint8_t count);
void voteMinor(uint8_t player_id, uint32_t minor); void voteMinor(uint8_t player_id, uint32_t minor);

View File

@ -238,7 +238,9 @@ void ConnectToServer::asynchronousUpdate()
// lobby room protocol if we're connected only // lobby room protocol if we're connected only
if(STKHost::get()->getPeers()[0]->isConnected()) if(STKHost::get()->getPeers()[0]->isConnected())
{ {
Protocol *p = new ClientLobbyRoomProtocol(m_server_address); ClientLobbyRoomProtocol *p =
LobbyRoomProtocol::create<ClientLobbyRoomProtocol>();
p->setAddress(m_server_address);
p->requestStart(); p->requestStart();
} }
} }

View File

@ -0,0 +1,24 @@
//
// SuperTuxKart - a fun racing game with go-kart
// Copyright (C) 2013-2015 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/protocols/lobby_room_protocol.hpp"
LobbyRoomProtocol *LobbyRoomProtocol::m_lobby = NULL;

View File

@ -59,11 +59,32 @@ public:
}; };
protected: protected:
static LobbyRoomProtocol *m_lobby;
/** The game setup. */ /** The game setup. */
GameSetup* m_setup; GameSetup* m_setup;
public: public:
/** Creates either a client or server lobby protocol as a singleton. */
template<typename S> static S* create()
{
assert(m_lobby == NULL);
m_lobby = new S();
return dynamic_cast<S*>(m_lobby);
} // create
// ------------------------------------------------------------------------
/** Returns the singleton client or server lobby protocol. */
static LobbyRoomProtocol *get()
{
assert(m_lobby);
return m_lobby;
} // get
// ------------------------------------------------------------------------
LobbyRoomProtocol(CallbackObject* callback_object) LobbyRoomProtocol(CallbackObject* callback_object)
: Protocol(PROTOCOL_LOBBY_ROOM, callback_object) : Protocol(PROTOCOL_LOBBY_ROOM, callback_object)
{ {
@ -74,6 +95,12 @@ public:
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
virtual void setup() = 0; virtual void setup() = 0;
virtual void update(float dt) = 0; virtual void update(float dt) = 0;
virtual void requestKartSelection(uint8_t player_id,
const std::string &kart_name)
{
assert(false); // Only defined in client
};
}; // class LobbyRoomProtocol }; // class LobbyRoomProtocol
#endif // LOBBY_ROOM_PROTOCOL_HPP #endif // LOBBY_ROOM_PROTOCOL_HPP

View File

@ -236,7 +236,11 @@ void ServerLobbyRoomProtocol::registerServer()
} // registerServer } // registerServer
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
/** This function informs each client to start the race, and then starts the /** This function is called when all track votes from the clients have
* been received. It broadcasts a message to all client to start the
* race
informs each client to start the race, and then starts the
* StartGameProtocol. * StartGameProtocol.
*/ */
void ServerLobbyRoomProtocol::startGame() void ServerLobbyRoomProtocol::startGame()

View File

@ -290,7 +290,8 @@ STKHost::STKHost(const irr::core::stringw &server_name)
} }
startListening(); startListening();
ProtocolManager::getInstance()->requestStart(new ServerLobbyRoomProtocol()); Protocol *p = LobbyRoomProtocol::create<ServerLobbyRoomProtocol>();
ProtocolManager::getInstance()->requestStart(p);
} // STKHost(server_name) } // STKHost(server_name)

View File

@ -133,8 +133,9 @@ void NetworkKartSelectionScreen::playerConfirm(const int playerID)
} }
if(playerID == PLAYER_ID_GAME_MASTER) // self if(playerID == PLAYER_ID_GAME_MASTER) // self
{ {
ClientLobbyRoomProtocol* protocol = static_cast<ClientLobbyRoomProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM)); LobbyRoomProtocol* protocol = LobbyRoomProtocol::get();
// FIXME SPLITSCREEN: we need to supply the global player id of the // FIXME SPLITSCREEN: we need to supply the global player id of the
// player selecting the kart here. For now ... just vote the same kart // player selecting the kart here. For now ... just vote the same kart
// for each local player. // for each local player.
@ -179,8 +180,8 @@ void NetworkKartSelectionScreen::playerSelected(uint8_t player_id,
Protocol* protocol = ProtocolManager::getInstance() Protocol* protocol = ProtocolManager::getInstance()
->getProtocol(PROTOCOL_LOBBY_ROOM); ->getProtocol(PROTOCOL_LOBBY_ROOM);
ClientLobbyRoomProtocol* clrp = ClientLobbyRoomProtocol* clrp =
static_cast<ClientLobbyRoomProtocol*>(protocol); dynamic_cast<ClientLobbyRoomProtocol*>(protocol);
assert(clrp);
// FIXME: for now we submit a vote from the authorised user // FIXME: for now we submit a vote from the authorised user
// for the various modes based on the settings in the race manager. // for the various modes based on the settings in the race manager.
// This needs more/better gui elements (and some should be set when // This needs more/better gui elements (and some should be set when
@ -209,10 +210,11 @@ bool NetworkKartSelectionScreen::onEscapePressed()
// then remove the lobby screen (you left the server) // then remove the lobby screen (you left the server)
StateManager::get()->popMenu(); StateManager::get()->popMenu();
ServerSelection::getInstance()->refresh(); ServerSelection::getInstance()->refresh();
Protocol *lobby = LobbyRoomProtocol::get();
// notify the server that we left // notify the server that we left
ClientLobbyRoomProtocol* protocol = static_cast<ClientLobbyRoomProtocol*>( ClientLobbyRoomProtocol* clrp =
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM)); dynamic_cast<ClientLobbyRoomProtocol*>(lobby);
if (protocol) if (clrp)
protocol->leave(); clrp->leave();
return true; // remove the screen return true; // remove the screen
} // onEscapePressed } // onEscapePressed

View File

@ -34,7 +34,6 @@
#include "input/input_manager.hpp" #include "input/input_manager.hpp"
#include "io/file_manager.hpp" #include "io/file_manager.hpp"
#include "network/network_player_profile.hpp" #include "network/network_player_profile.hpp"
#include "network/protocol_manager.hpp"
#include "network/protocols/client_lobby_room_protocol.hpp" #include "network/protocols/client_lobby_room_protocol.hpp"
#include "network/protocols/server_lobby_room_protocol.hpp" #include "network/protocols/server_lobby_room_protocol.hpp"
#include "network/servers_manager.hpp" #include "network/servers_manager.hpp"
@ -163,10 +162,10 @@ void NetworkingLobby::eventCallback(Widget* widget, const std::string& name,
{ {
if(NetworkConfig::get()->isServer()) if(NetworkConfig::get()->isServer())
{ {
ServerLobbyRoomProtocol* slrp = static_cast<ServerLobbyRoomProtocol*>( Protocol *p = LobbyRoomProtocol::get();
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM)); ServerLobbyRoomProtocol* slrp =
if (slrp) dynamic_cast<ServerLobbyRoomProtocol*>(p);
slrp->startSelection(); slrp->startSelection();
} }
else // client else // client
{ {
@ -199,8 +198,8 @@ void NetworkingLobby::tearDown()
bool NetworkingLobby::onEscapePressed() bool NetworkingLobby::onEscapePressed()
{ {
// notify the server that we left // notify the server that we left
ClientLobbyRoomProtocol* protocol = dynamic_cast<ClientLobbyRoomProtocol*>( ClientLobbyRoomProtocol* protocol =
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM)); dynamic_cast<ClientLobbyRoomProtocol*>(LobbyRoomProtocol::get());
if (protocol) if (protocol)
protocol->leave(); protocol->leave();
STKHost::get()->shutdown(); STKHost::get()->shutdown();

View File

@ -341,10 +341,9 @@ void RaceResultGUI::eventCallback(GUIEngine::Widget* widget,
if (name == "middle") // Continue button (return to server lobby) if (name == "middle") // Continue button (return to server lobby)
{ {
// Signal to the server that this client is back in the lobby now. // Signal to the server that this client is back in the lobby now.
Protocol* protocol = Protocol* protocol = LobbyRoomProtocol::get();
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM);
ClientLobbyRoomProtocol* clrp = ClientLobbyRoomProtocol* clrp =
static_cast<ClientLobbyRoomProtocol*>(protocol); dynamic_cast<ClientLobbyRoomProtocol*>(protocol);
if(clrp) if(clrp)
clrp->doneWithResults(); clrp->doneWithResults();
backToLobby(); backToLobby();

View File

@ -26,7 +26,6 @@
#include "guiengine/widgets/icon_button_widget.hpp" #include "guiengine/widgets/icon_button_widget.hpp"
#include "io/file_manager.hpp" #include "io/file_manager.hpp"
#include "network/network_player_profile.hpp" #include "network/network_player_profile.hpp"
#include "network/protocol_manager.hpp"
#include "network/protocols/client_lobby_room_protocol.hpp" #include "network/protocols/client_lobby_room_protocol.hpp"
#include "network/stk_host.hpp" #include "network/stk_host.hpp"
#include "states_screens/state_manager.hpp" #include "states_screens/state_manager.hpp"
@ -90,10 +89,10 @@ void TracksScreen::eventCallback(Widget* widget, const std::string& name,
{ {
if(STKHost::existHost()) if(STKHost::existHost())
{ {
Protocol* protocol = ProtocolManager::getInstance() Protocol* protocol = LobbyRoomProtocol::get();
->getProtocol(PROTOCOL_LOBBY_ROOM);
ClientLobbyRoomProtocol* clrp = ClientLobbyRoomProtocol* clrp =
static_cast<ClientLobbyRoomProtocol*>(protocol); dynamic_cast<ClientLobbyRoomProtocol*>(protocol);
assert(clrp); // server never shows the track screen.
// FIXME SPLITSCREEN: we need to supply the global player id of the // FIXME SPLITSCREEN: we need to supply the global player id of the
// player selecting the track here. For now ... just vote the same // player selecting the track here. For now ... just vote the same
// track for each local player. // track for each local player.