aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--oop2eindopdr/Pokedex.cpp6
-rw-r--r--oop2eindopdr/Pokedex.h7
-rw-r--r--oop2eindopdr/PokemonCard.cpp54
-rw-r--r--oop2eindopdr/PokemonCard.h25
-rw-r--r--oop2eindopdr/main.cpp4
5 files changed, 58 insertions, 38 deletions
diff --git a/oop2eindopdr/Pokedex.cpp b/oop2eindopdr/Pokedex.cpp
index 93764b4..9eb997f 100644
--- a/oop2eindopdr/Pokedex.cpp
+++ b/oop2eindopdr/Pokedex.cpp
@@ -4,6 +4,8 @@
#include "CacheManager.h"
#include "Pokedex.h"
+#include "PokemonTCGAPIClient.h"
+#include "PokemonCard.h"
Pokedex::Pokedex() {
this->cache = new CacheManager("./cache");
@@ -24,7 +26,7 @@ void Pokedex::load_collection_remote() {
std::vector<PokemonCard*> remote_cards = api->get_set_cards("swshp");
for (PokemonCard* card : remote_cards) {
- card->set_cache(this->cache);
+ card->set_pokedex(this);
this->cards.push_back(card);
}
this->verify_collection();
@@ -37,7 +39,7 @@ void Pokedex::load_collection_local() {
std::fstream& cache_collection = *cache->cache_get("index");
std::string id;
while (std::getline(cache_collection, id)) {
- this->cards.push_back(new PokemonCard::from_cache(this->cache, id));
+ this->cards.push_back(new PokemonCard::from_cache(this, id));
}
}
diff --git a/oop2eindopdr/Pokedex.h b/oop2eindopdr/Pokedex.h
index e5730ee..1d6a269 100644
--- a/oop2eindopdr/Pokedex.h
+++ b/oop2eindopdr/Pokedex.h
@@ -4,8 +4,9 @@
#include <vector>
#include "CacheManager.h"
-#include "PokemonCard.h"
-#include "PokemonTCGAPIClient.h"
+
+class PokemonCard;
+class PokemonTCGAPIClient;
/** @brief user pokedex class, handles caching and api access silently */
class Pokedex {
@@ -24,6 +25,8 @@ private:
CacheManager* cache = nullptr;
PokemonTCGAPIClient* api = nullptr;
+ friend class PokemonCard;
+
public:
Pokedex();
virtual ~Pokedex();
diff --git a/oop2eindopdr/PokemonCard.cpp b/oop2eindopdr/PokemonCard.cpp
index 96eb7fc..8ce18a8 100644
--- a/oop2eindopdr/PokemonCard.cpp
+++ b/oop2eindopdr/PokemonCard.cpp
@@ -1,10 +1,12 @@
-#include "PokemonCard.h"
-
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
+#include "Pokedex.h"
+#include "PokemonCard.h"
+#include "PokemonTCGAPIClient.h"
+
using std::endl;
std::string PokemonCard::prefix_cache_path(const char* filename) {
@@ -17,14 +19,14 @@ std::ostream& operator << (std::ostream& output, const PokemonCard& card) {
output << "market value: " << card.value << endl;
output << "attacks: " << endl;
for (std::string attack : card.attacks)
- output << "\t" << attack << endl;
+ output << " - " << attack << endl;
return output;
}
-PokemonCard::PokemonCard(CacheManager* cache_ref) {
- set_cache(cache_ref);
- if (this->cache_ref == nullptr) return;
+PokemonCard::PokemonCard(Pokedex* pokedex) {
+ set_pokedex(pokedex);
+ if (this->pokedex == nullptr) return;
}
PokemonCard::~PokemonCard() {
@@ -32,44 +34,44 @@ 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_cache(CacheManager* cache_ref) {
- this->cache_ref = cache_ref;
+void PokemonCard::set_pokedex(Pokedex* pokedex) {
+ this->pokedex = pokedex;
}
void PokemonCard::verify_files() {
- if (this->cache_ref->cache_exists(prefix_cache_path("complete"))) return;
+ if (this->pokedex->cache->cache_exists(prefix_cache_path("complete"))) return;
- if (!this->cache_ref->cache_exists(prefix_cache_path("info.json"))) {
- std::fstream& info_file = *this->cache_ref->cache_get(prefix_cache_path("info.json"));
+ 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->cache_ref->cache_exists(prefix_cache_path("card.png")) ||
- !this->cache_ref->cache_exists(prefix_cache_path("card_hires.png"))) {
+ 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->cache_ref->cache_get(prefix_cache_path("complete"))->close();
+ this->pokedex->cache->cache_get(prefix_cache_path("complete"))->close();
}
void PokemonCard::download_files() {
- this->cache_ref->cache_get(prefix_cache_path("card.png"))->close();
- this->cache_ref->cache_get(prefix_cache_path("card_hires.png"))->close();
+ 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<std::string>();
this->name = raw_data["name"].get<std::string>();
- try {
- this->hp = std::stoul(raw_data["hp"].get<std::string>());
- } catch (nlohmann::json::type_error) {
- this->hp = 0; // some NPCs/auxiliary cards have hp = null
- }
- this->value = 0.f; // fetch using fetch_market_value
+ if (!raw_data["hp"].is_null()) this->hp = std::stoul(raw_data["hp"].get<std::string>());
+ if (raw_data.contains("tcgplayer")) this->value = raw_data["tcgplayer"]["prices"]["holofoil"]["market"].get<double>();
for (nlohmann::json raw_attack : raw_data["attacks"])
this->attacks.push_back(raw_attack["name"]);
@@ -79,10 +81,14 @@ void PokemonCard::raw_load_json(nlohmann::json raw_data) {
void PokemonCard::raw_load_cache(const char* cache_path) {
this->verify_files();
- std::fstream& info_json_file = *this->cache_ref->cache_get(std::string(cache_path) + "/info.json");
+ 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 + ")";
+}
diff --git a/oop2eindopdr/PokemonCard.h b/oop2eindopdr/PokemonCard.h
index 5341866..1ac6708 100644
--- a/oop2eindopdr/PokemonCard.h
+++ b/oop2eindopdr/PokemonCard.h
@@ -4,8 +4,13 @@
#include <string>
#include <vector>
+class PokemonCard; // forward declaration for main.h
+
+#include "main.h"
#include "CacheManager.h"
+class Pokedex;
+
/** @brief single pokemon card */
class PokemonCard {
private:
@@ -14,23 +19,25 @@ private:
virtual void raw_load_json(nlohmann::json raw_data);
virtual void raw_load_cache(const char* cache_path);
- CacheManager* cache_ref = nullptr;
+ Pokedex* pokedex = nullptr;
/** @brief add cache path before filename */
virtual std::string prefix_cache_path(const char* filename);
public:
- PokemonCard(CacheManager* cache_ref = nullptr);
+ PokemonCard(Pokedex* pokedex = nullptr);
virtual ~PokemonCard();
/** @brief string stream output (for printing card) */
friend std::ostream& operator << (std::ostream& output, const PokemonCard& card);
- std::string id; /** @brief pokemon id (with set prefix) */
- std::string name; /** @brief pokemon name */
- unsigned hp; /** @brief pokemon max health points */
- double value; /** @brief pokemon card current market value */
- std::vector<std::string> attacks; /** @brief list of possible attacks */
+ std::string id = ""; /** @brief pokemon id (with set prefix) */
+ std::string name = ""; /** @brief pokemon name */
+ unsigned hp = 0; /** @brief pokemon max health points */
+ double value = 0.f; /** @brief pokemon card current market value */
+ std::vector<std::string> attacks = {}; /** @brief list of possible attacks */
+
+ virtual std::string short_identifier();
/** @brief use API to fetch `this->hp` */
virtual void fetch_market_value();
@@ -44,7 +51,7 @@ public:
struct from_cache;
/** @brief set cache */
- virtual void set_cache(CacheManager* cache_ref);
+ virtual void set_pokedex(Pokedex* pokedex);
};
struct PokemonCard::from_json : public PokemonCard {
@@ -54,7 +61,7 @@ struct PokemonCard::from_json : public PokemonCard {
};
struct PokemonCard::from_cache : public PokemonCard {
- from_cache(CacheManager* cache_ref, std::string cache_path) : PokemonCard(cache_ref) {
+ from_cache(Pokedex* pokedex, std::string cache_path) : PokemonCard(pokedex) {
PokemonCard::raw_load_cache(cache_path.c_str());
}
};
diff --git a/oop2eindopdr/main.cpp b/oop2eindopdr/main.cpp
index 4a1e8c4..873e273 100644
--- a/oop2eindopdr/main.cpp
+++ b/oop2eindopdr/main.cpp
@@ -3,6 +3,7 @@
#include "main.h"
#include "Pokedex.h"
+#include "PokemonCard.h"
using std::endl;
using std::cout;
@@ -25,11 +26,12 @@ int interactive_mode() {
if (card_count == 0) {
cout << "no cards found" << endl;
} else if (card_count == 1) {
+ cards[0]->fetch_market_value();
cout << "found card:" << endl << *cards[0];
} else {
cout << "found cards:" << endl;
for (PokemonCard* card : cards)
- cout << *card << endl;
+ cout << card->short_identifier() << endl;
}
}
return EXIT_SUCCESS;