-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcontest.go
300 lines (258 loc) · 7.43 KB
/
contest.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
package goforces
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"time"
)
//Contest represents a Codeforces Contest
type Contest struct {
DurationSeconds int64 `json:"durationSeconds"`
Frozen bool `json:"frozen"`
ID int64 `json:"id"`
Name string `json:"name"`
Phase string `json:"phase"`
RelativeTimeSeconds int64 `json:"relativeTimeSeconds"`
StartTimeSeconds int64 `json:"startTimeSeconds"`
Type string `json:"type"`
}
//Div2 returns boolean whether contest'name contains "Div. 2"
func (c Contest) Div2() bool {
return strings.Contains(c.Name, "Div. 2")
}
// Finished returns boolean whether contest was over
func (c Contest) Finished() bool {
return c.Phase == "FINISHED"
}
// Before returns boolean whether contest hasn't started.
func (c Contest) Before() bool {
return c.Phase == "BEFORE"
}
// Coding returns boolean whether contest is now being held.
func (c Contest) Coding() bool {
return c.Phase == "CODING"
}
// ContestURL returns the contest's url.
func (c Contest) ContestURL() string {
return fmt.Sprintf("http://codeforces.com/contest/%d", c.ID)
}
//GetContestHacks implements /contest.hacks
func (c *Client) GetContestHacks(ctx context.Context, contestID int) ([]Hack, error) {
c.Logger.Println("GetContestHacks contestId: ", contestID)
v := url.Values{}
v.Add("contestId", strconv.Itoa(contestID))
spath := "/contest.hacks" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, nil, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type HacksResponse struct {
Status string `json:"status"`
Result []Hack `json:"result"`
}
var resp HacksResponse
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
var hacks []Hack
hacks = resp.Result
return hacks, nil
}
//ContestListOptions represents the option of /contest.list
type ContestListOptions struct {
Gym bool `url:"gym"`
}
//GetContestList implements /contest.list
func (c *Client) GetContestList(ctx context.Context, options *ContestListOptions) ([]Contest, error) {
c.Logger.Println("GetContestList : ", options)
v := url.Values{}
spath := "/contest.list" + "?" + v.Encode() + "&"
req, err := c.newRequest(ctx, "GET", spath, options, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []Contest `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
var contests []Contest
contests = resp.Result
return contests, nil
}
//GetContestRatingChanges implements /contest.ratingChanges
func (c *Client) GetContestRatingChanges(ctx context.Context, contestID int) ([]RatingChange, error) {
c.Logger.Println("GetContestRatingChange contestId: ", contestID)
v := url.Values{}
v.Add("contestId", strconv.Itoa(contestID))
spath := "/contest.ratingChanges" + "?" + v.Encode()
req, err := c.newRequest(ctx, "GET", spath, nil, nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []RatingChange `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
var ratingChanges []RatingChange
ratingChanges = resp.Result
return ratingChanges, nil
}
//ContestStatndingsOptions represents the option of /contest.standings
type ContestStatndingsOptions struct {
From int
Count int
Handles []string
Room int
ShowUnofficial bool
}
func (o *ContestStatndingsOptions) options() interface{} {
if o == nil {
return nil
}
type option struct {
From int `url:"from"`
Count int `url:"count"`
Handles string `url:"handles"`
Room int `url:"room"`
ShowUnofficial bool `url:"showUnofficial"`
}
return &option{From: o.From, Count: o.Count, Handles: strings.Join(o.Handles, ";"), Room: o.Room, ShowUnofficial: o.ShowUnofficial}
}
//GetContestStandings implements /contest.standings
func (c *Client) GetContestStandings(ctx context.Context, contestID int, options *ContestStatndingsOptions) (*Standings, error) {
c.Logger.Println("GetContestStandings: ", contestID, options)
v := url.Values{}
// Check if APIKey and APISecret exist to make an authenticated request
if c.APIKey != "" && c.APISecret != "" {
v.Add("apiKey", c.APIKey)
v.Add("contestId", strconv.Itoa(contestID))
v.Add("time", strconv.FormatInt(time.Now().Unix(), 10))
if options != nil {
if len(options.Handles) > 0 {
v.Add("handles", strings.Join(options.Handles, ";"))
}
if options.Count > 0 {
v.Add("count", string(options.Count))
}
if options.From > 0 {
v.Add("from", string(options.From))
}
if options.Room > 0 {
v.Add("room", string(options.Room))
}
if options.ShowUnofficial {
v.Add("showUnofficial", "true")
}
}
apiSig := generateAPISig("contest.standings", c.APISecret, v)
v.Add("apiSig", apiSig)
// All options is already inside the url at this point because apiSig need
// to hash the url having all the parameters. If options is added after the
// hash, CF can`t check hash properly
options = nil
} else {
v.Add("contestId", strconv.Itoa(contestID))
}
spath := "/contest.standings" + "?" + v.Encode() + "&"
req, err := c.newRequest(ctx, "GET", spath, options.options(), nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result Standings `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
return &resp.Result, nil
}
//ContestStatusOptions represents the option of /contest.status
type ContestStatusOptions struct {
From int
Count int
Handle string
}
func (o *ContestStatusOptions) options() interface{} {
if o == nil {
return nil
}
type option struct {
From int `url:"from"`
Count int `url:"count"`
Handle string `url:"handle"`
}
return &option{From: o.From, Count: o.Count, Handle: o.Handle}
}
//GetContestStatus implements /contest.status
func (c *Client) GetContestStatus(ctx context.Context, contestID int, options *ContestStatusOptions) ([]Submission, error) {
c.Logger.Println("GetContestStatus: ", contestID, options)
v := url.Values{}
v.Add("contestId", strconv.Itoa(contestID))
//check options
spath := "/contest.status" + "?" + v.Encode() + "&"
req, err := c.newRequest(ctx, "GET", spath, options.options(), nil)
if err != nil {
return nil, err
}
res, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
type Response struct {
Status string `json:"status"`
Result []Submission `json:"result"`
}
var resp Response
if err := decodeBody(res, &resp); err != nil {
return nil, err
}
//check status
if resp.Status != "OK" {
return nil, fmt.Errorf("Status Error: %s", res.Status)
}
return resp.Result, nil
}