R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Lobby.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** rtype
4** File description:
5** Lobby.hpp - Main lobby for player connections
6*/
7
8#pragma once
9
10#include <atomic>
11#include <chrono>
12#include <cstdint>
13#include <memory>
14#include <mutex>
15#include <unordered_map>
16#include <vector>
17
18namespace server {
19
20 class RoomManager;
21
26 struct LobbyPlayer {
27 uint32_t playerId;
28 std::string playerName;
29 bool isReady;
31 };
32
43 class Lobby {
44 public:
49 explicit Lobby(std::shared_ptr<RoomManager> roomManager);
50 ~Lobby() = default;
51
58 bool addPlayer(uint32_t playerId, const std::string &playerName);
59
65 bool removePlayer(uint32_t playerId);
66
73 bool updatePlayerName(uint32_t playerId, const std::string &newName);
74
80 const LobbyPlayer *getPlayer(uint32_t playerId) const;
81
86 std::vector<LobbyPlayer> getAllPlayers() const;
87
92 size_t getPlayerCount() const;
93
99 bool startMatchmaking(uint32_t playerId);
100
106 bool cancelMatchmaking(uint32_t playerId);
107
114 bool joinRoom(uint32_t playerId, const std::string &roomId);
115
124 std::string createCustomRoom(uint32_t hostPlayerId, const std::string &roomName, size_t maxPlayers,
125 bool isPrivate);
126
132 void setPlayerReady(uint32_t playerId, bool ready);
133
138 std::shared_ptr<RoomManager> getRoomManager() const { return _roomManager; }
139
140 private:
141 std::unordered_map<uint32_t, LobbyPlayer> _players;
142 std::shared_ptr<RoomManager> _roomManager;
143 mutable std::mutex _mutex;
144
145 // Static counter for unique room ID generation
146 static std::atomic<uint64_t> _nextRoomId;
147 };
148
149} // namespace server
Main lobby where players connect before joining rooms.
Definition Lobby.hpp:43
bool updatePlayerName(uint32_t playerId, const std::string &newName)
Update a player's display name.
Definition Lobby.cpp:57
bool startMatchmaking(uint32_t playerId)
Start matchmaking for a player.
Definition Lobby.cpp:101
std::mutex _mutex
Definition Lobby.hpp:143
bool joinRoom(uint32_t playerId, const std::string &roomId)
Join a specific room by ID.
Definition Lobby.cpp:148
std::shared_ptr< RoomManager > _roomManager
Definition Lobby.hpp:142
std::vector< LobbyPlayer > getAllPlayers() const
Get all players in lobby.
Definition Lobby.cpp:83
bool addPlayer(uint32_t playerId, const std::string &playerName)
Add a player to the lobby.
Definition Lobby.cpp:21
~Lobby()=default
std::string createCustomRoom(uint32_t hostPlayerId, const std::string &roomName, size_t maxPlayers, bool isPrivate)
Create a custom room.
Definition Lobby.cpp:176
std::shared_ptr< RoomManager > getRoomManager() const
Get room manager.
Definition Lobby.hpp:138
void setPlayerReady(uint32_t playerId, bool ready)
Set player ready status (for custom rooms)
Definition Lobby.cpp:211
size_t getPlayerCount() const
Get number of players in lobby.
Definition Lobby.cpp:96
std::unordered_map< uint32_t, LobbyPlayer > _players
Definition Lobby.hpp:141
bool cancelMatchmaking(uint32_t playerId)
Cancel matchmaking for a player.
Definition Lobby.cpp:127
static std::atomic< uint64_t > _nextRoomId
Definition Lobby.hpp:146
const LobbyPlayer * getPlayer(uint32_t playerId) const
Get player info.
Definition Lobby.cpp:72
bool removePlayer(uint32_t playerId)
Remove a player from the lobby.
Definition Lobby.cpp:40
Information about a player in the lobby.
Definition Lobby.hpp:26
uint32_t playerId
Definition Lobby.hpp:27
std::string playerName
Definition Lobby.hpp:28