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

Manages all connected Players. More...

#include <PlayerManager.hpp>

Static Public Member Functions

static bool TryGetPlayer (const std::string &player_id, Player *&player_ptr)
 
static bool AddOrGetPlayer (std::string name, const std::string &player_id, Player *&player_ptr)
 
static bool RemovePlayer (const std::string &player_id, Player *&player)
 

Static Private Attributes

static std::shared_mutex rw_lock
 
static std::unordered_map< std::string, Player * > players_lut = {}
 

Detailed Description

Manages all connected Players.

The PlayerManager only exists on the server side. It stores all connected users since starting the server. It offers functionality to retrieve players by id or adding players when they first connect to the server.

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. Its members are referenced by other classes without them having to instatiate a PlayerManager as a member var. We consider this bad practice but changing would require a lot of refactoring so we decided to keep it as is.

Member Function Documentation

◆ AddOrGetPlayer()

bool PlayerManager::AddOrGetPlayer ( std::string  name,
const std::string &  player_id,
Player *&  player_ptr 
)
static

Returns true if the desired Player player_id was found or false otherwise. The found Player is written into player_ptr. If the player was not found, a new Player is created (constructor) and added to the unordered_map of Players.

◆ RemovePlayer()

bool PlayerManager::RemovePlayer ( const std::string &  player_id,
Player *&  player 
)
static

Returns true if the desired Player player_id was found and removed or false otherwise.

◆ TryGetPlayer()

bool PlayerManager::TryGetPlayer ( const std::string &  player_id,
Player *&  player_ptr 
)
static

Returns true if the desired Player player_id was found or false otherwise. The found Player is written into player_ptr.

Member Data Documentation

◆ players_lut

std::unordered_map< std::string, Player * > PlayerManager::players_lut = {}
staticprivate

Unordered_map of all connected players. The Player::id (in UniqueSerializable) serves as a key.

◆ rw_lock

std::shared_mutex PlayerManager::rw_lock
inlinestaticprivate

Mutex to lock the unordered_map of players for reading and writing.


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