-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (133 loc) · 3.7 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
package main
import (
_ "embed"
"fmt"
"html/template"
"log"
"net/http"
"strings"
"time"
)
type BaseData struct {
Active string
Year int
}
var (
smbTemplate = template.Must(template.ParseFiles("templates/smb.html", "templates/base.html"))
maddenTemplate = template.Must(template.ParseFiles("templates/madden.html", "templates/base.html"))
ff2023Template = template.Must(template.ParseFiles("templates/ff2023.html", "templates/base.html"))
ff2024Template = template.Must(template.ParseFiles("templates/ff2024.html", "templates/base.html"))
basementTemplate = template.Must(template.ParseFiles("templates/basement.html", "templates/base.html"))
)
//go:embed ff2023_stats.txt
var ff2023Stats string
//go:embed static/bracket.svg
var bracketSVG string
type PlayerStats struct {
Rank string
Name template.HTML
WinLossTie string
PointsFor string
PointsAgainst string
Streak string
WaiverBudget string
Waiver string
Moves string
}
var players []PlayerStats
var playerLinks = map[template.HTML]string{
"Goin rogue": "https://www.youtube.com/watch?v=aX97OF1p4nU&t=48s",
}
func main() {
// Skip the first line, which is just column headers.
for _, line := range strings.Split(ff2023Stats, "\n")[1:] {
log.Printf("processing line %q", line)
fields := strings.Split(line, "\t")
if len(fields) != 9 {
log.Printf("expected to find 9 fields, but %d for line: %q", len(fields), line)
continue
}
player := PlayerStats{
Rank: strings.ReplaceAll(fields[0], "*", ""),
Name: template.HTML(fields[1][5:]), // Note: This always starts with the useless string "logo".
WinLossTie: fields[2],
PointsFor: fields[3],
PointsAgainst: fields[4],
Streak: fields[5],
WaiverBudget: fields[6],
Waiver: fields[7],
Moves: fields[8],
}
if link, ok := playerLinks[player.Name]; ok {
player.Name = template.HTML(fmt.Sprintf(`<a href="%s">%s</a>`, link, player.Name))
}
players = append(players, player)
}
http.HandleFunc("/", index)
http.HandleFunc("/madden", madden)
http.HandleFunc("/ff2023", ff2023)
http.HandleFunc("/ff2024", ff2024)
http.HandleFunc("/basement", basement)
http.Handle("/static/", http.StripPrefix("/static/", http.FileServer(http.Dir("./static"))))
log.Printf("Starting...")
log.Fatal(http.ListenAndServe(":8080", nil))
}
func index(response http.ResponseWriter, request *http.Request) {
var data = &struct {
BaseData BaseData
}{
BaseData: BaseData{
Active: "smb",
Year: time.Now().Year(),
},
}
smbTemplate.ExecuteTemplate(response, "base", data)
}
func madden(response http.ResponseWriter, request *http.Request) {
var data = &struct {
BaseData BaseData
}{
BaseData: BaseData{
Active: "madden",
Year: time.Now().Year(),
},
}
maddenTemplate.ExecuteTemplate(response, "base", data)
}
func ff2023(response http.ResponseWriter, request *http.Request) {
var data = &struct {
Players []PlayerStats
Bracket template.HTML
BaseData BaseData
}{
Players: players,
BaseData: BaseData{
Active: "ff2023",
Year: time.Now().Year(),
},
Bracket: template.HTML(bracketSVG),
}
ff2023Template.ExecuteTemplate(response, "base", data)
}
func ff2024(response http.ResponseWriter, request *http.Request) {
var data = &struct {
BaseData BaseData
}{
BaseData: BaseData{
Active: "ff2024",
Year: time.Now().Year(),
},
}
ff2024Template.ExecuteTemplate(response, "base", data)
}
func basement(response http.ResponseWriter, request *http.Request) {
var data = &struct {
BaseData BaseData
}{
BaseData: BaseData{
Active: "basement",
Year: time.Now().Year(),
},
}
basementTemplate.ExecuteTemplate(response, "base", data)
}