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

Automatic matchmaking service. More...

#include <MatchmakingService.hpp>

Inheritance diagram for server::MatchmakingService:
Inheritance graph
Collaboration diagram for server::MatchmakingService:
Collaboration graph

Public Member Functions

 MatchmakingService (size_t minPlayers=2, size_t maxPlayers=4, std::shared_ptr< server::EventBus > eventBus=nullptr)
 Construct matchmaking service.
 
 ~MatchmakingService () override=default
 
void addPlayer (uint32_t playerId) override
 Add a player to the matchmaking queue.
 
void removePlayer (uint32_t playerId) override
 Remove a player from the matchmaking queue.
 
void tick () override
 Process matchmaking queue and create matches Called periodically by the server.
 
size_t getQueueSize () const override
 Get the number of players waiting in queue.
 
void setMatchCreatedCallback (MatchCreatedCallback callback) override
 Set callback for when a match is created.
 
std::pair< std::shared_ptr< Room >, bool > findOrCreateMatch (uint32_t playerId, const std::vector< std::shared_ptr< Room > > &availableRooms, bool allowSpectator=true) override
 Find an available room or add player to matchmaking queue.
 
std::vector< PlayerQueueInfogetWaitingPlayers () const
 Get list of waiting players.
 
void setMinPlayers (size_t min)
 Set minimum players required to start a match.
 
void setMaxPlayers (size_t max)
 Set maximum players per match.
 
std::string getStatistics () const
 Get statistics about matchmaking.
 
- Public Member Functions inherited from server::IMatchmakingService
virtual ~IMatchmakingService ()=default
 

Private Member Functions

bool _tryCreateMatch ()
 Try to create a match from waiting players.
 
std::string _generateRoomId ()
 Generate unique room ID for a new match.
 

Private Attributes

size_t _minPlayers
 
size_t _maxPlayers
 
std::shared_ptr< server::EventBus_eventBus
 
std::vector< PlayerQueueInfo_waitingPlayers
 
std::mutex _mutex
 
MatchCreatedCallback _matchCreatedCallback
 
uint32_t _totalMatchesCreated {0}
 
uint32_t _totalPlayersMatched {0}
 

Detailed Description

Automatic matchmaking service.

Manages matchmaking for players in two modes:

  1. Manual Matchmaking (queue-based):
    • Players are added to a waiting queue via addPlayer()
    • tick() is called periodically to create matches when min players reached
    • Creates balanced matches (2-4 players)
  2. Auto-Matchmaking (instant + queue):
    • findOrCreateMatch() tries to find an existing room first (instant join)
    • If no room available, automatically adds to queue
    • Provides best user experience with minimal wait time

Features:

  • Automatic match creation when minimum players reached
  • Configurable min/max players per match
  • Wait time tracking
  • Callback notification when match is created
  • Thread-safe operations

Definition at line 53 of file MatchmakingService.hpp.

Constructor & Destructor Documentation

◆ MatchmakingService()

server::MatchmakingService::MatchmakingService ( size_t  minPlayers = 2,
size_t  maxPlayers = 4,
std::shared_ptr< server::EventBus eventBus = nullptr 
)
explicit

Construct matchmaking service.

Parameters
minPlayersMinimum players to start a match (default: 2)
maxPlayersMaximum players per match (default: 4)
eventBusShared event bus for server-wide events

Definition at line 16 of file MatchmakingService.cpp.

References _maxPlayers, _minPlayers, and LOG_INFO.

◆ ~MatchmakingService()

server::MatchmakingService::~MatchmakingService ( )
overridedefault

Member Function Documentation

◆ _generateRoomId()

std::string server::MatchmakingService::_generateRoomId ( )
private

Generate unique room ID for a new match.

Returns
Room ID string

Definition at line 138 of file MatchmakingService.cpp.

References _totalMatchesCreated.

Referenced by _tryCreateMatch().

◆ _tryCreateMatch()

bool server::MatchmakingService::_tryCreateMatch ( )
private

Try to create a match from waiting players.

Returns
true if a match was created

Definition at line 75 of file MatchmakingService.cpp.

References _eventBus, _generateRoomId(), _matchCreatedCallback, _maxPlayers, _minPlayers, _totalMatchesCreated, _totalPlayersMatched, _waitingPlayers, LOG_ERROR, and LOG_INFO.

Referenced by tick().

Here is the call graph for this function:

◆ addPlayer()

void server::MatchmakingService::addPlayer ( uint32_t  playerId)
overridevirtual

Add a player to the matchmaking queue.

Parameters
playerIdPlayer ID to add

Implements server::IMatchmakingService.

Definition at line 28 of file MatchmakingService.cpp.

References _mutex, _waitingPlayers, server::PlayerQueueInfo::joinTime, LOG_INFO, LOG_WARNING, and server::PlayerQueueInfo::playerId.

◆ findOrCreateMatch()

