-
Notifications
You must be signed in to change notification settings - Fork 0
/
democracy.py
202 lines (147 loc) · 7.13 KB
/
democracy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
import time, random, queue
class Vote:
def __init__(self, voter: str, choice):
self.voter = voter
self.choice = choice
class YayOrNay:
def __init__(self, yay_length: int, viewers: int, command, majority = 0.6):
self.voters = []
self.majority = int(viewers * majority)
self.has_passed = False
self.command = command
self.yay_end_time = time.time() + yay_length
def cast(self, voter: str):
if voter not in self.voters:
self.voters += [voter]
if len(self.voters) >= self.majority:
self.has_passed = True
def is_over(self):
return time.time() >= self.yay_end_time
class Ballot:
def __init__(self, ballot_length: int, purgatory = False):
self.cast_votes = {}
if purgatory:
self.ballot_end_time = ballot_length
else:
self.ballot_end_time = time.time() + ballot_length
self.purgatory = purgatory
def cast_vote(self, vote: Vote):
"""
Casts a `Vote` into the `Ballot`.
Args:
vote (Vote): The vote being cast.
Returns:
"retract": If the vote is empty.
"success": If the vote gets counted.
"""
if self.purgatory:
self.ballot_end_time += time.time()
self.purgatory = False
for choice in self.cast_votes:
if vote.voter in self.cast_votes[choice]:
self.cast_votes[choice].remove(vote.voter)
self.trim()
if vote.choice == "":
# This is for retracted vote, which the bot should respond to.
return "retract"
if vote.choice not in self.cast_votes.keys():
self.cast_votes[vote.choice] = []
self.cast_votes[vote.choice] += [vote.voter]
return "success"
def end(self):
"""
Ends the `Ballot` being run.
Returns:
tuple:
Any: The winning choice
int: The number of votes it received
bool: If there was a tie
"""
largest_number_of_votes = 0
winning_choice = []
for choice in self.cast_votes:
number_of_votes = len(self.cast_votes[choice])
if number_of_votes > largest_number_of_votes:
largest_number_of_votes = number_of_votes
winning_choice = [choice]
elif number_of_votes == largest_number_of_votes and number_of_votes != 0:
winning_choice += [choice]
is_tied = len(winning_choice) > 1
if is_tied:
winning_choice = random.choice(winning_choice)
winning_voters = self.cast_votes[winning_choice]
elif winning_choice == []:
winning_choice = ""
winning_voters = ""
else:
winning_choice = winning_choice[0]
winning_voters = self.cast_votes[winning_choice]
return winning_choice, largest_number_of_votes, is_tied, winning_voters
def is_over(self):
if self.purgatory:
return False
else:
return time.time() >= self.ballot_end_time
def time_left(self):
return self.ballot_end_time - time.time()
def trim(self):
empty_votes = []
for choice in self.cast_votes:
if self.cast_votes[choice] == []:
empty_votes += [choice]
for choice in empty_votes:
self.cast_votes.pop(choice)
class Democracy:
def __init__(self):
self.is_active = True
self.has_alerted = False
self.manifest_queue = queue.Queue()
self.current_ballot = Ballot(0x7fffffff, True)
self.yay_vote_exists = False
self.yay_vote = YayOrNay(0x7fffffff, 0x7fffffff, "")
self.sequential_empty_ballots = 0
self.empty_ballot_limit = 3
def create_yay_vote(self, sender: str, command, viewers: int, yay_length = 30):
if self.yay_vote_exists:
self.manifest_queue.put(("message", "There is already a YAY vote on! Wait until that expires!"))
return
self.yay_vote_exists = True
self.yay_vote = YayOrNay(yay_length, viewers, command)
self.yay_vote.cast(sender)
if self.yay_vote.has_passed:
return
self.manifest_queue.put(("message", f"{sender} has started a vote for {command}! Type 'YAY' to agree!"))
self.manifest_queue.put(("message", f"There needs to be {self.yay_vote.majority} YAY votes to pass! It expires in {yay_length} seconds."))
return
def democracy_loop(self, ballot_length = 45):
self.current_ballot = Ballot(ballot_length)
while self.is_active:
if not (self.has_alerted or self.is_purgatory()) and self.current_ballot.time_left() <= 10:
self.manifest_queue.put(("message", "There is 10 seconds left on the vote!"))
self.has_alerted = True
if self.current_ballot.is_over():
ballot_results = self.current_ballot.end()
self.manifest_queue.put(("newballot", "newballot"))
self.has_alerted = False
if ballot_results[0] == "":
self.manifest_queue.put(("message", "No vote has won! Starting new vote..."))
self.sequential_empty_ballots += 1
self.current_ballot = Ballot(ballot_length)
if self.sequential_empty_ballots >= self.empty_ballot_limit:
self.manifest_queue.put(("message", f"{self.empty_ballot_limit} unsuccessful votes in a row! Stopping timer..."))
self.current_ballot = Ballot(ballot_length, True)
continue
self.manifest_queue.put(("command", ballot_results[0]))
self.manifest_queue.put(("message", f"The winning vote is {ballot_results[0]}! Starting new vote..."))
self.manifest_queue.put(("thankyou", ballot_results[3]))
self.sequential_empty_ballots = 0
self.current_ballot = Ballot(ballot_length)
if self.yay_vote_exists:
if self.yay_vote.is_over():
self.manifest_queue.put(("message", f"The YAY vote for {self.yay_vote.command} has expired."))
self.yay_vote_exists = False
if self.yay_vote.has_passed:
self.manifest_queue.put(("command", self.yay_vote.command))
self.yay_vote_exists = False
def is_purgatory(self):
return self.sequential_empty_ballots >= self.empty_ballot_limit