#include "PokemonCard.h" #include #include #include #include using std::endl; std::ostream& operator << (std::ostream& output, const PokemonCard& card) { output << "name: " << card.name << " (" << card.id << ")" << endl; output << "HP: " << std::fixed << std::setprecision(2) << card.hp << endl; output << "market value: " << card.value << endl; output << "attacks: " << endl; for (std::string attack : card.attacks) output << "\t" << attack << endl; return output; } PokemonCard::PokemonCard(CacheManager* cache_ref) { set_cache(cache_ref); if (this->cache_ref == nullptr) return; } PokemonCard::~PokemonCard() { } void PokemonCard::fetch_market_value() { } void PokemonCard::set_cache(CacheManager* cache_ref) { this->cache_ref = cache_ref; } void PokemonCard::verify_files() { } void PokemonCard::download_files() { } void PokemonCard::raw_load_json(nlohmann::json raw_data) { this->id = raw_data["id"].get(); this->name = raw_data["name"].get(); try { this->hp = std::stoul(raw_data["hp"].get()); } catch (nlohmann::json::type_error) { this->hp = 0; // some NPCs/auxiliary cards have hp = null } this->value = 0.f; // fetch using fetch_market_value for (nlohmann::json raw_attack : raw_data["attacks"]) this->attacks.push_back(raw_attack["name"]); } void PokemonCard::raw_load_cache(const char* cache_path) { std::string info_json_location = cache_path; info_json_location.append("/info.json"); this->verify_files(); std::fstream& info_json_file = *this->cache_ref->cache_get(info_json_location.c_str()); std::stringstream info_json_content; info_json_content << info_json_file.rdbuf(); nlohmann::json raw_json = nlohmann::json::parse(info_json_content.str()); this->raw_load_json(raw_json); }