11#include <unordered_map>
123 for (
auto *peer : peers) {
136 const std::unordered_map<GameruleKey, float> &gamerules) {
139 for (
const auto &[key, value] : gamerules) {
143 for (
auto *peer : peers) {
156 const std::unordered_map<std::string, float> &gamerules) {
159 for (
const auto &[key, value] : gamerules) {
163 for (
auto *peer : peers) {
180 std::vector<uint8_t> payload = packet.
serialize();
181 std::vector<uint8_t> message =
185 std::unique_ptr<IPacket> netPacket =
187 peer->
send(std::move(netPacket), 0);
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.
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.
constexpr uint32_t getPlayerSpawnY() const
constexpr uint32_t getPlayerSpawnX() const
constexpr uint32_t getDefaultPlayerSpeed() const
constexpr uint32_t getDefaultPlayerDamage() const
constexpr uint32_t getDefaultPlayerHealth() const
constexpr float getDefaultPlayerFireRate() const
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)
~GameruleBroadcaster()=default
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)
GameruleBroadcaster()=default
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.