diff --git a/GoBot/discord/backend/trust.go b/GoBot/discord/backend/trust.go index 4545390..a924614 100644 --- a/GoBot/discord/backend/trust.go +++ b/GoBot/discord/backend/trust.go @@ -14,6 +14,30 @@ var trustCache map[string]float64 var ErrNotMember = errors.New("trust: only members have trust value") func GetTrust(s *discordgo.Session, discordID string) (float64, error) { + rawScore, err := GetRawTrust(s, discordID) + if err != nil { + return 0.0, err + } + + //Calculate active level + totalLevel, err := db.GetTotalActiveLevel() + if err != nil { + return rawScore, nil + } + if totalLevel < 1000000 { + return rawScore, nil + } + 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 + } + activeBuff := float64(activeLevel) / float64(totalLevel) + + return rawScore * (1 + activeBuff), nil +} + +func GetRawTrust(s *discordgo.Session, discordID string) (float64, error) { if v, ok := trustCache[discordID]; ok { return v, nil } @@ -41,24 +65,10 @@ func GetTrust(s *discordgo.Session, discordID string) (float64, error) { for _, v := range votes { total += float64(v) } - voteResult := total / float64(len(votes)) - - //Calculate active level - totalLevel, err := db.GetTotalActiveLevel() - if err != nil { - return voteResult, nil - } - if totalLevel < 1000000 { - return voteResult, nil - } - 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 - } - activeBuff := float64(activeLevel) / float64(totalLevel) - return voteResult * (1 + activeBuff), nil + rawScore := total / float64(len(votes)) + trustCache[discordID] = rawScore + return rawScore, nil } func GetTotalTrust(s *discordgo.Session) (float64, error) {