-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathladder.go
195 lines (168 loc) · 4.54 KB
/
ladder.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
package poeapi
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
"sync"
)
const (
maxLadderLimit = 200
maxLadderPages = 75 // 15000 % 200
defaultLadderType = "league"
labyrinthLadderType = "labyrinth"
pvpLadderType = "pvp"
earliestLabyrinthTime = 1456790400 // March 1, 2016: One day before 3.2.0.
)
// GetLadderOptions contains the request parameters for the ladder endpoint.
// All parameters are optional with the exception of ID.
type GetLadderOptions struct {
// The name of the league whose ladder you want to retrieve.
ID string
// The realm of the ladder.
// Valid options: 'pc', 'xbox', or 'sony'.
Realm string
// The type of league whose ladder you want to retrieve.
// Valid options: 'league', 'pvp', or 'labyrinth'.
Type string
// Associate UUIDs with each character returned for tracking purposes.
UniqueIDs bool
// Only include the given account in results.
AccountName string
// Difficulty of the Labyrinth ladder to retrieve.
// Valid options: 'Normal', 'Cruel', 'Merciless', or 'Eternal'.
LabyrinthDifficulty string
// Start time of the Labyrinth ladder to retrieve. This is a Unix timestamp.
LabyrinthStartTime int
// Internal use only.
limit int
// Internal use only.
offset int
}
func (opts GetLadderOptions) toQueryParams() string {
u := url.Values{}
if opts.Realm != "" {
u.Add("realm", opts.Realm)
}
if opts.Type != "" {
u.Add("type", opts.Type)
}
u.Add("track", strconv.FormatBool(opts.UniqueIDs))
if opts.AccountName != "" && opts.Type == defaultLadderType {
u.Add("accountName", opts.AccountName)
}
if opts.LabyrinthDifficulty != "" && opts.Type == labyrinthLadderType {
u.Add("difficulty", opts.LabyrinthDifficulty)
}
if opts.LabyrinthStartTime != 0 && opts.Type == labyrinthLadderType {
u.Add("start", strconv.Itoa(opts.LabyrinthStartTime))
}
if opts.limit != 0 {
u.Add("limit", strconv.Itoa(opts.limit))
}
if opts.offset != 0 {
u.Add("offset", strconv.Itoa(opts.offset))
}
return u.Encode()
}
func validateGetLadderOptions(opts GetLadderOptions) error {
if opts.ID == "" {
return ErrMissingID
}
if _, ok := validRealms[opts.Realm]; opts.Realm != "" && !ok {
return ErrInvalidRealm
}
if _, ok := validLadderTypes[opts.Type]; opts.Type != "" && !ok {
return ErrInvalidLadderType
}
if opts.limit < 1 || opts.limit > maxLadderLimit {
return ErrInvalidLimit
}
if opts.offset < 0 || opts.offset > maxLadderLimit*maxLadderPages {
return ErrInvalidOffset
}
if opts.Type == labyrinthLadderType {
return validateGetLabyrinthLadderOptions(opts)
}
return nil
}
func validateGetLabyrinthLadderOptions(opts GetLadderOptions) error {
if opts.LabyrinthDifficulty != "" {
if _, ok := validLabyrinthDifficulties[opts.LabyrinthDifficulty]; !ok {
return ErrInvalidDifficulty
}
}
if opts.LabyrinthStartTime < 0 {
return ErrInvalidLabyrinthStartTime
}
if opts.LabyrinthStartTime > 0 && opts.LabyrinthStartTime < earliestLabyrinthTime {
return ErrInvalidLabyrinthStartTime
}
return nil
}
func (c *client) GetLadder(opts GetLadderOptions) (Ladder, error) {
entries := make([]LadderEntry, 0)
opts.limit = maxLadderLimit
// Make one initial request to determine the size of the ladder.
first, err := c.getLadderPage(opts)
if err != nil {
return Ladder{}, err
}
ladderSize := first.TotalEntries
if ladderSize <= maxLadderLimit {
return first, nil
}
// If there are entries remaining, make further requests.
var (
wg sync.WaitGroup
lock sync.RWMutex
errCh = make(chan error, maxLadderPages)
)
for i := maxLadderLimit; i < ladderSize; i += maxLadderLimit {
wg.Add(1)
go func(offset int) {
subOpts := opts
subOpts.offset = offset
page, err := c.getLadderPage(subOpts)
if err != nil {
errCh <- err
}
lock.Lock()
entries = append(entries, page.Entries...)
lock.Unlock()
wg.Done()
}(i)
}
wg.Wait()
select {
case err, ok := <-errCh:
if ok {
return Ladder{}, err
}
default:
// Continue.
}
// Copy first page to get top-level values.
ladder := first
ladder.Entries = entries
return ladder, nil
}
func (c *client) getLadderPage(opts GetLadderOptions) (Ladder, error) {
if err := validateGetLadderOptions(opts); err != nil {
return Ladder{}, err
}
url := fmt.Sprintf("%s/%s?%s", c.formatURL(laddersEndpoint), opts.ID,
opts.toQueryParams())
resp, err := c.get(url)
if err != nil {
return Ladder{}, err
}
return parseLadderResponse(resp)
}
func parseLadderResponse(resp string) (Ladder, error) {
ladder := Ladder{}
if err := json.Unmarshal([]byte(resp), &ladder); err != nil {
return Ladder{}, err
}
return ladder, nil
}