go: discord/backend: Implement actual trust vote caching

master
Luther Wen Xu 2019-10-20 17:19:14 +07:00
parent b6685d22bb
commit c0965b86aa
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
1 changed files with 27 additions and 17 deletions

@ -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) {