R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
server::Room Class Reference

Concrete implementation of IRoom with game instance management. More...

#include <Room.hpp>

Inheritance diagram for server::Room:
Inheritance graph
Collaboration diagram for server::Room:
Collaboration graph

Public Member Functions

 Room (const std::string &id, const std::string &name="", size_t maxPlayers=4, bool isPrivate=false, float gameSpeedMultiplier=1.0f, std::shared_ptr< EventBus > eventBus=nullptr)
 Construct a room.
 
 ~Room () override=default
 
bool join (uint32_t playerId) override
 Join a player to the room.
 
bool joinAsSpectator (uint32_t playerId) override
 Join a player to the room as a spectator.
 
bool leave (uint32_t playerId) override
 Remove a player from the room.
 
std::string getId () const override
 Get the room's unique identifier.
 
std::string getName () const override
 Get the room's display name.
 
RoomState getState () const override
 Get current state of the room.
 
void setState (RoomState state) override
 Set the state of the room.
 
size_t getPlayerCount () const override
 Get number of players currently in room.
 
size_t getMaxPlayers () const override
 Get maximum number of players allowed.
 
bool isFull () const override
 Check if room is full.
 
std::vector< uint32_t > getPlayers () const override
 Get list of player IDs in this room.
 
std::vector< uint32_t > getSpectators () const override
 Get list of spectator IDs in this room.
 
bool hasPlayer (uint32_t playerId) const override
 Check if a player is in this room.
 
bool hasSpectator (uint32_t playerId) const override
 Check if a spectator is in this room.
 
RoomInfo getInfo () const override
 Get room information.
 
std::shared_ptr< IGameLogicgetGameLogic () const
 Get the game logic instance.
 
ServerLoopgetServerLoop () const
 Get the server loop instance for this room.
 
bool startGame ()
 Start the game for this room.
 
void requestStartGame ()
 Request to start the game (initiates countdown)
 
void update (float deltaTime)
 Update the room state (called by server loop)
 
void setHost (uint32_t playerId)
 Set the host player ID.
 
uint32_t getHost () const
 Get the host player ID.
 
void broadcastChatMessage (uint32_t senderId, const std::string &senderName, const std::string &message)
 Broadcast a chat message to all players in the room.
 
bool isPrivate () const
 Check if room is private.
 
float getGameSpeedMultiplier () const
 Get the game speed multiplier.
 
bool isGameStartSent () const
 Check if GameStart has been sent.
 
void setGameStartSent (bool sent)
 Mark GameStart as sent.
 
bool tryMarkGameStartSent ()
 Atomically check and set GameStart sent flag (thread-safe)
 
- Public Member Functions inherited from server::IRoom
virtual ~IRoom ()=default
 

Private Attributes

std::string _id
 
std::string _name
 
RoomState _state
 
size_t _maxPlayers
 
bool _isPrivate
 
float _gameSpeedMultiplier
 
uint32_t _hostPlayerId
 
std::vector< uint32_t > _players
 
std::vector< uint32_t > _spectators
 
std::shared_ptr< IGameLogic_gameLogic
 
std::unique_ptr< ServerLoop_gameLoop
 
std::shared_ptr< EventBus_eventBus
 
std::mutex _mutex
 
bool _gameStartSent
 

Detailed Description

Concrete implementation of IRoom with game instance management.

Definition at line 26 of file Room.hpp.

Constructor & Destructor Documentation

◆ Room()

server::Room::Room ( const std::string &  id,
const std::string &  name = "",
size_t  maxPlayers = 4,
bool  isPrivate = false,
float  gameSpeedMultiplier = 1.0f,
std::shared_ptr< EventBus eventBus = nullptr 
)
explicit

Construct a room.

Parameters
idUnique room identifier
nameDisplay name for the room
maxPlayersMaximum number of players (default: 4)
isPrivateWhether the room is private (default: false)
gameSpeedMultiplierGame speed multiplier (default: 1.0)
eventBusShared event bus for server-wide events

Definition at line 21 of file Room.cpp.

References _eventBus, _gameLogic, _gameLoop, _gameSpeedMultiplier, _id, _isPrivate, _maxPlayers, _name, LOG_ERROR, and LOG_INFO.

◆ ~Room()

server::Room::~Room ( )
overridedefault

Member Function Documentation

◆ broadcastChatMessage()

void server::Room::broadcastChatMessage ( uint32_t  senderId,
const std::string &  senderName,
const std::string &  message 
)

Broadcast a chat message to all players in the room.

Parameters
senderIdID of the player sending the message
senderNameName of the player sending the message
messageThe message text

Definition at line 265 of file Room.cpp.

References _id, and LOG_INFO.

◆ getGameLogic()

std::shared_ptr< IGameLogic > server::Room::getGameLogic ( ) const
inline

Get the game logic instance.

Returns
Shared pointer to game logic (may be nullptr)

Definition at line 62 of file Room.hpp.

References _gameLogic.

◆ getGameSpeedMultiplier()

float server::Room::getGameSpeedMultiplier ( ) const
inline

