-
Notifications
You must be signed in to change notification settings - Fork 4
/
admin_votes.go
181 lines (170 loc) · 4.26 KB
/
admin_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
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
package main
import (
"errors"
"net/http"
"strings"
"time"
"github.com/gorilla/mux"
)
type Ranking struct {
Rank int
Teams []Team
}
// getCondorcetResult returns the ranking of teams based on the condorcet method
// https://en.wikipedia.org/wiki/Condorcet_method
func getCondorcetResult() []Ranking {
type teamPair struct {
winner *Team
loser *Team
majority float32
}
var allPairs []teamPair
var ret []Ranking
for i := 0; i < len(m.jam.Teams); i++ {
for j := i + 1; j < len(m.jam.Teams); j++ {
// For each pairing find a winner
winner, pct, _ := findWinnerBetweenTeams(&m.jam.Teams[i], &m.jam.Teams[j])
newPair := new(teamPair)
if winner != nil {
newPair.winner = winner
if winner.UUID == m.jam.Teams[i].UUID {
newPair.loser = &m.jam.Teams[j]
} else {
newPair.loser = &m.jam.Teams[i]
}
newPair.majority = pct
} else {
newPair.winner = &m.jam.Teams[i]
newPair.loser = &m.jam.Teams[j]
newPair.majority = 50
}
allPairs = append(allPairs, *newPair)
}
}
// initialize map of team wins
teamWins := make(map[string]int)
for i := range m.jam.Teams {
teamWins[m.jam.Teams[i].UUID] = 0
}
// Figure out how many wins each team has
for i := range allPairs {
if allPairs[i].majority != 50 {
teamWins[allPairs[i].winner.UUID]++
}
}
// Rank them by wins
rankedWins := make(map[int][]string)
for k, v := range teamWins {
rankedWins[v] = append(rankedWins[v], k)
}
currRank := 1
for len(rankedWins) > 0 {
topWins := 0
for k, _ := range rankedWins {
if k > topWins {
topWins = k
}
}
nR := new(Ranking)
nR.Rank = currRank
for i := range rankedWins[topWins] {
tm, _ := m.jam.GetTeamById(rankedWins[topWins][i])
if tm != nil {
nR.Teams = append(nR.Teams, *tm)
}
}
ret = append(ret, *nR)
delete(rankedWins, topWins)
currRank++
}
return ret
}
// This is a helper function for calculating results
func uuidIsInRankingSlice(uuid string, sl []Ranking) bool {
for _, v := range sl {
for i := range v.Teams {
if v.Teams[i].UUID == uuid {
return true
}
}
}
return false
}
// findWinnerBetweenTeams returns the team that got the most votes
// and the percentage of votes they received
// or an error if a winner couldn't be determined.
func findWinnerBetweenTeams(tm1, tm2 *Team) (*Team, float32, error) {
// tally gets incremented for a tm1 win, decremented for a tm2 win
var tm1votes, tm2votes float32
for _, v := range m.jam.Votes {
for _, chc := range v.Choices {
if chc.Team == tm1.UUID {
tm1votes++
break
} else if chc.Team == tm2.UUID {
tm2votes++
break
}
}
}
ttlVotes := tm1votes + tm2votes
if tm1votes > tm2votes {
return tm1, 100 * (tm1votes / ttlVotes), nil
} else if tm1votes < tm2votes {
return tm2, 100 * (tm2votes / ttlVotes), nil
}
return nil, 50, errors.New("Unable to determine a winner")
}
func getInstantRunoffResult() []Team {
var ret []Team
return ret
}
func handleAdminVotes(w http.ResponseWriter, req *http.Request, page *pageData) {
vars := mux.Vars(req)
page.SubTitle = "Votes"
type vpdVote struct {
Timestamp string
ClientId string
Choices []Team
VoterStatus string
Discovery string
}
type votePageData struct {
AllVotes []vpdVote
Results []Ranking
VoterStatuses map[string]int
}
vpd := new(votePageData)
vpd.VoterStatuses = make(map[string]int)
now := time.Now()
dayThresh := time.Date(now.Year(), now.Month(), now.Day(), 0, 0, 0, 0, now.Location())
for i := range m.jam.Votes {
v := new(vpdVote)
if m.jam.Votes[i].Timestamp.Before(dayThresh) {
v.Timestamp = m.jam.Votes[i].Timestamp.Format("Jan _2 15:04")
} else {
v.Timestamp = m.jam.Votes[i].Timestamp.Format(time.Kitchen)
}
v.ClientId = m.jam.Votes[i].ClientId
for _, choice := range m.jam.Votes[i].Choices {
for _, fndTm := range m.jam.Teams {
if fndTm.UUID == choice.Team {
v.Choices = append(v.Choices, fndTm)
break
}
}
}
v.VoterStatus = m.jam.Votes[i].VoterStatus
if strings.TrimSpace(v.VoterStatus) != "" {
vpd.VoterStatuses[v.VoterStatus]++
}
v.Discovery = m.jam.Votes[i].Discovery
vpd.AllVotes = append(vpd.AllVotes, *v)
}
vpd.Results = getCondorcetResult()
page.TemplateData = vpd
switch vars["function"] {
default:
page.show("admin-votes.html", w)
}
}