Eile Mit Weile
Loading...
Searching...
No Matches
GameInstanceManager Class Reference

Manages all active GameInstances. More...

#include <GameInstanceManager.hpp>

Static Public Member Functions

static bool TryGetGameInstance (const std::string &game_id, GameInstance *&game_instance_ptr)
 
static bool TryGetPlayerAndGameInstance (const std::string &player_id, Player *&player, GameInstance *&game_instance_ptr, std::string &err)
 
static bool TryAddPlayerToAnyGame (Player *player, GameInstance *&game_instance_ptr, std::string &err)
 
static bool TryAddPlayer (Player *player, GameInstance *&game_instance_ptr, std::string &err)
 
static bool TryRemovePlayer (Player *player, const std::string &game_id, std::string &err)
 
static bool TryRemovePlayer (Player *player, GameInstance *&game_instance_ptr, std::string &err)
 

Static Private Member Functions

static GameInstanceCreateNewGame ()
 
static GameInstanceFindJoinableGameInstance ()
 

Static Private Attributes

static std::shared_mutex games_lut_lock
 
static std::unordered_map< std::string, GameInstance * > games_lut = {}
 

Detailed Description

Manages all active GameInstances.

The GameInstanceManager only exists on the server side. It stores all currently active games and offers functionality to retrieve game instances by id and adding players to games. If a new player requests to join a game but no valid GameInstance is available, then this class will generate a new GameInstance and add it to the unordered_map of (active) game instances.

Note: This class design is taken from https://gitlab.ethz.ch/hraymond/cse-lama-example-project. They made the choice to have all members be static, which is why this class is a singleton and should not be instantiated. It also references the PlayerManager and ServerNetworkManager classes without having to instantiate them as member vars. We consider this bad practice but changing would require a lot of refactoring so we decided to keep it as is.

Member Function Documentation

◆ CreateNewGame()

GameInstance * GameInstanceManager::CreateNewGame ( )
staticprivate

Creates a new GameInstance and adds it to the unordered_map of game instances.

◆ FindJoinableGameInstance()

GameInstance * GameInstanceManager::FindJoinableGameInstance ( )
staticprivate

Finds a non-full, non-started game instance or creates a new one if none is found.

◆ TryAddPlayer()

bool GameInstanceManager::TryAddPlayer ( Player player,
GameInstance *&  game_instance_ptr,
std::string &  err 
)
static

Try to add Player to the provided game_instance_ptr. Returns true if success and false otherwise. Error message will be written into err.

◆ TryAddPlayerToAnyGame()

bool GameInstanceManager::TryAddPlayerToAnyGame ( Player player,
GameInstance *&  game_instance_ptr,
std::string &  err 
)
static

Try to add Player to any GameInstance. Returns true if Player is successfully added to a GameInstance. The joined GameInstance will be written into game_instance_ptr. Error message will be written into err.

◆ TryGetGameInstance()

bool GameInstanceManager::TryGetGameInstance ( const std::string &  game_id,
GameInstance *&  game_instance_ptr 
)
static

returns true if the desired game_instance 'game_id' was found or false otherwise. The found game instance is written into game_instance_ptr.

◆ TryGetPlayerAndGameInstance()

bool GameInstanceManager::TryGetPlayerAndGameInstance ( const std::string &  player_id,
Player *&  player,
GameInstance *&  game_instance_ptr,
std::string &  err 
)
static

returns true if the desired player player_id was found and is connected to a game_instance. The found player and GameInstance will be written into player and game_instance_ptr Error message will be written into err.

◆ TryRemovePlayer() [1/2]

bool GameInstanceManager::TryRemovePlayer ( Player player,
const std::string &  game_id,
std::string &  err 
)
static

Try to remove Player from the provided game_id (see GameInstance::GetId). Returns true if success and false otherwise. Error message will be written into err.

◆ TryRemovePlayer() [2/2]

bool GameInstanceManager::TryRemovePlayer ( Player player,
GameInstance *&  game_instance_ptr,
std::string &  err 
)
static

Try to remove Player from the provided game_instance_ptr. Returns true if success and false otherwise. Error message will be written into err.

Member Data Documentation

◆ games_lut

std::unordered_map< std::string, GameInstance * > GameInstanceManager::games_lut = {}
staticprivate

Unordered_map of all active game instances.

◆ games_lut_lock

std::shared_mutex GameInstanceManager::games_lut_lock
inlinestaticprivate

Mutex to lock the unordered_map of game instances for reading and writing.


The documentation for this class was generated from the following files: