-
Notifications
You must be signed in to change notification settings - Fork 0
/
bgl.go
151 lines (120 loc) · 2.93 KB
/
bgl.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
// This file was generated from JSON Schema using quicktype, do not modify it directly.
// To parse and unparse this JSON data, add this code to your project and do:
//
// matchResult, err := UnmarshalMatchResult(bytes)
// bytes, err = matchResult.Marshal()
package main
import (
"encoding/json"
"io/ioutil"
"log"
"net/http"
"reflect"
)
func GetBGLTeams() TeamsResponse {
url := "https://api.beegame.gg/teams/?is_active=true"
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
log.Println(err)
}
res, err := client.Do(req)
if err != nil {
log.Println(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
var teamsResult TeamsResponse
err = json.Unmarshal(body, &teamsResult)
if err != nil {
log.Println(err)
}
return teamsResult
}
func GetTeamInfo(c chan []Team) {
log.Println("Fetching Team Information from BGL...")
bglTeams := GetBGLTeams()
teams := []Team{}
for _, v := range bglTeams.Teams {
team := Team{}
team.Name = v.Name
team.Tier = v.Circuit
team.Img = KQBAvatarImage
team.Stats.MatchesWon = v.Wins
team.Stats.MatchesLost = v.Losses
teams = append(teams, team)
}
c <- teams
}
func GetBGLMatches() MatchResponse {
// url := "https://api.beegame.gg/matches/?days=7&scheduled=true&limit=100"
url := "https://api.beegame.gg/matches/?round_is_current=true&limit=100"
method := "GET"
client := &http.Client{}
req, err := http.NewRequest(method, url, nil)
if err != nil {
log.Println(err)
}
res, err := client.Do(req)
if err != nil {
log.Println(err)
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Println(err)
}
var matchResponse MatchResponse
err = json.Unmarshal(body, &matchResponse)
if err != nil {
log.Println(err)
}
return matchResponse
}
func MatchesToTeamMap(matches MatchResponse) MatchMap {
matchMap := make(MatchMap)
for _, match := range matches.Results {
if isNilFixed(match.Result) {
title := formatTeamName(match.Away.Name) + " vs " + formatTeamName(match.Home.Name)
home := Team{
Name: match.Home.Name,
Img: KQBAvatarImage,
Stats: Stats{
MatchesWon: match.Home.Wins,
MatchesLost: match.Home.Losses,
},
}
away := Team{
Name: match.Away.Name,
Img: KQBAvatarImage,
Stats: Stats{
MatchesWon: match.Away.Wins,
MatchesLost: match.Away.Losses,
},
}
matchMap[title] = []Team{away, home}
}
}
return matchMap
}
func GetMatchInfo(c chan MatchMap) {
log.Println("Fetching Match Information from BGL...")
matches := GetBGLMatches()
matchMap := MatchesToTeamMap(matches)
c <- matchMap
}
type MatchMap map[string][]Team
func isNilFixed(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Ptr, reflect.Map, reflect.Array, reflect.Chan, reflect.Slice:
return reflect.ValueOf(i).IsNil()
}
return false
}