aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-12-24 20:48:52 +0100
committerlonkaars <loek@pipeframe.xyz>2022-12-24 20:48:52 +0100
commit2a9f99562f17842341f5d7fc970ee1e3e30f6e25 (patch)
tree99df056b333cd7b25a368db8308102557678950e
parenta2bff9393ac5e39b15534871e2bdbfc1f2fa7909 (diff)
fetch cards from api if not in cache
-rw-r--r--oop2eindopdr/Pokedex.cpp31
-rw-r--r--oop2eindopdr/Pokedex.h3
-rw-r--r--oop2eindopdr/PokemonCard.h5
-rw-r--r--oop2eindopdr/PokemonTCGAPIClient.cpp10
-rw-r--r--oop2eindopdr/PokemonTCGAPIClient.h1
-rw-r--r--oop2eindopdr/main.cpp9
-rw-r--r--oop2eindopdr/main.h5
7 files changed, 40 insertions, 24 deletions
diff --git a/oop2eindopdr/Pokedex.cpp b/oop2eindopdr/Pokedex.cpp
index 9eb997f..ee66f4c 100644
--- a/oop2eindopdr/Pokedex.cpp
+++ b/oop2eindopdr/Pokedex.cpp
@@ -56,15 +56,42 @@ void Pokedex::verify_collection() {
cache->update_cache();
}
-std::vector<PokemonCard*> Pokedex::search_cards_by_id(std::string query) {
+std::vector<PokemonCard*> Pokedex::search_cards_by_id_local(std::string query) {
std::vector<PokemonCard*> out(this->cards.size());
// https://cplusplus.com/reference/algorithm/copy_if/
auto it = std::copy_if(this->cards.begin(), this->cards.end(), out.begin(), [&](const PokemonCard* card) {
- return card->id.find(query) != std::string::npos;
+ return card->id.find(query) != std::string::npos || card->name.find(query) != std::string::npos;
});
out.resize(std::distance(out.begin(), it));
return out;
}
+std::vector<PokemonCard*> Pokedex::search_cards_by_id_remote(std::string query) {
+ std::vector<PokemonCard*> api_cards = api->get_cards_by_query((std::string("name:\"") + query + "\" OR id:\"" + query + "\"").c_str());
+ std::vector<PokemonCard*> out;
+
+ for (PokemonCard* api_card : api_cards) {
+ if (std::any_of(this->cards.begin(), this->cards.end(), [api_card] (PokemonCard* card) {
+ return card->id == api_card->id; // remove any duplicate local/api cards
+ })) {
+ delete api_card;
+ continue;
+ }
+ this->cards.push_back(api_card);
+ api_card->set_pokedex(this);
+ out.push_back(api_card);
+ }
+ this->verify_collection();
+
+ return out;
+}
+
+std::vector<PokemonCard*> Pokedex::search_cards_by_id(std::string query) {
+ std::vector<PokemonCard*> out = search_cards_by_id_local(query);
+ if (out.size() == 0) out = search_cards_by_id_remote(query);
+
+ return out;
+}
+
diff --git a/oop2eindopdr/Pokedex.h b/oop2eindopdr/Pokedex.h
index 1d6a269..28d0d31 100644
--- a/oop2eindopdr/Pokedex.h
+++ b/oop2eindopdr/Pokedex.h
@@ -27,6 +27,9 @@ private:
friend class PokemonCard;
+ virtual std::vector<PokemonCard*> search_cards_by_id_local(std::string query);
+ virtual std::vector<PokemonCard*> search_cards_by_id_remote(std::string query);
+
public:
Pokedex();
virtual ~Pokedex();
diff --git a/oop2eindopdr/PokemonCard.h b/oop2eindopdr/PokemonCard.h
index 1ac6708..691110a 100644
--- a/oop2eindopdr/PokemonCard.h
+++ b/oop2eindopdr/PokemonCard.h
@@ -4,11 +4,6 @@
#include <string>
#include <vector>
-class PokemonCard; // forward declaration for main.h
-
-#include "main.h"
-#include "CacheManager.h"
-
class Pokedex;
/** @brief single pokemon card */
diff --git a/oop2eindopdr/PokemonTCGAPIClient.cpp b/oop2eindopdr/PokemonTCGAPIClient.cpp
index 335c15c..3285f6a 100644
--- a/oop2eindopdr/PokemonTCGAPIClient.cpp
+++ b/oop2eindopdr/PokemonTCGAPIClient.cpp
@@ -32,10 +32,8 @@ nlohmann::json PokemonTCGAPIClient::raw_get_card(const char* id) {
return raw_request("/cards", cpr::Parameters{{"q", full_query}});
}
-std::vector<PokemonCard*> PokemonTCGAPIClient::get_set_cards(const char* set_name) {
- std::string query = "set.id:";
- query.append(set_name);
- nlohmann::json raw_data = raw_get_cards(query.c_str());
+std::vector<PokemonCard*> PokemonTCGAPIClient::get_cards_by_query(const char* query) {
+ nlohmann::json raw_data = raw_get_cards(query);
std::vector<PokemonCard*> out;
for (nlohmann::json card_json : raw_data["data"]) {
out.push_back(new PokemonCard::from_json(card_json));
@@ -43,6 +41,10 @@ std::vector<PokemonCard*> PokemonTCGAPIClient::get_set_cards(const char* set_nam
return out;
}
+std::vector<PokemonCard*> PokemonTCGAPIClient::get_set_cards(const char* set_name) {
+ return get_cards_by_query((std::string("set.id:") + std::string(set_name)).c_str());
+}
+
PokemonCard* PokemonTCGAPIClient::get_full_card(const char* id) {
nlohmann::json raw_cards = raw_get_card(id);
if (raw_cards["data"].size() == 0) return nullptr;
diff --git a/oop2eindopdr/PokemonTCGAPIClient.h b/oop2eindopdr/PokemonTCGAPIClient.h
index c6b6a23..cbbcf0d 100644
--- a/oop2eindopdr/PokemonTCGAPIClient.h
+++ b/oop2eindopdr/PokemonTCGAPIClient.h
@@ -19,6 +19,7 @@ private:
virtual nlohmann::json raw_get_sets(const char* query);
public:
+ virtual std::vector<PokemonCard*> get_cards_by_query(const char* query);
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();
diff --git a/oop2eindopdr/main.cpp b/oop2eindopdr/main.cpp
index 873e273..4cd99ec 100644
--- a/oop2eindopdr/main.cpp
+++ b/oop2eindopdr/main.cpp
@@ -1,7 +1,6 @@
#include <cstdlib>
#include <iostream>
-#include "main.h"
#include "Pokedex.h"
#include "PokemonCard.h"
@@ -17,7 +16,7 @@ int interactive_mode() {
std::string search_query;
for(;;) {
- cout << "card id?: ";
+ cout << "card id or name?: ";
if (!std::getline(cin, search_query)) break;
std::vector<PokemonCard*> cards = g_pokedex->search_cards_by_id(search_query);
@@ -51,12 +50,6 @@ int export_mode(int argc, char** argv) {
}
int main(int argc, char** argv) {
- // PokemonTCGAPIClient api_client_test;
- // std::vector<std::string> sets = api_client_test.get_sets();
- // for (auto s : sets)
- // cout << s << endl;
-
- // return EXIT_SUCCESS;
g_pokedex = new Pokedex();
if (argc == 1) { // no arguments specified
diff --git a/oop2eindopdr/main.h b/oop2eindopdr/main.h
deleted file mode 100644
index 0263d62..0000000
--- a/oop2eindopdr/main.h
+++ /dev/null
@@ -1,5 +0,0 @@
-#pragma once
-
-#include "Pokedex.h"
-
-extern Pokedex* g_pokedex;