25 default:
return "Unknown";
34 default:
return "Unknown";
46 uint32_t maxPlayers,
float difficulty,
float speed,
RoomType type,
47 uint32_t creatorSessionId, uint32_t levelId, uint32_t seed,
48 uint32_t durationMinutes)
53 , _maxPlayers(maxPlayers)
54 , _state(
State::Waiting)
56 , _creatorSessionId(creatorSessionId)
59 , _difficulty(difficulty)
61 , durationMinutes(durationMinutes)
63 log::info(
"Room '{}' (ID: {}) created with max players: {} by session {}",
74 std::lock_guard lock(
_mutex);
80 const uint32_t sessionId = player->getId();
84 std::lock_guard lock(
_mutex);
87 log::warning(
"Player {} cannot join Room '{}' (ID: {}): banned",
90 log::warning(
"Player {} cannot join Room '{}' (ID: {}): Room is full",
93 log::warning(
"Player {} cannot join Room '{}' (ID: {}): Game already started",
97 [sessionId](
const auto &entry) { return entry.first->getId() == sessionId; });
103 _players.emplace_back(player, type);
104 log::info(
"Player {} joined Room '{}' (ID: {})",
106 username = player->getUsername();
114 const uint8_t success = added ? 1 : 0;
132 bool removed =
false;
133 std::string username;
136 std::lock_guard lock(
_mutex);
139 for (
const auto &entry :
_players) {
140 if (entry.first->getId() == sessionId) {
141 username = entry.first->getUsername();
146 _players.remove_if([sessionId](
const auto &entry) {
147 return entry.first->getId() == sessionId;
152 log::info(
"Player with Session ID {} removed from Room '{}' (ID: {})",
155 log::warning(
"Player with Session ID {} not found in Room '{}' (ID: {})",
172 std::vector<uint32_t> sessions;
174 std::lock_guard lock(
_mutex);
176 for (
const auto &entry :
_players) {
177 sessions.push_back(entry.first->getId());
183 payload.username[0] =
'\0';
184 std::strncpy(payload.message, message.c_str(),
sizeof(payload.message) - 1);
185 payload.message[
sizeof(payload.message) - 1] =
'\0';
190 for (uint32_t sid : sessions) {
197 std::lock_guard lock(
_mutex);
203 std::lock_guard lock(
_mutex);
209 std::lock_guard lock(
_mutex);
211 std::list<PlayerPtr> out;
213 for (
const auto &entry :
_players) {
214 out.push_back(entry.first);
256 std::lock_guard lock(
_mutex);
258 for (
const auto &entry :
_players) {
259 if (entry.first->getId() == sessionId) {
264 log::warning(
"Player with Session ID {} not found in Room '{}' (ID: {})",
271 std::lock_guard lock(
_mutex);
274 if (entry.first->getId() == sessionId) {
277 log::info(
"Player with Session ID {} set to type {} in Room '{}' (ID: {})",
284 log::warning(
"Player with Session ID {} not found in Room '{}' (ID: {})",
290 std::lock_guard lock(
_mutex);
291 for (
const auto &entry :
_players) {
311 std::lock_guard lock(
_mutex);
312 return static_cast<uint32_t
>(
_players.size());
317 std::lock_guard lock(
_mutex);
323 std::lock_guard lock(
_mutex);
327 log::warning(
"Room '{}' (ID: {}) cannot start game: Invalid state",
339 std::lock_guard lock(
_mutex);
341 log::warning(
"Room '{}' (ID: {}) cannot finish game: Invalid state",
355 const int ticks =
static_cast<int>(
_scoreTick);
358 std::lock_guard lock(
_mutex);
363 auto &player = entry.first;
367 player->addScore(ticks);
381 std::vector<uint32_t> sessions;
383 std::lock_guard lock(
_mutex);
390 for (
const auto& entry :
_players) {
391 sessions.push_back(entry.first->getId());
395 std::vector<net::EntitySnapshotPayload> snapshots;
402 if (!transformsRes || !networkIdsRes || !roomIdsRes)
405 auto& transforms = transformsRes->get();
406 auto& networkIds = networkIdsRes->get();
407 auto& roomIds = roomIdsRes->get();
409 for (
auto entity : transforms.entities()) {
410 if (!networkIds.has(entity) || !roomIds.has(entity))
412 if (roomIds[entity].
id !=
_id)
415 Vec2f velocity{0.0f, 0.0f};
417 auto& velocities = velocitiesRes->get();
418 if (velocities.has(entity)) {
419 const auto& vel = velocities[entity];
420 velocity = vel.direction * vel.speed;
424 snapshots.push_back({
425 networkIds[entity].id,
426 transforms[entity].position,
428 transforms[entity].rotation
434 if (snapshots.empty())
440 static_cast<uint16_t
>(snapshots.size())
442 packet << header << snapshots;
444 for (uint32_t sid : sessions) {
Logger declaration with support for multiple log levels.
auto get(this const Self &self) -> std::expected< std::reference_wrapper< ConstLike< Self, SparseArray< T > > >, rtp::Error >
Network packet with header and serializable body.
System to handle network-related operations on the server side.
void sendPacketToSession(uint32_t sessionId, const net::Packet &packet, net::NetworkMode mode)
Send a packet to a specific session.
float getDifficulty(void) const
Get the difficulty level of the room.
PlayerType
Enum representing the type of player in the room.
@ Spectator
Spectator player.
@ None
Undefined player type.
void banUser(const std::string &username)
std::list< std::pair< PlayerPtr, PlayerType > > _players
List of player ptr's in the room.
std::mutex _mutex
Mutex to protect access to room state.
bool hasActivePlayers() const
Check if there are any active players (non-spectators)
uint32_t _id
Unique room identifier.
uint32_t getLevelId(void) const
Get the level identifier of the room.
PlayerType getPlayerType(uint32_t sessionId) const
Get the type of a player in the room.
float _startedDt
Delta time accumulator since game start.
uint32_t getCurrentPlayerCount(void) const
Get the current number of players in the room.
void removePlayer(uint32_t sessionId, bool disconnected=false)
Remove a player from the room.
uint32_t durationMinutes
Duration of the game in minutes.
uint32_t _maxPlayers
Maximum number of players allowed.
RoomType _type
Type of the room.
RoomType getType(void) const
Get the type of the room.
const std::list< PlayerPtr > getPlayers(void) const
Get the list of players in the room.
uint32_t getDurationMinutes(void) const
Get the duration in minutes of the room.
void broadcastRoomState(uint32_t serverTick)
Broadcast the current room state to all connected players.
float getSpeed(void) const
Get the speed multiplier of the room.
std::string _name
Name of the room.
uint32_t getId(void) const
Get the unique identifier of the room.
bool addPlayer(const PlayerPtr &player, PlayerType type=PlayerType::Player)
Add a player to the room.
State
Enum representing the state of the room.
@ InGame
Game in progress.
@ Waiting
Waiting for players.
~Room()
Destructor for Room.
Room(ecs::Registry ®istry, NetworkSyncSystem &network, uint32_t id, const std::string &name, uint32_t maxPlayers, float difficulty, float speed, RoomType type, uint32_t creatorSessionId=0, uint32_t levelId=1, uint32_t seed=0, uint32_t durationMinutes=0)
Constructor for Room.
void broadcastSystemMessage(const std::string &message)
float _scoreTick
Score tick accumulator.
NetworkSyncSystem _network
Reference to the server network manager.
bool startGame(float dt)
Start the game in the room.
bool canJoin(void) const
Add a player to the room.
uint32_t _seed
Seed for random generation in the room.
uint32_t _levelId
Level identifier for the room.
void setPlayerType(uint32_t sessionId, PlayerType type)
Set the type of a player in the room.
State _state
Current state of the room.
float _speed
Speed multiplier for the room 0 -> 2.
std::string getName(void) const
Get the name of the room.
State getState(void) const
Get the current state of the room.
std::unordered_set< std::string > _bannedUsers
Banned usernames.
uint32_t getSeed(void) const
Get the random seed of the room.
void update(uint32_t serverTick, float dt)
Update the room state.
float _difficulty
Difficulty multiplier for the room 0 -> 1.
@ Private
Private room type, hosted by the player.
@ Public
Public room type, can be joined by anyone.
@ Lobby
Lobby room type, Only used for the principal Lobby.
void forceFinishGame(void)
Finish the game in the room.
bool isBanned(const std::string &username) const
ecs::Registry & _registry
Reference to the entity registry.
uint32_t getMaxPlayers(void) const
Get the maximum number of players allowed in the room.
File : RenderSystem.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
void warning(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log a warning message.
void info(LogFmt< std::type_identity_t< Args >... > fmt, Args &&...args) noexcept
Log an informational message.
@ RoomChatReceived
Chat message received in room.
@ JoinRoom
Request to join a room.
@ RoomUpdate
Notification of room update.
@ ScoreUpdate
Player score update.
File : GameManager.hpp License: MIT Author : Elias Josué HAJJAR LLAUQUEN elias-josue....
std::shared_ptr< Player > PlayerPtr
Shared pointer type for Player.
constexpr std::string_view toString(ErrorCode e) noexcept
Convert an ErrorCode to its string representation.
Component representing a network identifier for an entity.
Component representing a network identifier for an entity.
Component representing a 2D velocity.
Data for receiving chat messages in a room Server OpCode.
uint32_t sessionId
Sender's session identifier.
Player score update notification data.