aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2022-12-24 15:13:43 +0100
committerlonkaars <loek@pipeframe.xyz>2022-12-24 15:13:43 +0100
commitd372e2ec7c67b848d7f67146506453af6d6a5379 (patch)
treeb1e6c4d3cebeb45b7573548f4b228d7e18fb9d45
parent1a267bb841a2a0b8d0bfe478b62d664d7457ae7a (diff)
cache working (no image downloads yet)
-rw-r--r--oop2eindopdr/CacheManager.cpp16
-rw-r--r--oop2eindopdr/CacheManager.h2
-rw-r--r--oop2eindopdr/Pokedex.cpp18
-rw-r--r--oop2eindopdr/PokemonCard.cpp29
-rw-r--r--oop2eindopdr/PokemonCard.h3
5 files changed, 54 insertions, 14 deletions
diff --git a/oop2eindopdr/CacheManager.cpp b/oop2eindopdr/CacheManager.cpp
index fad7b65..b8c368e 100644
--- a/oop2eindopdr/CacheManager.cpp
+++ b/oop2eindopdr/CacheManager.cpp
@@ -35,20 +35,26 @@ void CacheManager::update_cache() {
std::fstream& date = *cache_get("date");
this->age = std::time(nullptr);
date << this->age;
+ date.close();
}
+std::fstream* CacheManager::cache_get(std::string filename) { return cache_get(filename.c_str()); }
std::fstream* CacheManager::cache_get(const char* filename) {
- std::fstream* file = new std::fstream(prefix_cache_path(filename), std::ios::out | std::ios::in);
- this->files.push_back(file);
+ std::filesystem::path fixed_path = prefix_cache_path(filename); // fix duplicate slashes and add prefix
+ std::filesystem::create_directories(fixed_path.parent_path()); // make sure folder exists
+ if (!std::filesystem::exists(fixed_path))
+ std::fstream(fixed_path, std::ios::trunc | std::ios::out).close(); // touch file
+ std::fstream* file = new std::fstream(fixed_path.c_str(), std::ios::out | std::ios::in); // open file
+ this->files.push_back(file); // add file pointer (for closing all files on exit)
return file;
}
+bool CacheManager::cache_exists(std::string filename) { return cache_exists(filename.c_str()); }
bool CacheManager::cache_exists(const char* filename) {
return std::filesystem::exists(prefix_cache_path(filename));
}
+
std::string CacheManager::prefix_cache_path(const char* filename) {
- std::string out = this->cache_path;
- out.append(filename);
- return out;
+ return this->cache_path + "/" + std::string(filename);
}
diff --git a/oop2eindopdr/CacheManager.h b/oop2eindopdr/CacheManager.h
index 7b740ec..e441b1b 100644
--- a/oop2eindopdr/CacheManager.h
+++ b/oop2eindopdr/CacheManager.h
@@ -31,8 +31,10 @@ public:
/** @brief get fstream for file in cache or create file */
virtual std::fstream* cache_get(const char* filename);
+ virtual std::fstream* cache_get(std::string filename);
/** @brief check if file exists in cache */
virtual bool cache_exists(const char* filename);
+ virtual bool cache_exists(std::string filename);
/** @brief currently opened cache update unix timestamp */
uint64_t age;
diff --git a/oop2eindopdr/Pokedex.cpp b/oop2eindopdr/Pokedex.cpp
index 1ffdd20..93764b4 100644
--- a/oop2eindopdr/Pokedex.cpp
+++ b/oop2eindopdr/Pokedex.cpp
@@ -32,16 +32,26 @@ void Pokedex::load_collection_remote() {
}
void Pokedex::load_collection_local() {
+ std::cout << "local cards found, using cache..." << std::endl;
+
std::fstream& cache_collection = *cache->cache_get("index");
- std::string card;
- while (std::getline(cache_collection, card)) {
- this->cards.push_back(new PokemonCard::from_cache(this->cache, card));
+ std::string id;
+ while (std::getline(cache_collection, id)) {
+ this->cards.push_back(new PokemonCard::from_cache(this->cache, id));
}
}
void Pokedex::verify_collection() {
- for (PokemonCard* card : this->cards)
+ 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) {
diff --git a/oop2eindopdr/PokemonCard.cpp b/oop2eindopdr/PokemonCard.cpp
index 69a6e61..96eb7fc 100644
--- a/oop2eindopdr/PokemonCard.cpp
+++ b/oop2eindopdr/PokemonCard.cpp
@@ -7,6 +7,10 @@
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;
@@ -36,11 +40,25 @@ void PokemonCard::set_cache(CacheManager* 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) {
@@ -54,13 +72,14 @@ void PokemonCard::raw_load_json(nlohmann::json raw_data) {
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) {
- 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::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();
diff --git a/oop2eindopdr/PokemonCard.h b/oop2eindopdr/PokemonCard.h
index 873d327..5341866 100644
--- a/oop2eindopdr/PokemonCard.h
+++ b/oop2eindopdr/PokemonCard.h
@@ -16,6 +16,9 @@ private:
CacheManager* cache_ref = nullptr;
+ /** @brief add cache path before filename */
+ virtual std::string prefix_cache_path(const char* filename);
+
public:
PokemonCard(CacheManager* cache_ref = nullptr);
virtual ~PokemonCard();