Get the game speed multiplier.

Returns
Game speed multiplier value

Definition at line 121 of file Room.hpp.

References _gameSpeedMultiplier.

◆ getHost()

uint32_t server::Room::getHost ( ) const
inline

Get the host player ID.

Returns
Host player ID (0 if no host)

Definition at line 100 of file Room.hpp.

References _hostPlayerId.

◆ getId()

std::string server::Room::getId ( ) const
inlineoverridevirtual

Get the room's unique identifier.

Returns
Room ID

Implements server::IRoom.

Definition at line 45 of file Room.hpp.

References _id.

◆ getInfo()

RoomInfo server::Room::getInfo ( ) const
overridevirtual

Get room information.

Returns
RoomInfo struct

Implements server::IRoom.

Definition at line 161 of file Room.cpp.

References _hostPlayerId, _id, _isPrivate, _maxPlayers, _mutex, _name, _players, and _state.

◆ getMaxPlayers()

size_t server::Room::getMaxPlayers ( ) const
inlineoverridevirtual

Get maximum number of players allowed.

Returns
Max players

Implements server::IRoom.

Definition at line 50 of file Room.hpp.

References _maxPlayers.

◆ getName()

std::string server::Room::getName ( ) const
inlineoverridevirtual

Get the room's display name.

Returns
Room name

Implements server::IRoom.

Definition at line 46 of file Room.hpp.

References _name.

◆ getPlayerCount()

size_t server::Room::getPlayerCount ( ) const
overridevirtual

Get number of players currently in room.

Returns
Player count

Implements server::IRoom.

Definition at line 138 of file Room.cpp.

References _mutex, and _players.

◆ getPlayers()

std::vector< uint32_t > server::Room::getPlayers ( ) const
overridevirtual

Get list of player IDs in this room.

Returns
Vector of player IDs

Implements server::IRoom.

Definition at line 143 of file Room.cpp.

References _mutex, and _players.

◆ getServerLoop()

ServerLoop * server::Room::getServerLoop ( ) const
inline

Get the server loop instance for this room.

Returns
Pointer to ServerLoop (may be nullptr)

Definition at line 68 of file Room.hpp.

References _gameLoop.

◆ getSpectators()

std::vector< uint32_t > server::Room::getSpectators ( ) const
overridevirtual

Get list of spectator IDs in this room.

Returns
Vector of spectator IDs

Implements server::IRoom.

Definition at line 148 of file Room.cpp.

References _mutex, and _spectators.

◆ getState()

RoomState server::Room::getState ( ) const
inlineoverridevirtual

Get current state of the room.

Returns
RoomState

Implements server::IRoom.

Definition at line 47 of file Room.hpp.

References _state.

◆ hasPlayer()

bool server::Room::hasPlayer ( uint32_t  playerId) const
overridevirtual

Check if a player is in this room.

Parameters
playerIdPlayer ID to check
Returns
true if player is in room

Implements server::IRoom.

Definition at line 153 of file Room.cpp.

References _players.

Referenced by join(), joinAsSpectator(), and setHost().

◆ hasSpectator()

bool server::Room::hasSpectator ( uint32_t  playerId) const
overridevirtual

Check if a spectator is in this room.

Parameters
playerIdSpectator ID to check
Returns
true if spectator is in room

Implements server::IRoom.

Definition at line 157 of file Room.cpp.

References _spectators.

Referenced by joinAsSpectator().

◆ isFull()

bool server::Room::isFull ( ) const
inlineoverridevirtual

Check if room is full.

Returns
true if full, false otherwise

Implements server::IRoom.

Definition at line 51 of file Room.hpp.

References _maxPlayers, and _players.

Referenced by join().

◆ isGameStartSent()

bool server::Room::isGameStartSent ( ) const
inline

Check if GameStart has been sent.

Returns
true if GameStart has been sent to players

Definition at line 127 of file Room.hpp.

References _gameStartSent.

◆ isPrivate()

bool server::Room::isPrivate ( ) const
inline

Check if room is private.

Returns
true if room is private

Definition at line 115 of file Room.hpp.

References _isPrivate.

◆ join()

bool server::Room::join ( uint32_t  playerId)
overridevirtual

Join a player to the room.

Parameters
playerIdUnique identifier of the player
Returns
true if player successfully joined, false otherwise

Implements server::IRoom.

Definition at line 53 of file Room.cpp.

References _hostPlayerId, _id, _maxPlayers, _mutex, _players, _state, hasPlayer(), isFull(), LOG_INFO, LOG_WARNING, server::STARTING, and server::WAITING.

Here is the call graph for this function:

◆ joinAsSpectator()

bool server::Room::joinAsSpectator ( uint32_t  playerId)
overridevirtual

Join a player to the room as a spectator.

Parameters
playerIdUnique identifier of the player
Returns
true if spectator successfully joined, false otherwise

Implements server::IRoom.

Definition at line 80 of file Room.cpp.

References _id, _mutex, _spectators, hasPlayer(), hasSpectator(), LOG_INFO, and LOG_WARNING.

