-
Notifications
You must be signed in to change notification settings - Fork 0
/
votes.go
52 lines (40 loc) · 1.18 KB
/
votes.go
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
package meekstv
import "github.com/shawntoffel/meekstv/events"
func (m *meekStv) distributeVotes() {
for _, ballot := range m.Ballots {
remainder := m.Scale
for _, pref := range ballot.Preferences {
candidate := m.Pool.Candidate(pref)
votes := remainder * candidate.Weight * int64(ballot.Count) / m.Scale
m.giveVotesToCandidate(*candidate, votes)
remainder = remainder * (m.Scale - candidate.Weight) / m.Scale
if remainder == 0 {
break
}
}
}
}
func (m *meekStv) giveVotesToCandidate(meekCandidate MeekCandidate, votes int64) {
if meekCandidate.Status == Excluded {
return
}
m.Pool.AddVotes(meekCandidate.Id, votes)
}
func (m *meekStv) settleWeight(candidate MeekCandidate) {
if candidate.Votes == 0 || candidate.Status != "Elected" {
return
}
previous := candidate.Weight
newWeight := (m.Quota * previous) / candidate.Votes
remainder := newWeight % candidate.Votes
if remainder > 0 {
newWeight = newWeight + 1
}
if newWeight > m.Scale {
newWeight = m.Scale
}
if previous != newWeight {
m.Pool.SetWeight(candidate.Id, newWeight)
m.AddEvent(&events.WeightAdjusted{Name: candidate.Name, Previous: previous, Current: newWeight, Scale: m.Scale})
}
}