blob: 96eb7fc53de882d013ffd6c740a35879ce140d84 (
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
|
#include "PokemonCard.h"
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
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 << "\t" << attack << endl;
return output;
}
PokemonCard::PokemonCard(CacheManager* cache_ref) {
set_cache(cache_ref);
if (this->cache_ref == nullptr) return;
}
PokemonCard::~PokemonCard() {
}
void PokemonCard::fetch_market_value() {
}
void PokemonCard::set_cache(CacheManager* cache_ref) {
this->cache_ref = cache_ref;
}
void PokemonCard::verify_files() {
if (this->cache_ref->cache_exists(prefix_cache_path("complete"))) return;
if (!this->cache_ref->cache_exists(prefix_cache_path("info.json"))) {
std::fstream& info_file = *this->cache_ref->cache_get(prefix_cache_path("info.json"));
info_file << this->raw_data.dump();
info_file.close();
}
if (!this->cache_ref->cache_exists(prefix_cache_path("card.png")) ||
!this->cache_ref->cache_exists(prefix_cache_path("card_hires.png"))) {
download_files();
}
this->cache_ref->cache_get(prefix_cache_path("complete"))->close();
}
void PokemonCard::download_files() {
this->cache_ref->cache_get(prefix_cache_path("card.png"))->close();
this->cache_ref->cache_get(prefix_cache_path("card_hires.png"))->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>();
try {
this->hp = std::stoul(raw_data["hp"].get<std::string>());
} catch (nlohmann::json::type_error) {
this->hp = 0; // some NPCs/auxiliary cards have hp = null
}
this->value = 0.f; // fetch using fetch_market_value
for (nlohmann::json raw_attack : raw_data["attacks"])
this->attacks.push_back(raw_attack["name"]);
// 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->verify_files();
std::fstream& info_json_file = *this->cache_ref->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);
}
|