go: discord/modules: Prevent changed votes from causing a vote to pass/reject

master
Luther Wen Xu 2019-10-20 17:25:52 +07:00
parent c0965b86aa
commit 38e4f1facc
Signed by: chanbakjsd
GPG Key ID: B7D77E3E9D102B70
1 changed files with 20 additions and 8 deletions

@ -18,6 +18,8 @@ type confirmedResult struct {
IsPositive bool
}
const invalidVote = -1
var voteMutex sync.Mutex
var toAnnounceResultList []confirmedResult
@ -25,6 +27,9 @@ func ListenToVoteFinishes(s *discordgo.Session) {
for {
voteMutex.Lock()
for _, v := range toAnnounceResultList {
if v.VoteID == invalidVote {
continue
}
message.AuditInfo(s, fmt.Sprintf("Announcing the result of vote %d.", v.VoteID))
if err := db.FinishVote(v.VoteID); err != nil {
message.AuditError(s, "", err)
@ -72,13 +77,13 @@ func checkForVoteResult(s *discordgo.Session, id int) {
lowestPossible := (currentScore + remainingTrust) / totalGlobalTrust
highestPossible := (currentScore + remainingTrust*5) / totalGlobalTrust
voteMutex.Lock()
defer voteMutex.Unlock()
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
}
}
@ -87,15 +92,11 @@ func checkForVoteResult(s *discordgo.Session, id int) {
VoteID: id,
IsPositive: false,
})
voteMutex.Unlock()
}
if lowestPossible >= 3.5 {
} else 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
}
}
@ -104,6 +105,17 @@ func checkForVoteResult(s *discordgo.Session, id int) {
VoteID: id,
IsPositive: true,
})
voteMutex.Unlock()
} else {
//Nothing happened. Make sure it's not in the queue.
for k, v := range toAnnounceResultList {
if v.VoteID == id {
//It's already queued to be announced in the next announcement cycle.
toAnnounceResultList[k] = confirmedResult{
VoteID: invalidVote,
IsPositive: false,
}
return
}
}
}
}