Mercurial > minori
annotate src/core/anime.cc @ 276:ec0a2b5493f8
ini: simplify INI code, use templates less
less magic voodoo code
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Mon, 22 Apr 2024 19:10:28 -0400 |
parents | 22f9aacf6ac1 |
children | 9b6e12c14a1e |
rev | line source |
---|---|
9 | 1 /* |
2 * anime.cpp: defining of custom anime-related | |
3 * datatypes & variables | |
4 */ | |
5 #include "core/anime.h" | |
6 #include "core/date.h" | |
7 #include "core/session.h" | |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
195
diff
changeset
|
8 #include "core/strings.h" |
178
bc8d2ccff09c
win32/dark: use existing STL classes for dwmapi
Paper <mrpapersonic@gmail.com>
parents:
81
diff
changeset
|
9 |
9 | 10 #include <algorithm> |
11 #include <string> | |
12 #include <vector> | |
275
22f9aacf6ac1
osx: don't dereference NULL pointers
Paper <paper@paper.us.eu.org>
parents:
265
diff
changeset
|
13 #include <cassert> |
9 | 14 |
15 namespace Anime { | |
16 | |
17 /* User list data */ | |
18 bool Anime::IsInUserList() const { | |
264 | 19 if (list_info_.has_value()) |
9 | 20 return true; |
21 return false; | |
22 } | |
23 | |
24 void Anime::AddToUserList() { | |
265
ff0b2052b234
*: add missing utf8proc files
Paper <paper@paper.us.eu.org>
parents:
264
diff
changeset
|
25 ListInformation list; |
264 | 26 list_info_.emplace(list); |
9 | 27 } |
28 | |
29 void Anime::RemoveFromUserList() { | |
30 list_info_.reset(); | |
31 } | |
32 | |
33 ListStatus Anime::GetUserStatus() const { | |
264 | 34 assert(list_info_.has_value()); |
9 | 35 return list_info_->status; |
36 } | |
37 | |
38 int Anime::GetUserProgress() const { | |
264 | 39 assert(list_info_.has_value()); |
9 | 40 return list_info_->progress; |
41 } | |
42 | |
43 int Anime::GetUserScore() const { | |
264 | 44 assert(list_info_.has_value()); |
9 | 45 return list_info_->score; |
46 } | |
47 | |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
48 std::string Anime::GetUserPresentableScore() const { |
264 | 49 assert(list_info_.has_value()); |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
50 const int score = list_info_->score; |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
51 if (score == 0) |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
52 return ""; |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
53 |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
54 switch (session.config.anime_list.score_format) { |
186
6ef31dbb90ca
anime: no unnecessary conversion to floating point
Paper <mrpapersonic@gmail.com>
parents:
185
diff
changeset
|
55 case ScoreFormat::POINT_10_DECIMAL: |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
195
diff
changeset
|
56 return Strings::ToUtf8String(score / 10) + "." + Strings::ToUtf8String(score % 10); |
258 | 57 case ScoreFormat::POINT_10: return Strings::ToUtf8String(score / 10); |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
58 case ScoreFormat::POINT_5: { |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
59 std::string stars = ""; |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
60 |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
61 for (int i = 0; i < 100; i += 20) |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
62 stars.append((i <= score) ? "★" : "☆"); |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
63 |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
64 return stars; |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
65 } |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
66 case ScoreFormat::POINT_3: { |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
67 if (score >= 100) |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
68 return ":)"; |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
69 else if (score >= 66) |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
70 return ":|"; |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
71 else if (score >= 33) |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
72 return ":("; |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
73 else |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
74 return ""; |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
75 } |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
76 default: |
258 | 77 case ScoreFormat::POINT_100: return Strings::ToUtf8String(score); |
185
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
78 } |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
79 } |
62e336597bb7
anime list: add support for different score formats
Paper <mrpapersonic@gmail.com>
parents:
183
diff
changeset
|
80 |
9 | 81 Date Anime::GetUserDateStarted() const { |
264 | 82 assert(list_info_.has_value()); |
9 | 83 return list_info_->started; |
84 } | |
85 | |
86 Date Anime::GetUserDateCompleted() const { | |
264 | 87 assert(list_info_.has_value()); |
9 | 88 return list_info_->completed; |
89 } | |
90 | |
91 bool Anime::GetUserIsPrivate() const { | |
264 | 92 assert(list_info_.has_value()); |
9 | 93 return list_info_->is_private; |
94 } | |
95 | |
96 unsigned int Anime::GetUserRewatchedTimes() const { | |
264 | 97 assert(list_info_.has_value()); |
9 | 98 return list_info_->rewatched_times; |
99 } | |
100 | |
101 bool Anime::GetUserIsRewatching() const { | |
264 | 102 assert(list_info_.has_value()); |
9 | 103 return list_info_->rewatching; |
104 } | |
105 | |
106 uint64_t Anime::GetUserTimeUpdated() const { | |
264 | 107 assert(list_info_.has_value()); |
9 | 108 return list_info_->updated; |
109 } | |
110 | |
111 std::string Anime::GetUserNotes() const { | |
264 | 112 assert(list_info_.has_value()); |
9 | 113 return list_info_->notes; |
114 } | |
115 | |
116 void Anime::SetUserStatus(ListStatus status) { | |
264 | 117 assert(list_info_.has_value()); |
9 | 118 list_info_->status = status; |
119 } | |
120 | |
10 | 121 void Anime::SetUserScore(int score) { |
264 | 122 assert(list_info_.has_value()); |
10 | 123 list_info_->score = score; |
124 } | |
125 | |
9 | 126 void Anime::SetUserProgress(int progress) { |
264 | 127 assert(list_info_.has_value()); |
9 | 128 list_info_->progress = progress; |
129 } | |
130 | |
131 void Anime::SetUserDateStarted(Date const& started) { | |
264 | 132 assert(list_info_.has_value()); |
9 | 133 list_info_->started = started; |
134 } | |
135 | |
136 void Anime::SetUserDateCompleted(Date const& completed) { | |
264 | 137 assert(list_info_.has_value()); |
9 | 138 list_info_->completed = completed; |
139 } | |
140 | |
141 void Anime::SetUserIsPrivate(bool is_private) { | |
264 | 142 assert(list_info_.has_value()); |
9 | 143 list_info_->is_private = is_private; |
144 } | |
145 | |
146 void Anime::SetUserRewatchedTimes(int rewatched) { | |
264 | 147 assert(list_info_.has_value()); |
9 | 148 list_info_->rewatched_times = rewatched; |
149 } | |
150 | |
151 void Anime::SetUserIsRewatching(bool rewatching) { | |
264 | 152 assert(list_info_.has_value()); |
9 | 153 list_info_->rewatching = rewatching; |
154 } | |
155 | |
156 void Anime::SetUserTimeUpdated(uint64_t updated) { | |
264 | 157 assert(list_info_.has_value()); |
9 | 158 list_info_->updated = updated; |
159 } | |
160 | |
161 void Anime::SetUserNotes(std::string const& notes) { | |
264 | 162 assert(list_info_.has_value()); |
9 | 163 list_info_->notes = notes; |
164 } | |
165 | |
166 /* Series data */ | |
167 int Anime::GetId() const { | |
168 return info_.id; | |
169 } | |
170 | |
171 std::string Anime::GetRomajiTitle() const { | |
172 return info_.title.romaji; | |
173 } | |
174 | |
175 std::string Anime::GetEnglishTitle() const { | |
176 return info_.title.english; | |
177 } | |
178 | |
179 std::string Anime::GetNativeTitle() const { | |
180 return info_.title.native; | |
181 } | |
182 | |
183 std::vector<std::string> Anime::GetTitleSynonyms() const { | |
184 std::vector<std::string> result; | |
195
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
185 |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
186 auto add_to_synonyms = [this](std::vector<std::string>& vec, std::string key) { |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
187 if (!key.empty() && !std::count(vec.begin(), vec.end(), key) && key != GetUserPreferredTitle()) |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
188 vec.push_back(key); |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
189 }; |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
190 |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
191 add_to_synonyms(result, info_.title.english); |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
192 add_to_synonyms(result, info_.title.romaji); |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
193 add_to_synonyms(result, info_.title.native); |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
194 |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
195 for (auto& synonym : info_.synonyms) |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
196 add_to_synonyms(result, synonym); |
975a3f0965e2
locale: only attempt loading locales after QApplication is init'd
Paper <mrpapersonic@gmail.com>
parents:
186
diff
changeset
|
197 |
9 | 198 return result; |
199 } | |
200 | |
201 int Anime::GetEpisodes() const { | |
202 return info_.episodes; | |
203 } | |
204 | |
205 SeriesStatus Anime::GetAiringStatus() const { | |
206 return info_.status; | |
207 } | |
208 | |
209 Date Anime::GetAirDate() const { | |
210 return info_.air_date; | |
211 } | |
212 | |
213 std::vector<std::string> Anime::GetGenres() const { | |
214 return info_.genres; | |
215 } | |
216 | |
217 std::vector<std::string> Anime::GetProducers() const { | |
218 return info_.producers; | |
219 } | |
220 | |
221 SeriesFormat Anime::GetFormat() const { | |
222 return info_.format; | |
223 } | |
224 | |
225 SeriesSeason Anime::GetSeason() const { | |
226 return info_.season; | |
227 } | |
228 | |
229 int Anime::GetAudienceScore() const { | |
230 return info_.audience_score; | |
231 } | |
232 | |
233 std::string Anime::GetSynopsis() const { | |
234 return info_.synopsis; | |
235 } | |
236 | |
237 int Anime::GetDuration() const { | |
238 return info_.duration; | |
239 } | |
240 | |
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
241 std::string Anime::GetPosterUrl() const { |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
242 return info_.poster_url; |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
243 } |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
244 |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
245 std::string Anime::GetServiceUrl() const { |
183
01d259b9c89f
pages/torrents.cc: parse feed descriptions separately
Paper <mrpapersonic@gmail.com>
parents:
178
diff
changeset
|
246 /* todo: add support for other services... */ |
211
7cf53145de11
strings: use templates for ToInt, std::to_string -> Strings::ToUtf8String
Paper <mrpapersonic@gmail.com>
parents:
195
diff
changeset
|
247 return "https://anilist.co/anime/" + Strings::ToUtf8String(GetId()); |
67
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
248 } |
442065432549
poster: make posters link to AniList
Paper <mrpapersonic@gmail.com>
parents:
66
diff
changeset
|
249 |
9 | 250 void Anime::SetId(int id) { |
251 info_.id = id; | |
252 } | |
253 | |
254 void Anime::SetRomajiTitle(std::string const& title) { | |
255 info_.title.romaji = title; | |
256 } | |
257 | |
258 void Anime::SetEnglishTitle(std::string const& title) { | |
259 info_.title.english = title; | |
260 } | |
261 | |
262 void Anime::SetNativeTitle(std::string const& title) { | |
263 info_.title.native = title; | |
264 } | |
265 | |
266 void Anime::SetTitleSynonyms(std::vector<std::string> const& synonyms) { | |
267 info_.synonyms = synonyms; | |
268 } | |
269 | |
270 void Anime::AddTitleSynonym(std::string const& synonym) { | |
271 info_.synonyms.push_back(synonym); | |
272 } | |
273 | |
274 void Anime::SetEpisodes(int episodes) { | |
275 info_.episodes = episodes; | |
276 } | |
277 | |
278 void Anime::SetAiringStatus(SeriesStatus status) { | |
279 info_.status = status; | |
280 } | |
281 | |
282 void Anime::SetAirDate(Date const& date) { | |
283 info_.air_date = date; | |
284 } | |
285 | |
286 void Anime::SetGenres(std::vector<std::string> const& genres) { | |
287 info_.genres = genres; | |
288 } | |
289 | |
290 void Anime::SetProducers(std::vector<std::string> const& producers) { | |
291 info_.producers = producers; | |
292 } | |
293 | |
294 void Anime::SetFormat(SeriesFormat format) { | |
295 info_.format = format; | |
296 } | |
297 | |
298 void Anime::SetSeason(SeriesSeason season) { | |
299 info_.season = season; | |
300 } | |
301 | |
302 void Anime::SetAudienceScore(int audience_score) { | |
303 info_.audience_score = audience_score; | |
304 } | |
305 | |
306 void Anime::SetSynopsis(std::string synopsis) { | |
307 info_.synopsis = synopsis; | |
308 } | |
309 | |
310 void Anime::SetDuration(int duration) { | |
311 info_.duration = duration; | |
312 } | |
313 | |
66
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
314 void Anime::SetPosterUrl(std::string url) { |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
315 info_.poster_url = url; |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
316 } |
6481c5aed3e1
posters: add poster widget...
Paper <mrpapersonic@gmail.com>
parents:
36
diff
changeset
|
317 |
9 | 318 std::string Anime::GetUserPreferredTitle() const { |
319 switch (session.config.anime_list.language) { | |
320 case TitleLanguage::NATIVE: return (GetNativeTitle().empty()) ? GetRomajiTitle() : GetNativeTitle(); | |
321 case TitleLanguage::ENGLISH: return (GetEnglishTitle().empty()) ? GetRomajiTitle() : GetEnglishTitle(); | |
322 default: break; | |
323 } | |
324 return GetRomajiTitle(); | |
325 } | |
326 | |
327 } // namespace Anime |