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::RoomSystem Class Reference

System to handle room-related operations on the server side. More...

#include <RoomSystem.hpp>

Inheritance diagram for rtp::server::RoomSystem:
Collaboration diagram for rtp::server::RoomSystem:

Public Types

using RoomStartedCb = std::function< void(uint32_t roomId)>
 Callback type for when a room starts.
 

Public Member Functions

void setOnRoomStarted (RoomStartedCb cb)
 Set the callback to be invoked when a room starts.
 
 RoomSystem (ServerNetwork &network, ecs::Registry &registry, NetworkSyncSystem &networkSync)
 Constructor for RoomSystem.
 
void update (float dt) override
 Update system logic for one frame.
 
uint32_t createRoom (uint32_t sessionId, const std::string &roomName, uint8_t maxPlayers, float difficulty, float speed, Room::RoomType type, uint32_t levelId, uint32_t seed, uint32_t durationMinutes)
 Create a new room based on client request.
 
bool joinRoom (PlayerPtr player, uint32_t roomId, bool asSpectator=false)
 Join an existing room based on client request.
 
bool joinLobby (PlayerPtr player)
 Join the lobby room.
 
bool leaveRoom (PlayerPtr player)
 Leave the current room based on client request.
 
void disconnectPlayer (uint32_t sessionId)
 Disconnect a player from all rooms.
 
void listAllRooms (uint32_t sessionId)
 List all available rooms based on client request.
 
void chatInRoom (uint32_t sessionId, const net::Packet &packet)
 Handle chat message in the room based on client request.
 
void launchReadyRooms (float dt)
 Launch all rooms that are ready to start the game.
 
std::shared_ptr< RoomgetRoom (uint32_t roomId)
 Get a room by its ID.
 
- Public Member Functions inherited from rtp::ecs::ISystem
virtual ~ISystem () noexcept=default
 

Private Member Functions

void despawnPlayerEntity (const PlayerPtr &player, const std::shared_ptr< Room > &room)
 

Private Attributes

ServerNetwork_network
 Reference to the server network manager.
 
NetworkSyncSystem_networkSync
 Reference to the network sync system.
 
ecs::Registry_registry
 Reference to the entity registry.
 
std::map< uint32_t, std::shared_ptr< Room > > _rooms {}
 Map of room ID to Room instances.
 
std::map< uint32_t, uint32_t > _playerRoomMap
 Map of player session ID to room ID.
 
uint32_t _nextRoomId = 1
 Next available room ID.
 
uint32_t _lobbyId = 0
 ID of the main lobby room.
 
std::mutex _mutex
 Mutex for thread-safe operations.
 
RoomStartedCb _onRoomStarted
 Callback for when a room starts.
 

Detailed Description

System to handle room-related operations on the server side.

Definition at line 33 of file RoomSystem.hpp.

Member Typedef Documentation

◆ RoomStartedCb

Callback type for when a room starts.

Definition at line 40 of file RoomSystem.hpp.

Constructor & Destructor Documentation

◆ RoomSystem()

rtp::server::RoomSystem::RoomSystem ( ServerNetwork network,
ecs::Registry registry,
NetworkSyncSystem networkSync 
)

Constructor for RoomSystem.

Parameters
networkReference to the server network manager
registryReference to the entity registry

Definition at line 22 of file RoomSystem.cpp.

References _lobbyId, _networkSync, _nextRoomId, _registry, _rooms, rtp::log::info(), and rtp::server::Room::Lobby.

Member Function Documentation

◆ chatInRoom()

void rtp::server::RoomSystem::chatInRoom ( uint32_t  sessionId,
const net::Packet packet 
)

Handle chat message in the room based on client request.

Parameters
sessionIdID of the network session
packetPacket containing the chat message data

Definition at line 296 of file RoomSystem.cpp.

◆ createRoom()

uint32_t rtp::server::RoomSystem::createRoom ( uint32_t  sessionId,
const std::string &  roomName,
uint8_t  maxPlayers,
float  difficulty,
float  speed,
Room::RoomType  type,
uint32_t  levelId,
uint32_t  seed,
uint32_t  durationMinutes 
)

Create a new room based on client request.

Parameters
sessionIdID of the network session
roomNameName of the room
maxPlayersMaximum number of players allowed in the room
difficultyDifficulty setting for the room
speedSpeed setting for the room
typeType of the room (Public/Private)
levelIdID of the level to be played in the room
seedSeed for level generation
durationMinutesDuration of the game in minutes
Returns
ID of the newly created room

Definition at line 43 of file RoomSystem.cpp.

References _networkSync, _nextRoomId, _registry, _rooms, and rtp::log::info().

◆ despawnPlayerEntity()

◆ disconnectPlayer()

void rtp::server::RoomSystem::disconnectPlayer ( uint32_t  sessionId)

Disconnect a player from all rooms.

Parameters
sessionIdID of the network session

