/
main.go
115 lines (99 loc) · 2.93 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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strconv"
"fyne.io/fyne/app"
)
const (
IglAPIURL = "https://indy-gaming-league-api.herokuapp.com/api/circuits/"
IglWestID = "5e4c6b5178d46abdfeb49e71"
IglEastID = "5e4b295560f132acbb31b8f5"
PlaceholderImage = "https://bulma.io/images/placeholders/128x128.png"
KQBAvatarImage = "/avatar.png"
)
type Team struct {
Name string
Img string
Div int
Tier int
Stats Stats
}
type Stats struct {
GamesWon int
GamesLost int
MatchesWon int
MatchesLost int
}
var h Team = Team{"Blue Team", PlaceholderImage, 1, 1, Stats{1, 1, 1, 1}}
var a Team = Team{"Gold Team", PlaceholderImage, 1, 1, Stats{1, 1, 1, 1}}
var s Scoreboard = Scoreboard{&h, &a, 0, 0, 0, 0}
func setupLogs() {
f, err := os.OpenFile("./output.log", os.O_APPEND|os.O_CREATE|os.O_RDWR, 0666)
if err != nil {
fmt.Printf("error opening file: %v", err)
}
// don't forget to close it
// defer f.Close()
// assign it to the standard logger
log.SetOutput(f)
}
func main() {
// Configure log output to file
setupLogs()
// Setup HTTP handler and websocket handler
StartHTTPServer()
// Create Fyne App
myApp := app.New()
myApp.SetIcon(resourceIconPng)
_ = GameType(myApp)
myApp.Run()
// Called before application exist
tidyUp()
}
func GetTeamInfo(url string, c chan []Team) {
fmt.Println("Fetching Team Information from IGL...")
resp, err := http.Get(url)
if err != nil {
// handle error
log.Println("Could not fetch team info from IGL")
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// jsonFile, err := os.Open("response.json")
// // if we os.Open returns an error then handle it
// if err != nil {
// fmt.Println(err)
// }
// fmt.Println("Successfully Opened users.json")
// // defer the closing of our jsonFile so that we can parse it later on
// defer jsonFile.Close()
// byteValue, _ := ioutil.ReadAll(jsonFile)
var result map[string]interface{}
json.Unmarshal([]byte(body), &result)
results := result["data"].([]interface{})
teams := []Team{}
for _, v := range results {
t := v.(map[string]interface{})["team"]
s := v.(map[string]interface{})["stats"]
test := Team{}
test.Name = t.(map[string]interface{})["formattedName"].(string)
test.Tier = int(t.(map[string]interface{})["tier"].(float64))
test.Div = int(t.(map[string]interface{})["div"].(float64))
if t.(map[string]interface{})["logo"] != nil {
test.Img = t.(map[string]interface{})["logo"].(string)
} else {
test.Img = KQBAvatarImage
}
test.Stats.GamesWon, _ = strconv.Atoi(s.(map[string]interface{})["Games Won"].(string))
test.Stats.GamesLost, _ = strconv.Atoi(s.(map[string]interface{})["Games Lost"].(string))
test.Stats.MatchesWon, _ = strconv.Atoi(s.(map[string]interface{})["Matches Won"].(string))
test.Stats.MatchesLost, _ = strconv.Atoi(s.(map[string]interface{})["Matches Lost"].(string))
teams = append(teams, test)
}
c <- teams
}