-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
152 lines (123 loc) · 3.42 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"html/template"
"log"
"net/http"
"sort"
"strings"
)
var templates *template.Template
func httpInit() {
// Handler init
http.HandleFunc("/", mainHandler)
http.HandleFunc("/new-pool/", newPoolHandler)
http.HandleFunc("/pool/", poolHandler)
http.HandleFunc("/save/", saveHandler)
http.HandleFunc("/pool/edit/", editHandler)
http.HandleFunc("/pool/glicko/", glickoHandler)
http.HandleFunc("/pool/tournament/", tournamentHandler)
// Fileserver init
http.Handle("/pools/", http.StripPrefix("/pools/", http.FileServer(http.Dir("pools"))))
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("static"))))
// Template init
templates = template.Must(template.ParseFiles("tmpl/main.html", "tmpl/new-pool.html", "tmpl/pool.html"))
}
// Call API and train dataset
func trainSet(poolName string) {
// MAKE SURE EMPTY POOL EXISTS
var urls []string
suffix := "?api_key=hRnTyyGav36S6bxcJzhY37kQktPrHs33mmTUXjti&include_participants=1&include_matches=1"
// Non-prefixed ones
for i := 3; i < 10; i++ {
for j := 0; j < 10; j++ {
// Special cases
if i == 3 && j == 0 {
continue
}
if i == 3 && j == 7 {
continue
}
url := fmt.Sprintf("https://api.challonge.com/v1/tournaments/4qm%d%d%s", i, j, suffix)
urls = append(urls, url)
}
}
//100 - 106
for i := 0; i < 5; i++ {
url := fmt.Sprintf("https://api.challonge.com/v1/tournaments/4qm10%d%s", i, suffix)
urls = append(urls, url)
}
// Prefixed ones
for i := 0; i < 3; i++ {
for j := 0; j < 10; j++ {
if i == 0 && j < 5 {
continue
}
if i == 2 && j == 2 {
break
}
url := fmt.Sprintf("https://api.challonge.com/v1/tournaments/4qs-4qm1%d%d%s", i, j, suffix)
urls = append(urls, url)
}
}
for _, url := range urls {
println(url)
}
// Calculate glicko for each tournament URL
for _, url := range urls {
pool, err := readPool(poolName)
if err != nil {
log.Fatal(err)
}
println("CALCULATING ", url)
glicko := calculateGlicko(getTournament(url), 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, poolName)
if err != nil {
log.Fatal(err)
}
println("SUCCESS FOR ", url)
}
}
// Writes the seedings and placements for a given tournament in URL.
func writeSeedsAndPlacements(url, n string) {
r := getTournament(url)
var d strings.Builder
d.WriteString("Name Seed Placement\n")
for _, s := range r.Participants {
d.WriteString(fmt.Sprintf("%s %d %d\n", s.Participant.Name, s.Participant.Seed, s.Participant.Placement))
}
writeTestResults(n, []byte(d.String()))
}
func main() {
//trainSet("4-quarters")
//writeSeedsAndPlacements("https://api.challonge.com/v1/tournaments/4qs-4qm132?api_key=hRnTyyGav36S6bxcJzhY37kQktPrHs33mmTUXjti&include_participants=1&include_matches=1", "132")
log.SetFlags(log.Llongfile)
httpInit()
println("Listening on 8080...")
http.ListenAndServe(":8080", nil)
}