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())
{
auto ge = RaceEventManager::getInstance()->getProtocol();
auto ge = RaceEventManager::get()->getProtocol();
assert(ge);
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())
{
RaceEventManager::getInstance()->kartFinishedRace(this, time);
RaceEventManager::get()->kartFinishedRace(this, time);
} // isServer
// 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/client_lobby.hpp"
#include "network/protocols/server_lobby.hpp"
#include "network/race_event_manager.hpp"
#include "network/rewind_manager.hpp"
#include "network/rewind_queue.hpp"
#include "network/server.hpp"
@ -1739,6 +1740,7 @@ void clearGlobalVariables()
STKHost::clear();
RaceManager::clear();
ProjectileManager::clear();
RaceEventManager::clear();
music_manager = NULL;
irr_driver = NULL;
#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
// The race event manager will update world in case of an online race
if ( RaceEventManager::getInstance() &&
RaceEventManager::getInstance()->isRunning() )
RaceEventManager::getInstance()->update(ticks, fast_forward);
if (RaceEventManager::get() && RaceEventManager::get()->isRunning())
RaceEventManager::get()->update(ticks, fast_forward);
else
World::getWorld()->updateWorld(ticks);
} // updateRace

View File

@ -434,7 +434,7 @@ void ClientLobby::update(int ticks)
}
break;
case RACE_FINISHED:
if (!RaceEventManager::getInstance()->protocolStopped() ||
if (!RaceEventManager::get()->protocolStopped() ||
!GameProtocol::emptyInstance())
return;
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
// disconnects
bool in_game_world = World::getWorld() &&
RaceEventManager::getInstance() &&
RaceEventManager::getInstance()->isRunning() &&
!RaceEventManager::getInstance()->isRaceOver();
RaceEventManager::get() &&
RaceEventManager::get()->isRunning() &&
!RaceEventManager::get()->isRaceOver();
if (!in_game_world)
SFXManager::get()->quickSound("appear");
@ -1128,7 +1128,7 @@ void ClientLobby::raceFinished(Event* event)
}
// stop race protocols
RaceEventManager::getInstance()->stop();
RaceEventManager::get()->stop();
ProtocolManager::lock()->findAndTerminate(PROTOCOL_GAME_EVENTS);
ProtocolManager::lock()->findAndTerminate(PROTOCOL_CONTROLLER_EVENTS);
m_state.store(RACE_FINISHED);
@ -1149,9 +1149,9 @@ void ClientLobby::backToLobby(Event *event)
m_auto_started = false;
m_state.store(CONNECTED);
if (RaceEventManager::getInstance())
if (RaceEventManager::get())
{
RaceEventManager::getInstance()->stop();
RaceEventManager::get()->stop();
ProtocolManager::lock()->findAndTerminate(PROTOCOL_GAME_EVENTS);
}
auto gp = GameProtocol::lock();

View File

@ -57,8 +57,7 @@ LobbyProtocol::LobbyProtocol()
// ----------------------------------------------------------------------------
LobbyProtocol::~LobbyProtocol()
{
if (RaceEventManager::getInstance())
RaceEventManager::getInstance()->stop();
RaceEventManager::destroy();
delete m_game_setup;
joinStartGameThread();
} // ~LobbyProtocol
@ -78,7 +77,9 @@ void LobbyProtocol::loadWorld()
// ---------------------
// This creates the network world.
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
// use all input devices.

View File

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

View File

@ -7,7 +7,39 @@
#include "network/protocols/game_events_protocol.hpp"
#include "network/rewind_manager.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()
{
m_running = false;

View File

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

View File

@ -49,7 +49,6 @@
#include "network/protocol_manager.hpp"
#include "network/network_config.hpp"
#include "network/network_string.hpp"
#include "network/race_event_manager.hpp"
#include "replay/replay_play.hpp"
#include "scriptengine/property_animator.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_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.
m_saved_gp = SavedGrandPrix::getSavedGP(
@ -679,7 +678,7 @@ void RaceManager::next()
if(m_track_number<(int)m_tracks.size())
{
if (m_major_mode == MAJOR_MODE_GRAND_PRIX &&
!RaceEventManager::getInstance()->isRunning() )
!NetworkConfig::get()->isNetworking())
{
// Saving GP state
saveGP();
@ -842,7 +841,7 @@ void RaceManager::exitRace(bool delete_world)
{
PlayerManager::getCurrentPlayer()->grandPrixFinished();
if (m_major_mode == MAJOR_MODE_GRAND_PRIX &&
!RaceEventManager::getInstance()->isRunning() )
!NetworkConfig::get()->isNetworking())
{
if(m_saved_gp != NULL)
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
// if not in a network world, setup player karts
if (!RaceEventManager::getInstance<RaceEventManager>()->isRunning())
if (!NetworkConfig::get()->isNetworking())
setupPlayerKartInfo(); // do this setup player kart
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
// and network timer is synchronized, and no game protocols exist
bool no_gep = !RaceEventManager::getInstance() ||
RaceEventManager::getInstance()->protocolStopped();
bool no_gep = !RaceEventManager::get() ||
RaceEventManager::get()->protocolStopped();
bool no_gp = GameProtocol::emptyInstance();
if (t &&
STKHost::get()->getNetworkTimerSynchronizer()->isSynchronised() &&