Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
rtp::server::Room Class Reference

Represents a game room in the server. More...

#include <Room.hpp>

Collaboration diagram for rtp::server::Room:

Public Types

enum  State { Waiting , InGame , Finished }
 Enum representing the state of the room. More...
 
enum  RoomType { Lobby , Public , Private }
 
enum  PlayerType { None , Player , Spectator }
 Enum representing the type of player in the room. More...
 

Public Member Functions

 Room (ecs::Registry &registry, NetworkSyncSystem &network, uint32_t id, const std::string &name, uint32_t maxPlayers, float difficulty, float speed, RoomType type, uint32_t creatorSessionId=0, uint32_t levelId=1, uint32_t seed=0, uint32_t durationMinutes=0)
 Constructor for Room.
 
 ~Room ()
 Destructor for Room.
 
bool canJoin (void) const
 Add a player to the room.
 
bool addPlayer (const PlayerPtr &player, PlayerType type=PlayerType::Player)
 Add a player to the room.
 
void removePlayer (uint32_t sessionId, bool disconnected=false)
 Remove a player from the room.
 
const std::list< PlayerPtrgetPlayers (void) const
 Get the list of players in the room.
 
uint32_t getId (void) const
 Get the unique identifier of the room.
 
RoomType getType (void) const
 Get the type of the room.
 
float getDifficulty (void) const
 Get the difficulty level of the room.
 
float getSpeed (void) const
 Get the speed multiplier of the room.
 
uint32_t getLevelId (void) const
 Get the level identifier of the room.
 
uint32_t getSeed (void) const
 Get the random seed of the room.
 
uint32_t getDurationMinutes (void) const
 Get the duration in minutes of the room.
 
PlayerType getPlayerType (uint32_t sessionId) const
 Get the type of a player in the room.
 
void setPlayerType (uint32_t sessionId, PlayerType type)
 Set the type of a player in the room.
 
bool hasActivePlayers () const
 Check if there are any active players (non-spectators)
 
std::string getName (void) const
 Get the name of the room.
 
uint32_t getMaxPlayers (void) const
 Get the maximum number of players allowed in the room.
 
uint32_t getCurrentPlayerCount (void) const
 Get the current number of players in the room.
 
State getState (void) const
 Get the current state of the room.
 
bool startGame (float dt)
 Start the game in the room.
 
void forceFinishGame (void)
 Finish the game in the room.
 
void update (uint32_t serverTick, float dt)
 Update the room state.
 
void broadcastRoomState (uint32_t serverTick)
 Broadcast the current room state to all connected players.
 
void banUser (const std::string &username)
 
bool isBanned (const std::string &username) const
 

Private Member Functions

void broadcastSystemMessage (const std::string &message)
 

Private Attributes

NetworkSyncSystem _network
 Reference to the server network manager.
 
ecs::Registry_registry
 Reference to the entity registry.
 
uint32_t _id
 Unique room identifier.
 
std::string _name
 Name of the room.
 
uint32_t _maxPlayers
 Maximum number of players allowed.
 
std::list< std::pair< PlayerPtr, PlayerType > > _players
 List of player ptr's in the room.
 
State _state
 Current state of the room.
 
RoomType _type
 Type of the room.
 
uint32_t _creatorSessionId
 Session ID of the room creator (Administrator)
 
std::pair< std::string, int > _bestRoomScore
 Best score achieved in the room.
 
uint32_t _levelId = 0
 Level identifier for the room.
 
uint32_t _seed = 0
 Seed for random generation in the room.
 
float _difficulty = 0
 Difficulty multiplier for the room 0 -> 1.
 
float _speed = 0
 Speed multiplier for the room 0 -> 2.
 
uint32_t durationMinutes = 0
 Duration of the game in minutes.
 
float _startedDt = 0.0f
 Delta time accumulator since game start.
 
uint32_t _currentTimeSeconds
 Current time in seconds since the game started.
 
std::mutex _mutex
 Mutex to protect access to room state.
 
std::unordered_set< std::string > _bannedUsers
 Banned usernames.
 
float _scoreTick {0.0f}
 Score tick accumulator.
 

Detailed Description

Represents a game room in the server.

A Room can hold multiple players and manage their states.

