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

@ -21,7 +21,6 @@
#include "main_loop.hpp"
#include "network/network_config.hpp"
#include "network/network_player_profile.hpp"
#include "network/protocol_manager.hpp"
#include "network/stk_host.hpp"
#include "network/protocols/client_lobby_room_protocol.hpp"
#include "network/protocols/server_lobby_room_protocol.hpp"
@ -80,26 +79,22 @@ void* NetworkConsole::mainLoop(void* data)
else if (str == "start" && NetworkConfig::get()->isServer())
{
ServerLobbyRoomProtocol* protocol =
static_cast<ServerLobbyRoomProtocol*>
(ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
assert(protocol);
dynamic_cast<ServerLobbyRoomProtocol*>(LobbyRoomProtocol::get());
protocol->startGame();
}
else if (str == "selection" && NetworkConfig::get()->isServer())
{
ServerLobbyRoomProtocol* protocol =
static_cast<ServerLobbyRoomProtocol*>
(ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
assert(protocol);
dynamic_cast<ServerLobbyRoomProtocol*>(LobbyRoomProtocol::get());
protocol->startSelection();
}
else if (str == "select" && NetworkConfig::get()->isClient())
{
std::string str2;
getline(std::cin, str2);
Protocol* protocol =
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM);
ClientLobbyRoomProtocol* clrp = static_cast<ClientLobbyRoomProtocol*>(protocol);
ServerLobbyRoomProtocol* protocol =
dynamic_cast<ServerLobbyRoomProtocol*>(LobbyRoomProtocol::get());
ClientLobbyRoomProtocol* clrp = dynamic_cast<ClientLobbyRoomProtocol*>(protocol);
std::vector<NetworkPlayerProfile*> players =
STKHost::get()->getMyPlayerProfiles();
// 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::string str2;
getline(std::cin, str2);
Protocol* protocol = ProtocolManager::getInstance()
->getProtocol(PROTOCOL_LOBBY_ROOM);
LobbyRoomProtocol* protocol = LobbyRoomProtocol::get();
ClientLobbyRoomProtocol* clrp =
static_cast<ClientLobbyRoomProtocol*>(protocol);
dynamic_cast<ClientLobbyRoomProtocol*>(protocol);
std::vector<NetworkPlayerProfile*> players =
STKHost::get()->getMyPlayerProfiles();
if (str2 == "track")

View File

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

View File

@ -35,11 +35,12 @@
#include "states_screens/state_manager.hpp"
#include "utils/log.hpp"
ClientLobbyRoomProtocol::
ClientLobbyRoomProtocol(const TransportAddress& server_address)
// ============================================================================
ClientLobbyRoomProtocol::ClientLobbyRoomProtocol()
: LobbyRoomProtocol(NULL)
{
m_server_address.copy(server_address);
m_server_address.clear();
m_server = NULL;
setHandleDisconnections(true);
} // ClientLobbyRoomProtocol
@ -50,6 +51,13 @@ ClientLobbyRoomProtocol::~ClientLobbyRoomProtocol()
{
} // ClientLobbyRoomProtocol
//-----------------------------------------------------------------------------
/** Sets the address of the server.
*/
void ClientLobbyRoomProtocol::setAddress(const TransportAddress &address)
{
m_server_address.copy(address);
} // setAddress
//-----------------------------------------------------------------------------
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
* in this case).
*/

View File

@ -50,11 +50,12 @@ private:
STATE m_state;
public:
ClientLobbyRoomProtocol(const TransportAddress& server_address);
ClientLobbyRoomProtocol();
virtual ~ClientLobbyRoomProtocol();
void requestKartSelection(uint8_t player_id,
const std::string &kart_name);
virtual void requestKartSelection(uint8_t player_id,
const std::string &kart_name) OVERRIDE;
void setAddress(const TransportAddress &address);
void voteMajor(uint8_t player_id, uint32_t major);
void voteRaceCount(uint8_t player_id, uint8_t count);
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
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();
}
}

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:
static LobbyRoomProtocol *m_lobby;
/** The game setup. */
GameSetup* m_setup;
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)
: Protocol(PROTOCOL_LOBBY_ROOM, callback_object)
{
@ -74,6 +95,12 @@ public:
// ------------------------------------------------------------------------
virtual void setup() = 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
#endif // LOBBY_ROOM_PROTOCOL_HPP

View File

@ -236,7 +236,11 @@ void ServerLobbyRoomProtocol::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.
*/
void ServerLobbyRoomProtocol::startGame()

View File

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

View File

@ -133,8 +133,9 @@ void NetworkKartSelectionScreen::playerConfirm(const int playerID)
}
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
// player selecting the kart here. For now ... just vote the same kart
// for each local player.
@ -179,8 +180,8 @@ void NetworkKartSelectionScreen::playerSelected(uint8_t player_id,
Protocol* protocol = ProtocolManager::getInstance()
->getProtocol(PROTOCOL_LOBBY_ROOM);
ClientLobbyRoomProtocol* clrp =
static_cast<ClientLobbyRoomProtocol*>(protocol);
dynamic_cast<ClientLobbyRoomProtocol*>(protocol);
assert(clrp);
// FIXME: for now we submit a vote from the authorised user
// for the various modes based on the settings in the race manager.
// 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)
StateManager::get()->popMenu();
ServerSelection::getInstance()->refresh();
Protocol *lobby = LobbyRoomProtocol::get();
// notify the server that we left
ClientLobbyRoomProtocol* protocol = static_cast<ClientLobbyRoomProtocol*>(
ProtocolManager::getInstance()->getProtocol(PROTOCOL_LOBBY_ROOM));
if (protocol)
protocol->leave();
ClientLobbyRoomProtocol* clrp =
dynamic_cast<ClientLobbyRoomProtocol*>(lobby);
if (clrp)
clrp->leave();
return true; // remove the screen
} // onEscapePressed

View File

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

View File

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

View File

@ -26,7 +26,6 @@
#include "guiengine/widgets/icon_button_widget.hpp"
#include "io/file_manager.hpp"
#include "network/network_player_profile.hpp"
#include "network/protocol_manager.hpp"
#include "network/protocols/client_lobby_room_protocol.hpp"
#include "network/stk_host.hpp"
#include "states_screens/state_manager.hpp"
@ -90,10 +89,10 @@ void TracksScreen::eventCallback(Widget* widget, const std::string& name,
{
if(STKHost::existHost())
{
Protocol* protocol = ProtocolManager::getInstance()
->getProtocol(PROTOCOL_LOBBY_ROOM);
Protocol* protocol = LobbyRoomProtocol::get();
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
// player selecting the track here. For now ... just vote the same
// track for each local player.