From 6a830429533e043d594a5db62549aad144dfef5d Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Thu, 26 Jul 2018 15:34:08 +0100 Subject: [PATCH 1/5] Add UI for displaying the user's average score for a genre --- pages/genre/genre.pixy | 5 +++-- pages/genre/genre.scarlet | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) create mode 100644 pages/genre/genre.scarlet diff --git a/pages/genre/genre.pixy b/pages/genre/genre.pixy index 973c253b..c5e89b99 100644 --- a/pages/genre/genre.pixy +++ b/pages/genre/genre.pixy @@ -1,8 +1,9 @@ component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore float64) h1(title=fmt.Sprint(len(animes)) + " anime")= strings.Title(genre) - //- if user != nil - //- h2= fmt.Sprintf("%.1f", userScore) + .average-score + if user != nil + p= fmt.Sprintf("Average Score: %.1f", userScore) .corner-buttons-hide-on-mobile if user != nil diff --git a/pages/genre/genre.scarlet b/pages/genre/genre.scarlet new file mode 100644 index 00000000..71f7a3be --- /dev/null +++ b/pages/genre/genre.scarlet @@ -0,0 +1,2 @@ +.average-score + text-align center \ No newline at end of file From f94a5d3d8b0ce6db0fd6e660302715615a700480 Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Thu, 26 Jul 2018 17:06:47 +0100 Subject: [PATCH 2/5] Add functionality to display the total number of anime a user has completed for a genre on its page --- pages/genre/genre.go | 24 +++++++++++++++++++++++- pages/genre/genre.pixy | 6 +++--- pages/genre/genre.scarlet | 5 +++-- 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/pages/genre/genre.go b/pages/genre/genre.go index 312c8df0..e4ab127e 100644 --- a/pages/genre/genre.go +++ b/pages/genre/genre.go @@ -24,6 +24,7 @@ func Get(ctx *aero.Context) string { } userScore := averageScore(user, animes) + userCompleted := totalCompleted(user, animes) arn.SortAnimeByQuality(animes) @@ -31,7 +32,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)) } // containsLowerCase tells you whether the given element exists when all elements are lowercased. @@ -71,3 +72,24 @@ func averageScore(user *arn.User, animes []*arn.Anime) float64 { return scores / 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 { + userAnime := completedList.Find(anime.ID) + + if userAnime != nil { + count++; + } + } + + return count; +} diff --git a/pages/genre/genre.pixy b/pages/genre/genre.pixy index c5e89b99..e92061c6 100644 --- a/pages/genre/genre.pixy +++ b/pages/genre/genre.pixy @@ -1,9 +1,9 @@ -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) h1(title=fmt.Sprint(len(animes)) + " anime")= strings.Title(genre) - .average-score + .average-score-total-completed if user != nil - p= fmt.Sprintf("Average Score: %.1f", userScore) + p= fmt.Sprintf("Average Score: %.1f | Total Completed: %d", userScore, userCompleted) .corner-buttons-hide-on-mobile if user != nil diff --git a/pages/genre/genre.scarlet b/pages/genre/genre.scarlet index 71f7a3be..254172df 100644 --- a/pages/genre/genre.scarlet +++ b/pages/genre/genre.scarlet @@ -1,2 +1,3 @@ -.average-score - text-align center \ No newline at end of file +.average-score-total-completed + text-align center + font-size 1.25em \ No newline at end of file From 8891ea4369599386bb5f22aa143c3ad9e221c188 Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Fri, 27 Jul 2018 18:10:18 +0100 Subject: [PATCH 3/5] Small tweaks --- pages/genre/genre.pixy | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pages/genre/genre.pixy b/pages/genre/genre.pixy index e92061c6..2537ffa1 100644 --- a/pages/genre/genre.pixy +++ b/pages/genre/genre.pixy @@ -1,9 +1,9 @@ component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore float64, userCompleted int) h1(title=fmt.Sprint(len(animes)) + " anime")= strings.Title(genre) - .average-score-total-completed - if user != nil - p= fmt.Sprintf("Average Score: %.1f | Total Completed: %d", userScore, userCompleted) + if user != nil + .average-score-total-completed + p= fmt.Sprintf("Average rating: %." + strconv.Itoa(user.Settings().Format.RatingsPrecision) + "f | Total completed: %d", userScore, userCompleted) .corner-buttons-hide-on-mobile if user != nil From 320649b81d099aebd6f284122e778e9f567f5cfd Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Sat, 28 Jul 2018 15:47:25 +0100 Subject: [PATCH 4/5] Small refactorings --- pages/genre/genre.go | 8 +++----- pages/genre/genre.pixy | 11 +++++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/pages/genre/genre.go b/pages/genre/genre.go index e4ab127e..2c8c5b6e 100644 --- a/pages/genre/genre.go +++ b/pages/genre/genre.go @@ -84,12 +84,10 @@ func totalCompleted(user *arn.User, animes []*arn.Anime) int { completedList := user.AnimeList().FilterStatus(arn.AnimeListStatusCompleted) for _, anime := range animes { - userAnime := completedList.Find(anime.ID) - - if userAnime != nil { - count++; + if completedList.Contains(anime.ID) { + count++ } } - return count; + return count } diff --git a/pages/genre/genre.pixy b/pages/genre/genre.pixy index 2537ffa1..7045716c 100644 --- a/pages/genre/genre.pixy +++ b/pages/genre/genre.pixy @@ -1,9 +1,7 @@ component Genre(genre string, animes []*arn.Anime, user *arn.User, userScore float64, userCompleted int) h1(title=fmt.Sprint(len(animes)) + " anime")= strings.Title(genre) - if user != nil - .average-score-total-completed - p= fmt.Sprintf("Average rating: %." + strconv.Itoa(user.Settings().Format.RatingsPrecision) + "f | Total completed: %d", userScore, userCompleted) + GenreStatistics(user, userScore, userCompleted) .corner-buttons-hide-on-mobile if user != nil @@ -13,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) + 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 From 41607cbf98f91b30d09d6f973cb2c9f9f2b2729f Mon Sep 17 00:00:00 2001 From: Ashley Allen Date: Mon, 30 Jul 2018 13:06:31 +0100 Subject: [PATCH 5/5] 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