R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
RoomManager.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** RoomManager.hpp
6*/
7
8#pragma once
9
10#include <memory>
11#include <mutex>
12#include <unordered_map>
15
16namespace server {
17
18 // Callback type for when a match is created and players should be notified
19 using RoomCreatedCallback = std::function<void(std::shared_ptr<Room>)>;
20
31 class RoomManager : public IRoomManager {
32 public:
34 explicit RoomManager(std::shared_ptr<MatchmakingService> matchmaking,
35 std::shared_ptr<EventBus> eventBus = nullptr);
36 ~RoomManager() override = default;
37
38 std::shared_ptr<Room> createRoom(const std::string &id, const std::string &name = "",
39 size_t maxPlayers = 4, bool isPrivate = false,
40 float gameSpeedMultiplier = 1.0f) override;
41 std::shared_ptr<Room> getRoom(const std::string &id) override;
42 bool removeRoom(const std::string &id) override;
43 std::vector<std::shared_ptr<Room>> getAllRooms() override;
44 std::vector<std::shared_ptr<Room>> getPublicRooms() override;
45 size_t getRoomCount() const override;
46 bool update(float deltaTime) override;
47
52 void addPlayerToMatchmaking(uint32_t playerId);
53
58 void removePlayerFromMatchmaking(uint32_t playerId);
59
64 std::shared_ptr<MatchmakingService> getMatchmaking() { return _matchmaking; }
65
71
77 std::shared_ptr<Room> getRoomByPlayer(uint32_t playerId);
78
84
85 private:
90 void _onMatchCreated(std::shared_ptr<Room> room);
91
92 std::unordered_map<std::string, std::shared_ptr<Room>> _rooms;
93 std::shared_ptr<MatchmakingService> _matchmaking;
94 std::shared_ptr<EventBus> _eventBus;
96 mutable std::recursive_mutex
97 _mutex; // Recursive to allow getAllRooms() from event handlers during update()
98 };
99
100} // namespace server
Interface for managing game rooms.
Manages all game rooms and matchmaking.
std::shared_ptr< Room > createRoom(const std::string &id, const std::string &name="", size_t maxPlayers=4, bool isPrivate=false, float gameSpeedMultiplier=1.0f) override
Create a new room.
bool update(float deltaTime) override
Update all rooms (called by server loop)
std::shared_ptr< Room > getRoom(const std::string &id) override
Retrieve a room by ID.
RoomCreatedCallback _roomCreatedCallback
void setRoomCreatedCallback(RoomCreatedCallback callback)
Set callback for when a room is created by matchmaking.
std::vector< std::shared_ptr< Room > > getAllRooms() override
Get all active rooms.
void removePlayerFromMatchmaking(uint32_t playerId)
Remove player from matchmaking queue.
void cleanupFinishedRooms()
Clean up finished rooms Removes rooms that are in FINISHED state and have no players.
std::shared_ptr< MatchmakingService > _matchmaking
bool removeRoom(const std::string &id) override
Remove a room by ID.
std::recursive_mutex _mutex
std::shared_ptr< MatchmakingService > getMatchmaking()
Get the matchmaking service.
size_t getRoomCount() const override
Get number of active rooms.
std::shared_ptr< EventBus > _eventBus
std::shared_ptr< Room > getRoomByPlayer(uint32_t playerId)
Get room by player ID (find which room a player is in)
~RoomManager() override=default
std::vector< std::shared_ptr< Room > > getPublicRooms() override
Get all public rooms (not private)
void addPlayerToMatchmaking(uint32_t playerId)
Add player to matchmaking queue.
void _onMatchCreated(std::shared_ptr< Room > room)
Handle match created by matchmaking service.
std::unordered_map< std::string, std::shared_ptr< Room > > _rooms
std::function< void(std::shared_ptr< Room >)> RoomCreatedCallback