2019-10-10 13:59:00 +07:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/bwmarrin/discordgo"
|
|
|
|
|
2019-10-12 08:07:10 +07:00
|
|
|
"TerraOceanBot/db"
|
|
|
|
)
|
2019-10-10 13:59:00 +07:00
|
|
|
|
|
|
|
type confirmedResult struct {
|
|
|
|
VoteID int
|
|
|
|
IsPositive bool
|
|
|
|
}
|
|
|
|
|
|
|
|
var voteMutex sync.Mutex
|
|
|
|
var toAnnounceResultList []confirmedResult
|
|
|
|
|
|
|
|
func listenToVoteFinishes(s *discordgo.Session) {
|
|
|
|
for {
|
|
|
|
voteMutex.Lock()
|
|
|
|
for _, v := range toAnnounceResultList {
|
|
|
|
auditLog(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-10 13:59:00 +07:00
|
|
|
auditLog(s, fmt.Sprintf("Error while finishing vote %d.", v.VoteID))
|
|
|
|
}
|
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-10 13:59:00 +07:00
|
|
|
s.ChannelMessageEditEmbed(voteChannel, messageID, showVoteStatus(voteTypes[voteType].EmbedBuilder(v.VoteID, voteName), v.IsPositive))
|
|
|
|
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 {
|
|
|
|
auditLog(s, fmt.Sprintf("Error while calculating vote result for ID %d: %s", id, err.Error()))
|
|
|
|
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-11 15:20:12 +07:00
|
|
|
trust, err := getTrust(s, vote.UserID)
|
2019-10-11 12:25:09 +07:00
|
|
|
if err != nil {
|
|
|
|
auditLog(s, fmt.Sprintf("Error while getting vote trust for user ID %d: %s", vote.UserID, err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
currentScore += float64(vote.Value) * trust
|
|
|
|
totalTrust += float64(trust)
|
2019-10-10 13:59:00 +07:00
|
|
|
}
|
|
|
|
|
2019-10-11 12:25:09 +07:00
|
|
|
totalGlobalTrust, err := getTotalTrust(s)
|
|
|
|
if err != nil {
|
|
|
|
auditLog(s, fmt.Sprintf("Error while getting total vote trust: %s", err.Error()))
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auditLog(s, fmt.Sprintf("Rejection of vote ID %d has been confirmed.", id))
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auditLog(s, fmt.Sprintf("Acceptance of vote ID %d has been confirmed.", id))
|
|
|
|
toAnnounceResultList = append(toAnnounceResultList, confirmedResult{
|
|
|
|
VoteID: id,
|
|
|
|
IsPositive: true,
|
|
|
|
})
|
|
|
|
voteMutex.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func showVoteStatus(embed *discordgo.MessageEmbed, isPositive bool) *discordgo.MessageEmbed {
|
|
|
|
if isPositive {
|
|
|
|
embed.Color = 0x00F000
|
|
|
|
embed.Title = "投票通过 Vote Passed"
|
|
|
|
} else {
|
|
|
|
embed.Color = 0xF00000
|
|
|
|
embed.Title = "投票不通过 Vote Rejected"
|
|
|
|
}
|
|
|
|
return embed
|
|
|
|
}
|