aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-12-24 13:37:21 +0100
committerlonkaars <loek@pipeframe.xyz>2022-12-24 13:37:21 +0100
commit2590dd61794f0e2fdf5dd0f1bbf7f71791df8d0c (patch)
tree2e8d63025b0c5bfd7ac38fa1e7bfef70322fd92e
parent95f1aca8b061e60f81f636349e0b9ff76a114bb0 (diff)
downloading and parsing swshp set works
-rw-r--r--oop2eindopdr/Pokedex.cpp18
-rw-r--r--oop2eindopdr/Pokedex.h2
-rw-r--r--oop2eindopdr/PokemonCard.cpp41
-rw-r--r--oop2eindopdr/PokemonCard.h9
-rw-r--r--oop2eindopdr/PokemonTCGAPIClient.cpp23
-rw-r--r--oop2eindopdr/PokemonTCGAPIClient.h2
6 files changed, 88 insertions, 7 deletions
diff --git a/oop2eindopdr/Pokedex.cpp b/oop2eindopdr/Pokedex.cpp
index 24de74b..a38cefd 100644
--- a/oop2eindopdr/Pokedex.cpp
+++ b/oop2eindopdr/Pokedex.cpp
@@ -1,11 +1,17 @@
#include <fstream>
+#include <iostream>
#include "CacheManager.h"
#include "Pokedex.h"
Pokedex::Pokedex() {
this->cache = new CacheManager("./cache");
- this->load_collection_local();
+ this->api = new PokemonTCGAPIClient();
+
+ if (this->cache->cache_exists("index"))
+ this->load_collection_local();
+ else
+ this->load_collection_remote();
}
Pokedex::~Pokedex() {
@@ -13,14 +19,22 @@ Pokedex::~Pokedex() {
}
void Pokedex::load_collection_remote() {
+ std::cout << "no local cards found, building cache..." << std::endl;
+ std::vector<PokemonCard*> remote_cards = api->get_set_cards("swshp");
+ for (PokemonCard* card : remote_cards) {
+ card->set_cache(this->cache);
+ card->verify_files();
+ this->cards.push_back(card);
+ }
+ cache->update_cache();
}
void Pokedex::load_collection_local() {
std::fstream& cache_collection = *cache->cache_get("index");
std::string card;
while (std::getline(cache_collection, card)) {
- this->cards.push_back(new PokemonCard::from_cache(cache, card));
+ this->cards.push_back(new PokemonCard::from_cache(this->cache, card));
}
}
diff --git a/oop2eindopdr/Pokedex.h b/oop2eindopdr/Pokedex.h
index 5e20367..e5730ee 100644
--- a/oop2eindopdr/Pokedex.h
+++ b/oop2eindopdr/Pokedex.h
@@ -5,6 +5,7 @@
#include "CacheManager.h"
#include "PokemonCard.h"
+#include "PokemonTCGAPIClient.h"
/** @brief user pokedex class, handles caching and api access silently */
class Pokedex {
@@ -21,6 +22,7 @@ private:
/** @brief cache connection */
CacheManager* cache = nullptr;
+ PokemonTCGAPIClient* api = nullptr;
public:
Pokedex();
diff --git a/oop2eindopdr/PokemonCard.cpp b/oop2eindopdr/PokemonCard.cpp
index 9c4b8a7..69a6e61 100644
--- a/oop2eindopdr/PokemonCard.cpp
+++ b/oop2eindopdr/PokemonCard.cpp
@@ -1,6 +1,7 @@
#include "PokemonCard.h"
#include <iostream>
+#include <fstream>
#include <sstream>
#include <iomanip>
@@ -18,7 +19,8 @@ std::ostream& operator << (std::ostream& output, const PokemonCard& card) {
}
PokemonCard::PokemonCard(CacheManager* cache_ref) {
-
+ set_cache(cache_ref);
+ if (this->cache_ref == nullptr) return;
}
PokemonCard::~PokemonCard() {
@@ -28,3 +30,40 @@ 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<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
+ 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);
+}
diff --git a/oop2eindopdr/PokemonCard.h b/oop2eindopdr/PokemonCard.h
index a19ef60..873d327 100644
--- a/oop2eindopdr/PokemonCard.h
+++ b/oop2eindopdr/PokemonCard.h
@@ -14,8 +14,10 @@ private:
virtual void raw_load_json(nlohmann::json raw_data);
virtual void raw_load_cache(const char* cache_path);
+ CacheManager* cache_ref = nullptr;
+
public:
- PokemonCard(CacheManager* cache_ref);
+ PokemonCard(CacheManager* cache_ref = nullptr);
virtual ~PokemonCard();
/** @brief string stream output (for printing card) */
@@ -37,10 +39,13 @@ public:
struct from_json; // named constructors (defined below)
struct from_cache;
+
+ /** @brief set cache */
+ virtual void set_cache(CacheManager* cache_ref);
};
struct PokemonCard::from_json : public PokemonCard {
- from_json(CacheManager* cache_ref, nlohmann::json json_data) : PokemonCard(cache_ref) {
+ from_json(nlohmann::json json_data) : PokemonCard() {
PokemonCard::raw_load_json(json_data);
}
};
diff --git a/oop2eindopdr/PokemonTCGAPIClient.cpp b/oop2eindopdr/PokemonTCGAPIClient.cpp
index 0c950fd..335c15c 100644
--- a/oop2eindopdr/PokemonTCGAPIClient.cpp
+++ b/oop2eindopdr/PokemonTCGAPIClient.cpp
@@ -19,15 +19,34 @@ nlohmann::json PokemonTCGAPIClient::raw_request(const char* endpoint, cpr::Param
}
nlohmann::json PokemonTCGAPIClient::raw_get_cards(const char* query) {
- return raw_request("/cards", cpr::Parameters{{"q", query}});
+ return raw_request("/cards", cpr::Parameters{{"q", query}, {"select", "id,name,hp,attacks,number,images"}});
}
nlohmann::json PokemonTCGAPIClient::raw_get_sets(const char* query) {
return raw_request("/sets", cpr::Parameters{{"q", query}});
}
+nlohmann::json PokemonTCGAPIClient::raw_get_card(const char* id) {
+ std::string full_query = "id:";
+ full_query.append(id);
+ return raw_request("/cards", cpr::Parameters{{"q", full_query}});
+}
+
std::vector<PokemonCard*> PokemonTCGAPIClient::get_set_cards(const char* set_name) {
- return {};
+ std::string query = "set.id:";
+ query.append(set_name);
+ nlohmann::json raw_data = raw_get_cards(query.c_str());
+ std::vector<PokemonCard*> out;
+ for (nlohmann::json card_json : raw_data["data"]) {
+ out.push_back(new PokemonCard::from_json(card_json));
+ }
+ return out;
+}
+
+PokemonCard* PokemonTCGAPIClient::get_full_card(const char* id) {
+ nlohmann::json raw_cards = raw_get_card(id);
+ if (raw_cards["data"].size() == 0) return nullptr;
+ return new PokemonCard::from_json(raw_cards["data"][0]);
}
std::vector<std::string> PokemonTCGAPIClient::get_sets() {
diff --git a/oop2eindopdr/PokemonTCGAPIClient.h b/oop2eindopdr/PokemonTCGAPIClient.h
index 241586b..c6b6a23 100644
--- a/oop2eindopdr/PokemonTCGAPIClient.h
+++ b/oop2eindopdr/PokemonTCGAPIClient.h
@@ -15,10 +15,12 @@ private:
const char* API_URL = "https://api.pokemontcg.io/v2";
virtual nlohmann::json raw_request(const char* endpoint, cpr::Parameters params);
virtual nlohmann::json raw_get_cards(const char* query);
+ virtual nlohmann::json raw_get_card(const char* id);
virtual nlohmann::json raw_get_sets(const char* query);
public:
virtual std::vector<PokemonCard*> get_set_cards(const char* set_name);
+ virtual PokemonCard* get_full_card(const char* id);
virtual std::vector<std::string> get_sets();
};