mirror of
https://github.com/alterware/iw4-validator.git
synced 2025-06-27 14:51:49 +00:00
feature(map_rotation): better error handling (#4)
This commit is contained in:
@ -8,22 +8,41 @@ using namespace std::literals;
|
||||
|
||||
namespace map_rotation
|
||||
{
|
||||
namespace
|
||||
{
|
||||
bool is_valid_key(const std::string& key)
|
||||
{
|
||||
static std::array<const char*, 3> keys =
|
||||
{
|
||||
"map",
|
||||
"gametype",
|
||||
"exec",
|
||||
};
|
||||
|
||||
return std::ranges::find(keys, key) != std::end(keys);
|
||||
}
|
||||
}
|
||||
|
||||
rotation_data::rotation_data()
|
||||
: index_(0)
|
||||
{
|
||||
}
|
||||
|
||||
void rotation_data::randomize()
|
||||
{
|
||||
std::random_device rd;
|
||||
std::mt19937 gen(rd());
|
||||
|
||||
std::ranges::shuffle(this->rotation_entries_, gen);
|
||||
}
|
||||
|
||||
void rotation_data::add_entry(const std::string& key, const std::string& value)
|
||||
{
|
||||
this->rotation_entries_.emplace_back(std::make_pair(key, value));
|
||||
this->rotation_entries_.emplace_back(key, value);
|
||||
}
|
||||
|
||||
std::size_t rotation_data::get_entries_size() const noexcept
|
||||
{
|
||||
return this->rotation_entries_.size();
|
||||
}
|
||||
|
||||
rotation_data::rotation_entry& rotation_data::get_next_entry()
|
||||
{
|
||||
const auto index = this->index_;
|
||||
++this->index_ %= this->rotation_entries_.size();
|
||||
return this->rotation_entries_.at(index);
|
||||
}
|
||||
|
||||
bool rotation_data::contains(const std::string& key, const std::string& value) const
|
||||
@ -39,18 +58,6 @@ namespace map_rotation
|
||||
return this->rotation_entries_.empty();
|
||||
}
|
||||
|
||||
std::size_t rotation_data::get_entries_size() const noexcept
|
||||
{
|
||||
return this->rotation_entries_.size();
|
||||
}
|
||||
|
||||
rotation_data::rotation_entry& rotation_data::get_next_entry()
|
||||
{
|
||||
const auto index = this->index_;
|
||||
++this->index_ %= this->rotation_entries_.size();
|
||||
return this->rotation_entries_.at(index);
|
||||
}
|
||||
|
||||
void rotation_data::parse(const std::string& data)
|
||||
{
|
||||
const auto tokens = utils::string::split(data, ' ');
|
||||
@ -59,13 +66,13 @@ namespace map_rotation
|
||||
const auto& key = tokens[i];
|
||||
const auto& value = tokens[i + 1];
|
||||
|
||||
if (key == "map"s || key == "gametype"s)
|
||||
if (is_valid_key(key))
|
||||
{
|
||||
this->add_entry(key, value);
|
||||
}
|
||||
else
|
||||
{
|
||||
throw map_rotation_parse_error();
|
||||
throw map_rotation_parse_error(utils::string::va("Invalid key '%s'", key.data()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,9 +2,33 @@
|
||||
|
||||
namespace map_rotation
|
||||
{
|
||||
struct map_rotation_parse_error : public std::exception
|
||||
#define DEFAULT_ERROR_MSG "Map Rotation Parse Error"
|
||||
|
||||
class map_rotation_parse_error : public std::runtime_error
|
||||
{
|
||||
[[nodiscard]] const char* what() const noexcept override { return "Map Rotation Parse Error"; }
|
||||
static std::string fmt(const std::string& message)
|
||||
{
|
||||
std::string error = DEFAULT_ERROR_MSG;
|
||||
|
||||
if (!message.empty())
|
||||
{
|
||||
error.append(": ");
|
||||
error.append(message);
|
||||
}
|
||||
|
||||
return error;
|
||||
}
|
||||
|
||||
public:
|
||||
map_rotation_parse_error(const std::string& message)
|
||||
: std::runtime_error(fmt(message))
|
||||
{
|
||||
}
|
||||
|
||||
map_rotation_parse_error()
|
||||
: std::runtime_error(DEFAULT_ERROR_MSG)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
class rotation_data
|
||||
@ -14,14 +38,14 @@ namespace map_rotation
|
||||
|
||||
rotation_data();
|
||||
|
||||
void randomize();
|
||||
|
||||
void add_entry(const std::string& key, const std::string& value);
|
||||
|
||||
[[nodiscard]] std::size_t get_entries_size() const noexcept;
|
||||
|
||||
[[nodiscard]] rotation_entry& get_next_entry();
|
||||
|
||||
[[nodiscard]] bool contains(const std::string& key, const std::string& value) const;
|
||||
[[nodiscard]] bool empty() const noexcept;
|
||||
[[nodiscard]] std::size_t get_entries_size() const noexcept;
|
||||
[[nodiscard]] rotation_entry& get_next_entry();
|
||||
|
||||
void parse(const std::string& data);
|
||||
|
||||
|
Reference in New Issue
Block a user