Definition at line 198 of file RoomSystem.cpp.

References _mutex, _playerRoomMap, _rooms, despawnPlayerEntity(), and rtp::server::Room::Lobby.

◆ getRoom()

◆ joinLobby()

bool rtp::server::RoomSystem::joinLobby ( PlayerPtr  player)

Join the lobby room.

Parameters
sessionIdID of the network session

Definition at line 161 of file RoomSystem.cpp.

References _lobbyId, and joinRoom().

◆ joinRoom()

bool rtp::server::RoomSystem::joinRoom ( PlayerPtr  player,
uint32_t  roomId,
bool  asSpectator = false 
)

◆ launchReadyRooms()

void rtp::server::RoomSystem::launchReadyRooms ( float  dt)

◆ leaveRoom()

bool rtp::server::RoomSystem::leaveRoom ( PlayerPtr  player)

Leave the current room based on client request.

Parameters
sessionIdID of the network session
packetPacket containing the room leave data

Definition at line 166 of file RoomSystem.cpp.

References _mutex, _playerRoomMap, _rooms, despawnPlayerEntity(), and rtp::server::Room::Lobby.

Referenced by joinRoom().

◆ listAllRooms()

void rtp::server::RoomSystem::listAllRooms ( uint32_t  sessionId)

List all available rooms based on client request.

Parameters
sessionIdID of the network session
packetPacket containing the room list request data

Definition at line 234 of file RoomSystem.cpp.

References _mutex, _network, _rooms, rtp::log::info(), rtp::server::Room::InGame, rtp::server::Room::Lobby, rtp::server::Room::Private, rtp::server::Room::Public, rtp::net::RoomInfo::roomId, rtp::net::RoomList, rtp::server::ServerNetwork::sendPacket(), and rtp::net::TCP.

◆ setOnRoomStarted()

void rtp::server::RoomSystem::setOnRoomStarted ( RoomStartedCb  cb)
inline

Set the callback to be invoked when a room starts.

Parameters
cbCallback function

Definition at line 46 of file RoomSystem.hpp.

References _onRoomStarted.

◆ update()

void rtp::server::RoomSystem::update ( float  dt)
overridevirtual

Update system logic for one frame.

Parameters
dtTime elapsed since last update in seconds
Note
Currently not used

Implements rtp::ecs::ISystem.

Definition at line 34 of file RoomSystem.cpp.

References _rooms, and launchReadyRooms().

Member Data Documentation

◆ _lobbyId

uint32_t rtp::server::RoomSystem::_lobbyId = 0
private

ID of the main lobby room.

Definition at line 141 of file RoomSystem.hpp.

Referenced by RoomSystem(), and joinLobby().

◆ _mutex

std::mutex rtp::server::RoomSystem::_mutex
mutableprivate

Mutex for thread-safe operations.

Definition at line 142 of file RoomSystem.hpp.

Referenced by disconnectPlayer(), getRoom(), joinRoom(), launchReadyRooms(), leaveRoom(), and listAllRooms().

◆ _network

ServerNetwork& rtp::server::RoomSystem::_network
private

Reference to the server network manager.

Definition at line 134 of file RoomSystem.hpp.

Referenced by joinRoom(), launchReadyRooms(), and listAllRooms().

◆ _networkSync

NetworkSyncSystem& rtp::server::RoomSystem::_networkSync
private

Reference to the network sync system.

Definition at line 135 of file RoomSystem.hpp.

Referenced by RoomSystem(), createRoom(), and despawnPlayerEntity().

◆ _nextRoomId

uint32_t rtp::server::RoomSystem::_nextRoomId = 1
private

Next available room ID.

Definition at line 140 of file RoomSystem.hpp.

Referenced by RoomSystem(), and createRoom().

◆ _onRoomStarted

RoomStartedCb rtp::server::RoomSystem::_onRoomStarted
private

Callback for when a room starts.

Definition at line 143 of file RoomSystem.hpp.

Referenced by launchReadyRooms(), and setOnRoomStarted().

◆ _playerRoomMap

std::map<uint32_t, uint32_t> rtp::server::RoomSystem::_playerRoomMap
private

Map of player session ID to room ID.

Definition at line 139 of file RoomSystem.hpp.

Referenced by disconnectPlayer(), joinRoom(), and leaveRoom().

◆ _registry

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

Reference to the entity registry.

Definition at line 136 of file RoomSystem.hpp.

Referenced by RoomSystem(), createRoom(), and despawnPlayerEntity().

◆ _rooms

std::map<uint32_t, std::shared_ptr<Room> > rtp::server::RoomSystem::_rooms {}
private

Map of room ID to Room instances.

Definition at line 138 of file RoomSystem.hpp.

Referenced by RoomSystem(), createRoom(), disconnectPlayer(), getRoom(), joinRoom(), launchReadyRooms(), leaveRoom(), listAllRooms(), and update().


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