From de78268b20b3d76a30317eb84e1736c3af08bd2d Mon Sep 17 00:00:00 2001 From: Azareal Date: Mon, 8 Oct 2018 15:34:25 +1000 Subject: [PATCH] Added level progress indicators, still WIP. Fixed a bug where GetLevelScore wouldn't work and simplified it slightly. Removed the account_dash_next_level phrase. Added the account_dash_level phrase. --- common/pages.go | 7 ++++++- common/template_init.go | 2 +- common/utils.go | 27 +++++++++++---------------- langs/english.json | 2 +- routes/account.go | 7 ++++++- routes/profile.go | 7 ++++++- templates/account_own_edit.html | 2 +- templates/profile.html | 3 +++ 8 files changed, 35 insertions(+), 22 deletions(-) diff --git a/common/pages.go b/common/pages.go index 47b04f64..446c4fbb 100644 --- a/common/pages.go +++ b/common/pages.go @@ -130,6 +130,8 @@ type ProfilePage struct { *Header ItemList []ReplyUser ProfileOwner User + CurrentScore int + NextScore int } type CreateTopicPage struct { @@ -152,7 +154,10 @@ type EmailListPage struct { type AccountDashPage struct { *Header - MFASetup bool + MFASetup bool + CurrentScore int + NextScore int + NextLevel int } type PanelStats struct { diff --git a/common/template_init.go b/common/template_init.go index e018e5ff..17614c47 100644 --- a/common/template_init.go +++ b/common/template_init.go @@ -214,7 +214,7 @@ func CompileTemplates() error { varList = make(map[string]tmpl.VarItem) header.Title = "User 526" - ppage := ProfilePage{header, replyList, user} + ppage := ProfilePage{header, replyList, user, 0, 0} // TODO: Use the score from user to generate the currentScore and nextScore profileTmpl, err := compile("profile", "common.ProfilePage", ppage) if err != nil { return err diff --git a/common/utils.go b/common/utils.go index 61c2f258..8fbaa7f6 100644 --- a/common/utils.go +++ b/common/utils.go @@ -114,9 +114,8 @@ func RelativeTime(t time.Time) string { return fmt.Sprintf("%d minutes ago", int(seconds/60)) case seconds < 7200: return "an hour ago" - default: - return fmt.Sprintf("%d hours ago", int(seconds/60/60)) } + return fmt.Sprintf("%d hours ago", int(seconds/60/60)) } // TODO: Write a test for this @@ -192,9 +191,8 @@ func ConvertUnit(num int) (int, string) { return num / 1000000, "M" case num >= 1000: return num / 1000, "K" - default: - return num, "" } + return num, "" } // TODO: Write a test for this @@ -212,9 +210,8 @@ func ConvertFriendlyUnit(num int) (int, string) { return num / 1000000, " million" case num >= 1000: return num / 1000, " thousand" - default: - return num, "" } + return num, "" } // TODO: Make slugs optional for certain languages across the entirety of Gosora? @@ -369,6 +366,7 @@ func WordCount(input string) (count int) { if input == "" { return 0 } + var inSpace bool for _, value := range input { if unicode.IsSpace(value) || unicode.IsPunct(value) { @@ -380,6 +378,7 @@ func WordCount(input string) (count int) { inSpace = false } } + return count + 1 } @@ -407,21 +406,17 @@ func GetLevel(score int) (level int) { // TODO: Write a test for this func GetLevelScore(getLevel int) (score int) { var base float64 = 25 - var current, prev float64 - var level int - expFactor := 2.8 + var current float64 + var expFactor = 2.8 - for i := 1; ; i++ { + for i := 1; i <= getLevel; i++ { _, bit := math.Modf(float64(i) / 10) if bit == 0 { expFactor += 0.1 } - current = base + math.Pow(float64(i), expFactor) + (prev / 3) - prev = current - level++ - if level <= getLevel { - break - } + current = base + math.Pow(float64(i), expFactor) + (current / 3) + //fmt.Println("level: ", i) + //fmt.Println("current: ", current) } return int(math.Ceil(current)) } diff --git a/langs/english.json b/langs/english.json index 54e531e0..0a7e34e9 100644 --- a/langs/english.json +++ b/langs/english.json @@ -448,7 +448,7 @@ "account_dash_2fa_setup":"Setup your two-factor authentication.", "account_dash_2fa_manage":"Remove or manage your two-factor authentication.", - "account_dash_next_level":"Progress to next level.", + "account_dash_level":"Level %d", "account_dash_security_notice":"Security", "account_avatar_select":"Select", "account_avatar_update_button":"Upload", diff --git a/routes/account.go b/routes/account.go index 461a7177..d3196d56 100644 --- a/routes/account.go +++ b/routes/account.go @@ -389,7 +389,12 @@ func AccountEdit(w http.ResponseWriter, r *http.Request, user common.User) commo mfaSetup = true } - pi := common.AccountDashPage{header, mfaSetup} + // Normalise the score so that the user sees their relative progress to the next level rather than showing them their total score + prevScore := common.GetLevelScore(user.Level) + currentScore := user.Score - prevScore + nextScore := common.GetLevelScore(user.Level+1) - prevScore + + pi := common.AccountDashPage{header, mfaSetup, currentScore, nextScore, user.Level + 1} if common.RunPreRenderHook("pre_render_account_own_edit", w, r, &user, &pi) { return nil } diff --git a/routes/profile.go b/routes/profile.go index 7ca29a59..78699525 100644 --- a/routes/profile.go +++ b/routes/profile.go @@ -116,7 +116,12 @@ func ViewProfile(w http.ResponseWriter, r *http.Request, user common.User) commo return common.InternalError(err, w, r) } - ppage := common.ProfilePage{header, replyList, *puser} + // Normalise the score so that the user sees their relative progress to the next level rather than showing them their total score + prevScore := common.GetLevelScore(puser.Level) + currentScore := puser.Score - prevScore + nextScore := common.GetLevelScore(puser.Level+1) - prevScore + + ppage := common.ProfilePage{header, replyList, *puser, currentScore, nextScore} if common.RunPreRenderHook("pre_render_profile", w, r, &user, &ppage) { return nil } diff --git a/templates/account_own_edit.html b/templates/account_own_edit.html index c29ba2ad..ef9ce14f 100644 --- a/templates/account_own_edit.html +++ b/templates/account_own_edit.html @@ -21,7 +21,7 @@
{{if not .MFASetup}}{{lang "account_dash_2fa_setup"}}{{else}}{{lang "account_dash_2fa_manage"}}{{end}} {{lang "account_dash_security_notice"}}
-
{{lang "account_dash_next_level"}}
+
Level {{.CurrentUser.Level}}: [{{.CurrentScore}} / {{.NextScore}}]
diff --git a/templates/profile.html b/templates/profile.html index cbca363e..6f3be6f7 100644 --- a/templates/profile.html +++ b/templates/profile.html @@ -13,6 +13,9 @@
+ {{if not .CurrentUser.Loggedin}}{{else}}