blob: 95dc93ca619e57eaae50a9711e0cbc028b75ffd6 (
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
|
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include "Pokedex.h"
#include "PokemonCard.h"
#include "PokemonTCGAPIClient.h"
#include "DownloadManager.h"
using std::endl;
std::string PokemonCard::prefix_cache_path(const char* filename) {
return this->id + "/" + std::string(filename);
}
std::ostream& operator << (std::ostream& output, const PokemonCard& card) {
output << "name: " << card.name << " (" << card.id << ")" << endl;
output << "HP: " << std::fixed << std::setprecision(2) << card.hp << endl;
output << "market value: " << card.value << endl;
output << "attacks: " << endl;
for (std::string attack : card.attacks)
output << " - " << attack << endl;
return output;
}
PokemonCard::PokemonCard(Pokedex* pokedex) {
set_pokedex(pokedex);
if (this->pokedex == nullptr) return;
}
PokemonCard::~PokemonCard() {
if (this->image_download_thread != nullptr) delete this->image_download_thread;
}
void PokemonCard::fetch_market_value() {
//TODO: garbage?
std::cout << "getting market value for " << this->short_identifier() << "..." << std::endl;
PokemonCard* full_card = this->pokedex->api->get_full_card(this->id.c_str());
this->value = full_card->value;
delete full_card;
}
void PokemonCard::set_pokedex(Pokedex* pokedex) {
this->pokedex = pokedex;
}
void PokemonCard::verify_files() {
if (this->pokedex->cache->cache_exists(prefix_cache_path("complete"))) return;
if (!this->pokedex->cache->cache_exists(prefix_cache_path("info.json"))) {
std::fstream& info_file = *this->pokedex->cache->cache_get(prefix_cache_path("info.json"));
info_file << this->raw_data.dump();
info_file.close();
}
if (!this->pokedex->cache->cache_exists(prefix_cache_path("card.png")) ||
!this->pokedex->cache->cache_exists(prefix_cache_path("card_hires.png"))) {
download_files();
}
}
void PokemonCard::download_files() {
const std::string card_normal_path = this->pokedex->cache->prefix_cache_path(prefix_cache_path("card.png").c_str());
const std::string card_hires_path = this->pokedex->cache->prefix_cache_path(prefix_cache_path("card_hires.png").c_str());
std::thread* card_normal_dl = this->pokedex->download_manager->wget(this->url_card_normal, card_normal_path);
std::thread* card_hires_dl = this->pokedex->download_manager->wget(this->url_card_hires, card_hires_path);
this->image_download_thread = new std::thread([card_hires_dl, card_normal_dl, this] {
card_normal_dl->join();
card_hires_dl->join();
this->pokedex->cache->cache_get(prefix_cache_path("complete"))->close();
});
}
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>();
if (!raw_data["hp"].is_null()) this->hp = std::stoul(raw_data["hp"].get<std::string>());
if (raw_data.contains("tcgplayer")) this->value = raw_data["tcgplayer"]["prices"]["holofoil"]["market"].get<double>();
for (nlohmann::json raw_attack : raw_data["attacks"])
this->attacks.push_back(raw_attack["name"]);
this->url_card_normal = raw_data["images"]["small"].get<std::string>();
this->url_card_hires = raw_data["images"]["large"].get<std::string>();
// json needs to be written here so complete api response is preserved
this->raw_data = raw_data;
}
void PokemonCard::raw_load_cache(const char* cache_path) {
this->id = cache_path;
this->verify_files();
std::fstream& info_json_file = *this->pokedex->cache->cache_get(std::string(cache_path) + "/info.json");
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);
info_json_file.close();
}
std::string PokemonCard::short_identifier() {
return this->name + " (" + this->id + ")";
}
std::string PokemonCard::image_location() {
return this->pokedex->cache->prefix_cache_path(prefix_cache_path("card.png").c_str());
}
std::string PokemonCard::image_location_hires() {
return this->pokedex->cache->prefix_cache_path(prefix_cache_path("card_hires.png").c_str());
}
|