R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
MatchmakingService.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created by hugo on 06/12/2025
4** File description:
5** MatchmakingService.hpp
6*/
7
8#pragma once
9
10#include <chrono>
11#include <cstdint>
12#include <memory>
13#include <mutex>
14#include <unordered_map>
15#include <vector>
17
18namespace server {
19 class EventBus; // Forward declaration
20
26 uint32_t playerId;
27 std::chrono::steady_clock::time_point joinTime;
28 };
29
54 public:
61 explicit MatchmakingService(size_t minPlayers = 2, size_t maxPlayers = 4,
62 std::shared_ptr<server::EventBus> eventBus = nullptr);
63 ~MatchmakingService() override = default;
64
65 void addPlayer(uint32_t playerId) override;
66 void removePlayer(uint32_t playerId) override;
67 void tick() override;
68 size_t getQueueSize() const override;
69 void setMatchCreatedCallback(MatchCreatedCallback callback) override;
70 std::pair<std::shared_ptr<Room>, bool> findOrCreateMatch(
71 uint32_t playerId, const std::vector<std::shared_ptr<Room>> &availableRooms,
72 bool allowSpectator = true) override;
73
78 std::vector<PlayerQueueInfo> getWaitingPlayers() const;
79
84 void setMinPlayers(size_t min);
85
90 void setMaxPlayers(size_t max);
91
96 std::string getStatistics() const;
97
98 private:
103 bool _tryCreateMatch();
104
109 std::string _generateRoomId();
110
113 std::shared_ptr<server::EventBus> _eventBus;
114
115 std::vector<PlayerQueueInfo> _waitingPlayers;
116 mutable std::mutex _mutex;
117
119
120 // Statistics
123 };
124
125} // namespace server
Type-safe event publication/subscription system.
Definition EventBus.hpp:44
Interface for a matchmaking system.
Automatic matchmaking service.
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
~MatchmakingService() override=default
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.
bool _tryCreateMatch()
Try to create a match from waiting players.
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