From 41607cbf98f91b30d09d6f973cb2c9f9f2b2729f Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Mon, 30 Jul 2018 13:06:31 +0100 Subject: [PATCH] Add functionality to calculate the global average rating for a genre --- pages/genre/genre.go | 25 +++++++++++++++++++++---- pages/genre/genre.pixy | 6 +++--- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/pages/genre/genre.go b/pages/genre/genre.go index 2c8c5b6e..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,8 +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) @@ -32,7 +34,7 @@ func Get(ctx *aero.Context) string { animes = animes[:animePerPage] } - return ctx.HTML(components.Genre(genreName, animes, user, userScore, userCompleted)) + return ctx.HTML(components.Genre(genreName, animes, user, userScore, userCompleted, globalScore)) } // containsLowerCase tells you whether the given element exists when all elements are lowercased. @@ -46,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 } @@ -73,6 +75,21 @@ 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 { diff --git a/pages/genre/genre.pixy b/pages/genre/genre.pixy index 7045716c..de4079e8 100644 --- a/pages/genre/genre.pixy +++ b/pages/genre/genre.pixy @@ -1,7 +1,7 @@ -component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore float64, userCompleted int) +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) - GenreStatistics(user, userScore, userCompleted) + GenreStatistics(user, userScore, userCompleted, globalScore) .corner-buttons-hide-on-mobile if user != nil @@ -13,7 +13,7 @@ component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore flo AnimeGrid(animes, user) -component GenreStatistics(user *arn.User, userScore float64, userCompleted int) +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