|
| 1 | +/* Hyperspace Reward Names |
| 2 | + * |
| 3 | + * From: https://github.com/PokemonAutomation/ |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +#include <map> |
| 8 | +#include "Common/Cpp/Json/JsonValue.h" |
| 9 | +#include "Common/Cpp/Json/JsonArray.h" |
| 10 | +#include "Common/Cpp/Json/JsonObject.h" |
| 11 | +#include "CommonFramework/Globals.h" |
| 12 | +#include "PokemonLZA_HyperspaceRewardNames.h" |
| 13 | + |
| 14 | +namespace PokemonAutomation{ |
| 15 | +namespace NintendoSwitch{ |
| 16 | +namespace PokemonLZA{ |
| 17 | + |
| 18 | + |
| 19 | +struct HyperspaceRewardNamesDatabase{ |
| 20 | + HyperspaceRewardNamesDatabase(); |
| 21 | + |
| 22 | + static const HyperspaceRewardNamesDatabase& instance(){ |
| 23 | + static HyperspaceRewardNamesDatabase database; |
| 24 | + return database; |
| 25 | + } |
| 26 | + |
| 27 | + static const std::string NULL_SLUG; |
| 28 | + std::vector<std::string> ordered_list; |
| 29 | + std::map<std::string, HyperspaceRewardNames> database; |
| 30 | + std::map<std::string, std::string> reverse_lookup; |
| 31 | +}; |
| 32 | +const std::string HyperspaceRewardNamesDatabase::NULL_SLUG; |
| 33 | + |
| 34 | +HyperspaceRewardNamesDatabase::HyperspaceRewardNamesDatabase() |
| 35 | +{ |
| 36 | + // Load a list of tournament prize slugs in the desired order: |
| 37 | + // ["potion", "fresh-water", ... ] |
| 38 | + std::string path_slugs = RESOURCE_PATH() + "PokemonSV/AAT/TournamentPrizeList.json"; //TODO: Get reward list |
| 39 | + JsonValue json_slugs = load_json_file(path_slugs); |
| 40 | + JsonArray& slugs = json_slugs.to_array_throw(path_slugs); |
| 41 | + |
| 42 | + // Load a map of tournament prize slugs to item names in all languages, e.g.: |
| 43 | + // { |
| 44 | + // "potion": { |
| 45 | + // "eng": "Potion", |
| 46 | + // "deu": "Trank", |
| 47 | + // ... |
| 48 | + // }, |
| 49 | + // .... |
| 50 | + // } |
| 51 | + std::string path_disp = RESOURCE_PATH() + "PokemonSV/AAT/TournamentPrizeNameDisplay.json"; //TODO: Get reward list |
| 52 | + JsonValue json_disp = load_json_file(path_disp); |
| 53 | + JsonObject& item_disp = json_disp.to_object_throw(path_disp); |
| 54 | + |
| 55 | + for (auto& item : slugs){ |
| 56 | + std::string& slug = item.to_string_throw(path_slugs); |
| 57 | + |
| 58 | + JsonObject& auction_item_name_dict = item_disp.get_object_throw(slug, path_disp); |
| 59 | + std::string& display_name = auction_item_name_dict.get_string_throw("eng", path_disp); |
| 60 | + |
| 61 | + ordered_list.push_back(slug); |
| 62 | + database[std::move(slug)].m_display_name = std::move(display_name); |
| 63 | + } |
| 64 | + |
| 65 | + for (const auto& item : database){ |
| 66 | + reverse_lookup[item.second.m_display_name] = item.first; |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +const HyperspaceRewardNames& get_hyperspace_reward_name(const std::string& slug){ |
| 71 | + const std::map<std::string, HyperspaceRewardNames>& database = HyperspaceRewardNamesDatabase::instance().database; |
| 72 | + auto iter = database.find(slug); |
| 73 | + if (iter == database.end()){ |
| 74 | + throw InternalProgramError( |
| 75 | + nullptr, PA_CURRENT_FUNCTION, |
| 76 | + "Hyperspace reward slug not found in database: " + slug |
| 77 | + ); |
| 78 | + } |
| 79 | + return iter->second; |
| 80 | +} |
| 81 | +const std::string& parse_hyperspace_reward_name(const std::string& display_name){ |
| 82 | + const std::map<std::string, std::string>& database = HyperspaceRewardNamesDatabase::instance().reverse_lookup; |
| 83 | + auto iter = database.find(display_name); |
| 84 | + if (iter == database.end()){ |
| 85 | + throw InternalProgramError( |
| 86 | + nullptr, PA_CURRENT_FUNCTION, |
| 87 | + "Hyperspace reward name not found in database: " + display_name |
| 88 | + ); |
| 89 | + } |
| 90 | + return iter->second; |
| 91 | +} |
| 92 | +const std::string& parse_hyperspace_reward_name_nothrow(const std::string& display_name){ |
| 93 | + const std::map<std::string, std::string>& database = HyperspaceRewardNamesDatabase::instance().reverse_lookup; |
| 94 | + auto iter = database.find(display_name); |
| 95 | + if (iter == database.end()){ |
| 96 | + return HyperspaceRewardNamesDatabase::NULL_SLUG; |
| 97 | + } |
| 98 | + return iter->second; |
| 99 | +} |
| 100 | + |
| 101 | + |
| 102 | +const std::vector<std::string>& HYPERSPACE_REWARD_SLUGS(){ |
| 103 | + return HyperspaceRewardNamesDatabase::instance().ordered_list; |
| 104 | +} |
| 105 | + |
| 106 | + |
| 107 | +} |
| 108 | +} |
| 109 | +} |
0 commit comments