1 | <?php |
||
7 | class VoteHandler |
||
8 | { |
||
9 | /** |
||
10 | * Election object. |
||
11 | * |
||
12 | * @var \Michaelc\Voting\STV\Election; |
||
13 | */ |
||
14 | protected $election; |
||
15 | |||
16 | /** |
||
17 | * Array of all ballots in election. |
||
18 | * |
||
19 | * @var \MichaelC\Voting\STV\Ballot[] |
||
20 | */ |
||
21 | protected $ballots; |
||
22 | |||
23 | /** |
||
24 | * Quota of votes needed for a candidate to be elected. |
||
25 | * |
||
26 | * @var int |
||
27 | */ |
||
28 | protected $quota; |
||
29 | |||
30 | /** |
||
31 | * Number of candidates elected so far. |
||
32 | * |
||
33 | * @var int |
||
34 | */ |
||
35 | protected $electedCandidates; |
||
36 | |||
37 | /** |
||
38 | * Invalid ballots. |
||
39 | * |
||
40 | * @var \MichaelC\Voting\STV\Ballot[] |
||
41 | */ |
||
42 | protected $rejectedBallots; |
||
43 | |||
44 | /** |
||
45 | * Logger. |
||
46 | * |
||
47 | * @var \Psr\Log\LoggerInterface |
||
48 | */ |
||
49 | protected $logger; |
||
50 | |||
51 | /** |
||
52 | * Constructor. |
||
53 | * |
||
54 | * @param Election $election |
||
55 | */ |
||
56 | 1 | public function __construct(Election $election, Logger $logger) |
|
63 | |||
64 | /** |
||
65 | * Run the election. |
||
66 | * |
||
67 | * @return \MichaelC\Voting\STV\Candidate[] Winning candidates |
||
68 | */ |
||
69 | 1 | public function run() |
|
70 | { |
||
71 | 1 | $this->logger->notice('Starting to run an election'); |
|
72 | |||
73 | 1 | $this->rejectInvalidBallots(); |
|
74 | 1 | $this->quota = $this->getQuota(); |
|
75 | |||
76 | 1 | $this->firstStep(); |
|
77 | |||
78 | 1 | $candidates = $this->election->getActiveCandidates(); |
|
79 | |||
80 | 1 | while ($this->electedCandidates < $this->election->getWinnersCount()) { |
|
81 | 1 | if (!$this->checkCandidates($candidates)) { |
|
82 | $this->eliminateCandidates($candidates); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | 1 | $this->logger->notice('Election complete'); |
|
87 | |||
88 | 1 | return $this->election->getElectedCandidates(); |
|
89 | } |
||
90 | |||
91 | /** |
||
92 | * Perform the initial vote allocation. |
||
93 | * |
||
94 | * @return |
||
95 | */ |
||
96 | 1 | protected function firstStep() |
|
97 | { |
||
98 | 1 | $this->logger->info('Beginning the first step'); |
|
99 | |||
100 | 1 | foreach ($this->ballots as $i => $ballot) { |
|
101 | 1 | $this->logger->debug("Processing ballot $i in stage 1", |
|
102 | 1 | ['ballot' => $ballot] |
|
103 | ); |
||
104 | |||
105 | 1 | $this->allocateVotes($ballot); |
|
106 | } |
||
107 | |||
108 | 1 | $this->logger->notice('First step complete', |
|
109 | 1 | ['candidatesStatus' => $this->election->getCandidatesStatus()] |
|
110 | ); |
||
111 | |||
112 | 1 | return; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * Check if any candidates have reached the quota and can be elected. |
||
117 | * |
||
118 | * @param array $candidates Array of active candidates to check |
||
119 | * |
||
120 | * @return bool Whether any candidates were changed to elected |
||
121 | */ |
||
122 | 1 | protected function checkCandidates(array $candidates): bool |
|
123 | { |
||
124 | 1 | $elected = false; |
|
125 | |||
126 | 1 | $this->logger->info('Checking if candidates have passed quota'); |
|
127 | |||
128 | 1 | foreach ($candidates as $i => $candidate) { |
|
129 | 1 | $this->logger->debug('Checking candidate', ['candidate' => $candidate]); |
|
130 | |||
131 | 1 | if ($candidate->getVotes() >= $this->quota) { |
|
132 | 1 | $this->electCandidate($candidate); |
|
133 | 1 | $elected = true; |
|
134 | } |
||
135 | } |
||
136 | |||
137 | 1 | $this->logger->info("Candidate checking complete. Someone was elected: $elected"); |
|
138 | |||
139 | 1 | return $elected; |
|
140 | } |
||
141 | |||
142 | /** |
||
143 | * Allocate the next votes from a Ballot. |
||
144 | * |
||
145 | * @param Ballot $ballot The ballot to allocate the votes from |
||
146 | * @param float $multiplier Number to multiply the weight by (surplus) |
||
147 | * @param float $divisor The divisor of the weight (Total number of |
||
148 | * candidate votes) |
||
149 | * |
||
150 | * @return Ballot The same ballot passed in modified |
||
151 | */ |
||
152 | 1 | protected function allocateVotes(Ballot $ballot, float $multiplier = 1.0, float $divisor = 1.0): Ballot |
|
153 | { |
||
154 | 1 | $weight = $ballot->setWeight(($ballot->getWeight() * $multiplier) / $divisor); |
|
155 | 1 | $candidate = $ballot->getNextChoice(); |
|
156 | |||
157 | // TODO: Check if candidate is withdrawn |
||
158 | |||
159 | 1 | $this->logger->debug('Allocating votes of ballot', array( |
|
160 | 1 | 'ballot' => $ballot, |
|
161 | 1 | 'weight' => $weight, |
|
162 | 1 | 'candidate' => $candidate, |
|
163 | )); |
||
164 | |||
165 | 1 | if ($candidate !== null) { |
|
166 | 1 | $this->election->getCandidate($candidate)->addVotes($weight); |
|
167 | 1 | $ballot->incrementLevelUsed(); |
|
168 | 1 | $this->logger->debug('Vote added to candidate'); |
|
169 | } |
||
170 | |||
171 | 1 | return $ballot; |
|
172 | } |
||
173 | |||
174 | /** |
||
175 | * Transfer the votes from one candidate to other candidates. |
||
176 | * |
||
177 | * @param float $surplus The number of surplus votes to transfer |
||
178 | * @param Candidate $candidate The candidate being elected to transfer |
||
179 | * the votes from |
||
180 | * |
||
181 | * @return |
||
182 | */ |
||
183 | 1 | protected function transferSurplusVotes(float $surplus, Candidate $candidate) |
|
184 | { |
||
185 | 1 | $totalVotes = $candidate->getVotes(); |
|
186 | 1 | $candidateId = $candidate->getId(); |
|
187 | |||
188 | 1 | $this->logger->info('Transfering surplus votes', array( |
|
189 | 1 | 'surplus' => $surplus, |
|
190 | 1 | 'candidate' => $candidate, |
|
191 | 1 | 'totalVotes' => $totalVotes, |
|
192 | )); |
||
193 | |||
194 | 1 | foreach ($this->ballots as $i => $ballot) { |
|
195 | 1 | if ($ballot->getLastChoice() == $candidateId) { |
|
196 | 1 | $this->allocateVotes($ballot, $surplus, $totalVotes); |
|
197 | } |
||
198 | } |
||
199 | |||
200 | 1 | return; |
|
201 | } |
||
202 | |||
203 | /** |
||
204 | * Transfer the votes from one eliminated candidate to other candidates. |
||
205 | * |
||
206 | * @param Candidate $candidate Candidate being eliminated to transfer |
||
207 | * the votes from |
||
208 | * |
||
209 | * @return |
||
210 | */ |
||
211 | protected function transferEliminatedVotes(Candidate $candidate) |
||
228 | |||
229 | /** |
||
230 | * Elect a candidate after they've passed the threshold. |
||
231 | * |
||
232 | * @param \Michaelc\Voting\STV\Candidate $candidate |
||
233 | */ |
||
234 | 1 | protected function electCandidate(Candidate $candidate) |
|
235 | { |
||
236 | 1 | if ($candidate->getVotes() < $this->quota) { |
|
237 | throw new VotingException("We shouldn't be electing someone who hasn't met the quota"); |
||
238 | } |
||
239 | |||
240 | 1 | $this->logger->notice('Electing a candidate', array( |
|
241 | 1 | 'candidate' => $candidate, |
|
242 | )); |
||
243 | |||
244 | 1 | $candidate->setState(Candidate::ELECTED); |
|
245 | 1 | ++$this->electedCandidates; |
|
246 | |||
247 | 1 | if ($this->electedCandidates < $this->election->getWinnersCount()) { |
|
248 | 1 | $surplus = $candidate->getVotes() - $this->quota; |
|
249 | 1 | $this->transferSurplusVotes($surplus, $candidate); |
|
250 | } |
||
251 | |||
252 | 1 | return; |
|
253 | } |
||
254 | |||
255 | /** |
||
256 | * Eliminate the candidate with the lowest number of votes |
||
257 | * and reallocated their votes. |
||
258 | * |
||
259 | * TODO: Eliminate all lowest candidates after step one, then |
||
260 | * randomly choose. |
||
261 | * |
||
262 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
263 | * Array of active candidates |
||
264 | * |
||
265 | * @return int Number of candidates eliminated |
||
266 | */ |
||
267 | protected function eliminateCandidates(array $candidates): int |
||
282 | |||
283 | /** |
||
284 | * Get candidates with the lowest number of votes. |
||
285 | * |
||
286 | * @param \Michaelc\Voting\STV\Candidate[] $candidates |
||
287 | * Array of active candidates |
||
288 | * |
||
289 | * @return \Michaelc\Voting\STV\Candidate[] |
||
290 | * Candidates with lowest score |
||
291 | */ |
||
292 | public function getLowestCandidates(array $candidates): array |
||
313 | |||
314 | /** |
||
315 | * Reject any invalid ballots. |
||
316 | * |
||
317 | * @return int Number of rejected ballots |
||
318 | */ |
||
319 | 1 | protected function rejectInvalidBallots(): int |
|
336 | |||
337 | /** |
||
338 | * Check if ballot is valid. |
||
339 | * |
||
340 | * @param Ballot $ballot Ballot to test |
||
341 | * |
||
342 | * @return bool True if valid, false if invalid |
||
343 | */ |
||
344 | 1 | public function checkBallotValidity(Ballot $ballot): bool |
|
370 | |||
371 | /** |
||
372 | * Get the quota to win. |
||
373 | * |
||
374 | * @return int |
||
375 | */ |
||
376 | 1 | public function getQuota(): int |
|
388 | } |
||
389 |