R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
Room.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** Room.hpp
6*/
7
8#pragma once
9
10#include <cstdint>
11#include <memory>
12#include <mutex>
13#include <string>
14#include <vector>
19
20namespace server {
21
26 class Room : public IRoom {
27 public:
37 explicit Room(const std::string &id, const std::string &name = "", size_t maxPlayers = 4,
38 bool isPrivate = false, float gameSpeedMultiplier = 1.0f,
39 std::shared_ptr<EventBus> eventBus = nullptr);
40 ~Room() override = default;
41
42 bool join(uint32_t playerId) override;
43 bool joinAsSpectator(uint32_t playerId) override;
44 bool leave(uint32_t playerId) override;
45 std::string getId() const override { return _id; };
46 std::string getName() const override { return _name; };
47 RoomState getState() const override { return _state; };
48 void setState(RoomState state) override;
49 size_t getPlayerCount() const override;
50 size_t getMaxPlayers() const override { return _maxPlayers; };
51 bool isFull() const override { return _players.size() >= _maxPlayers; };
52 std::vector<uint32_t> getPlayers() const override;
53 std::vector<uint32_t> getSpectators() const override;
54 bool hasPlayer(uint32_t playerId) const override;
55 bool hasSpectator(uint32_t playerId) const override;
56 RoomInfo getInfo() const override;
57
62 std::shared_ptr<IGameLogic> getGameLogic() const { return _gameLogic; };
63
68 ServerLoop *getServerLoop() const { return _gameLoop.get(); };
69
74 bool startGame();
75
82 void requestStartGame();
83
88 void update(float deltaTime);
89
94 void setHost(uint32_t playerId);
95
100 uint32_t getHost() const { return _hostPlayerId; };
101
108 void broadcastChatMessage(uint32_t senderId, const std::string &senderName,
109 const std::string &message);
110
115 bool isPrivate() const { return _isPrivate; }
116
122
127 bool isGameStartSent() const { return _gameStartSent; }
128
132 void setGameStartSent(bool sent) { _gameStartSent = sent; }
133
139
140 private:
141 std::string _id;
142 std::string _name;
148 std::vector<uint32_t> _players;
149 std::vector<uint32_t> _spectators;
150 std::shared_ptr<IGameLogic> _gameLogic;
151 std::unique_ptr<ServerLoop> _gameLoop; // Dedicated game loop for this room
152 std::shared_ptr<EventBus> _eventBus; // Event bus for this room
153 mutable std::mutex _mutex; // Thread safety for player management
154 bool _gameStartSent; // Whether GameStart has been sent to players
155 };
156
157} // namespace server
Interface for a game room.
Definition IRoom.hpp:47
Concrete implementation of IRoom with game instance management.
Definition Room.hpp:26
RoomInfo getInfo() const override
Get room information.
Definition Room.cpp:161
bool leave(uint32_t playerId) override
Remove a player from the room.
Definition Room.cpp:93
void broadcastChatMessage(uint32_t senderId, const std::string &senderName, const std::string &message)
Broadcast a chat message to all players in the room.
Definition Room.cpp:265
uint32_t _hostPlayerId
Definition Room.hpp:147
bool isFull() const override
Check if room is full.
Definition Room.hpp:51
bool tryMarkGameStartSent()
Atomically check and set GameStart sent flag (thread-safe)
Definition Room.cpp:256
std::shared_ptr< EventBus > _eventBus
Definition Room.hpp:152
std::vector< uint32_t > _spectators
Definition Room.hpp:149
bool join(uint32_t playerId) override
Join a player to the room.
Definition Room.cpp:53
std::string _id
Definition Room.hpp:141
bool isPrivate() const
Check if room is private.
Definition Room.hpp:115
void setState(RoomState state) override
Set the state of the room.
Definition Room.cpp:129
bool isGameStartSent() const
Check if GameStart has been sent.
Definition Room.hpp:127
std::shared_ptr< IGameLogic > getGameLogic() const
Get the game logic instance.
Definition Room.hpp:62
void requestStartGame()
Request to start the game (initiates countdown)
Definition Room.cpp:249
bool _gameStartSent
Definition Room.hpp:154
float getGameSpeedMultiplier() const
Get the game speed multiplier.
Definition Room.hpp:121
size_t getMaxPlayers() const override
Get maximum number of players allowed.
Definition Room.hpp:50
bool hasSpectator(uint32_t playerId) const override
Check if a spectator is in this room.
Definition Room.cpp:157
uint32_t getHost() const
Get the host player ID.
Definition Room.hpp:100
bool _isPrivate
Definition Room.hpp:145
void setGameStartSent(bool sent)
Mark GameStart as sent.
Definition Room.hpp:132
std::vector< uint32_t > _players
Definition Room.hpp:148
void setHost(uint32_t playerId)
Set the host player ID.
Definition Room.cpp:242
std::string _name
Definition Room.hpp:142
std::string getName() const override
Get the room's display name.
Definition Room.hpp:46
std::vector< uint32_t > getPlayers() const override
Get list of player IDs in this room.
Definition Room.cpp:143
std::unique_ptr< ServerLoop > _gameLoop
Definition Room.hpp:151
std::string getId() const override
Get the room's unique identifier.
Definition Room.hpp:45
size_t getPlayerCount() const override
Get number of players currently in room.
Definition Room.cpp:138
RoomState getState() const override
Get current state of the room.
Definition Room.hpp:47
ServerLoop * getServerLoop() const
Get the server loop instance for this room.
Definition Room.hpp:68
size_t _maxPlayers
Definition Room.hpp:144
RoomState _state
Definition Room.hpp:143
float _gameSpeedMultiplier
Definition Room.hpp:146
std::shared_ptr< IGameLogic > _gameLogic
Definition Room.hpp:150
bool hasPlayer(uint32_t playerId) const override
Check if a player is in this room.
Definition Room.cpp:153
std::mutex _mutex
Definition Room.hpp:153
std::vector< uint32_t > getSpectators() const override
Get list of spectator IDs in this room.
Definition Room.cpp:148
void update(float deltaTime)
Update the room state (called by server loop)
Definition Room.cpp:230
~Room() override=default
bool startGame()
Start the game for this room.
Definition Room.cpp:167
bool joinAsSpectator(uint32_t playerId) override
Join a player to the room as a spectator.
Definition Room.cpp:80
Deterministic fixed-timestep game loop.
RoomState
State of a game room.
Definition IRoom.hpp:20
Information about a room.
Definition IRoom.hpp:31