From 1e5f6549289094ad826b34c538f4a9655d58e434 Mon Sep 17 00:00:00 2001 From: Jarmo Riikonen Date: Tue, 3 Jul 2018 13:13:38 +0300 Subject: [PATCH] Show only float with 1 decimal for consistency. --- pages/genre/genre.go | 26 ++++++++++++++++---------- pages/genre/genre.pixy | 7 ++++--- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/pages/genre/genre.go b/pages/genre/genre.go index 264212b5..7cb4ad22 100644 --- a/pages/genre/genre.go +++ b/pages/genre/genre.go @@ -1,7 +1,6 @@ package genre import ( - "strconv" "strings" "github.com/aerogo/aero" @@ -24,7 +23,7 @@ func Get(ctx *aero.Context) string { } } - userScore := averageGenreScore(user, animes) + userScore := averageScore(user, animes) arn.SortAnimeByQuality(animes) @@ -46,21 +45,28 @@ func containsLowerCase(array []string, search string) bool { return false } -func averageGenreScore(user *arn.User, animes []*arn.Anime) string { +// Counts users average score in genre +func averageScore(user *arn.User, animes []*arn.Anime) float64 { if user == nil { - return "" + return 0 } - counter := 0.0 + count := 0.0 scores := 0.0 - for _, anime := range animes { + animeList := user.AnimeList() - if user.AnimeList().Contains(anime.ID) { - scores = scores + user.AnimeList().Find(anime.ID).Rating.Overall - counter = counter + 1 + for _, anime := range animes { + userAnime := animeList.Find(anime.ID) + if userAnime != nil && !userAnime.Rating.IsNotRated() { + scores += userAnime.Rating.Overall + count += 1 } } - return strconv.FormatFloat(scores/counter, 'f', 6, 64) + if count == 0.0 || scores == 0.0 { + return 0 + } + + return scores / count } diff --git a/pages/genre/genre.pixy b/pages/genre/genre.pixy index 809d1e5a..d7d7dd50 100644 --- a/pages/genre/genre.pixy +++ b/pages/genre/genre.pixy @@ -1,7 +1,8 @@ -component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore string) +component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore float64) h1(title=fmt.Sprint(len(animes)) + " anime")= strings.Title(genre) - h2()= userScore - + if user != nil + h2= fmt.Sprintf("%.1f", userScore) + .corner-buttons-hide-on-mobile if user != nil button.action(data-trigger="click", data-action="hideAddedAnime", title="Hide anime in my collection")