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