-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathformat.go
More file actions
93 lines (78 loc) · 1.95 KB
/
format.go
File metadata and controls
93 lines (78 loc) · 1.95 KB
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
package main
import (
"fmt"
"time"
"github.com/Sadzeih/valcompbot/matches"
)
const (
tickerMd = `
%s
---
`
eventMd = `
**%s**
| Match | Starting in |
|:-----:|------------:|
%s
`
matchMdFmt = "| [%s](%s) | %s |\n"
)
func format(sidebar bool, m []matches.Upcoming) (string, error) {
var matchesMd string
eventMatches := make(map[string][]matches.Upcoming)
for _, match := range m {
eventName := match.EventName
if _, ok := eventMatches[eventName]; !ok {
eventMatches[eventName] = make([]matches.Upcoming, 0)
}
eventMatches[eventName] = append(eventMatches[eventName], match)
}
eventsMd := ""
for eventName, matchesInEvent := range eventMatches {
for _, match := range matchesInEvent {
startingIn := ""
matchTime := time.Time(match.Timestamp)
if matchTime.Before(time.Now()) {
if len(match.Match.Streams) != 0 {
startingIn = fmt.Sprintf("[LIVE](%s)", match.Match.Streams[0].Link)
} else {
startingIn = "LIVE"
}
} else {
startingIn = formatDuration(time.Until(matchTime))
}
matchStr := fmt.Sprintf("%s vs %s", match.Teams[0].Name, match.Teams[1].Name)
if match.Teams[0].Name == "" || match.Teams[1].Name == "" {
matchStr = fmt.Sprintf("*%s: %s*", match.Match.Info.Series, match.Match.Info.Subseries)
}
matchesMd += fmt.Sprintf(matchMdFmt,
matchStr,
match.MatchLink,
startingIn,
)
}
eventsMd += fmt.Sprintf(eventMd, eventName, matchesMd)
matchesMd = ""
}
md := fmt.Sprintf(tickerMd, eventsMd)
if sidebar {
return fmt.Sprintf(sidebarMd, md), nil
}
return md, nil
}
func formatDuration(d time.Duration) string {
od := d
dstr := ""
if d.Hours() >= 24 {
dstr += fmt.Sprintf("%dd", int(d.Hours()/24))
d -= time.Duration(int(d.Hours()/24)*24) * time.Hour
}
if d.Hours() >= 1 {
dstr += fmt.Sprintf("%dh", int(d.Minutes()/60))
d -= time.Duration(int(d.Minutes()/60)) * time.Hour
}
if od.Hours() <= 2 {
dstr += fmt.Sprintf("%dm", int(d.Minutes()))
}
return dstr
}