diff --git a/pages/genre/genre.go b/pages/genre/genre.go index 312c8df0..6682f179 100644 --- a/pages/genre/genre.go +++ b/pages/genre/genre.go @@ -10,6 +10,7 @@ import ( ) const animePerPage = 100 +const animeRatingThreshold = 5 // Get ... func Get(ctx *aero.Context) string { @@ -23,7 +24,9 @@ func Get(ctx *aero.Context) string { } } - userScore := averageScore(user, animes) + userScore := averageUserScore(user, animes) + userCompleted := totalCompleted(user, animes) + globalScore := averageGlobalScore(animes) arn.SortAnimeByQuality(animes) @@ -31,7 +34,7 @@ func Get(ctx *aero.Context) string { animes = animes[:animePerPage] } - return ctx.HTML(components.Genre(genreName, animes, user, userScore)) + return ctx.HTML(components.Genre(genreName, animes, user, userScore, userCompleted, globalScore)) } // containsLowerCase tells you whether the given element exists when all elements are lowercased. @@ -45,8 +48,8 @@ func containsLowerCase(array []string, search string) bool { return false } -// averageScore counts the user's average score for the given animes. -func averageScore(user *arn.User, animes []*arn.Anime) float64 { +// averageUserScore counts the user's average score for the given animes. +func averageUserScore(user *arn.User, animes []*arn.Anime) float64 { if user == nil { return 0 } @@ -71,3 +74,37 @@ func averageScore(user *arn.User, animes []*arn.Anime) float64 { return scores / count } + +// averageGlobalScore returns the average overall score for the given anime +func averageGlobalScore(animes []*arn.Anime) float64 { + sum := 0.0 + count := 0 + + for _, anime := range animes { + if anime.Rating.Count.Overall >= animeRatingThreshold { + sum += anime.Rating.Overall + count++ + } + } + + return sum / float64(count) +} + +// totalCompleted counts the number of animes the user has completed from a given list of anime +func totalCompleted(user *arn.User, animes []*arn.Anime) int { + if user == nil { + return 0 + } + + count := 0 + + completedList := user.AnimeList().FilterStatus(arn.AnimeListStatusCompleted) + + for _, anime := range animes { + if completedList.Contains(anime.ID) { + count++ + } + } + + return count +} diff --git a/pages/genre/genre.pixy b/pages/genre/genre.pixy index 973c253b..de4079e8 100644 --- a/pages/genre/genre.pixy +++ b/pages/genre/genre.pixy @@ -1,8 +1,7 @@ -component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore float64) +component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore float64, userCompleted int, globalScore float64) h1(title=fmt.Sprint(len(animes)) + " anime")= strings.Title(genre) - //- if user != nil - //- h2= fmt.Sprintf("%.1f", userScore) + GenreStatistics(user, userScore, userCompleted, globalScore) .corner-buttons-hide-on-mobile if user != nil @@ -12,4 +11,9 @@ component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore flo a.button(href="/genres", title="View genres") RawIcon("clone") - AnimeGrid(animes, user) \ No newline at end of file + AnimeGrid(animes, user) + +component GenreStatistics(user *arn.User, userScore float64, userCompleted int, globalScore float64) + if user != nil + .average-score-total-completed + p= fmt.Sprintf("Average rating: %." + strconv.Itoa(user.Settings().Format.RatingsPrecision) + "f | Total completed: %d", userScore, userCompleted) \ No newline at end of file diff --git a/pages/genre/genre.scarlet b/pages/genre/genre.scarlet new file mode 100644 index 00000000..254172df --- /dev/null +++ b/pages/genre/genre.scarlet @@ -0,0 +1,3 @@ +.average-score-total-completed + text-align center + font-size 1.25em \ No newline at end of file