From dfce4d0398a8bafbb7ad7a31345af181c0269c09 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 15 Apr 2021 15:23:23 -0400 Subject: Added spells --- src/utils.cc | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) (limited to 'src/utils.cc') diff --git a/src/utils.cc b/src/utils.cc index 009754b..1df3308 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -1,5 +1,6 @@ #include "utils.h" #include "json.hpp" +#include "settings.h" #include #include #include @@ -15,23 +16,35 @@ nlohmann::json utils::loadJson(const std::string& path) { return j; } +nlohmann::json utils::loadJson(const std::string& type, const std::string& name) { + for(auto data : utils::loadAllJson(settings::dummySettings.at(type))) { + if(data["name"] == name) { + return data; + } + } + throw std::invalid_argument("Unknown name: `" + name + "' for type `" + type + "'."); +} + void utils::saveJson(const nlohmann::json& data, const std::string& path) { std::ofstream f(path); f << std::setw(4) << data << std::endl; } -/*std::string utils::join(std::vector parts, std::string joiner) { - std::stringstream out; - bool isFirst = true; - for(std::string p : parts) { - if(! isFirst) { - out << joiner; +static std::map> cache; + +std::vector utils::loadAllJson(const std::string& directory) { + if(cache.contains(directory)) { + return cache[directory]; + } + std::vector ret; + for(auto path : std::filesystem::recursive_directory_iterator(directory)) { + if(path.path().extension() == ".json") { + ret.push_back(utils::loadJson(path.path())); } - isFirst = false; - out << p; } - return out.str(); -}*/ + cache[directory] = ret; + return ret; +} // Accepts coins formatted "X Yp" where X is an integer and Y is any of c, s, e, g, p. int utils::coins2copper(const std::string& coins) { -- cgit v1.2.3