Definition at line 35 of file Room.hpp.

Member Enumeration Documentation

◆ PlayerType

Enum representing the type of player in the room.

Enumerator
None 

Undefined player type.

Player 

Regular player.

Spectator 

Spectator player.

Definition at line 61 of file Room.hpp.

◆ RoomType

Enumerator
Lobby 

Lobby room type, Only used for the principal Lobby.

Public 

Public room type, can be joined by anyone.

Private 

Private room type, hosted by the player.

Definition at line 51 of file Room.hpp.

◆ State

Enum representing the state of the room.

Enumerator
Waiting 

Waiting for players.

InGame 

Game in progress.

Finished 

Game finished.

Definition at line 41 of file Room.hpp.

Constructor & Destructor Documentation

◆ Room()

rtp::server::Room::Room ( ecs::Registry registry,
NetworkSyncSystem network,
uint32_t  id,
const std::string &  name,
uint32_t  maxPlayers,
float  difficulty,
float  speed,
RoomType  type,
uint32_t  creatorSessionId = 0,
uint32_t  levelId = 1,
uint32_t  seed = 0,
uint32_t  durationMinutes = 0 
)

Constructor for Room.

Parameters
idUnique identifier for the room
nameName of the room
maxPlayersMaximum number of players allowed in the room

Definition at line 45 of file Room.cpp.

References _id, _maxPlayers, _name, and rtp::log::info().

◆ ~Room()

rtp::server::Room::~Room ( )

Destructor for Room.

Definition at line 67 of file Room.cpp.

References _id, _name, and rtp::log::info().

Member Function Documentation

◆ addPlayer()

bool rtp::server::Room::addPlayer ( const PlayerPtr player,
PlayerType  type = PlayerType::Player 
)

Add a player to the room.

Parameters
playerShared pointer to the player to add
Returns
true if the player was added successfully, false if the room is full

Definition at line 78 of file Room.cpp.

References _bannedUsers, _id, _maxPlayers, _mutex, _name, _network, _players, _state, _type, broadcastSystemMessage(), rtp::log::info(), rtp::net::JoinRoom, Lobby, Player, rtp::server::NetworkSyncSystem::sendPacketToSession(), Spectator, rtp::net::TCP, Waiting, and rtp::log::warning().

◆ banUser()

void rtp::server::Room::banUser ( const std::string &  username)

Definition at line 195 of file Room.cpp.

References _bannedUsers, and _mutex.

◆ broadcastRoomState()

void rtp::server::Room::broadcastRoomState ( uint32_t  serverTick)

Broadcast the current room state to all connected players.

Parameters
serverTickCurrent server tick count

Definition at line 379 of file Room.cpp.

References _id, _mutex, _network, _players, _registry, _state, _type, rtp::ecs::Registry::get(), InGame, Lobby, rtp::net::RoomUpdate, rtp::server::NetworkSyncSystem::sendPacketToSession(), and rtp::net::UDP.

Referenced by update().

◆ broadcastSystemMessage()

void rtp::server::Room::broadcastSystemMessage ( const std::string &  message)
private

◆ canJoin()

bool rtp::server::Room::canJoin ( void  ) const

Add a player to the room.

Parameters
playerShared pointer to the player to add
Returns
true if the player was added successfully, false if the room is full

Definition at line 72 of file Room.cpp.

References _maxPlayers, _mutex, and _players.

◆ forceFinishGame()

void rtp::server::Room::forceFinishGame ( void  )

Finish the game in the room.

Note
Can be useful for the administrator to force finish the game

Definition at line 337 of file Room.cpp.

References _id, _mutex, _name, _state, Finished, rtp::log::info(), InGame, and rtp::log::warning().

◆ getCurrentPlayerCount()

uint32_t rtp::server::Room::getCurrentPlayerCount ( void  ) const

Get the current number of players in the room.

Returns
Current player count

Definition at line 309 of file Room.cpp.

References _mutex, and _players.

◆ getDifficulty()

float rtp::server::Room::getDifficulty ( void  ) const

Get the difficulty level of the room.

Returns
Current difficulty level

Definition at line 229 of file Room.cpp.

References _difficulty.

◆ getDurationMinutes()