Here is the call graph for this function:

◆ leave()

bool server::Room::leave ( uint32_t  playerId)
overridevirtual

Remove a player from the room.

Parameters
playerIdUnique identifier of the player
Returns
true if player successfully left, false otherwise

Implements server::IRoom.

Definition at line 93 of file Room.cpp.

References _gameStartSent, _hostPlayerId, _id, _mutex, _players, _spectators, _state, server::IN_PROGRESS, LOG_INFO, setState(), server::STARTING, and server::WAITING.

Here is the call graph for this function:

◆ requestStartGame()

void server::Room::requestStartGame ( )

Request to start the game (initiates countdown)

This is called when the host requests to start the game. It transitions the room to STARTING state and begins the countdown.

Definition at line 249 of file Room.cpp.

References _id, _players, _state, LOG_INFO, startGame(), and server::WAITING.

Here is the call graph for this function:

◆ setGameStartSent()

void server::Room::setGameStartSent ( bool  sent)
inline

Mark GameStart as sent.

Definition at line 132 of file Room.hpp.

References _gameStartSent.

◆ setHost()

void server::Room::setHost ( uint32_t  playerId)

Set the host player ID.

Parameters
playerIdPlayer ID to set as host

Definition at line 242 of file Room.cpp.

References _hostPlayerId, _id, hasPlayer(), and LOG_INFO.

Here is the call graph for this function:

◆ setState()

void server::Room::setState ( RoomState  state)
overridevirtual

Set the state of the room.

Parameters
stateNew state

Implements server::IRoom.

Definition at line 129 of file Room.cpp.

References _name, _state, and LOG_INFO.

Referenced by leave(), startGame(), and update().

◆ startGame()

bool server::Room::startGame ( )

Start the game for this room.

Returns
true if game started successfully

Definition at line 167 of file Room.cpp.

References _gameLogic, _id, _mutex, _players, _state, server::IN_PROGRESS, LOG_ERROR, LOG_INFO, LOG_WARNING, setState(), server::STARTING, and server::WAITING.

Referenced by requestStartGame().

Here is the call graph for this function:

◆ tryMarkGameStartSent()

bool server::Room::tryMarkGameStartSent ( )

Atomically check and set GameStart sent flag (thread-safe)

Returns
true if flag was false and is now set to true, false if already sent

Definition at line 256 of file Room.cpp.

References _gameStartSent, and _mutex.

◆ update()

void server::Room::update ( float  deltaTime)

Update the room state (called by server loop)

Parameters
deltaTimeTime since last update

Definition at line 230 of file Room.cpp.

References _gameLogic, _id, _state, server::FINISHED, server::IN_PROGRESS, LOG_INFO, and setState().

Here is the call graph for this function:

Member Data Documentation

◆ _eventBus

std::shared_ptr<EventBus> server::Room::_eventBus
private

Definition at line 152 of file Room.hpp.

Referenced by Room().

◆ _gameLogic

std::shared_ptr<IGameLogic> server::Room::_gameLogic
private

Definition at line 150 of file Room.hpp.

Referenced by getGameLogic(), Room(), startGame(), and update().

◆ _gameLoop

std::unique_ptr<ServerLoop> server::Room::_gameLoop
private

Definition at line 151 of file Room.hpp.

Referenced by getServerLoop(), and Room().

◆ _gameSpeedMultiplier

float server::Room::_gameSpeedMultiplier
private

Definition at line 146 of file Room.hpp.

Referenced by getGameSpeedMultiplier(), and Room().

◆ _gameStartSent

bool server::Room::_gameStartSent
private

Definition at line 154 of file Room.hpp.

Referenced by isGameStartSent(), leave(), setGameStartSent(), and tryMarkGameStartSent().

◆ _hostPlayerId

uint32_t server::Room::_hostPlayerId
private

Definition at line 147 of file Room.hpp.

Referenced by getHost(), getInfo(), join(), leave(), and setHost().

◆ _id

std::string server::Room::_id
private

◆ _isPrivate

bool server::Room::_isPrivate
private

Definition at line 145 of file Room.hpp.

Referenced by getInfo(), isPrivate(), and Room().

◆ _maxPlayers

size_t server::Room::_maxPlayers
private

Definition at line 144 of file Room.hpp.

Referenced by getInfo(), getMaxPlayers(), isFull(), join(), and Room().

◆ _mutex

std::mutex server::Room::_mutex
mutableprivate

◆ _name

std::string server::Room::_name
private

Definition at line 142 of file Room.hpp.

Referenced by getInfo(), getName(), Room(), and setState().

◆ _players

std::vector<uint32_t> server::Room::_players
private

◆ _spectators

std::vector<uint32_t> server::Room::_spectators
private

Definition at line 149 of file Room.hpp.

Referenced by getSpectators(), hasSpectator(), joinAsSpectator(), and leave().

◆ _state

RoomState server::Room::_state
private

Definition at line 143 of file Room.hpp.

Referenced by getInfo(), getState(), join(), leave(), requestStartGame(), setState(), startGame(), and update().


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