blob: 93764b46b202e4780eac0cdb7c5f98bd3a4234df (
plain)
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
|
#include <fstream>
#include <iostream>
#include <algorithm>
#include "CacheManager.h"
#include "Pokedex.h"
Pokedex::Pokedex() {
this->cache = new CacheManager("./cache");
this->api = new PokemonTCGAPIClient();
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_cache(this->cache);
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->cache, id));
}
}
void Pokedex::verify_collection() {
std::fstream* index = nullptr;
if (!cache->cache_exists("index")) index = cache->cache_get("index");
for (PokemonCard* card : this->cards) {
card->verify_files();
if (index != nullptr) *index << card->id << "\n";
}
if (index != nullptr) index->close();
cache->update_cache();
}
std::vector<PokemonCard*> Pokedex::search_cards_by_id(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;
});
out.resize(std::distance(out.begin(), it));
return out;
}
|