[MapRotation]: Port changes from S1x (#629)

This commit is contained in:
Edo 2022-12-06 21:18:29 +00:00 committed by GitHub
parent d12188214a
commit fc97944a12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 18 deletions

View File

@ -26,7 +26,7 @@ namespace Components
this->rotationEntries_.emplace_back(std::make_pair(key, value)); this->rotationEntries_.emplace_back(std::make_pair(key, value));
} }
std::size_t MapRotation::RotationData::getEntriesSize() const std::size_t MapRotation::RotationData::getEntriesSize() const noexcept
{ {
return this->rotationEntries_.size(); return this->rotationEntries_.size();
} }
@ -47,7 +47,7 @@ namespace Components
const auto& key = tokens[i]; const auto& key = tokens[i];
const auto& value = tokens[i + 1]; const auto& value = tokens[i + 1];
if (key == "map" || key == "gametype") if (key == "map"s || key == "gametype"s)
{ {
this->addEntry(key, value); this->addEntry(key, value);
} }
@ -58,13 +58,17 @@ namespace Components
} }
} }
bool MapRotation::RotationData::empty() const noexcept
{
return this->rotationEntries_.empty();
}
bool MapRotation::RotationData::contains(const std::string& key, const std::string& value) const bool MapRotation::RotationData::contains(const std::string& key, const std::string& value) const
{ {
return std::ranges::any_of(this->rotationEntries_, return std::ranges::any_of(this->rotationEntries_, [&](const auto& entry)
[&](const auto& entry) {
{ return entry.first == key && entry.second == value;
return entry.first == key && entry.second == value; });
});
} }
nlohmann::json MapRotation::RotationData::to_json() const nlohmann::json MapRotation::RotationData::to_json() const
@ -74,18 +78,18 @@ namespace Components
for (const auto& [key, val] : this->rotationEntries_) for (const auto& [key, val] : this->rotationEntries_)
{ {
if (key == "map") if (key == "map"s)
{ {
mapVector.emplace_back(val); mapVector.emplace_back(val);
} }
else if (key == "gametype") else if (key == "gametype"s)
{ {
gametypeVector.emplace_back(val); gametypeVector.emplace_back(val);
} }
} }
nlohmann::json mapRotationJson = nlohmann::json auto mapRotationJson = nlohmann::json
{ {
{"maps", mapVector}, {"maps", mapVector},
{"gametypes", gametypeVector}, {"gametypes", gametypeVector},
@ -194,7 +198,7 @@ namespace Components
void MapRotation::ApplyGametype(const std::string& gametype) void MapRotation::ApplyGametype(const std::string& gametype)
{ {
assert(!gametype.empty()); assert(!gametype.empty());
Dvar::Var("g_gametype").set(gametype.data()); Dvar::Var("g_gametype").set(gametype);
} }
void MapRotation::RestartCurrentMap() void MapRotation::RestartCurrentMap()
@ -204,7 +208,7 @@ namespace Components
if (svMapname.empty()) if (svMapname.empty())
{ {
Logger::Print(Game::CON_CHANNEL_SERVER, "mapname dvar is empty! Defaulting to mp_afghan\n"); Logger::Print(Game::CON_CHANNEL_SERVER, "mapname dvar is empty! Defaulting to mp_afghan\n");
svMapname = "mp_afghan"; svMapname = "mp_afghan"s;
} }
ApplyMap(svMapname); ApplyMap(svMapname);
@ -212,7 +216,7 @@ namespace Components
void MapRotation::ApplyRotation(RotationData& rotation) void MapRotation::ApplyRotation(RotationData& rotation)
{ {
assert(rotation.getEntriesSize() != 0); assert(!rotation.empty());
// Continue to apply gametype until a map is found // Continue to apply gametype until a map is found
auto foundMap = false; auto foundMap = false;
@ -222,7 +226,7 @@ namespace Components
{ {
const auto& entry = rotation.getNextEntry(); const auto& entry = rotation.getNextEntry();
if (entry.first == "map") if (entry.first == "map"s)
{ {
Logger::Debug("Loading new map: '{}'", entry.second); Logger::Debug("Loading new map: '{}'", entry.second);
ApplyMap(entry.second); ApplyMap(entry.second);
@ -230,7 +234,7 @@ namespace Components
// Map was found so we exit the loop // Map was found so we exit the loop
foundMap = true; foundMap = true;
} }
else if (entry.first == "gametype") else if (entry.first == "gametype"s)
{ {
Logger::Debug("Applying new gametype: '{}'", entry.second); Logger::Debug("Applying new gametype: '{}'", entry.second);
ApplyGametype(entry.second); ApplyGametype(entry.second);
@ -260,7 +264,7 @@ namespace Components
Game::Dvar_SetString(*Game::sv_mapRotationCurrent, ""); Game::Dvar_SetString(*Game::sv_mapRotationCurrent, "");
if (rotationCurrent.getEntriesSize() == 0) if (rotationCurrent.empty())
{ {
Logger::Print(Game::CON_CHANNEL_SERVER, "{} is empty or contains invalid data. Restarting map\n", (*Game::sv_mapRotationCurrent)->name); Logger::Print(Game::CON_CHANNEL_SERVER, "{} is empty or contains invalid data. Restarting map\n", (*Game::sv_mapRotationCurrent)->name);
RestartCurrentMap(); RestartCurrentMap();
@ -302,7 +306,7 @@ namespace Components
} }
LoadMapRotation(); LoadMapRotation();
if (DedicatedRotation.getEntriesSize() == 0) if (DedicatedRotation.empty())
{ {
Logger::Print(Game::CON_CHANNEL_SERVER, "{} is empty or contains invalid data. Restarting map\n", (*Game::sv_mapRotation)->name); Logger::Print(Game::CON_CHANNEL_SERVER, "{} is empty or contains invalid data. Restarting map\n", (*Game::sv_mapRotation)->name);
RestartCurrentMap(); RestartCurrentMap();

View File

@ -30,11 +30,12 @@ namespace Components
// this method should be called to add a new entry (gamemode/map & value) // this method should be called to add a new entry (gamemode/map & value)
void addEntry(const std::string& key, const std::string& value); void addEntry(const std::string& key, const std::string& value);
[[nodiscard]] std::size_t getEntriesSize() const; [[nodiscard]] std::size_t getEntriesSize() const noexcept;
rotationEntry& getNextEntry(); rotationEntry& getNextEntry();
void parse(const std::string& data); void parse(const std::string& data);
[[nodiscard]] bool empty() const noexcept;
[[nodiscard]] bool contains(const std::string& key, const std::string& value) const; [[nodiscard]] bool contains(const std::string& key, const std::string& value) const;
[[nodiscard]] nlohmann::json to_json() const; [[nodiscard]] nlohmann::json to_json() const;