Turn race event manager into a singleton get for 2 processes

This commit is contained in:
Benau 2020-02-28 09:43:00 +08:00
parent ccdca532b5
commit 63bad92765
11 changed files with 83 additions and 47 deletions

View File

@ -181,7 +181,7 @@ bool LocalPlayerController::action(PlayerAction action, int value,
} }
else if (NetworkConfig::get()->isClient()) else if (NetworkConfig::get()->isClient())
{ {
auto ge = RaceEventManager::getInstance()->getProtocol(); auto ge = RaceEventManager::get()->getProtocol();
assert(ge); assert(ge);
ge->sendStartupBoost((uint8_t)m_kart->getWorldKartId()); ge->sendStartupBoost((uint8_t)m_kart->getWorldKartId());
} }

View File

@ -940,7 +940,7 @@ void Kart::finishedRace(float time, bool from_server)
{ {
if (NetworkConfig::get()->isServer()) if (NetworkConfig::get()->isServer())
{ {
RaceEventManager::getInstance()->kartFinishedRace(this, time); RaceEventManager::get()->kartFinishedRace(this, time);
} // isServer } // isServer
// Ignore local detection of a kart finishing a race in a // Ignore local detection of a kart finishing a race in a

View File

@ -225,6 +225,7 @@
#include "network/protocols/connect_to_server.hpp" #include "network/protocols/connect_to_server.hpp"
#include "network/protocols/client_lobby.hpp" #include "network/protocols/client_lobby.hpp"
#include "network/protocols/server_lobby.hpp" #include "network/protocols/server_lobby.hpp"
#include "network/race_event_manager.hpp"
#include "network/rewind_manager.hpp" #include "network/rewind_manager.hpp"
#include "network/rewind_queue.hpp" #include "network/rewind_queue.hpp"
#include "network/server.hpp" #include "network/server.hpp"
@ -1739,6 +1740,7 @@ void clearGlobalVariables()
STKHost::clear(); STKHost::clear();
RaceManager::clear(); RaceManager::clear();
ProjectileManager::clear(); ProjectileManager::clear();
RaceEventManager::clear();
music_manager = NULL; music_manager = NULL;
irr_driver = NULL; irr_driver = NULL;
#ifdef ENABLE_WIIUSE #ifdef ENABLE_WIIUSE

View File

@ -291,9 +291,8 @@ void MainLoop::updateRace(int ticks, bool fast_forward)
if (!World::getWorld()) return; // No race on atm - i.e. we are in menu if (!World::getWorld()) return; // No race on atm - i.e. we are in menu
// The race event manager will update world in case of an online race // The race event manager will update world in case of an online race
if ( RaceEventManager::getInstance() && if (RaceEventManager::get() && RaceEventManager::get()->isRunning())
RaceEventManager::getInstance()->isRunning() ) RaceEventManager::get()->update(ticks, fast_forward);
RaceEventManager::getInstance()->update(ticks, fast_forward);
else else
World::getWorld()->updateWorld(ticks); World::getWorld()->updateWorld(ticks);
} // updateRace } // updateRace

View File

@ -434,7 +434,7 @@ void ClientLobby::update(int ticks)
} }
break; break;
case RACE_FINISHED: case RACE_FINISHED:
if (!RaceEventManager::getInstance()->protocolStopped() || if (!RaceEventManager::get()->protocolStopped() ||
!GameProtocol::emptyInstance()) !GameProtocol::emptyInstance())
return; return;
if (!m_received_server_result) if (!m_received_server_result)
@ -573,9 +573,9 @@ void ClientLobby::disconnectedPlayer(Event* event)
// If in-game world exists the kart rewinder will know which player // If in-game world exists the kart rewinder will know which player
// disconnects // disconnects
bool in_game_world = World::getWorld() && bool in_game_world = World::getWorld() &&
RaceEventManager::getInstance() && RaceEventManager::get() &&
RaceEventManager::getInstance()->isRunning() && RaceEventManager::get()->isRunning() &&
!RaceEventManager::getInstance()->isRaceOver(); !RaceEventManager::get()->isRaceOver();
if (!in_game_world) if (!in_game_world)
SFXManager::get()->quickSound("appear"); SFXManager::get()->quickSound("appear");
@ -1128,7 +1128,7 @@ void ClientLobby::raceFinished(Event* event)
} }
// stop race protocols // stop race protocols
RaceEventManager::getInstance()->stop(); RaceEventManager::get()->stop();
ProtocolManager::lock()->findAndTerminate(PROTOCOL_GAME_EVENTS); ProtocolManager::lock()->findAndTerminate(PROTOCOL_GAME_EVENTS);
ProtocolManager::lock()->findAndTerminate(PROTOCOL_CONTROLLER_EVENTS); ProtocolManager::lock()->findAndTerminate(PROTOCOL_CONTROLLER_EVENTS);
m_state.store(RACE_FINISHED); m_state.store(RACE_FINISHED);
@ -1149,9 +1149,9 @@ void ClientLobby::backToLobby(Event *event)
m_auto_started = false; m_auto_started = false;
m_state.store(CONNECTED); m_state.store(CONNECTED);
if (RaceEventManager::getInstance()) if (RaceEventManager::get())
{ {
RaceEventManager::getInstance()->stop(); RaceEventManager::get()->stop();
ProtocolManager::lock()->findAndTerminate(PROTOCOL_GAME_EVENTS); ProtocolManager::lock()->findAndTerminate(PROTOCOL_GAME_EVENTS);
} }
auto gp = GameProtocol::lock(); auto gp = GameProtocol::lock();

