-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteam.go
284 lines (239 loc) · 7.49 KB
/
team.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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package etf2l
import (
"context"
"fmt"
"github.com/pkg/errors"
)
type Team struct {
Competitions map[string]TeamCompetition `json:"competitions"`
Country string `json:"country"`
Homepage string `json:"homepage"`
ID int `json:"id"`
Irc struct {
Channel string `json:"channel"`
Network string `json:"network"`
} `json:"irc"`
Name string `json:"name"`
Server string `json:"server"`
Steam SteamGroup `json:"steam"`
Tag string `json:"tag"`
Urls struct {
Matches string `json:"matches"`
Results string `json:"results"`
Self string `json:"self"`
Transfers string `json:"transfers"`
} `json:"urls"`
Players []TeamPlayer `json:"players"`
NameChanges []TeamNameChange `json:"name_changes"`
}
type TeamNameChange struct {
From string `json:"from"`
To string `json:"to"`
Time int `json:"time"`
}
type TeamPlayer struct {
Country string `json:"country"`
ID int `json:"id"`
Name string `json:"name"`
Role string `json:"role"`
Steam SteamPlayer `json:"steam"`
URL string `json:"url"`
}
type teamResponse struct {
Team Team `json:"team"`
Status Status `json:"status"`
}
func (client *Client) Team(ctx context.Context, httpClient HTTPExecutor, teamID int) (*Team, error) {
var resp teamResponse
if err := client.call(ctx, httpClient, fmt.Sprintf("/team/%d", teamID), nil, &resp); err != nil {
return nil, err
}
return &resp.Team, nil
}
type TransferPlayerInfo struct {
Country string `json:"country"`
ID int `json:"id"`
Name string `json:"name"`
Steam SteamPlayer `json:"steam"`
URL string `json:"url"`
}
type TeamTransfer struct {
Who TransferPlayerInfo `json:"who"`
By TransferPlayerInfo `json:"by"`
Team struct {
ID int `json:"id"`
Name string `json:"name"`
Steam struct {
Avatar string `json:"avatar"`
Group string `json:"group"`
} `json:"steam"`
Type string `json:"type"`
URL string `json:"url"`
} `json:"team"`
Time int `json:"time"`
Type string `json:"type"`
}
type teamTransfersResp struct {
Data []TeamTransfer `json:"data"`
Meta Meta `json:"meta"`
Links transferLinks `json:"links"`
}
func (resp teamTransfersResp) NextURL(r Recursive) (string, error) {
if !r.IsRecursive() || resp.Links.Next == nil {
return "", ErrEOF
}
nextPath, err := getPath(*resp.Links.Next)
if err != nil {
return "", err
}
return nextPath, nil
}
func (client *Client) TeamTransfers(ctx context.Context, httpClient HTTPExecutor, teamID int, opts Recursive) ([]TeamTransfer, error) {
curPath := fmt.Sprintf("/team/%d/transfers", teamID)
var transfers []TeamTransfer
for {
var resp teamTransfersResp
if err := client.call(ctx, httpClient, curPath, nil, &resp); err != nil {
return nil, err
}
transfers = append(transfers, resp.Data...)
nextURL, err := resp.NextURL(opts)
if err != nil {
if errors.Is(err, ErrEOF) {
break
}
return nil, err
}
curPath = nextURL
}
return transfers, nil
}
type TeamResult struct {
Clan1 Clan `json:"clan1"`
Clan2 Clan `json:"clan2"`
Competition CompetitionInfo `json:"competition"`
Defaultwin bool `json:"defaultwin"`
Division Division `json:"division"`
Result int `json:"result"`
Maps []string `json:"maps"`
R1 int `json:"r1"`
R2 int `json:"r2"`
Round string `json:"round"`
Time int `json:"time"`
Week int `json:"week"`
}
type pagedTeamResponse struct {
CurrentPage int `json:"current_page"`
Data []TeamResult `json:"data"`
FirstPageURL string `json:"first_page_url"`
From int `json:"from"`
LastPage int `json:"last_page"`
LastPageURL string `json:"last_page_url"`
Links []links `json:"links"`
NextPageURL *string `json:"next_page_url"`
Path string `json:"path"`
PerPage int `json:"per_page"`
PrevPageURL *string `json:"prev_page_url"`
To int `json:"to"`
Total int `json:"total"`
}
func (resp pagedTeamResponse) NextURL(r Recursive) (string, error) {
if !r.IsRecursive() || resp.NextPageURL == nil {
return "", ErrEOF
}
nextPath, err := getPath(*resp.NextPageURL)
if err != nil {
return "", err
}
return nextPath, nil
}
func (client *Client) TeamResults(ctx context.Context, httpClient HTTPExecutor, teamID int, opts Recursive) ([]TeamResult, error) {
curPath := fmt.Sprintf("/team/%d/results", teamID)
var bans []TeamResult
for {
var resp pagedTeamResponse
if err := client.call(ctx, httpClient, curPath, nil, &resp); err != nil {
return nil, err
}
bans = append(bans, resp.Data...)
nextURL, err := resp.NextURL(opts)
if err != nil {
if errors.Is(err, ErrEOF) {
break
}
return nil, err
}
curPath = nextURL
}
return bans, nil
}
type teamMatchesResp struct {
CurrentPage int `json:"current_page"`
Data []TeamResult `json:"data"`
FirstPageURL string `json:"first_page_url"`
From int `json:"from"`
LastPage int `json:"last_page"`
LastPageURL string `json:"last_page_url"`
Links []links `json:"links"`
NextPageURL *string `json:"next_page_url"`
Path string `json:"path"`
PerPage int `json:"per_page"`
PrevPageURL *string `json:"prev_page_url"`
To int `json:"to"`
Total int `json:"total"`
}
func (resp teamMatchesResp) NextURL(r Recursive) (string, error) {
if !r.IsRecursive() || resp.NextPageURL == nil {
return "", ErrEOF
}
nextPath, err := getPath(*resp.NextPageURL)
if err != nil {
return "", err
}
return nextPath, nil
}
type TeamMatchesOpts struct {
Recursive
// Team TeamID of the blu team.
Clan1 int `url:"clan1,omitempty"`
// Team TeamID of the red team.
Clan2 int `url:"clan2,omitempty"`
// Team TeamID of either team.
Vs int `url:"vs,omitempty"`
// If set to 1, returns matches that have yet to be played. If set to 0, returns matches that are over.
Scheduled int `url:"scheduled,omitempty"`
// Limit your search to a specific competition. Expects a competition TeamID.
Competition int `url:"competition,omitempty"`
// UNIX timestamp that limits results to everything after the timestamp.
From int `url:"from,omitempty"`
// UNIX timestamp that limits results to everything before the time.
To int `url:"to,omitempty"`
// Name of the division in which the competition was played.
Division string `url:"division,omitempty"`
// Name of the type of team.
TeamType string `url:"team_type,omitempty"`
// Name of the current round.
Round string `url:"round,omitempty"`
// A list of ETF2L user TeamID's. Returns only matches in which any of the provided players participated.
Players []int `url:"players,omitempty"`
}
func (client *Client) TeamMatches(ctx context.Context, httpClient HTTPExecutor, teamID int, opts Recursive) ([]TeamResult, error) {
curPath := fmt.Sprintf("/team/%d/results", teamID)
var bans []TeamResult
for {
var resp teamMatchesResp
if err := client.call(ctx, httpClient, curPath, nil, &resp); err != nil {
return nil, err
}
bans = append(bans, resp.Data...)
nextURL, err := resp.NextURL(opts)
if err != nil {
if errors.Is(err, ErrEOF) {
break
}
return nil, err
}
curPath = nextURL
}
return bans, nil
}