annotate src/core/anime_db.cpp @ 68:2417121d894e

*: normalize usage of layouts before, I used them two ways, once was by setting the layout later by using setLayout(QWidget), and the other was just using the constructor. I find the constructor to be easier to read, so I chose that one.
author Paper <mrpapersonic@gmail.com>
date Mon, 02 Oct 2023 21:33:25 -0400
parents fe719c109dbc
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
1 #include "core/anime_db.h"
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
2 #include "core/anime.h"
64
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
3 #include "core/strings.h"
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
4 #include <QDebug>
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
5
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
6 namespace Anime {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
7
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
8 int Database::GetTotalAnimeAmount() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
9 int total = 0;
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
10 for (const auto& a : items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
11 if (a.second.IsInUserList())
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
12 total++;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
13 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
14 return total;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
15 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
16
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
17 int Database::GetListsAnimeAmount(ListStatus status) {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
18 if (status == ListStatus::NOT_IN_LIST)
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
19 return 0;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
20 int total = 0;
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
21 for (const auto& a : items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
22 if (a.second.IsInUserList() && a.second.GetUserStatus() == status)
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
23 total++;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
24 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
25 return total;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
26 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
27
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
28 int Database::GetTotalEpisodeAmount() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
29 int total = 0;
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
30 for (const auto& a : items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
31 if (a.second.IsInUserList()) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
32 total += a.second.GetUserRewatchedTimes() * a.second.GetEpisodes();
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
33 total += a.second.GetUserProgress();
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
34 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
35 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
36 return total;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
37 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
38
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
39 /* Returns the total watched amount in minutes. */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
40 int Database::GetTotalWatchedAmount() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
41 int total = 0;
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
42 for (const auto& a : items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
43 if (a.second.IsInUserList()) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
44 total += a.second.GetDuration() * a.second.GetUserProgress();
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
45 total += a.second.GetEpisodes() * a.second.GetDuration() * a.second.GetUserRewatchedTimes();
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
46 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
47 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
48 return total;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
49 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
50
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
51 /* Returns the total planned amount in minutes.
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
52 Note that we should probably limit progress to the
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
53 amount of episodes, as AniList will let you
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
54 set episode counts up to 32768. But that should
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
55 rather be handled elsewhere. */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
56 int Database::GetTotalPlannedAmount() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
57 int total = 0;
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
58 for (const auto& a : items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
59 if (a.second.IsInUserList())
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
60 total += a.second.GetDuration() * (a.second.GetEpisodes() - a.second.GetUserProgress());
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
61 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
62 return total;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
63 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
64
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
65 /* I'm sure many will appreciate this being called an
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
66 "average" instead of a "mean" */
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
67 double Database::GetAverageScore() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
68 double avg = 0;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
69 int amt = 0;
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
70 for (const auto& a : items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
71 if (a.second.IsInUserList() && a.second.GetUserScore()) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
72 avg += a.second.GetUserScore();
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
73 amt++;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
74 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
75 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
76 return avg / amt;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
77 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
78
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
79 double Database::GetScoreDeviation() {
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
80 double squares_sum = 0, avg = GetAverageScore();
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
81 int amt = 0;
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
82 for (const auto& a : items) {
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
83 if (a.second.IsInUserList() && a.second.GetUserScore()) {
64
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
84 squares_sum += std::pow(static_cast<double>(a.second.GetUserScore()) - avg, 2);
10
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
85 amt++;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
86 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
87 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
88 return (amt > 0) ? std::sqrt(squares_sum / amt) : 0;
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
89 }
Paper <mrpapersonic@gmail.com>
parents: 9
diff changeset
90
64
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
91 int Database::GetAnimeFromTitle(std::string title) {
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
92 if (title.empty())
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
93 return 0;
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
94 for (const auto& a : items) {
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
95 if (a.second.GetUserPreferredTitle().find(title) != std::string::npos)
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
96 return a.second.GetId();
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
97 for (const auto& t : a.second.GetTitleSynonyms()) {
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
98 if (t.find(title) != std::string::npos) {
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
99 return a.second.GetId();
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
100 }
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
101 }
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
102 }
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
103 return 0;
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
104 }
fe719c109dbc *: update
Paper <mrpapersonic@gmail.com>
parents: 11
diff changeset
105
11
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
106 Database db;
fc1bf97c528b *: use C++11 standard
Paper <mrpapersonic@gmail.com>
parents: 10
diff changeset
107
9
5c0397762b53 INCOMPLETE: megacommit :)
Paper <mrpapersonic@gmail.com>
parents:
diff changeset
108 } // namespace Anime