View File

@ -57,8 +57,7 @@ LobbyProtocol::LobbyProtocol()
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
LobbyProtocol::~LobbyProtocol() LobbyProtocol::~LobbyProtocol()
{ {
if (RaceEventManager::getInstance()) RaceEventManager::destroy();
RaceEventManager::getInstance()->stop();
delete m_game_setup; delete m_game_setup;
joinStartGameThread(); joinStartGameThread();
} // ~LobbyProtocol } // ~LobbyProtocol
@ -78,7 +77,9 @@ void LobbyProtocol::loadWorld()
// --------------------- // ---------------------
// This creates the network world. // This creates the network world.
auto gep = std::make_shared<GameEventsProtocol>(); auto gep = std::make_shared<GameEventsProtocol>();
RaceEventManager::getInstance<RaceEventManager>()->start(gep); if (!RaceEventManager::get())
RaceEventManager::create();
RaceEventManager::get()->start(gep);
// Make sure that if there is only a single local player this player can // Make sure that if there is only a single local player this player can
// use all input devices. // use all input devices.

View File

@ -1684,8 +1684,8 @@ bool ServerLobby::canLiveJoinNow() const
*/ */
bool ServerLobby::worldIsActive() const bool ServerLobby::worldIsActive() const
{ {
return World::getWorld() && RaceEventManager::getInstance()->isRunning() && return World::getWorld() && RaceEventManager::get()->isRunning() &&
!RaceEventManager::getInstance()->isRaceOver() && !RaceEventManager::get()->isRaceOver() &&
World::getWorld()->getPhase() == WorldStatus::RACE_PHASE; World::getWorld()->getPhase() == WorldStatus::RACE_PHASE;
} // worldIsActive } // worldIsActive
@ -2048,8 +2048,8 @@ void ServerLobby::update(int ticks)
// Reset server to initial state if no more connected players // Reset server to initial state if no more connected players
if (m_rs_state.load() == RS_WAITING) if (m_rs_state.load() == RS_WAITING)
{ {
if ((RaceEventManager::getInstance() && if ((RaceEventManager::get() &&
!RaceEventManager::getInstance()->protocolStopped()) || !RaceEventManager::get()->protocolStopped()) ||
!GameProtocol::emptyInstance()) !GameProtocol::emptyInstance())
return; return;
@ -2064,8 +2064,8 @@ void ServerLobby::update(int ticks)
(STKHost::get()->getPlayersInGame() == 0 || (STKHost::get()->getPlayersInGame() == 0 ||
all_players_in_world_disconnected)) all_players_in_world_disconnected))
{ {
if (RaceEventManager::getInstance() && if (RaceEventManager::get() &&
RaceEventManager::getInstance()->isRunning()) RaceEventManager::get()->isRunning())
{ {
// Send a notification to all players who may have start live join // Send a notification to all players who may have start live join
// or spectate to go back to lobby // or spectate to go back to lobby
@ -2075,8 +2075,8 @@ void ServerLobby::update(int ticks)
sendMessageToPeersInServer(back_to_lobby, /*reliable*/true); sendMessageToPeersInServer(back_to_lobby, /*reliable*/true);
delete back_to_lobby; delete back_to_lobby;
RaceEventManager::getInstance()->stop(); RaceEventManager::get()->stop();
RaceEventManager::getInstance()->getProtocol()->requestTerminate(); RaceEventManager::get()->getProtocol()->requestTerminate();
GameProtocol::lock()->requestTerminate(); GameProtocol::lock()->requestTerminate();
} }
else if (auto ai = m_ai_peer.lock()) else if (auto ai = m_ai_peer.lock())
@ -2138,14 +2138,14 @@ void ServerLobby::update(int ticks)
m_state = WAIT_FOR_WORLD_LOADED; m_state = WAIT_FOR_WORLD_LOADED;
break; break;
case RACING: case RACING:
if (World::getWorld() && if (World::getWorld() && RaceEventManager::get() &&
RaceEventManager::getInstance<RaceEventManager>()->isRunning()) RaceEventManager::get()->isRunning())
{ {
checkRaceFinished(); checkRaceFinished();
} }
break; break;
case WAIT_FOR_RACE_STOPPED: case WAIT_FOR_RACE_STOPPED:
if (!RaceEventManager::getInstance()->protocolStopped() || if (!RaceEventManager::get()->protocolStopped() ||
!GameProtocol::emptyInstance()) !GameProtocol::emptyInstance())
return; return;
@ -2698,16 +2698,16 @@ void ServerLobby::checkIncomingConnectionRequests()
*/ */
void ServerLobby::checkRaceFinished() void ServerLobby::checkRaceFinished()
{ {
assert(RaceEventManager::getInstance()->isRunning()); assert(RaceEventManager::get()->isRunning());
assert(World::getWorld()); assert(World::getWorld());
if (!RaceEventManager::getInstance()->isRaceOver()) return; if (!RaceEventManager::get()->isRaceOver()) return;
Log::info("ServerLobby", "The game is considered finished."); Log::info("ServerLobby", "The game is considered finished.");
// notify the network world that it is stopped // notify the network world that it is stopped
RaceEventManager::getInstance()->stop(); RaceEventManager::get()->stop();
// stop race protocols before going back to lobby (end race) // stop race protocols before going back to lobby (end race)
RaceEventManager::getInstance()->getProtocol()->requestTerminate(); RaceEventManager::get()->getProtocol()->requestTerminate();
GameProtocol::lock()->requestTerminate(); GameProtocol::lock()->requestTerminate();
// Save race result before delete the world // Save race result before delete the world

View File

@ -7,7 +7,39 @@
#include "network/protocols/game_events_protocol.hpp" #include "network/protocols/game_events_protocol.hpp"
#include "network/rewind_manager.hpp" #include "network/rewind_manager.hpp"
#include "utils/profiler.hpp" #include "utils/profiler.hpp"
#include "utils/stk_process.hpp"
//=============================================================================
RaceEventManager* g_race_event_manager[PT_COUNT];
// ----------------------------------------------------------------------------
RaceEventManager* RaceEventManager::get()
{
ProcessType type = STKProcess::getType();
return g_race_event_manager[type];
} // get
// ----------------------------------------------------------------------------
void RaceEventManager::create()
{
ProcessType type = STKProcess::getType();
g_race_event_manager[type] = new RaceEventManager();
} // create
// ----------------------------------------------------------------------------
void RaceEventManager::destroy()
{
ProcessType type = STKProcess::getType();
delete g_race_event_manager[type];
g_race_event_manager[type] = NULL;
} // destroy
// ----------------------------------------------------------------------------
void RaceEventManager::clear()
{
memset(g_race_event_manager, 0, sizeof(g_race_event_manager));
} // clear
// ----------------------------------------------------------------------------
RaceEventManager::RaceEventManager() RaceEventManager::RaceEventManager()
{ {
m_running = false; m_running = false;

View File

@ -19,8 +19,6 @@
#ifndef NETWORK_WORLD_HPP #ifndef NETWORK_WORLD_HPP
#define NETWORK_WORLD_HPP #define NETWORK_WORLD_HPP
#include "input/input.hpp"
#include "utils/singleton.hpp"
#include <memory> #include <memory>
class Controller; class Controller;
@ -34,20 +32,25 @@ class Item;
* server to all clients. This object then triggers the right message * server to all clients. This object then triggers the right message
* from the various running protocols. * from the various running protocols.
*/ */
class RaceEventManager : public AbstractSingleton<RaceEventManager> class RaceEventManager
{ {
private: private:
bool m_running; bool m_running;
float m_race_time;
std::weak_ptr<GameEventsProtocol> m_game_events_protocol; std::weak_ptr<GameEventsProtocol> m_game_events_protocol;
friend class AbstractSingleton<RaceEventManager>; RaceEventManager();
~RaceEventManager();
RaceEventManager();
virtual ~RaceEventManager();
public: public:
// ----------------------------------------------------------------------------------------
static RaceEventManager* get();
// ----------------------------------------------------------------------------------------
static void create();
// ----------------------------------------------------------------------------------------
static void destroy();
// ----------------------------------------------------------------------------------------
static void clear();
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------
void update(int ticks, bool fast_forward); void update(int ticks, bool fast_forward);
// ------------------------------------------------------------------------ // ------------------------------------------------------------------------

View File

@ -49,7 +49,6 @@
#include "network/protocol_manager.hpp" #include "network/protocol_manager.hpp"
#include "network/network_config.hpp" #include "network/network_config.hpp"
#include "network/network_string.hpp" #include "network/network_string.hpp"
#include "network/race_event_manager.hpp"
#include "replay/replay_play.hpp" #include "replay/replay_play.hpp"
#include "scriptengine/property_animator.hpp" #include "scriptengine/property_animator.hpp"
#include "states_screens/grand_prix_cutscene.hpp" #include "states_screens/grand_prix_cutscene.hpp"
@ -375,7 +374,7 @@ void RaceManager::startNew(bool from_overworld)
m_num_laps = m_grand_prix.getLaps(); m_num_laps = m_grand_prix.getLaps();
m_reverse_track = m_grand_prix.getReverse(); m_reverse_track = m_grand_prix.getReverse();
if (!RaceEventManager::getInstance<RaceEventManager>()->isRunning()) if (!NetworkConfig::get()->isNetworking())
{ {
// We look if Player 1 has a saved version of this GP. // We look if Player 1 has a saved version of this GP.
m_saved_gp = SavedGrandPrix::getSavedGP( m_saved_gp = SavedGrandPrix::getSavedGP(
@ -678,8 +677,8 @@ void RaceManager::next()
m_track_number++; m_track_number++;
if(m_track_number<(int)m_tracks.size()) if(m_track_number<(int)m_tracks.size())
{ {
if( m_major_mode==MAJOR_MODE_GRAND_PRIX && if (m_major_mode == MAJOR_MODE_GRAND_PRIX &&
!RaceEventManager::getInstance()->isRunning() ) !NetworkConfig::get()->isNetworking())
{ {
// Saving GP state // Saving GP state
saveGP(); saveGP();
@ -841,8 +840,8 @@ void RaceManager::exitRace(bool delete_world)
m_track_number==(int)m_tracks.size() ) m_track_number==(int)m_tracks.size() )
{ {
PlayerManager::getCurrentPlayer()->grandPrixFinished(); PlayerManager::getCurrentPlayer()->grandPrixFinished();
if( m_major_mode==MAJOR_MODE_GRAND_PRIX && if (m_major_mode == MAJOR_MODE_GRAND_PRIX &&
!RaceEventManager::getInstance()->isRunning() ) !NetworkConfig::get()->isNetworking())
{ {
if(m_saved_gp != NULL) if(m_saved_gp != NULL)
m_saved_gp->remove(); m_saved_gp->remove();
@ -1052,7 +1051,7 @@ void RaceManager::startSingleRace(const std::string &track_ident,
setCoinTarget( 0 ); // Might still be set from a previous challenge setCoinTarget( 0 ); // Might still be set from a previous challenge
// if not in a network world, setup player karts // if not in a network world, setup player karts
if (!RaceEventManager::getInstance<RaceEventManager>()->isRunning()) if (!NetworkConfig::get()->isNetworking())
setupPlayerKartInfo(); // do this setup player kart setupPlayerKartInfo(); // do this setup player kart
startNew(from_overworld); startNew(from_overworld);

View File

@ -432,8 +432,8 @@ void NetworkingLobby::onUpdate(float delta)
// You can live join or spectator if u have the current play track // You can live join or spectator if u have the current play track
// and network timer is synchronized, and no game protocols exist // and network timer is synchronized, and no game protocols exist
bool no_gep = !RaceEventManager::getInstance() || bool no_gep = !RaceEventManager::get() ||
RaceEventManager::getInstance()->protocolStopped(); RaceEventManager::get()->protocolStopped();
bool no_gp = GameProtocol::emptyInstance(); bool no_gp = GameProtocol::emptyInstance();
if (t && if (t &&
STKHost::get()->getNetworkTimerSynchronizer()->isSynchronised() && STKHost::get()->getNetworkTimerSynchronizer()->isSynchronised() &&