diff --git a/src/Library/GrowthRates/GrowthRateLibrary.cpp b/src/Library/GrowthRates/GrowthRateLibrary.cpp index f6383df..cb46eb5 100644 --- a/src/Library/GrowthRates/GrowthRateLibrary.cpp +++ b/src/Library/GrowthRates/GrowthRateLibrary.cpp @@ -1,11 +1,31 @@ #include "GrowthRateLibrary.hpp" +#include +#include "../../Core/Exceptions/CreatureException.hpp" uint8_t CreatureLib::Library::GrowthRateLibrary::CalculateLevel(const std::string& growthRate, 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, 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}); } diff --git a/src/Library/GrowthRates/GrowthRateLibrary.hpp b/src/Library/GrowthRates/GrowthRateLibrary.hpp index 4092e66..21fccf9 100644 --- a/src/Library/GrowthRates/GrowthRateLibrary.hpp +++ b/src/Library/GrowthRates/GrowthRateLibrary.hpp @@ -24,7 +24,7 @@ namespace CreatureLib::Library { [[nodiscard]] uint8_t CalculateLevel(const std::string& growthRate, uint32_t experience) 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); }; }