R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
GameruleBroadcaster.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created on 07/01/2026.
4** File description:
5** GameruleBroadcaster.hpp
6*/
7
8#pragma once
9
10#include <memory>
11#include <unordered_map>
12#include <vector>
15#include "GameruleKeys.hpp"
20
21namespace server {
22
42 public:
45
54 static void sendAllGamerules(IPeer *peer, const GameRules &rules) {
55 if (!peer)
56 return;
57
59
60 // Add all gamerules using type-safe enum
62 static_cast<float>(rules.getDefaultPlayerHealth()));
64 static_cast<float>(rules.getDefaultPlayerSpeed()));
66 static_cast<float>(rules.getPlayerSpawnX()));
68 static_cast<float>(rules.getPlayerSpawnY()));
72 static_cast<float>(rules.getDefaultPlayerDamage()));
73
74 sendGamerulePacket(peer, packet);
75 }
76
83 static void sendGamerule(IPeer *peer, GameruleKey key, float value) {
84 sendGamerule(peer, GameruleKeys::toString(key), value);
85 }
86
93 static void sendGamerule(IPeer *peer, const std::string &key, float value) {
94 if (!peer)
95 return;
96
98 packet.addGamerule(key, value);
99 sendGamerulePacket(peer, packet);
100 }
101
108 static void broadcastGamerule(const std::vector<IPeer *> &peers, GameruleKey key, float value) {
109 broadcastGamerule(peers, GameruleKeys::toString(key), value);
110 }
111
118 static void broadcastGamerule(const std::vector<IPeer *> &peers, const std::string &key,
119 float value) {
121 packet.addGamerule(key, value);
122
123 for (auto *peer : peers) {
124 if (peer) {
125 sendGamerulePacket(peer, packet);
126 }
127 }
128 }
129
135 static void broadcastGamerules(const std::vector<IPeer *> &peers,
136 const std::unordered_map<GameruleKey, float> &gamerules) {
138
139 for (const auto &[key, value] : gamerules) {
140 packet.addGamerule(GameruleKeys::toString(key), value);
141 }
142
143 for (auto *peer : peers) {
144 if (peer) {
145 sendGamerulePacket(peer, packet);
146 }
147 }
148 }
149
155 static void broadcastGamerules(const std::vector<IPeer *> &peers,
156 const std::unordered_map<std::string, float> &gamerules) {
158
159 for (const auto &[key, value] : gamerules) {
160 packet.addGamerule(key, value);
161 }
162
163 for (auto *peer : peers) {
164 if (peer) {
165 sendGamerulePacket(peer, packet);
166 }
167 }
168 }
169
176 if (!peer)
177 return;
178
179 // Serialize the packet
180 std::vector<uint8_t> payload = packet.serialize();
181 std::vector<uint8_t> message =
183
184 // Create network packet and send reliably
185 std::unique_ptr<IPacket> netPacket =
186 createPacket(message, static_cast<int>(PacketFlag::RELIABLE));
187 peer->send(std::move(netPacket), 0);
188 }
189 };
190
191} // namespace server
GameruleKey
Type-safe enum for gamerule identifiers.
@ RELIABLE
Packet must be received by the target peer and resent if dropped.
std::unique_ptr< IPacket > createPacket(const std::vector< uint8_t > &data, uint32_t flags)
Create a network packet with the given data and flags.
Interface representing a remote peer in the network.
Definition IPeer.hpp:40
virtual bool send(std::unique_ptr< IPacket > packet, uint8_t channelID=0)=0
Send a packet to this peer.
Packet containing gamerule updates from server to client.
void addGamerule(const std::string &key, float value)
Add a gamerule to the packet.
std::vector< uint8_t > serialize() const
Serialize the packet to bytes using Cap'n Proto.
Centralized game rules and configuration.
Definition GameRules.hpp:21
constexpr uint32_t getPlayerSpawnY() const
Definition GameRules.hpp:30
constexpr uint32_t getPlayerSpawnX() const
Definition GameRules.hpp:29
constexpr uint32_t getDefaultPlayerSpeed() const
Definition GameRules.hpp:28
constexpr uint32_t getDefaultPlayerDamage() const
Definition GameRules.hpp:32
constexpr uint32_t getDefaultPlayerHealth() const
Definition GameRules.hpp:27
constexpr float getDefaultPlayerFireRate() const
Definition GameRules.hpp:31
Utility class to send gamerule updates to clients.
static void sendGamerule(IPeer *peer, GameruleKey key, float value)
Send a single gamerule update to a single client (type-safe)
static void broadcastGamerule(const std::vector< IPeer * > &peers, GameruleKey key, float value)
Broadcast a single gamerule update to multiple clients (type-safe)
static void sendGamerule(IPeer *peer, const std::string &key, float value)
Send a single gamerule update to a single client (string version)
static void broadcastGamerule(const std::vector< IPeer * > &peers, const std::string &key, float value)
Broadcast a single gamerule update to multiple clients (string version)
static void sendGamerulePacket(IPeer *peer, const RType::Messages::S2C::GamerulePacket &packet)
Send a pre-built gamerule packet to a single client.
static void broadcastGamerules(const std::vector< IPeer * > &peers, const std::unordered_map< GameruleKey, float > &gamerules)
Broadcast multiple gamerule updates to multiple clients (type-safe)
static void broadcastGamerules(const std::vector< IPeer * > &peers, const std::unordered_map< std::string, float > &gamerules)
Broadcast multiple gamerule updates to multiple clients (string version)
static void sendAllGamerules(IPeer *peer, const GameRules &rules)
Send all gamerules to a single client.
const char * toString(GameruleKey key)
Convert GameruleKey enum to string.
std::vector< uint8_t > createMessage(MessageType type, const std::vector< uint8_t > &payload)
Create a message with type and payload.