TerraOceanPlugin/GoBot/discord/modules/vote_result.go

109 lines
2.8 KiB
Go

2019-10-12 09:45:43 +07:00
package modules
2019-10-10 13:59:00 +07:00
import (
"fmt"
"sync"
"time"
"github.com/bwmarrin/discordgo"
2019-10-12 08:07:10 +07:00
"TerraOceanBot/db"
2019-10-12 09:45:43 +07:00
"TerraOceanBot/discord/backend"
"TerraOceanBot/discord/config"
2019-10-12 08:07:10 +07:00
)
2019-10-10 13:59:00 +07:00
type confirmedResult struct {
VoteID int
IsPositive bool
}
var voteMutex sync.Mutex
var toAnnounceResultList []confirmedResult
2019-10-12 09:45:43 +07:00
func ListenToVoteFinishes(s *discordgo.Session) {
2019-10-10 13:59:00 +07:00
for {
voteMutex.Lock()
for _, v := range toAnnounceResultList {
2019-10-12 13:49:41 +07:00
auditInfo(s, fmt.Sprintf("Announcing the result of vote %d.", v.VoteID))
2019-10-12 08:07:10 +07:00
if err := db.FinishVote(v.VoteID); err != nil {
2019-10-12 13:49:41 +07:00
auditError(s, "", err)
continue
2019-10-10 13:59:00 +07:00
}
2019-10-12 08:07:10 +07:00
voteType, _ := db.GetVoteType(v.VoteID)
voteName, _ := db.GetVoteName(v.VoteID)
messageID, _ := db.GetMessageIDFromVote(v.VoteID)
2019-10-12 15:33:06 +07:00
voteTypes[voteType].EmbedBuilder(v.VoteID, voteName).UpdateVoteStatus(v.IsPositive).Edit(s, config.VoteChannel, messageID)
2019-10-10 13:59:00 +07:00
voteTypes[voteType].ResultHandler(s, v.VoteID, voteName, v.IsPositive)
}
toAnnounceResultList = toAnnounceResultList[:0]
voteMutex.Unlock()
time.Sleep(60 * time.Second)
}
}
func checkForVoteResult(s *discordgo.Session, id int) {
2019-10-12 08:07:10 +07:00
votes, err := db.GetAllVoteChoices(id)
2019-10-10 13:59:00 +07:00
if err != nil {
2019-10-12 13:49:41 +07:00
auditError(s, "", err)
2019-10-10 13:59:00 +07:00
return
}
var currentScore, totalTrust float64
var absoluteRejectionVote bool
for _, vote := range votes {
2019-10-12 08:07:10 +07:00
if vote.Value == db.ForceRejectionVote {
2019-10-10 13:59:00 +07:00
absoluteRejectionVote = true
}
2019-10-12 09:45:43 +07:00
trust, err := backend.GetTrust(s, vote.UserID)
2019-10-11 12:25:09 +07:00
if err != nil {
2019-10-12 13:49:41 +07:00
auditError(s, "", err)
2019-10-11 12:25:09 +07:00
return
}
currentScore += float64(vote.Value) * trust
totalTrust += float64(trust)
2019-10-10 13:59:00 +07:00
}
2019-10-12 09:45:43 +07:00
totalGlobalTrust, err := backend.GetTotalTrust(s)
2019-10-11 12:25:09 +07:00
if err != nil {
2019-10-12 13:49:41 +07:00
auditError(s, "", err)
2019-10-11 12:25:09 +07:00
return
}
remainingTrust := totalGlobalTrust - totalTrust
lowestPossible := (currentScore + remainingTrust) / totalGlobalTrust
highestPossible := (currentScore + remainingTrust*5) / totalGlobalTrust
2019-10-10 13:59:00 +07:00
if highestPossible <= 3.5 || absoluteRejectionVote {
//Rejection confirmed.
voteMutex.Lock()
for _, v := range toAnnounceResultList {
if v.VoteID == id {
//It's already queued to be announced in the next announcement cycle.
voteMutex.Unlock()
return
}
}
2019-10-12 13:49:41 +07:00
auditInfo(s, fmt.Sprintf("Rejection of vote ID %d has been confirmed.", id))
2019-10-10 13:59:00 +07:00
toAnnounceResultList = append(toAnnounceResultList, confirmedResult{
VoteID: id,
IsPositive: false,
})
voteMutex.Unlock()
}
if lowestPossible >= 3.5 {
//Acceptance confirmed.
voteMutex.Lock()
for _, v := range toAnnounceResultList {
if v.VoteID == id {
//It's already queued to be announced in the next announcement cycle.
voteMutex.Unlock()
return
}
}
2019-10-12 13:49:41 +07:00
auditInfo(s, fmt.Sprintf("Acceptance of vote ID %d has been confirmed.", id))
2019-10-10 13:59:00 +07:00
toAnnounceResultList = append(toAnnounceResultList, confirmedResult{
VoteID: id,
IsPositive: true,
})
voteMutex.Unlock()
}
}