17 std::shared_ptr<EventBus> eventBus)
18 : _minPlayers(minPlayers), _maxPlayers(maxPlayers), _eventBus(eventBus) {
29 std::lock_guard<std::mutex> lock(
_mutex);
33 [playerId](
const PlayerQueueInfo &info) { return info.playerId == playerId; });
36 LOG_WARNING(
"Player ", playerId,
" is already in matchmaking queue");
42 info.
joinTime = std::chrono::steady_clock::now();
50 std::lock_guard<std::mutex> lock(
_mutex);
53 [playerId](
const PlayerQueueInfo &info) { return info.playerId == playerId; });
58 " players remaining)");
60 LOG_WARNING(
"Player ", playerId,
" not found in matchmaking queue");
65 std::lock_guard<std::mutex> lock(
_mutex);
84 std::vector<uint32_t> matchedPlayers;
85 matchedPlayers.reserve(matchSize);
87 for (
size_t i = 0; i < matchSize; ++i) {
96 std::shared_ptr<Room> room;
101 }
catch (
const std::exception &e) {
102 LOG_ERROR(
"Failed to create match room: ", e.what());
104 for (uint32_t playerId : matchedPlayers) {
105 _waitingPlayers.push_back({playerId, std::chrono::steady_clock::now()});
111 for (uint32_t playerId : matchedPlayers) {
112 room->join(playerId);
119 LOG_INFO(
"✓ Match created: Room '", roomId,
"' with ", matchedPlayers.size(),
" players");
122 std::ostringstream playerList;
123 for (
size_t i = 0; i < matchedPlayers.size(); ++i) {
126 playerList << matchedPlayers[i];
128 LOG_INFO(
" Players: ", playerList.str());
143 std::lock_guard<std::mutex> lock(
_mutex);
152 uint32_t playerId,
const std::vector<std::shared_ptr<Room>> &availableRooms,
bool allowSpectator) {
154 std::lock_guard<std::mutex> lock(
_mutex);
156 LOG_INFO(
"[MatchmakingService] Finding match for player ", playerId);
159 for (
const auto &room : availableRooms) {
161 LOG_INFO(
"[MatchmakingService] Found waiting room '", room->getId(),
"' for player ",
163 return {room,
false};
168 if (allowSpectator) {
169 for (
const auto &room : availableRooms) {
171 LOG_INFO(
"[MatchmakingService] No waiting rooms, player ", playerId,
" will spectate '",
179 LOG_INFO(
"[MatchmakingService] No immediate match, adding player ", playerId,
" to queue");
183 [playerId](
const PlayerQueueInfo &info) { return info.playerId == playerId; });
188 info.
joinTime = std::chrono::steady_clock::now();
191 " players waiting)");
194 return {
nullptr,
false};
198 std::lock_guard<std::mutex> lock(
_mutex);
203 std::lock_guard<std::mutex> lock(
_mutex);
206 LOG_INFO(
"Matchmaking min players set to ", min);
211 std::lock_guard<std::mutex> lock(
_mutex);
214 LOG_INFO(
"Matchmaking max players set to ", max);
219 std::lock_guard<std::mutex> lock(
_mutex);
220 std::ostringstream oss;
221 oss <<
"Matchmaking Statistics:\n";
std::vector< PlayerQueueInfo > _waitingPlayers
std::string getStatistics() const
Get statistics about matchmaking.
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.
void removePlayer(uint32_t playerId) override
Remove a player from the matchmaking queue.
MatchCreatedCallback _matchCreatedCallback
uint32_t _totalPlayersMatched
void tick() override
Process matchmaking queue and create matches Called periodically by the server.
std::shared_ptr< server::EventBus > _eventBus
std::string _generateRoomId()
Generate unique room ID for a new match.
size_t getQueueSize() const override
Get the number of players waiting in queue.
uint32_t _totalMatchesCreated
bool _tryCreateMatch()
Try to create a match from waiting players.
MatchmakingService(size_t minPlayers=2, size_t maxPlayers=4, std::shared_ptr< server::EventBus > eventBus=nullptr)
Construct matchmaking service.
void setMatchCreatedCallback(MatchCreatedCallback callback) override
Set callback for when a match is created.
std::vector< PlayerQueueInfo > getWaitingPlayers() const
Get list of waiting players.
void addPlayer(uint32_t playerId) override
Add a player to the matchmaking queue.
void setMaxPlayers(size_t max)
Set maximum players per match.
void setMinPlayers(size_t min)
Set minimum players required to start a match.
std::function< void(std::shared_ptr< Room >)> MatchCreatedCallback
Callback invoked when a match is created.
Information about a player in matchmaking queue.
std::chrono::steady_clock::time_point joinTime