From ec43666620c3f4f369ef5a2c35155c758fd6477c Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Fri, 18 Oct 2019 11:38:32 +0800 Subject: [PATCH] go: discord/backend: Consider active level while calculating trust level --- GoBot/db/active.go | 17 +++++++++++++++++ GoBot/discord/backend/trust.go | 15 ++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/GoBot/db/active.go b/GoBot/db/active.go index 465eee8..29b92f3 100644 --- a/GoBot/db/active.go +++ b/GoBot/db/active.go @@ -17,6 +17,23 @@ func GetActiveLevel(discordID string) (int, error) { return 0, ErrNotFound } +func GetTotalActiveLevel() (int, error) { + rows, err := db.Query("SELECT sum(level) FROM activeLevel") + if err != nil { + return 0, err + } + defer rows.Close() + if rows.Next() { + var totalActiveLevel int + err := rows.Scan(&totalActiveLevel) + if err != nil { + return 0, err + } + return totalActiveLevel, nil + } + return 0, ErrNotFound +} + func AddActiveLevel(discordID string, addedLevel int) error { _, err := db.Exec("INSERT INTO activeLevel(id, level) VALUES(?, ?) ON DUPLICATE KEY UPDATE level=level+?", discordID, addedLevel, addedLevel) return err diff --git a/GoBot/discord/backend/trust.go b/GoBot/discord/backend/trust.go index 2f0776a..fec2e7f 100644 --- a/GoBot/discord/backend/trust.go +++ b/GoBot/discord/backend/trust.go @@ -39,7 +39,20 @@ func GetTrust(s *discordgo.Session, discordID string) (float64, error) { for _, v := range votes { total += float64(v) } - return total / float64(len(votes)), nil + voteResult := total / float64(len(votes)) + + activeLevel, err := db.GetActiveLevel(discordID) + if err != nil && err != db.ErrNotFound { + //ErrNotFound is allowed. It just means the player hasn't played yet. + return 0.0, err + } + totalLevel, err := db.GetTotalActiveLevel() + if err != nil { + return 0.0, err + } + activeBuff := float64(activeLevel) / float64(totalLevel) + + return voteResult * (1 + activeBuff), nil } func GetTotalTrust(s *discordgo.Session) (float64, error) {