std::pair< std::shared_ptr< Room >, bool > server::MatchmakingService::findOrCreateMatch ( uint32_t  playerId,
const std::vector< std::shared_ptr< Room > > &  availableRooms,
bool  allowSpectator = true 
)
overridevirtual

Find an available room or add player to matchmaking queue.

Implements intelligent matchmaking strategy:

  1. Try to find a waiting room (instant join)
  2. If no waiting room, join as spectator to in-progress game (if allowed)
  3. If no matches available, add to queue for future match creation
Parameters
playerIdPlayer ID requesting matchmaking
availableRoomsList of currently available rooms to search
allowSpectatorWhether player can join as spectator if no waiting rooms
Returns
Pair of (room, isSpectator) if immediate match found, (nullptr, false) if added to queue

Implements server::IMatchmakingService.

Definition at line 151 of file MatchmakingService.cpp.

References _mutex, _waitingPlayers, server::IN_PROGRESS, server::PlayerQueueInfo::joinTime, LOG_INFO, server::PlayerQueueInfo::playerId, and server::WAITING.

◆ getQueueSize()

size_t server::MatchmakingService::getQueueSize ( ) const
overridevirtual

Get the number of players waiting in queue.

Returns
Queue size

Implements server::IMatchmakingService.

Definition at line 142 of file MatchmakingService.cpp.

References _mutex, and _waitingPlayers.

◆ getStatistics()

std::string server::MatchmakingService::getStatistics ( ) const

Get statistics about matchmaking.

Returns
String with statistics

Definition at line 218 of file MatchmakingService.cpp.

References _maxPlayers, _minPlayers, _mutex, _totalMatchesCreated, _totalPlayersMatched, and _waitingPlayers.

◆ getWaitingPlayers()

std::vector< PlayerQueueInfo > server::MatchmakingService::getWaitingPlayers ( ) const

Get list of waiting players.

Returns
Vector of player queue info

Definition at line 197 of file MatchmakingService.cpp.

References _mutex, and _waitingPlayers.

◆ removePlayer()

void server::MatchmakingService::removePlayer ( uint32_t  playerId)
overridevirtual

Remove a player from the matchmaking queue.

Parameters
playerIdPlayer ID to remove

Implements server::IMatchmakingService.

Definition at line 49 of file MatchmakingService.cpp.

References _mutex, _waitingPlayers, LOG_INFO, and LOG_WARNING.

◆ setMatchCreatedCallback()

void server::MatchmakingService::setMatchCreatedCallback ( MatchCreatedCallback  callback)
overridevirtual

Set callback for when a match is created.

Parameters
callbackFunction to call when match is ready

Implements server::IMatchmakingService.

Definition at line 147 of file MatchmakingService.cpp.

References _matchCreatedCallback.

◆ setMaxPlayers()

void server::MatchmakingService::setMaxPlayers ( size_t  max)

Set maximum players per match.

Parameters
maxMaximum player count (must be >= minPlayers)

Definition at line 210 of file MatchmakingService.cpp.

References _maxPlayers, _minPlayers, _mutex, and LOG_INFO.

◆ setMinPlayers()

void server::MatchmakingService::setMinPlayers ( size_t  min)

Set minimum players required to start a match.

Parameters
minMinimum player count (must be >= 1 and <= maxPlayers)

Definition at line 202 of file MatchmakingService.cpp.

References _maxPlayers, _minPlayers, _mutex, and LOG_INFO.

◆ tick()

void server::MatchmakingService::tick ( )
overridevirtual

Process matchmaking queue and create matches Called periodically by the server.

Implements server::IMatchmakingService.

Definition at line 64 of file MatchmakingService.cpp.

References _minPlayers, _mutex, _tryCreateMatch(), and _waitingPlayers.

Here is the call graph for this function:

Member Data Documentation

◆ _eventBus

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

Definition at line 113 of file MatchmakingService.hpp.

Referenced by _tryCreateMatch().

◆ _matchCreatedCallback

MatchCreatedCallback server::MatchmakingService::_matchCreatedCallback
private

Definition at line 118 of file MatchmakingService.hpp.

Referenced by _tryCreateMatch(), and setMatchCreatedCallback().

◆ _maxPlayers

size_t server::MatchmakingService::_maxPlayers
private

◆ _minPlayers

size_t server::MatchmakingService::_minPlayers
private

◆ _mutex

std::mutex server::MatchmakingService::_mutex
mutableprivate

◆ _totalMatchesCreated

uint32_t server::MatchmakingService::_totalMatchesCreated {0}
private

Definition at line 121 of file MatchmakingService.hpp.

Referenced by _generateRoomId(), _tryCreateMatch(), and getStatistics().

◆ _totalPlayersMatched

uint32_t server::MatchmakingService::_totalPlayersMatched {0}
private

Definition at line 122 of file MatchmakingService.hpp.

Referenced by _tryCreateMatch(), and getStatistics().

◆ _waitingPlayers

std::vector<PlayerQueueInfo> server::MatchmakingService::_waitingPlayers
private

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