/
helpers.go
100 lines (88 loc) · 1.87 KB
/
helpers.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
package main
import (
"bytes"
"encoding/json"
"fmt"
"image/color"
"io"
"os"
"regexp"
"strconv"
)
func secondsToTime(timer int) (hours, minutes, seconds int) {
minutes = timer / 60
seconds = timer % 60
hours = minutes / 60
minutes = minutes % 60
return hours, minutes, seconds
}
func prettifyBigValues(value int) string {
if value < 1000 {
return strconv.Itoa(value)
} else {
result := fmt.Sprintf("%.1fk", float64(value)/1000)
return result
}
}
func getPlayerLane(role int) string {
laneNames := [5]string{"Unknown", "Safe", "Mid", "Off", "Jun"}
return laneNames[role]
}
func getLeagueName(leagueID int, leaguesData oDotaLeaguesData) string {
for _, league := range leaguesData {
if league.Leagueid == leagueID {
return league.Name
}
}
return "League"
}
func loadConfig(path string, target interface{}) error {
buf := bytes.NewBuffer(nil)
f, _ := os.Open(path)
io.Copy(buf, f)
f.Close()
err := json.Unmarshal(buf.Bytes(), &target)
if err != nil {
return err
}
return nil
}
func stripPresentedBy(leagueName string) string {
m := regexp.MustCompile("[ –-]*(presented|powered) by.+$")
poweredByRemoved := m.ReplaceAllString(string(leagueName), "")
return poweredByRemoved
}
func getTeamName(name string, isRadiant bool) string {
if name != "" {
return name
}
if isRadiant {
return "Radiant"
}
return "Dire"
}
func parseHexColor(s string) (c color.RGBA) {
switch len(s) {
case 9:
_, _ = fmt.Sscanf(s, "#%02x%02x%02x%02x", &c.R, &c.G, &c.B, &c.A)
case 7:
c.A = 0xff
_, _ = fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B)
case 5:
c.A = 0xff
_, _ = fmt.Sscanf(s, "#%1x%1x%1x%1x", &c.R, &c.G, &c.B, &c.A)
// Double the hex digits:
c.R *= 17
c.G *= 17
c.B *= 17
c.A *= 17
case 4:
c.A = 0xff
_, _ = fmt.Sscanf(s, "#%1x%1x%1x", &c.R, &c.G, &c.B)
// Double the hex digits:
c.R *= 17
c.G *= 17
c.B *= 17
}
return
}