uint32_t rtp::server::Room::getDurationMinutes ( void  ) const

Get the duration in minutes of the room.

Returns
Duration in minutes

Definition at line 249 of file Room.cpp.

References durationMinutes.

◆ getId()

uint32_t rtp::server::Room::getId ( void  ) const

Get the unique identifier of the room.

Returns
Current room ID

Definition at line 219 of file Room.cpp.

References _id.

Referenced by rtp::server::startGame().

◆ getLevelId()

uint32_t rtp::server::Room::getLevelId ( void  ) const

Get the level identifier of the room.

Returns
Level ID

Definition at line 239 of file Room.cpp.

References _levelId.

◆ getMaxPlayers()

uint32_t rtp::server::Room::getMaxPlayers ( void  ) const

Get the maximum number of players allowed in the room.

Returns
Maximum player count

Definition at line 304 of file Room.cpp.

References _maxPlayers.

◆ getName()

std::string rtp::server::Room::getName ( void  ) const

Get the name of the room.

Returns
Current room name

Definition at line 299 of file Room.cpp.

References _name.

◆ getPlayers()

const std::list< PlayerPtr > rtp::server::Room::getPlayers ( void  ) const

Get the list of players in the room.

Returns
Vector of shared pointers to players in the room

Definition at line 207 of file Room.cpp.

References _mutex, and _players.

◆ getPlayerType()

Room::PlayerType rtp::server::Room::getPlayerType ( uint32_t  sessionId) const

Get the type of a player in the room.

Parameters
sessionIdUnique identifier of the player
Returns
PlayerType of the specified player

Definition at line 254 of file Room.cpp.

References _id, _mutex, _name, _players, None, and rtp::log::warning().

◆ getSeed()

uint32_t rtp::server::Room::getSeed ( void  ) const

Get the random seed of the room.

Returns
Seed value

Definition at line 244 of file Room.cpp.

References _seed.

◆ getSpeed()

float rtp::server::Room::getSpeed ( void  ) const

Get the speed multiplier of the room.

Returns
Current speed multiplier

Definition at line 234 of file Room.cpp.

References _speed.

◆ getState()

Room::State rtp::server::Room::getState ( void  ) const

Get the current state of the room.

Returns
Current room state

Definition at line 315 of file Room.cpp.

References _mutex, and _state.

◆ getType()

Room::RoomType rtp::server::Room::getType ( void  ) const

Get the type of the room.

Returns
Current room type

Definition at line 224 of file Room.cpp.

References _type.

◆ hasActivePlayers()

bool rtp::server::Room::hasActivePlayers ( ) const

Check if there are any active players (non-spectators)

Returns
true if at least one active player remains

Definition at line 288 of file Room.cpp.

References _mutex, _players, and Player.

◆ isBanned()

bool rtp::server::Room::isBanned ( const std::string &  username) const

Definition at line 201 of file Room.cpp.

References _bannedUsers, and _mutex.

◆ removePlayer()

void rtp::server::Room::removePlayer ( uint32_t  sessionId,
bool  disconnected = false 
)

Remove a player from the room.

Parameters
sessionIdUnique identifier of the player to remove

Definition at line 130 of file Room.cpp.

References _id, _mutex, _name, _players, _type, broadcastSystemMessage(), rtp::log::info(), Lobby, None, Spectator, and rtp::log::warning().

◆ setPlayerType()

void rtp::server::Room::setPlayerType ( uint32_t  sessionId,
PlayerType  type 
)

Set the type of a player in the room.

Parameters
sessionIdUnique identifier of the player
typeNew PlayerType to set

Definition at line 269 of file Room.cpp.

References _id, _mutex, _name, _players, rtp::log::info(), rtp::toString(), and rtp::log::warning().

◆ startGame()

bool rtp::server::Room::startGame ( float  dt)

Start the game in the room.

Parameters
dtGet the delta time since last update

Definition at line 321 of file Room.cpp.

References _id, _mutex, _name, _startedDt, _state, rtp::log::info(), InGame, Waiting, and rtp::log::warning().

◆ update()

void rtp::server::Room::update ( uint32_t  serverTick,
float  dt 
)

Update the room state.

Parameters
dtTime elapsed since last update in seconds

