/
leaderboard.go
84 lines (69 loc) · 1.72 KB
/
leaderboard.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
package vimeworld
import (
"context"
"fmt"
"net/http"
"strconv"
)
// AvailableLeaderboard struct.
type AvailableLeaderboard struct {
Type string `json:"type"`
Description string `json:"description"`
MaxSize int `json:"max_size"`
Sort []string `json:"sort"`
Season string `json:"season"`
}
// GetLeaderboardList returns available leaderboards.
func (c *Client) GetLeaderboardList(ctx context.Context) ([]*AvailableLeaderboard, error) {
var result []*AvailableLeaderboard
u := "leaderboard/list"
req, err := c.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
_, err = c.Do(ctx, req, &result)
if err != nil {
return nil, err
}
return result, nil
}
// Leaderboard struct.
type Leaderboard struct {
Meta LeaderboardResponseMeta `json:"leaderboard"`
Records []interface{} `json:"records"`
}
// LeaderboardResponseMeta struct.
type LeaderboardResponseMeta struct {
Type string `json:"type"`
Sort string `json:"sort"`
Offset int `json:"offset"`
Size int `json:"size"`
MaxSize int `json:"max_size"`
}
// GetLeaderboard returns leaderboard.
func (c *Client) GetLeaderboard(ctx context.Context, boardType, sort string, size, offset int) (*Leaderboard, error) {
var result Leaderboard
u := fmt.Sprintf("leaderboard/get/%s", boardType)
if sort != "" {
u += "/" + sort
}
if size > 0 {
u += "?size=" + strconv.Itoa(size)
}
if offset > 0 {
if size > 0 {
u += "&offset=" + strconv.Itoa(offset)
} else {
u += "?offset=" + strconv.Itoa(offset)
}
}
req, err := c.NewRequest(http.MethodGet, u, nil)
if err != nil {
return nil, err
}
_, err = c.Do(ctx, req, &result)
if err != nil {
return nil, err
}
return &result, nil
}