-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_practice.go
More file actions
107 lines (96 loc) · 2.56 KB
/
api_practice.go
File metadata and controls
107 lines (96 loc) · 2.56 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package sova
import (
"context"
"fmt"
"strconv"
)
type ErrNoModesAvailable struct {
Ranked bool
}
func (e ErrNoModesAvailable) Error() string {
if e.Ranked {
return "no ranked modes available"
}
return "no modes available"
}
// PracticeMode returns a list of all available practice modes, including
// ranked.
//
// /api/practice/mode/
func (api *API) PracticeMode(ctx context.Context) (PracticeModeResponse, error) {
resp, err := getAndUnmarshal[PracticeModeResponse](
api, ctx,
"practice/mode",
)
if err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, ErrNoModesAvailable{}
}
return resp, nil
}
// PracticeModeRanked returns a list of all available ranked practice modes.
//
// /api/practice/mode/ranked/
func (api *API) PracticeModeRanked(ctx context.Context) (PracticeModeRankedResponse, error) {
resp, err := getAndUnmarshal[PracticeModeRankedResponse](
api, ctx,
"practice/mode/ranked",
)
if err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, ErrNoModesAvailable{Ranked: true}
}
return resp, nil
}
type RankedStatisticsType int
const (
RankedStatisticsTypePlayer RankedStatisticsType = iota + 1
RankedStatisticsTypeMode
)
type ErrNoStatisticsAvailable struct {
Type RankedStatisticsType
For string
}
func (e ErrNoStatisticsAvailable) Error() string {
if e.For == "" {
return "no statistics available"
}
return fmt.Sprintf("no statistics available for %s", e.For)
}
// PracticeStatisticsElo tries to get player statistics for all ranked modes.
//
// /api/practice/statistics/elo/{player_id}/
func (api *API) PracticeStatisticsElo(ctx context.Context, playerID int) (PracticeStatisticsEloResponse, error) {
resp, err := getAndUnmarshal[PracticeStatisticsEloResponse](
api, ctx,
f("practice/statistics/elo/%d", playerID),
)
if err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, ErrNoStatisticsAvailable{Type: RankedStatisticsTypePlayer, For: strconv.Itoa(playerID)}
}
return resp, nil
}
// PracticeStatisticsLeaderboardElo fetches leaderboard entries (ranked players
// statistics) for a specific mode by its id.
//
// /api/practice/statistics/leaderboard/elo/{mode_id}/
func (api *API) PracticeStatisticsLeaderboardElo(ctx context.Context, modeID int) (StatisticsEloLeaderboardResponse, error) {
resp, err := getAndUnmarshal[StatisticsEloLeaderboardResponse](
api, ctx,
f("practice/statistics/leaderboard/elo/%d", modeID),
)
if err != nil {
return nil, err
}
if len(resp) == 0 {
return nil, ErrNoStatisticsAvailable{Type: RankedStatisticsTypeMode, For: strconv.Itoa(modeID)}
}
return resp, nil
}