#include #include #include #include #include "Pokedex.h" #include "PokemonCard.h" #include "PokemonTCGAPIClient.h" using std::endl; std::string PokemonCard::prefix_cache_path(const char* filename) { return this->id + "/" + std::string(filename); } 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 << " - " << attack << endl; return output; } PokemonCard::PokemonCard(Pokedex* pokedex) { set_pokedex(pokedex); if (this->pokedex == nullptr) return; } PokemonCard::~PokemonCard() { } void PokemonCard::fetch_market_value() { //TODO: garbage? std::cout << "getting market value for " << this->short_identifier() << "..." << std::endl; PokemonCard* full_card = this->pokedex->api->get_full_card(this->id.c_str()); this->value = full_card->value; delete full_card; } void PokemonCard::set_pokedex(Pokedex* pokedex) { this->pokedex = pokedex; } void PokemonCard::verify_files() { if (this->pokedex->cache->cache_exists(prefix_cache_path("complete"))) return; if (!this->pokedex->cache->cache_exists(prefix_cache_path("info.json"))) { std::fstream& info_file = *this->pokedex->cache->cache_get(prefix_cache_path("info.json")); info_file << this->raw_data.dump(); info_file.close(); } if (!this->pokedex->cache->cache_exists(prefix_cache_path("card.png")) || !this->pokedex->cache->cache_exists(prefix_cache_path("card_hires.png"))) { download_files(); } this->pokedex->cache->cache_get(prefix_cache_path("complete"))->close(); } void PokemonCard::download_files() { this->pokedex->cache->cache_get(prefix_cache_path("card.png"))->close(); this->pokedex->cache->cache_get(prefix_cache_path("card_hires.png"))->close(); } void PokemonCard::raw_load_json(nlohmann::json raw_data) { this->id = raw_data["id"].get(); this->name = raw_data["name"].get(); if (!raw_data["hp"].is_null()) this->hp = std::stoul(raw_data["hp"].get()); if (raw_data.contains("tcgplayer")) this->value = raw_data["tcgplayer"]["prices"]["holofoil"]["market"].get(); for (nlohmann::json raw_attack : raw_data["attacks"]) this->attacks.push_back(raw_attack["name"]); // json needs to be written here so complete api response is preserved this->raw_data = raw_data; } void PokemonCard::raw_load_cache(const char* cache_path) { this->verify_files(); std::fstream& info_json_file = *this->pokedex->cache->cache_get(std::string(cache_path) + "/info.json"); 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); } std::string PokemonCard::short_identifier() { return this->name + " (" + this->id + ")"; }