comparison src/core/anime.cc @ 202:71832ffe425a

animia: re-add kvm fd source this is all being merged from my wildly out-of-date laptop. SORRY! in other news, I edited the CI file to install the wayland client as well, so the linux CI build might finally get wayland stuff.
author Paper <paper@paper.us.eu.org>
date Tue, 02 Jan 2024 06:05:06 -0500
parents 975a3f0965e2
children 7cf53145de11
comparison
equal deleted inserted replaced
201:8f6f8dd2eb23 202:71832ffe425a
3 * datatypes & variables 3 * datatypes & variables
4 */ 4 */
5 #include "core/anime.h" 5 #include "core/anime.h"
6 #include "core/date.h" 6 #include "core/date.h"
7 #include "core/session.h" 7 #include "core/session.h"
8
8 #include <algorithm> 9 #include <algorithm>
9 #include <chrono>
10 #include <cmath>
11 #include <string> 10 #include <string>
12 #include <vector> 11 #include <vector>
13 12
14 namespace Anime { 13 namespace Anime {
15 14
41 int Anime::GetUserScore() const { 40 int Anime::GetUserScore() const {
42 assert(list_info_.get()); 41 assert(list_info_.get());
43 return list_info_->score; 42 return list_info_->score;
44 } 43 }
45 44
45 std::string Anime::GetUserPresentableScore() const {
46 assert(list_info_.get());
47 const int score = list_info_->score;
48 if (score == 0)
49 return "";
50
51 switch (session.config.anime_list.score_format) {
52 case ScoreFormat::POINT_10_DECIMAL:
53 return std::to_string(score / 10) + "." + std::to_string(score % 10);
54 case ScoreFormat::POINT_10:
55 return std::to_string(score / 10);
56 case ScoreFormat::POINT_5: {
57 std::string stars = "";
58
59 for (int i = 0; i < 100; i += 20)
60 stars.append((i <= score) ? "★" : "☆");
61
62 return stars;
63 }
64 case ScoreFormat::POINT_3: {
65 if (score >= 100)
66 return ":)";
67 else if (score >= 66)
68 return ":|";
69 else if (score >= 33)
70 return ":(";
71 else
72 return "";
73 }
74 default:
75 case ScoreFormat::POINT_100:
76 return std::to_string(score);
77 }
78 }
79
46 Date Anime::GetUserDateStarted() const { 80 Date Anime::GetUserDateStarted() const {
47 assert(list_info_.get()); 81 assert(list_info_.get());
48 return list_info_->started; 82 return list_info_->started;
49 } 83 }
50 84
145 return info_.title.native; 179 return info_.title.native;
146 } 180 }
147 181
148 std::vector<std::string> Anime::GetTitleSynonyms() const { 182 std::vector<std::string> Anime::GetTitleSynonyms() const {
149 std::vector<std::string> result; 183 std::vector<std::string> result;
150 #define IN_VECTOR(v, k) (std::count(v.begin(), v.end(), k)) 184
151 #define ADD_TO_SYNONYMS(v, k) \ 185 auto add_to_synonyms = [this](std::vector<std::string>& vec, std::string key) {
152 if (!k.empty() && !IN_VECTOR(v, k) && k != GetUserPreferredTitle()) \ 186 if (!key.empty() && !std::count(vec.begin(), vec.end(), key) && key != GetUserPreferredTitle())
153 v.push_back(k) 187 vec.push_back(key);
154 ADD_TO_SYNONYMS(result, info_.title.english); 188 };
155 ADD_TO_SYNONYMS(result, info_.title.romaji); 189
156 ADD_TO_SYNONYMS(result, info_.title.native); 190 add_to_synonyms(result, info_.title.english);
157 for (auto& synonym : info_.synonyms) { 191 add_to_synonyms(result, info_.title.romaji);
158 ADD_TO_SYNONYMS(result, synonym); 192 add_to_synonyms(result, info_.title.native);
159 } 193
160 #undef ADD_TO_SYNONYMS 194 for (auto& synonym : info_.synonyms)
161 #undef IN_VECTOR 195 add_to_synonyms(result, synonym);
196
162 return result; 197 return result;
163 } 198 }
164 199
165 int Anime::GetEpisodes() const { 200 int Anime::GetEpisodes() const {
166 return info_.episodes; 201 return info_.episodes;
205 std::string Anime::GetPosterUrl() const { 240 std::string Anime::GetPosterUrl() const {
206 return info_.poster_url; 241 return info_.poster_url;
207 } 242 }
208 243
209 std::string Anime::GetServiceUrl() const { 244 std::string Anime::GetServiceUrl() const {
245 /* todo: add support for other services... */
210 return "https://anilist.co/anime/" + std::to_string(GetId()); 246 return "https://anilist.co/anime/" + std::to_string(GetId());
211 } 247 }
212 248
213 void Anime::SetId(int id) { 249 void Anime::SetId(int id) {
214 info_.id = id; 250 info_.id = id;