/
main.go
266 lines (236 loc) · 6.63 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strconv"
"sync"
"syscall"
"github.com/manifoldco/promptui"
)
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() {
setupLogs()
var wg sync.WaitGroup
wg.Add(1)
StartHTTPServer()
SetupCloseHandler()
matchType := PromptIGLCustom()
log.Println("Selected Match Type: ", matchType)
if matchType == "Custom" {
s = CustomFlow()
} else if matchType == "IGL" {
s = IGLMatchFlow()
}
RunMatch()
wg.Wait()
}
func RunMatch() {
UpdateScoreBoard(&s)
for s.HomeGames < 3 && s.AwayGames < 3 {
err := RecordMapScore(&s)
// Allow exit out if prompt errors out
if err != nil {
os.Exit(0)
}
UpdateScoreBoard(&s)
}
fmt.Println("Game Complete! ")
fmt.Printf("Final Score: \n %s: %d\n %s: %d\n", s.Home.Name, s.HomeGames, s.Away.Name, s.AwayGames)
}
func IGLMatchFlow() Scoreboard {
apiUrl, err := PromptIglCircuit()
if err != nil {
os.Exit(0)
}
teams := GetTeamInfo(apiUrl)
home, err := PromptTeam("Blue", teams)
if err != nil {
os.Exit(0)
}
away, err := PromptTeam("Gold", teams)
if err != nil {
os.Exit(0)
}
return Scoreboard{&home, &away, 0, 0, 0, 0}
}
func GetTeamInfo(url string) []Team {
fmt.Println("Fetching Team Information from IGL...")
resp, err := http.Get(url)
if err != nil {
// handle error
}
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)
}
return teams
}
func PromptIglCircuit() (string, error) {
prompt := promptui.Select{
Label: fmt.Sprintf("Select IGL Circuit:"),
Items: []string{"KQB East", "KQB West"},
}
i, _, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return "", errors.New("Invalid Team Selection")
}
var url string
if i == 0 {
url = fmt.Sprintf("%s%s/results?bucket=igl-teamlogopics", IglAPIURL, IglEastID)
} else {
url = fmt.Sprintf("%s%s/results?bucket=igl-teamlogopics", IglAPIURL, IglWestID)
}
return url, nil
}
// PromptTeam
func PromptTeam(name string, teams []Team) (Team, error) {
// Any type can be given to the select's item as long as the templates properly implement the dot notation
// to display it.
// const emoji = "\U0001F41D"
var color string
if name == "Blue" {
color = "cyan"
} else {
color = "yellow"
}
templates := &promptui.SelectTemplates{
// Label: "{{ . }}?",
Active: fmt.Sprintf("%s {{ .Name | %s | bold }}", promptui.IconSelect, color),
Inactive: fmt.Sprintf(" {{ .Name | %s }}", color),
Selected: fmt.Sprintf("%s {{ .Name | %s | bold }}", promptui.IconGood, color),
Details: `
--------- Team ----------
{{ "Name:" | faint }} {{ .Name }}
{{ "Tier:" | faint }} {{ .Tier }}
{{ "Division:" | faint }} {{ .Div }}`,
}
prompt := promptui.Select{
Label: fmt.Sprintf("Select %s Team", name),
Items: teams,
Templates: templates,
Size: 6,
}
i, _, err := prompt.Run()
if err != nil {
log.Printf("Prompt failed %v\n", err)
return Team{}, errors.New("Map Score Prompt Failed")
}
return teams[i], nil
}
// RecordMapScore
func RecordMapScore(s *Scoreboard) error {
// Any type can be given to the select's item as long as the templates properly implement the dot notation
// to display it.
type MapWinPrompt struct {
Option string
Color string
ID int
}
// const emoji = "\U0001F525"
m := []MapWinPrompt{
{fmt.Sprintf("%s (Blue) Won Map", s.Home.Name), "cyan", 0},
{fmt.Sprintf("%s (Gold) Won Map", s.Away.Name), "yellow", 1},
}
templates := &promptui.SelectTemplates{
Label: fmt.Sprintf("%s {{ . | red | bold }}?", promptui.IconInitial),
Active: fmt.Sprintf("%s {{ .Option | red | bold }}", promptui.IconSelect),
Inactive: " {{ .Option | faint }}",
Selected: fmt.Sprintf("%s {{ .Option | red | bold }}", promptui.IconGood),
}
prompt := promptui.Select{
Label: fmt.Sprintf("Select Who Won Map"),
Items: m,
Templates: templates,
Size: 2,
}
i, _, err := prompt.Run()
if m[i].ID == 0 {
s.IncrementHome()
fmt.Printf("%s Score: %d Games %d Maps\n", s.Home.Name, s.HomeGames, s.HomeMaps)
} else if m[i].ID == 1 {
s.IncrementAway()
fmt.Printf("%s: %d Games %d Maps\n", s.Away.Name, s.AwayGames, s.AwayMaps)
}
if err != nil {
log.Printf("Map Score Prompt failed %v\n", err)
return errors.New("Map Score Prompt Failed")
}
return nil
}
func SetupCloseHandler() {
c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
fmt.Println("\r- Ctrl+C pressed in Terminal")
os.Exit(0)
}()
}