Mercurial > minori
changeset 177:122fad646f81
anime/db: upgrade to c++17 style, make things easier to read
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Fri, 01 Dec 2023 13:32:29 -0500 |
parents | 121c2d5b321f |
children | bc8d2ccff09c |
files | include/core/anime_db.h src/core/anime_db.cc src/main.cc |
diffstat | 3 files changed, 63 insertions(+), 56 deletions(-) [+] |
line wrap: on
line diff
--- a/include/core/anime_db.h Fri Dec 01 13:12:26 2023 -0500 +++ b/include/core/anime_db.h Fri Dec 01 13:32:29 2023 -0500 @@ -11,23 +11,20 @@ class Database { public: std::unordered_map<int, Anime> items; - int GetTotalAnimeAmount(); - int GetTotalEpisodeAmount(); - int GetTotalWatchedAmount(); - int GetTotalPlannedAmount(); + size_t GetTotalAnimeAmount(); + size_t GetTotalEpisodeAmount(); + size_t GetTotalWatchedAmount(); + size_t GetTotalPlannedAmount(); double GetAverageScore(); double GetScoreDeviation(); - int GetListsAnimeAmount(ListStatus status); + size_t GetListsAnimeAmount(ListStatus status); int GetAnimeFromTitle(const std::string& title); bool GetDatabaseAsJSON(nlohmann::json& json); bool SaveDatabaseToDisk(); bool ParseDatabaseJSON(const nlohmann::json& json); - bool LoadDatabaseFromFile(); - - protected: - bool ParseAnimeInfoJSON(const nlohmann::json& json); + bool LoadDatabaseFromDisk(); }; extern Database db;
--- a/src/core/anime_db.cc Fri Dec 01 13:12:26 2023 -0500 +++ b/src/core/anime_db.cc Fri Dec 01 13:32:29 2023 -0500 @@ -14,46 +14,48 @@ namespace Anime { -int Database::GetTotalAnimeAmount() { - int total = 0; - for (const auto& a : items) { - if (a.second.IsInUserList()) +size_t Database::GetTotalAnimeAmount() { + size_t total = 0; + + for (const auto& [id, anime] : items) + if (anime.IsInUserList()) total++; - } + return total; } -int Database::GetListsAnimeAmount(ListStatus status) { +size_t Database::GetListsAnimeAmount(ListStatus status) { if (status == ListStatus::NOT_IN_LIST) return 0; - int total = 0; - for (const auto& a : items) { - if (a.second.IsInUserList() && a.second.GetUserStatus() == status) + + size_t total = 0; + + for (const auto& [id, anime] : items) + if (anime.IsInUserList() && anime.GetUserStatus() == status) total++; - } + return total; } -int Database::GetTotalEpisodeAmount() { - int total = 0; - for (const auto& a : items) { - if (a.second.IsInUserList()) { - total += a.second.GetUserRewatchedTimes() * a.second.GetEpisodes(); - total += a.second.GetUserProgress(); - } - } +size_t Database::GetTotalEpisodeAmount() { + size_t total = 0; + + for (const auto& [id, anime] : items) + if (anime.IsInUserList()) + total += anime.GetUserRewatchedTimes() * anime.GetEpisodes() + anime.GetUserProgress(); + return total; } /* Returns the total watched amount in minutes. */ -int Database::GetTotalWatchedAmount() { - int total = 0; - for (const auto& a : items) { - if (a.second.IsInUserList()) { - total += a.second.GetDuration() * a.second.GetUserProgress(); - total += a.second.GetEpisodes() * a.second.GetDuration() * a.second.GetUserRewatchedTimes(); - } - } +size_t Database::GetTotalWatchedAmount() { + size_t total = 0; + + for (const auto& [id, anime] : items) + if (anime.IsInUserList()) + total += anime.GetDuration() * anime.GetUserProgress() + + anime.GetEpisodes() * anime.GetDuration() * anime.GetUserRewatchedTimes(); + return total; } @@ -62,24 +64,26 @@ amount of episodes, as AniList will let you set episode counts up to 32768. But that should rather be handled elsewhere. */ -int Database::GetTotalPlannedAmount() { - int total = 0; - for (const auto& a : items) { - if (a.second.IsInUserList()) - total += a.second.GetDuration() * (a.second.GetEpisodes() - a.second.GetUserProgress()); - } +size_t Database::GetTotalPlannedAmount() { + size_t total = 0; + + for (const auto& [id, anime] : items) + if (anime.IsInUserList()) + total += anime.GetDuration() * (anime.GetEpisodes() - anime.GetUserProgress()); + return total; } -/* In Taiga this is called a mean, but "average" is +/* In Taiga this is called the mean, but "average" is what's primarily used in conversation, at least in the U.S. */ double Database::GetAverageScore() { double avg = 0; - int amt = 0; - for (const auto& a : items) { - if (a.second.IsInUserList() && a.second.GetUserScore()) { - avg += a.second.GetUserScore(); + size_t amt = 0; + + for (const auto& [id, anime] : items) { + if (anime.IsInUserList() && anime.GetUserScore()) { + avg += anime.GetUserScore(); amt++; } } @@ -88,17 +92,19 @@ double Database::GetScoreDeviation() { double squares_sum = 0, avg = GetAverageScore(); - int amt = 0; - for (const auto& a : items) { - if (a.second.IsInUserList() && a.second.GetUserScore()) { - squares_sum += std::pow(static_cast<double>(a.second.GetUserScore()) - avg, 2); + size_t amt = 0; + + for (const auto& [id, anime] : items) { + if (anime.IsInUserList() && anime.GetUserScore()) { + squares_sum += std::pow(static_cast<double>(anime.GetUserScore()) - avg, 2); amt++; } } + return (amt > 0) ? std::sqrt(squares_sum / amt) : 0; } -template <typename T, typename U> +template<typename T, typename U> static T get_lowest_in_map(const std::unordered_map<T, U>& map) { if (map.size() <= 0) return 0; @@ -161,6 +167,7 @@ if (!anime.IsInUserList()) return false; + // clang-format off json = { {"status", Translate::ToString(anime.GetUserStatus())}, {"progress", anime.GetUserProgress()}, @@ -173,11 +180,13 @@ {"updated", anime.GetUserTimeUpdated()}, {"notes", anime.GetUserNotes()} }; + // clang-format on return true; } static bool GetAnimeAsJSON(const Anime& anime, nlohmann::json& json) { + // clang-format off json = { {"id", anime.GetId()}, {"title", { @@ -198,6 +207,7 @@ {"duration", anime.GetDuration()}, {"poster_url", anime.GetPosterUrl()} }; + // clang-format on nlohmann::json user; if (GetListDataAsJSON(anime, user)) @@ -250,12 +260,12 @@ return true; } -bool Database::ParseAnimeInfoJSON(const nlohmann::json& json) { +static bool ParseAnimeInfoJSON(const nlohmann::json& json, Database& database) { int id = JSON::GetNumber(json, "/id"_json_pointer, 0); if (!id) return false; - Anime& anime = items[id]; + Anime& anime = database.items[id]; anime.SetId(id); anime.SetNativeTitle(JSON::GetString<std::string>(json, "/title/native"_json_pointer, "")); @@ -282,12 +292,12 @@ bool Database::ParseDatabaseJSON(const nlohmann::json& json) { for (const auto& anime_json : json) - ParseAnimeInfoJSON(anime_json); + ParseAnimeInfoJSON(anime_json, *this); return true; } -bool Database::LoadDatabaseFromFile() { +bool Database::LoadDatabaseFromDisk() { std::filesystem::path db_path = Filesystem::GetAnimeDBPath(); Filesystem::CreateDirectories(db_path);