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