-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
203 lines (177 loc) · 6.21 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
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
package main
import (
"errors"
"fmt"
"html/template"
"github.com/spf13/viper"
"gopkg.in/pg.v4"
"log"
"net/http"
"strings"
)
var db *pg.DB
var urlPrefix string
type IndexResult struct {
Prefix string
Error error
}
type MatchupResults struct {
Enemy string
Prefix string
Role string
SummonerName string
Matchups []ChampionMatchup
ResultsLength int
}
func main() {
viper.SetConfigName("config")
viper.AddConfigPath(".")
viper.SetConfigType("json")
err := viper.ReadInConfig()
if err != nil {
panic(fmt.Errorf("Fatal error config file: %s \n", err))
}
username := viper.GetString("postgres.username")
password := viper.GetString("postgres.password")
urlPrefix = viper.GetString("urlPrefix")
db = pg.Connect(&pg.Options{
User: username,
Password: password,
})
err = createSchema(db)
if err != nil {
panic(err)
}
err = setupRiotApi()
if err != nil {
panic(err)
}
// var summoner MySummoner
// err := db.Model(&summoner).Column("summoner.*", "Masteries").First()
// if err != nil {
// panic(err)
// }
// select win_rate, enemy from champion_matchups where champion = 'Tryndamere' and role = 'Top' and games > 50 order by win_rate desc;
// select win_rate, champion from champion_matchups where enemy = 'Garen' and role = 'Top' and games > 50 order by win_rate desc;
// enemyChamp := "Maokai"
// enemyKey := CHAMPION_KEYS[enemyChamp]
// getSummonerMasteriesAndSave("Yaw Hide", db)
// getSummonerMasteriesAndSave("EuglossaCognata", db)
// // get all win rates between champions
// for _, champion := range CHAMPIONS {
// getWinrateForChampion(champion, db)
// }
// summoner, err := getSummonerById(NormalizeSummonerName("Yaw Hide"), db)
// if err != nil {
// panic(err)
// }
// getMatchups(summoner.SummonerId, "57", db)
serveSingle("/sitemap.xml", "./static/sitemap.xml")
serveSingle("/favicon.ico", "./static/favicon.ico")
serveSingle("/robots.txt", "./static/robots.txt")
http.HandleFunc(urlPrefix + "", Index)
http.HandleFunc(urlPrefix + "matchup", GetMatchup)
fs := http.FileServer(http.Dir("static"))
http.Handle(urlPrefix + "static/", http.StripPrefix(urlPrefix + "static/", fs))
fmt.Println("Server started")
// analytics
http.HandleFunc(urlPrefix + "analytics/index", AnalyzeIndex)
http.HandleFunc(urlPrefix + "analytics/matchup", AnalyzeMatchup)
http.HandleFunc(urlPrefix + "analytics/external", AnalyzeExternalLink)
err = http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}
func serveSingle(pattern string, filename string) {
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filename)
})
}
func Index(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
result := IndexResult{urlPrefix, nil}
t, _ := template.ParseFiles("index.html")
a := AnalyticsPage{"/", r.Referer()}
// fmt.Println(a)
err := db.Create(&a)
if err != nil {
fmt.Println("Failed to save page analyics", err)
}
t.Execute(w, result)
} else {
http.Redirect(w, r, "/", 301)
}
}
func GetMatchup(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" {
// redirect to /
http.Redirect(w, r, "/", 301)
} else if r.Method == "POST" {
r.ParseForm()
enemy := r.Form.Get("enemy")
region := strings.ToLower(r.Form.Get("region"))
role := r.Form.Get("role")
summonerName := r.Form.Get("name")
if summonerName == "" || enemy == "" || role == "" || !RIOT_REGIONS[region] {
var err string
if summonerName == "" {
err = "Summoner Name"
} else if enemy == "" {
err = "Enemy Champion"
} else if role == "" {
err = "Role"
} else if !RIOT_REGIONS[region] {
err = "Region"
}
t, _ := template.ParseFiles("index.html")
result := IndexResult{urlPrefix, errors.New(err)}
t.Execute(w, result)
return
}
enemy = NormalizeChampion(enemy)
if CHAMPION_KEYS[enemy] == "" {
t, _ := template.ParseFiles("index.html")
result := IndexResult{urlPrefix, errors.New("Enemy Champion")}
t.Execute(w, result)
return
}
// fmt.Println("enemy:", enemy, CHAMPION_KEYS[enemy])
// fmt.Println("region:", region)
// fmt.Println("role:", role)
// fmt.Println("summoner name:", summonerName)
a := AnalyticsPage{"/matchup", r.Referer()}
// fmt.Println(a)
err := db.Create(&a)
if err != nil {
fmt.Println("Failed to save index analyics", err)
}
summoner, err := getOrCreateSummoner(region, summonerName, db)
if err != nil {
t, _ := template.ParseFiles("index.html")
fmt.Println("Error get or create summoner", err)
result := IndexResult{urlPrefix, err}
t.Execute(w, result)
return
}
matchups, pm, err := getMatchups(summoner.SummonerId, CHAMPION_KEYS[enemy], role, db)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
for index, matchup := range matchups {
matchups[index].SetChampion(CHAMPION_KEYS_BY_KEY_PROPER_CASING[matchup.Champion])
for _, p := range pm {
if p.Champion == matchup.Champion {
matchups[index].UpdatePersonalData(p)
}
}
}
// fmt.Println(CHAMPION_KEYS_BY_KEY_PROPER_CASING[CHAMPION_KEYS[enemy]], urlPrefix, role, summonerName, matchups)
result := MatchupResults{CHAMPION_KEYS_BY_KEY_PROPER_CASING[CHAMPION_KEYS[enemy]], urlPrefix, role, summonerName, matchups, len(matchups)}
t, _ := template.ParseFiles("matchups.html")
t.Execute(w, result)
} else {
http.Redirect(w, r, "/", 301)
}
}