aboutsummaryrefslogtreecommitdiff
path: root/oop2eindopdr/PokemonCard.cpp
blob: 69a6e61bcbe13b34ad9600eb902d330ece9544c6 (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
#include "PokemonCard.h"

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>

using std::endl;

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() {
	
}

void PokemonCard::download_files() {

}

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"]);
}

void PokemonCard::raw_load_cache(const char* cache_path) {
	std::string info_json_location = cache_path;
	info_json_location.append("/info.json");
	this->verify_files();
	std::fstream& info_json_file = *this->cache_ref->cache_get(info_json_location.c_str());
	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);
}