diff src/core/anime_db.cc @ 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 9613d72b097e
line wrap: on
line diff
--- 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);