aboutsummaryrefslogtreecommitdiff
path: root/oop2eindopdr/Pokedex.cpp
blob: ee66f4ce36317fc19f0c0271e4167cb91289365c (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
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
#include <fstream>
#include <iostream>
#include <algorithm>

#include "CacheManager.h"
#include "Pokedex.h"
#include "PokemonTCGAPIClient.h"
#include "PokemonCard.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_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));
	}
}

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_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 || 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;
}