R-Type
Distributed multiplayer game engine in C++
Loading...
Searching...
No Matches
ClientGameRules.hpp
Go to the documentation of this file.
1/*
2** EPITECH PROJECT, 2025
3** Created on 07/01/2026.
4** File description:
5** ClientGameRules.hpp
6*/
7
8#pragma once
9
10#include <mutex>
11#include <string>
12#include <unordered_map>
13#include "GameruleKeys.hpp"
14
15namespace client {
16
38 public:
44 static ClientGameRules instance;
45 return instance;
46 }
47
48 // Delete copy constructor and assignment operator
51
58 float get(GameruleKey key, float defaultValue = 0.0f) const {
59 return get(GameruleKeys::toString(key), defaultValue);
60 }
61
68 float get(const std::string &key, float defaultValue = 0.0f) const {
69 std::scoped_lock lock(mutex_);
70 auto it = rules_.find(key);
71 return (it != rules_.end()) ? it->second : defaultValue;
72 }
73
79 void update(GameruleKey key, float value) { update(GameruleKeys::toString(key), value); }
80
86 void update(const std::string &key, float value) {
87 std::scoped_lock lock(mutex_);
88 rules_[key] = value;
89 }
90
95 void updateMultiple(const std::unordered_map<std::string, float> &gamerules) {
96 std::scoped_lock lock(mutex_);
97 for (const auto &[key, value] : gamerules) {
98 rules_[key] = value;
99 }
100 }
101
107 bool has(GameruleKey key) const { return has(GameruleKeys::toString(key)); }
108
114 bool has(const std::string &key) const {
115 std::scoped_lock lock(mutex_);
116 return rules_.find(key) != rules_.end();
117 }
118
122 void clear() {
123 std::scoped_lock lock(mutex_);
124 rules_.clear();
125 }
126
131 size_t size() const {
132 std::scoped_lock lock(mutex_);
133 return rules_.size();
134 }
135
136 private:
137 ClientGameRules() = default;
138 ~ClientGameRules() = default;
139
140 mutable std::mutex mutex_;
141 std::unordered_map<std::string, float> rules_;
142 };
143
144} // namespace client
GameruleKey
Type-safe enum for gamerule identifiers.
Client-side storage for game rules synchronized from server.
float get(const std::string &key, float defaultValue=0.0f) const
Get a gamerule value (string version)
void update(const std::string &key, float value)
Update a gamerule value (string version)
ClientGameRules & operator=(const ClientGameRules &)=delete
static ClientGameRules & getInstance()
Get the singleton instance.
void updateMultiple(const std::unordered_map< std::string, float > &gamerules)
Update multiple gamerules at once.
float get(GameruleKey key, float defaultValue=0.0f) const
Get a gamerule value (type-safe with enum)
std::unordered_map< std::string, float > rules_
bool has(GameruleKey key) const
Check if a gamerule exists (type-safe with enum)
void clear()
Clear all gamerules.
void update(GameruleKey key, float value)
Update a gamerule value (type-safe with enum)
ClientGameRules(const ClientGameRules &)=delete
bool has(const std::string &key) const
Check if a gamerule exists (string version)
size_t size() const
Get the number of stored gamerules.
const char * toString(GameruleKey key)
Convert GameruleKey enum to string.