Air-Trap 1.0.0
A multiplayer R-Type clone game engine built with C++23 and ECS architecture
Loading...
Searching...
No Matches
Room.hpp
Go to the documentation of this file.
1
8#ifndef RTYPE_GAME_ROOM_HPP_
9 #define RTYPE_GAME_ROOM_HPP_
10
11 #include <cstdint>
12 #include <string>
13 #include <vector>
14 #include <mutex>
15 #include "Game/Player.hpp"
16 #include <memory>
17 #include <list>
18 #include <algorithm>
19 #include <unordered_set>
21 #include "Systems/NetworkSyncSystem.hpp"
22 #include "RType/ECS/Registry.hpp"
23
28namespace rtp::server
29{
35 class Room {
36 public:
46
56
66
67 public:
74 Room(ecs::Registry& registry, NetworkSyncSystem& network, uint32_t id, const std::string &name,
75 uint32_t maxPlayers, float difficulty, float speed, RoomType type,
76 uint32_t creatorSessionId = 0, uint32_t levelId = 1, uint32_t seed = 0,
77 uint32_t durationMinutes = 0);
78
82 ~Room();
83
89 bool canJoin(void) const;
90
96 bool addPlayer(const PlayerPtr &player, PlayerType type = PlayerType::Player);
97
102 void removePlayer(uint32_t sessionId, bool disconnected = false);
103
108 const std::list<PlayerPtr> getPlayers(void) const;
109
114 uint32_t getId(void) const;
115
120 RoomType getType(void) const;
121
126 float getDifficulty(void) const;
127
132 float getSpeed(void) const;
133
138 uint32_t getLevelId(void) const;
139
144 uint32_t getSeed(void) const;
145
150 uint32_t getDurationMinutes(void) const;
151
157 PlayerType getPlayerType(uint32_t sessionId) const;
158
164 void setPlayerType(uint32_t sessionId, PlayerType type);
165
170 bool hasActivePlayers() const;
171
176 std::string getName(void) const;
177
182 uint32_t getMaxPlayers(void) const;
183
188 uint32_t getCurrentPlayerCount(void) const;
189
194 State getState(void) const;
195
200 bool startGame(float dt);
201
206 void forceFinishGame(void);
207
212 void update(uint32_t serverTick, float dt);
213
218 void broadcastRoomState(uint32_t serverTick);
219
220 void banUser(const std::string &username);
221 bool isBanned(const std::string &username) const;
222
223 private:
224 void broadcastSystemMessage(const std::string &message);
228 uint32_t _id;
229 std::string _name;
230 uint32_t _maxPlayers;
231 std::list<std::pair<PlayerPtr, PlayerType>>
237 std::pair<std::string, int>
240 uint32_t _levelId = 0;
241 uint32_t _seed = 0;
242 float _difficulty = 0;
243 float _speed = 0;
244 uint32_t durationMinutes = 0;
246 float _startedDt = 0.0f;
249 mutable std::mutex _mutex;
250 std::unordered_set<std::string> _bannedUsers;
251 float _scoreTick{0.0f};
252 };
253} // namespace rtp::server
254
255#endif /* !RTYPE_GAME_ROOM_HPP_ */
Network packet implementation for R-Type protocol.
System to handle network-related operations on the server side.
Represents a game room in the server.
Definition Room.hpp:35
std::pair< std::string, int > _bestRoomScore
Best score achieved in the room.
Definition Room.hpp:238
float getDifficulty(void) const
Get the difficulty level of the room.
Definition Room.cpp:229
PlayerType
Enum representing the type of player in the room.
Definition Room.hpp:61
@ Spectator
Spectator player.
Definition Room.hpp:64
@ Player
Regular player.
Definition Room.hpp:63
@ None
Undefined player type.
Definition Room.hpp:62
void banUser(const std::string &username)
Definition Room.cpp:195
std::list< std::pair< PlayerPtr, PlayerType > > _players
List of player ptr's in the room.
Definition Room.hpp:232
std::mutex _mutex
Mutex to protect access to room state.
Definition Room.hpp:249
bool hasActivePlayers() const
Check if there are any active players (non-spectators)
Definition Room.cpp:288
uint32_t _id
Unique room identifier.
Definition Room.hpp:228
uint32_t getLevelId(void) const
Get the level identifier of the room.
Definition Room.cpp:239
PlayerType getPlayerType(uint32_t sessionId) const
Get the type of a player in the room.
Definition Room.cpp:254
float _startedDt
Delta time accumulator since game start.
Definition Room.hpp:246
uint32_t getCurrentPlayerCount(void) const
Get the current number of players in the room.
Definition Room.cpp:309
void removePlayer(uint32_t sessionId, bool disconnected=false)
Remove a player from the room.
Definition Room.cpp:130
uint32_t durationMinutes
Duration of the game in minutes.
Definition Room.hpp:244
uint32_t _maxPlayers
Maximum number of players allowed.
Definition Room.hpp:230
RoomType _type
Type of the room.
Definition Room.hpp:234
RoomType getType(void) const
Get the type of the room.
Definition Room.cpp:224
const std::list< PlayerPtr > getPlayers(void) const
Get the list of players in the room.
Definition Room.cpp:207
uint32_t getDurationMinutes(void) const
Get the duration in minutes of the room.
Definition Room.cpp:249
void broadcastRoomState(uint32_t serverTick)
Broadcast the current room state to all connected players.
Definition Room.cpp:379
float getSpeed(void) const
Get the speed multiplier of the room.
Definition Room.cpp:234
std::string _name
Name of the room.
Definition Room.hpp:229
uint32_t getId(void) const
Get the unique identifier of the room.
Definition Room.cpp:219
bool addPlayer(const PlayerPtr &player, PlayerType type=PlayerType::Player)
Add a player to the room.
Definition Room.cpp:78
State
Enum representing the state of the room.
Definition Room.hpp:41
@ InGame
Game in progress.
Definition Room.hpp:43
@ Waiting
Waiting for players.
Definition Room.hpp:42
@ Finished
Game finished.
Definition Room.hpp:44
~Room()
Destructor for Room.
Definition Room.cpp:67
void broadcastSystemMessage(const std::string &message)
Definition Room.cpp:170
float _scoreTick
Score tick accumulator.
Definition Room.hpp:251
NetworkSyncSystem _network
Reference to the server network manager.
Definition Room.hpp:225
bool startGame(float dt)
Start the game in the room.
Definition Room.cpp:321
bool canJoin(void) const
Add a player to the room.
Definition Room.cpp:72
uint32_t _seed
Seed for random generation in the room.
Definition Room.hpp:241
uint32_t _levelId
Level identifier for the room.
Definition Room.hpp:240
void setPlayerType(uint32_t sessionId, PlayerType type)
Set the type of a player in the room.
Definition Room.cpp:269
State _state
Current state of the room.
Definition Room.hpp:233
float _speed
Speed multiplier for the room 0 -> 2.
Definition Room.hpp:243
std::string getName(void) const
Get the name of the room.
Definition Room.cpp:299
State getState(void) const
Get the current state of the room.
Definition Room.cpp:315
std::unordered_set< std::string > _bannedUsers
Banned usernames.
Definition Room.hpp:250
uint32_t getSeed(void) const
Get the random seed of the room.
Definition Room.cpp:244
void update(uint32_t serverTick, float dt)
Update the room state.
Definition Room.cpp:350
uint32_t _creatorSessionId
Session ID of the room creator (Administrator)
Definition Room.hpp:235
float _difficulty
Difficulty multiplier for the room 0 -> 1.
Definition Room.hpp:242
@ Private
Private room type, hosted by the player.
Definition Room.hpp:54
@ Public
Public room type, can be joined by anyone.
Definition Room.hpp:53
@ Lobby
Lobby room type, Only used for the principal Lobby.
Definition Room.hpp:52
void forceFinishGame(void)
Finish the game in the room.
Definition Room.cpp:337
bool isBanned(const std::string &username) const
Definition Room.cpp:201
ecs::Registry & _registry
Reference to the entity registry.
Definition Room.hpp:226
uint32_t getMaxPlayers(void) const
Get the maximum number of players allowed in the room.
Definition Room.cpp:304
uint32_t _currentTimeSeconds
Current time in seconds since the game started.
Definition Room.hpp:248
File : GameManager.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
std::shared_ptr< Player > PlayerPtr
Shared pointer type for Player.
Definition Player.hpp:178