diff options
-rw-r--r-- | oop2eindopdr/CacheManager.cpp | 10 | ||||
-rw-r--r-- | oop2eindopdr/CacheManager.h | 9 | ||||
-rw-r--r-- | oop2eindopdr/Pokedex.cpp | 17 | ||||
-rw-r--r-- | oop2eindopdr/Pokedex.h | 4 | ||||
-rw-r--r-- | oop2eindopdr/PokemonCard.cpp | 2 | ||||
-rw-r--r-- | oop2eindopdr/PokemonCard.h | 22 | ||||
-rw-r--r-- | oop2eindopdr/main.cpp | 1 | ||||
-rw-r--r-- | oop2eindopdr/readme.md | 14 |
8 files changed, 64 insertions, 15 deletions
diff --git a/oop2eindopdr/CacheManager.cpp b/oop2eindopdr/CacheManager.cpp index 6fffa2c..fad7b65 100644 --- a/oop2eindopdr/CacheManager.cpp +++ b/oop2eindopdr/CacheManager.cpp @@ -38,11 +38,17 @@ void CacheManager::update_cache() { } std::fstream* CacheManager::cache_get(const char* filename) { - std::fstream* file = new std::fstream(filename, std::ios::out | std::ios::in); + std::fstream* file = new std::fstream(prefix_cache_path(filename), std::ios::out | std::ios::in); this->files.push_back(file); return file; } bool CacheManager::cache_exists(const char* filename) { - return true; + 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; } diff --git a/oop2eindopdr/CacheManager.h b/oop2eindopdr/CacheManager.h index 9587e24..7b740ec 100644 --- a/oop2eindopdr/CacheManager.h +++ b/oop2eindopdr/CacheManager.h @@ -6,8 +6,12 @@ /** @brief cache storage manager */ class CacheManager { private: - std::string cache_path; /** @brief currently opened cache location */ + /** @brief currently opened cache location */ + std::string cache_path; + /** @brief pointers to open file handles */ std::vector<std::fstream*> files; + /** @brief add cache path before filename */ + virtual std::string prefix_cache_path(const char* filename); public: /** @brief initialize CacheManager class at `cache_path` */ CacheManager(const char* cache_path); @@ -21,8 +25,7 @@ public: /** * @brief check cache file structure * - * automatically updates chache if stale and/or creates cache when - * non-existant + * automatically creates cache when non-existant */ virtual void verify_cache(); diff --git a/oop2eindopdr/Pokedex.cpp b/oop2eindopdr/Pokedex.cpp index 67bc2dc..24de74b 100644 --- a/oop2eindopdr/Pokedex.cpp +++ b/oop2eindopdr/Pokedex.cpp @@ -1,11 +1,15 @@ +#include <fstream> + +#include "CacheManager.h" #include "Pokedex.h" Pokedex::Pokedex() { - + this->cache = new CacheManager("./cache"); + this->load_collection_local(); } Pokedex::~Pokedex() { - + delete this->cache; } void Pokedex::load_collection_remote() { @@ -13,11 +17,16 @@ void Pokedex::load_collection_remote() { } void Pokedex::load_collection_local() { - + 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(cache, card)); + } } void Pokedex::verify_collection() { - + for (PokemonCard* card : this->cards) + card->verify_files(); } std::vector<PokemonCard*> Pokedex::search_cards_by_id(std::string query) { diff --git a/oop2eindopdr/Pokedex.h b/oop2eindopdr/Pokedex.h index 91a0315..5e20367 100644 --- a/oop2eindopdr/Pokedex.h +++ b/oop2eindopdr/Pokedex.h @@ -3,6 +3,7 @@ #include <string> #include <vector> +#include "CacheManager.h" #include "PokemonCard.h" /** @brief user pokedex class, handles caching and api access silently */ @@ -18,6 +19,9 @@ private: /** @brief verify all cards in collection */ virtual void verify_collection(); + /** @brief cache connection */ + CacheManager* cache = nullptr; + public: Pokedex(); virtual ~Pokedex(); diff --git a/oop2eindopdr/PokemonCard.cpp b/oop2eindopdr/PokemonCard.cpp index 3d20eff..9c4b8a7 100644 --- a/oop2eindopdr/PokemonCard.cpp +++ b/oop2eindopdr/PokemonCard.cpp @@ -17,7 +17,7 @@ std::ostream& operator << (std::ostream& output, const PokemonCard& card) { return output; } -PokemonCard::PokemonCard() { +PokemonCard::PokemonCard(CacheManager* cache_ref) { } diff --git a/oop2eindopdr/PokemonCard.h b/oop2eindopdr/PokemonCard.h index 1b9721a..a19ef60 100644 --- a/oop2eindopdr/PokemonCard.h +++ b/oop2eindopdr/PokemonCard.h @@ -4,13 +4,18 @@ #include <string> #include <vector> +#include "CacheManager.h" + /** @brief single pokemon card */ class PokemonCard { private: nlohmann::json raw_data; + virtual void raw_load_json(nlohmann::json raw_data); + virtual void raw_load_cache(const char* cache_path); + public: - PokemonCard(); + PokemonCard(CacheManager* cache_ref); virtual ~PokemonCard(); /** @brief string stream output (for printing card) */ @@ -30,7 +35,18 @@ public: /** @brief download images */ virtual void download_files(); - static PokemonCard* from_json(nlohmann::json raw_data); - static PokemonCard* from_cache(const char* card_path); + struct from_json; // named constructors (defined below) + struct from_cache; }; +struct PokemonCard::from_json : public PokemonCard { + from_json(CacheManager* cache_ref, nlohmann::json json_data) : PokemonCard(cache_ref) { + PokemonCard::raw_load_json(json_data); + } +}; + +struct PokemonCard::from_cache : public PokemonCard { + from_cache(CacheManager* cache_ref, std::string cache_path) : PokemonCard(cache_ref) { + PokemonCard::raw_load_cache(cache_path.c_str()); + } +}; diff --git a/oop2eindopdr/main.cpp b/oop2eindopdr/main.cpp index 0ebc185..4a1e8c4 100644 --- a/oop2eindopdr/main.cpp +++ b/oop2eindopdr/main.cpp @@ -3,7 +3,6 @@ #include "main.h" #include "Pokedex.h" -#include "PokemonTCGAPIClient.h" using std::endl; using std::cout; diff --git a/oop2eindopdr/readme.md b/oop2eindopdr/readme.md index e3c75d9..35d4f1b 100644 --- a/oop2eindopdr/readme.md +++ b/oop2eindopdr/readme.md @@ -56,18 +56,30 @@ cache/ info.json card.png card_hires.png + complete swshp-SWSH002/ info.json card.png card_hires.png + complete swshp-SWSH..../ ``` - `date` bevat een unix timestamp wanneer de cache voor het laatst geupdate is - (gebruikt om te checken of de cache stale is) + (gebruikt om te checken of de cache stale is) (misschien overbodig) - de mappen direct onder de cache map zijn de volledige id's van de pokemon - `index` bevat op losse regels de namen van de mappen met pokemon info - `info.json` bevat voor elke kaart de json api response voor die kaart +- `complete` is een leeg bestand die bijhoudt of een kaart 'compleet' is. dit + houdt in dat alle bestanden in een kaartmap compleet gedownload zijn, + geschreven zijn en de bestanden weer netjes gesloten zijn. + +de cache wordt bij initialisatie gevuld met de `swshp` set aan kaarten (wordt +opgevraagd aan de API). wanneer een kaart gezocht wordt die niet bekend is bij +de Pokedex klasse zal deze automatisch terugvallen op de API om tussen alle +kaarten te zoeken. als de API de kaart wel kan vinden zal deze automatisch +geindexeerd worden in `index` en de bijbehorende bestanden zullen in een losse +map gedownload worden. ## zip formaat (voorbeeld) |