1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#include <fstream>
#include <iostream>
#include <algorithm>
#include <cctype>
#include "CacheManager.h"
#include "DownloadManager.h"
#include "Pokedex.h"
#include "PokemonTCGAPIClient.h"
#include "PokemonCard.h"
Pokedex::Pokedex() {
this->cache = new CacheManager("./cache");
this->api = new PokemonTCGAPIClient();
this->download_manager = new DownloadManager();
if (this->cache->cache_exists("index"))
this->load_collection_local();
else
this->load_collection_remote();
}
Pokedex::~Pokedex() {
delete this->cache;
}
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_pokedex(this);
this->cards.push_back(card);
}
this->verify_collection();
cache->update_cache();
}
void Pokedex::load_collection_local() {
std::cout << "local cards found, using cache..." << std::endl;
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, id));
}
cache_collection.close();
}
void Pokedex::verify_collection() {
std::fstream& index = *cache->cache_get("index");
for (PokemonCard* card : this->cards) {
card->verify_files();
index << card->id << "\n";
}
index.close();
cache->update_cache();
}
std::string Pokedex::lower(std::string input) {
std::string out(input.size(), ' ');
std::transform(input.begin(), input.end(), out.begin(), tolower);
return out;
}
std::vector<PokemonCard*> Pokedex::search_cards_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 lower(card->id).find(lower(query)) != std::string::npos ||
lower(card->name).find(lower(query)) != std::string::npos;
});
out.resize(std::distance(out.begin(), it));
return out;
}
std::vector<PokemonCard*> Pokedex::search_cards_remote(std::string query) {
std::cout << "couldn't find card in cache, trying api..." << std::endl;
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(std::string query) {
std::vector<PokemonCard*> out = search_cards_local(query);
if (out.size() == 0) out = search_cards_remote(query);
return out;
}
PokemonCard* Pokedex::get_card_by_id(std::string id) {
for (PokemonCard* card : this->cards)
if (card->id == id) return card;
return nullptr;
}
|