Add functionality to calculate the global average rating for a genre

This commit is contained in:
Ashley Allen 2018-07-30 13:06:31 +01:00
parent 320649b81d
commit 41607cbf98
2 changed files with 24 additions and 7 deletions

View File

@ -10,6 +10,7 @@ import (
) )
const animePerPage = 100 const animePerPage = 100
const animeRatingThreshold = 5
// Get ... // Get ...
func Get(ctx *aero.Context) string { 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) userCompleted := totalCompleted(user, animes)
globalScore := averageGlobalScore(animes)
arn.SortAnimeByQuality(animes) arn.SortAnimeByQuality(animes)
@ -32,7 +34,7 @@ func Get(ctx *aero.Context) string {
animes = animes[:animePerPage] 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. // 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 return false
} }
// averageScore counts the user's average score for the given animes. // averageUserScore counts the user's average score for the given animes.
func averageScore(user *arn.User, animes []*arn.Anime) float64 { func averageUserScore(user *arn.User, animes []*arn.Anime) float64 {
if user == nil { if user == nil {
return 0 return 0
} }
@ -73,6 +75,21 @@ func averageScore(user *arn.User, animes []*arn.Anime) float64 {
return scores / count 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 // 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 { func totalCompleted(user *arn.User, animes []*arn.Anime) int {
if user == nil { if user == nil {

View File

@ -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) h1(title=fmt.Sprint(len(animes)) + " anime")= strings.Title(genre)
GenreStatistics(user, userScore, userCompleted) GenreStatistics(user, userScore, userCompleted, globalScore)
.corner-buttons-hide-on-mobile .corner-buttons-hide-on-mobile
if user != nil if user != nil
@ -13,7 +13,7 @@ component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore flo
AnimeGrid(animes, user) 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 if user != nil
.average-score-total-completed .average-score-total-completed
p= fmt.Sprintf("Average rating: %." + strconv.Itoa(user.Settings().Format.RatingsPrecision) + "f | Total completed: %d", userScore, userCompleted) p= fmt.Sprintf("Average rating: %." + strconv.Itoa(user.Settings().Format.RatingsPrecision) + "f | Total completed: %d", userScore, userCompleted)