#include "BuildMoves.hpp" #include #include #include "../../extern/json.hpp" using json = nlohmann::json; #define GET(o, objectKey, key) \ auto _##objectKey = o[#objectKey]; \ if (_##objectKey.is_null()) { \ std::cout << "Failed to retrieve key '" << #objectKey << "' for object with value '" << key << "' in file '" \ << path << "'\n"; \ return nullptr; \ } static PkmnLib::Library::MoveCategory ParseCategory(const std::string& input) { if (input == "physical") return PkmnLib::Library::MoveCategory::Physical; else if (input == "special") return PkmnLib::Library::MoveCategory::Special; else if (input == "status") return PkmnLib::Library::MoveCategory::Status; std::cout << "Invalid move category '" << input << ".\n"; return static_cast(255); } PkmnLib::Library::MoveLibrary* BuildMoves::Build(const std::string& path, const CreatureLib::Library::TypeLibrary* types) { std::ifstream fileStream(path.c_str()); if (fileStream.fail()) { std::cout << "Failed to load Move data file at '" << path << "'\n"; return nullptr; } auto lib = new PkmnLib::Library::MoveLibrary(); json j; fileStream >> j; for (auto i : j.items()) { auto val = i.value(); GET(val, name, i); GET(val, type, i); GET(val, power, i); GET(val, pp, i); GET(val, accuracy, i); GET(val, priority, i); GET(val, target, i); GET(val, category, i); GET(val, flags, i); if (_pp.get() == 0) continue; auto type = types->GetTypeId(_type.get()); auto category = ParseCategory(_category.get()); if (static_cast(category) == 255) return nullptr; CreatureLib::Library::AttackTarget target; if (!CreatureLib::Library::AttackTargetHelper::TryParse(_target.get().c_str(), target)) { std::cout << "Invalid target: '" << _target.get() << "' for move with name '" << _name.get() << "'\n"; return nullptr; } auto flags = std::unordered_set(); for (auto flagIndex : _flags.items()) { flags.insert(Arbutils::CaseInsensitiveConstString(flagIndex.value().get())); } auto move = new PkmnLib::Library::MoveData( _name.get(), type, category, _power.get(), _accuracy.get(), _pp.get(), target, _priority.get(), flags); lib->Insert(Arbutils::CaseInsensitiveConstString(move->GetName()), move); } return lib; } #undef GET