From 38e4f1facc41256f19aa73223a17e5c1a64b3e35 Mon Sep 17 00:00:00 2001 From: Luther Wen Xu Date: Sun, 20 Oct 2019 17:25:52 +0800 Subject: [PATCH] go: discord/modules: Prevent changed votes from causing a vote to pass/reject --- GoBot/discord/modules/vote_result.go | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/GoBot/discord/modules/vote_result.go b/GoBot/discord/modules/vote_result.go index d1e21e3..94d8a2d 100644 --- a/GoBot/discord/modules/vote_result.go +++ b/GoBot/discord/modules/vote_result.go @@ -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 + } + } } }