Make growth rate library case insensitive, add exception if not found.
continuous-integration/drone/push Build is passing Details

This commit is contained in:
Deukhoofd 2020-02-16 10:07:01 +01:00
parent f37e27378e
commit 252be18630
Signed by: Deukhoofd
GPG Key ID: ADF2E9256009EDCE
2 changed files with 23 additions and 3 deletions

View File

@ -1,11 +1,31 @@
#include "GrowthRateLibrary.hpp" #include "GrowthRateLibrary.hpp"
#include <algorithm>
#include "../../Core/Exceptions/CreatureException.hpp"
uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const std::string& growthRate, uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const std::string& growthRate,
uint32_t experience) const { uint32_t experience) const {
return _growthRates.at(growthRate)->CalculateLevel(experience); auto g = growthRate;
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
auto find = _growthRates.find(g);
if (find == _growthRates.end()) {
throw CreatureException("Invalid growth rate was requested.");
}
return find->second->CalculateLevel(experience);
} }
uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const std::string& growthRate, uint32_t CreatureLib::Library::GrowthRateLibrary::CalculateExperience(const std::string& growthRate,
uint8_t level) const { uint8_t level) const {
return _growthRates.at(growthRate)->CalculateExperience(level); auto g = growthRate;
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
auto find = _growthRates.find(g);
if (find == _growthRates.end()) {
throw CreatureException("Invalid growth rate was requested.");
}
return find->second->CalculateExperience(level);
}
void CreatureLib::Library::GrowthRateLibrary::AddGrowthRate(const std::string& name,
CreatureLib::Library::GrowthRate* rate) {
auto g = name;
std::transform(g.begin(), g.end(), g.begin(), ::tolower);
_growthRates.insert({g, rate});
} }

View File

@ -24,7 +24,7 @@ namespace CreatureLib::Library {
[[nodiscard]] uint8_t CalculateLevel(const std::string& growthRate, uint32_t experience) const; [[nodiscard]] uint8_t CalculateLevel(const std::string& growthRate, uint32_t experience) const;
[[nodiscard]] uint32_t CalculateExperience(const std::string& growthRate, uint8_t level) const; [[nodiscard]] uint32_t CalculateExperience(const std::string& growthRate, uint8_t level) const;
void AddGrowthRate(std::string name, GrowthRate* rate) { _growthRates.insert({name, rate}); } void AddGrowthRate(const std::string& name, GrowthRate* rate);
}; };
} }