Definition at line 350 of file Room.cpp.

References _mutex, _network, _players, _scoreTick, _state, broadcastRoomState(), InGame, Player, rtp::net::ScoreUpdate, rtp::server::NetworkSyncSystem::sendPacketToSession(), and rtp::net::TCP.

Member Data Documentation

◆ _bannedUsers

std::unordered_set<std::string> rtp::server::Room::_bannedUsers
private

Banned usernames.

Definition at line 250 of file Room.hpp.

Referenced by addPlayer(), banUser(), and isBanned().

◆ _bestRoomScore

std::pair<std::string, int> rtp::server::Room::_bestRoomScore
private

Best score achieved in the room.

Definition at line 238 of file Room.hpp.

◆ _creatorSessionId

uint32_t rtp::server::Room::_creatorSessionId
private

Session ID of the room creator (Administrator)

Definition at line 235 of file Room.hpp.

◆ _currentTimeSeconds

uint32_t rtp::server::Room::_currentTimeSeconds
private

Current time in seconds since the game started.

Definition at line 248 of file Room.hpp.

◆ _difficulty

float rtp::server::Room::_difficulty = 0
private

Difficulty multiplier for the room 0 -> 1.

Definition at line 242 of file Room.hpp.

Referenced by getDifficulty().

◆ _id

uint32_t rtp::server::Room::_id
private

Unique room identifier.

Definition at line 228 of file Room.hpp.

Referenced by Room(), ~Room(), addPlayer(), broadcastRoomState(), forceFinishGame(), getId(), getPlayerType(), removePlayer(), setPlayerType(), and startGame().

◆ _levelId

uint32_t rtp::server::Room::_levelId = 0
private

Level identifier for the room.

Definition at line 240 of file Room.hpp.

Referenced by getLevelId().

◆ _maxPlayers

uint32_t rtp::server::Room::_maxPlayers
private

Maximum number of players allowed.

Definition at line 230 of file Room.hpp.

Referenced by Room(), addPlayer(), canJoin(), and getMaxPlayers().

◆ _mutex

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

◆ _name

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

Name of the room.

Definition at line 229 of file Room.hpp.

Referenced by Room(), ~Room(), addPlayer(), forceFinishGame(), getName(), getPlayerType(), removePlayer(), setPlayerType(), and startGame().

◆ _network

NetworkSyncSystem rtp::server::Room::_network
private

Reference to the server network manager.

Definition at line 225 of file Room.hpp.

Referenced by addPlayer(), broadcastRoomState(), broadcastSystemMessage(), and update().

◆ _players

std::list<std::pair<PlayerPtr, PlayerType> > rtp::server::Room::_players
private

◆ _registry

ecs::Registry& rtp::server::Room::_registry
private

Reference to the entity registry.

Definition at line 226 of file Room.hpp.

Referenced by broadcastRoomState().

◆ _scoreTick

float rtp::server::Room::_scoreTick {0.0f}
private

Score tick accumulator.

Definition at line 251 of file Room.hpp.

Referenced by update().

◆ _seed

uint32_t rtp::server::Room::_seed = 0
private

Seed for random generation in the room.

Definition at line 241 of file Room.hpp.

Referenced by getSeed().

◆ _speed

float rtp::server::Room::_speed = 0
private

Speed multiplier for the room 0 -> 2.

Definition at line 243 of file Room.hpp.

Referenced by getSpeed().

◆ _startedDt

float rtp::server::Room::_startedDt = 0.0f
private

Delta time accumulator since game start.

Definition at line 246 of file Room.hpp.

Referenced by startGame().

◆ _state

State rtp::server::Room::_state
private

Current state of the room.

Definition at line 233 of file Room.hpp.

Referenced by addPlayer(), broadcastRoomState(), forceFinishGame(), getState(), startGame(), and update().

◆ _type

RoomType rtp::server::Room::_type
private

Type of the room.

Definition at line 234 of file Room.hpp.

Referenced by addPlayer(), broadcastRoomState(), getType(), and removePlayer().

◆ durationMinutes

uint32_t rtp::server::Room::durationMinutes = 0
private

Duration of the game in minutes.

Definition at line 244 of file Room.hpp.

Referenced by getDurationMinutes().


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