annotate src/anime.cpp @ 7:07a9095eaeed

Update Refactored some code, moved some around
author Paper <mrpapersonic@gmail.com>
date Thu, 24 Aug 2023 23:11:38 -0400
parents 1d82f6e04d7d
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
1 /*
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
2 * anime.cpp: defining of custom anime-related
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
3 * datatypes & variables
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
4 */
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
5 #include <chrono>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
6 #include <string>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
7 #include <vector>
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
8 #include <cmath>
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
9 #include <algorithm>
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
10 #include "anilist.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
11 #include "anime.h"
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
12 #include "date.h"
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
13 #include "session.h"
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
14
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
15 std::map<enum AnimeWatchingStatus, std::string> AnimeWatchingToStringMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
16 {CURRENT, "Watching"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
17 {PLANNING, "Planning"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
18 {COMPLETED, "Completed"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
19 {DROPPED, "Dropped"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
20 {PAUSED, "On hold"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
21 {REPEATING, "Rewatching"}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
22 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
23
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
24 std::map<enum AnimeAiringStatus, std::string> AnimeAiringToStringMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
25 {FINISHED, "Finished"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
26 {RELEASING, "Airing"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
27 {NOT_YET_RELEASED, "Not aired yet"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
28 {CANCELLED, "Cancelled"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
29 {HIATUS, "On hiatus"}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
30 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
31
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
32 std::map<enum AnimeSeason, std::string> AnimeSeasonToStringMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
33 {UNKNOWN, "Unknown"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
34 {WINTER, "Winter"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
35 {SPRING, "Spring"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
36 {SUMMER, "Summer"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
37 {FALL, "Fall"}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
38 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
39
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
40 std::map<enum AnimeFormat, std::string> AnimeFormatToStringMap = {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
41 {TV, "TV"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
42 {TV_SHORT, "TV short"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
43 {MOVIE, "Movie"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
44 {SPECIAL, "Special"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
45 {OVA, "OVA"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
46 {ONA, "ONA"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
47 {MUSIC, "Music video"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
48 /* these should NEVER be in the list. naybe we should
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
49 remove them? */
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
50 {MANGA, "Manga"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
51 {NOVEL, "Novel"},
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
52 {ONE_SHOT, "One-shot"}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
53 };
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
54
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
55 Anime::Anime() {}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
56 Anime::Anime(const Anime& a) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
57 status = a.status;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
58 progress = a.progress;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
59 score = a.score;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
60 started = a.started;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
61 completed = a.completed;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
62 updated = a.updated;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
63 notes = a.notes;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
64 id = a.id;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
65 title = a.title;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
66 episodes = a.episodes;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
67 airing = a.airing;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
68 air_date = a.air_date;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
69 genres = a.genres;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
70 producers = a.producers;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
71 type = a.type;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
72 season = a.season;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
73 audience_score = a.audience_score;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
74 synopsis = a.synopsis;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
75 duration = a.duration;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
76 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
77
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 3
diff changeset
78 std::string Anime::GetUserPreferredTitle() {
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
79 switch (session.config.anime_list.language) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
80 case NATIVE:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
81 return (title.native.empty()) ? title.romaji : title.native;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
82 case ENGLISH:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
83 return (title.english.empty()) ? title.romaji : title.english;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
84 default:
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
85 return title.romaji;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
86 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
87 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
88
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
89 std::vector<std::string> Anime::GetTitleSynonyms() {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
90 std::vector<std::string> result;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
91 #define IN_VECTOR(v, k) \
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
92 (std::count(v.begin(), v.end(), k))
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
93 #define ADD_TO_SYNONYMS(v, k) \
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
94 if (!k.empty() && !IN_VECTOR(v, k) && k != GetUserPreferredTitle()) v.push_back(k)
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
95 ADD_TO_SYNONYMS(result, title.english);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
96 ADD_TO_SYNONYMS(result, title.romaji);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
97 ADD_TO_SYNONYMS(result, title.native);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
98 for (auto& synonym : synonyms) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
99 ADD_TO_SYNONYMS(result, synonym);
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
100 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
101 #undef ADD_TO_SYNONYMS
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
102 #undef IN_VECTOR
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
103 return result;
6
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 3
diff changeset
104 }
1d82f6e04d7d Update: add first parts to the settings dialog
Paper <mrpapersonic@gmail.com>
parents: 3
diff changeset
105
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
106 void AnimeList::Add(Anime& anime) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
107 if (anime_id_to_anime.contains(anime.id))
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
108 return;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
109 anime_list.push_back(anime);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
110 anime_id_to_anime.emplace(anime.id, &anime);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
111 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
112
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
113 void AnimeList::Insert(size_t pos, Anime& anime) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
114 if (anime_id_to_anime.contains(anime.id))
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
115 return;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
116 anime_list.insert(anime_list.begin()+pos, anime);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
117 anime_id_to_anime.emplace(anime.id, &anime);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
118 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
119
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
120 void AnimeList::Delete(size_t index) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
121 anime_list.erase(anime_list.begin()+index);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
122 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
123
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
124 void AnimeList::Clear() {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
125 anime_list.clear();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
126 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
127
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
128 size_t AnimeList::Size() const {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
129 return anime_list.size();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
130 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
131
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
132 std::vector<Anime>::iterator AnimeList::begin() noexcept {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
133 return anime_list.begin();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
134 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
135
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
136 std::vector<Anime>::iterator AnimeList::end() noexcept {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
137 return anime_list.end();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
138 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
139
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
140 std::vector<Anime>::const_iterator AnimeList::cbegin() noexcept {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
141 return anime_list.cbegin();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
142 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
143
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
144 std::vector<Anime>::const_iterator AnimeList::cend() noexcept {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
145 return anime_list.cend();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
146 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
147
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
148 AnimeList::AnimeList() {}
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
149 AnimeList::AnimeList(const AnimeList& l) {
3
190ded9438c0 Fix many warnings
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
150 for (unsigned long long i = 0; i < l.Size(); i++) {
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
151 anime_list.push_back(Anime(l[i]));
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
152 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
153 name = l.name;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
154 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
155
7
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
156 AnimeList& AnimeList::operator=(const AnimeList& l) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
157 if (this != &l) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
158 for (unsigned long long i = 0; i < l.Size(); i++) {
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
159 this->anime_list.push_back(Anime(l[i]));
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
160 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
161 this->name = l.name;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
162 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
163 return *this;
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
164 }
Paper <mrpapersonic@gmail.com>
parents: 6
diff changeset
165
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
166 AnimeList::~AnimeList() {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
167 anime_list.clear();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
168 anime_list.shrink_to_fit();
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
169 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
170
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
171 Anime* AnimeList::AnimeById(int id) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
172 return anime_id_to_anime.contains(id) ? anime_id_to_anime[id] : nullptr;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
173 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
174
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
175 bool AnimeList::AnimeInList(int id) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
176 return anime_id_to_anime.contains(id);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
177 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
178
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
179 int AnimeList::GetAnimeIndex(Anime& anime) const {
3
190ded9438c0 Fix many warnings
Paper <mrpapersonic@gmail.com>
parents: 2
diff changeset
180 for (unsigned long long i = 0; i < Size(); i++) {
2
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
181 if (&anime_list.at(i) == &anime) { // lazy
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
182 return i;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
183 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
184 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
185 return -1;
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
186 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
187
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
188 Anime& AnimeList::operator[](std::size_t index) {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
189 return anime_list.at(index);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
190 }
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
191
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
192 const Anime& AnimeList::operator[](std::size_t index) const {
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
193 return anime_list.at(index);
Paper <mrpapersonic@gmail.com>
parents: 1
diff changeset
194 }