-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.go
319 lines (262 loc) · 6.78 KB
/
handler.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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package main
import (
"encoding/json"
"github.com/tidwall/gjson"
"io/ioutil"
"log"
"net/http"
"reflect"
"regexp"
"sort"
"strings"
"github.com/ec16431/autournament/model"
)
func mainHandler(w http.ResponseWriter, r *http.Request) {
page := model.Page{Title: "Autournament"}
ctx := buildContext(page)
renderTemplate(w, "main", ctx)
}
func newPoolHandler(w http.ResponseWriter, r *http.Request) {
body := r.FormValue("body")
page := model.Page{r.URL.Path, []byte(body)}
ctx := buildContext(page)
renderTemplate(w, "new-pool", ctx)
}
func saveHandler(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
log.Fatal(err)
}
name := r.Form["name"]
players := r.Form["player"]
pool := model.Pool{PoolName: name[0]}
for _, p := range players {
if len(p) > 0 {
player := *model.NewPlayer(p)
pool.Players = append(pool.Players, player)
}
}
poolJson, err := json.Marshal(pool)
if err != nil {
log.Fatal(err)
}
pName, err := saveJson(poolJson, pool.PoolName)
if err != nil {
log.Fatal(err)
}
page := model.Page{Title: name[0]}
ctx := buildContext(page, pool)
ctx["pName"] = pName
renderTemplate(w, "pool", ctx)
}
func poolHandler(w http.ResponseWriter, r *http.Request) {
pName := r.URL.Path[len("/pool/"):]
if len(pName) == 0 {
pools := listPools()
page := model.Page{Title: "Pools"}
ctx := buildContext(page)
ctx["pools"] = pools
ctx["listPools"] = true
renderTemplate(w, "pool", ctx)
return
}
pName = strings.ToLower(pName)
res, err := readPool(pName)
if err != nil {
http.NotFound(w, r)
return
}
page := model.Page{Title: res.PoolName}
ctx := make(map[string]interface{})
ctx["pName"] = pName
ctx["Pool"] = res
ctx["Page"] = page
renderTemplate(w, "pool", ctx)
}
func editHandler(w http.ResponseWriter, r *http.Request) {
var editReq model.EditPlayersReq
err := json.NewDecoder(r.Body).Decode(&editReq)
if err != nil {
log.Fatal(err)
}
if len(editReq.NewPlayers) != 0 || len(editReq.RemovedPlayers) != 0 {
pool, err := readPool(editReq.Name)
if err != nil {
log.Fatal(err)
}
// Remove players
for _, r := range editReq.RemovedPlayers {
for i, p := range pool.Players {
if r == p.Name {
pool.Players = append(pool.Players[:i], pool.Players[i+1:]...)
}
}
}
// Add players
for _, p := range editReq.NewPlayers {
if playerExists(p, *pool) {
continue
}
pool.Players = append(pool.Players, *model.NewPlayer(p))
}
sort.Slice(pool.Players, func(i, j int) bool {
return pool.Players[i].Glicko.R > pool.Players[j].Glicko.R
})
j, err := json.Marshal(pool)
if err != nil {
log.Fatal(err)
}
saveJson(j, editReq.Name)
}
w.WriteHeader(http.StatusOK)
}
func glickoHandler(w http.ResponseWriter, r *http.Request) {
var glickoReq model.GlickoEditReq
err := json.NewDecoder(r.Body).Decode(&glickoReq)
if err != nil {
log.Fatal(err)
}
pool, err := readPool(glickoReq.Name)
if err != nil {
log.Fatal(err)
}
glicko := calculateGlicko(getTournament(glickoReq.ChallongeURL), pool)
// Merge new ranked players with ones that didn't participate
addPlayer := true
for _, p := range pool.Players {
for j, gp := range glicko {
if p.Name == gp.Name {
addPlayer = false
glicko[j].ResetAbsences() // Player attended
break
}
}
if addPlayer {
p.IncreaseAbsence()
glicko = append(glicko, p)
}
addPlayer = true
}
pool.Players = glicko
sort.Slice(pool.Players, func(i, j int) bool {
return pool.Players[i].Glicko.R > pool.Players[j].Glicko.R
})
j, err := json.Marshal(pool)
if err != nil {
log.Fatal(err)
}
_, err = saveJson(j, glickoReq.Name)
if err != nil {
log.Fatal(err)
}
w.WriteHeader(http.StatusOK)
}
func tournamentHandler(w http.ResponseWriter, r *http.Request) {
var tournReq model.TournamentReq
err := json.NewDecoder(r.Body).Decode(&tournReq)
if err != nil {
log.Fatal(err)
}
pool, err := readPool(tournReq.Name)
if err != nil {
log.Fatal(err)
}
var includedPlayers []model.Player
for _, n := range tournReq.Players {
for _, p := range pool.Players {
if n == p.Name {
includedPlayers = append(includedPlayers, p)
break
}
}
}
// Sort based on how much their absences should influence their placement
sort.Slice(includedPlayers, func(i, j int) bool {
p1 := includedPlayers[i]
p2 := includedPlayers[j]
return p1.Glicko.R-float64((10*p1.Absences)) > p2.Glicko.R-float64((10*p2.Absences))
})
j, err := json.Marshal(includedPlayers)
if err != nil {
log.Fatal(err)
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(j)
}
func buildContext(objs ...interface{}) map[string]interface{} {
r := regexp.MustCompile(`\w*\.`) // Remove dot-separated package prefixes
var ctx = make(map[string]interface{})
for _, o := range objs {
n := r.ReplaceAllString(reflect.TypeOf(o).String(), (""))
ctx[n] = o
}
return ctx
}
func renderTemplate(w http.ResponseWriter, tmpl string, context map[string]interface{}) {
err := templates.ExecuteTemplate(w, tmpl+".html", context)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
// Return the list of participants and matches for a given challonge bracket
func getTournament(url string) model.ChallongeReq {
tJson, err := challongeGetReq(url)
if err != nil {
log.Fatal(err)
}
pJson := gjson.Get(string(tJson), "tournament.participants")
mJson := gjson.Get(string(tJson), "tournament.matches")
// Participant list
var participants []model.ChallongeParticipant
err = json.Unmarshal([]byte(pJson.Raw), &participants)
if err != nil {
log.Fatal(err)
}
// Matches list
var matches []model.ChallongeMatch
err = json.Unmarshal([]byte(mJson.Raw), &matches)
if err != nil {
log.Fatal(err)
}
// Make ID to Name map
// REFACTOR TO OWN FUNCTION
IDMap := make(map[int]string)
for _, p := range participants {
IDMap[p.Participant.ID] = p.Participant.Name
}
res := model.ChallongeReq{participants, matches, IDMap}
return res
}
// Return JSON dataset of a Challonge tournament bracket.
func challongeGetReq(url string) ([]byte, error) {
client := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
log.Printf("ERROR- Constructing request: <%v>", err)
return nil, err
}
req.Header.Add("Accept", "application/json")
req.Header.Add("Content-Type", "application/json")
resp, err := client.Do(req)
if err != nil {
log.Println("ERROR- Calling Challonge: ", err)
return nil, err
}
defer resp.Body.Close()
contents, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println("ERROR- Reading Challonge response: ", err)
return nil, err
}
return contents, nil
}
// Returns true if player p exists in pool
func playerExists(p string, pool model.Pool) bool {
for _, poolP := range pool.Players {
if p == poolP.Name {
return true
}
}
return false
}