10
+ − 1 #include "core/anime_db.h"
+ − 2 #include "core/anime.h"
64
+ − 3 #include "core/strings.h"
+ − 4 #include <QDebug>
10
+ − 5
+ − 6 namespace Anime {
+ − 7
+ − 8 int Database::GetTotalAnimeAmount() {
+ − 9 int total = 0;
11
+ − 10 for (const auto& a : items) {
+ − 11 if (a.second.IsInUserList())
10
+ − 12 total++;
+ − 13 }
+ − 14 return total;
+ − 15 }
+ − 16
+ − 17 int Database::GetListsAnimeAmount(ListStatus status) {
+ − 18 if (status == ListStatus::NOT_IN_LIST)
+ − 19 return 0;
+ − 20 int total = 0;
11
+ − 21 for (const auto& a : items) {
+ − 22 if (a.second.IsInUserList() && a.second.GetUserStatus() == status)
10
+ − 23 total++;
+ − 24 }
+ − 25 return total;
+ − 26 }
+ − 27
+ − 28 int Database::GetTotalEpisodeAmount() {
+ − 29 int total = 0;
11
+ − 30 for (const auto& a : items) {
+ − 31 if (a.second.IsInUserList()) {
+ − 32 total += a.second.GetUserRewatchedTimes() * a.second.GetEpisodes();
+ − 33 total += a.second.GetUserProgress();
10
+ − 34 }
+ − 35 }
+ − 36 return total;
+ − 37 }
+ − 38
+ − 39 /* Returns the total watched amount in minutes. */
+ − 40 int Database::GetTotalWatchedAmount() {
+ − 41 int total = 0;
11
+ − 42 for (const auto& a : items) {
+ − 43 if (a.second.IsInUserList()) {
+ − 44 total += a.second.GetDuration() * a.second.GetUserProgress();
+ − 45 total += a.second.GetEpisodes() * a.second.GetDuration() * a.second.GetUserRewatchedTimes();
10
+ − 46 }
+ − 47 }
+ − 48 return total;
+ − 49 }
+ − 50
+ − 51 /* Returns the total planned amount in minutes.
+ − 52 Note that we should probably limit progress to the
+ − 53 amount of episodes, as AniList will let you
+ − 54 set episode counts up to 32768. But that should
+ − 55 rather be handled elsewhere. */
+ − 56 int Database::GetTotalPlannedAmount() {
+ − 57 int total = 0;
11
+ − 58 for (const auto& a : items) {
+ − 59 if (a.second.IsInUserList())
+ − 60 total += a.second.GetDuration() * (a.second.GetEpisodes() - a.second.GetUserProgress());
10
+ − 61 }
+ − 62 return total;
+ − 63 }
+ − 64
+ − 65 /* I'm sure many will appreciate this being called an
+ − 66 "average" instead of a "mean" */
+ − 67 double Database::GetAverageScore() {
+ − 68 double avg = 0;
+ − 69 int amt = 0;
11
+ − 70 for (const auto& a : items) {
+ − 71 if (a.second.IsInUserList() && a.second.GetUserScore()) {
+ − 72 avg += a.second.GetUserScore();
10
+ − 73 amt++;
+ − 74 }
+ − 75 }
+ − 76 return avg / amt;
+ − 77 }
+ − 78
+ − 79 double Database::GetScoreDeviation() {
+ − 80 double squares_sum = 0, avg = GetAverageScore();
+ − 81 int amt = 0;
11
+ − 82 for (const auto& a : items) {
+ − 83 if (a.second.IsInUserList() && a.second.GetUserScore()) {
64
+ − 84 squares_sum += std::pow(static_cast<double>(a.second.GetUserScore()) - avg, 2);
10
+ − 85 amt++;
+ − 86 }
+ − 87 }
+ − 88 return (amt > 0) ? std::sqrt(squares_sum / amt) : 0;
+ − 89 }
+ − 90
64
+ − 91 int Database::GetAnimeFromTitle(std::string title) {
+ − 92 if (title.empty())
+ − 93 return 0;
+ − 94 for (const auto& a : items) {
+ − 95 if (a.second.GetUserPreferredTitle().find(title) != std::string::npos)
+ − 96 return a.second.GetId();
+ − 97 for (const auto& t : a.second.GetTitleSynonyms()) {
+ − 98 if (t.find(title) != std::string::npos) {
+ − 99 return a.second.GetId();
+ − 100 }
+ − 101 }
+ − 102 }
+ − 103 return 0;
+ − 104 }
+ − 105
11
+ − 106 Database db;
+ − 107
9
+ − 108 } // namespace Anime