-
Notifications
You must be signed in to change notification settings - Fork 2
/
champion_mastery.go
85 lines (77 loc) · 2.76 KB
/
champion_mastery.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
package lol
import (
"fmt"
"log"
"net/http"
)
// A ChampionMastery represents the mastery level of a payer with a
// given champion
type ChampionMastery struct {
Champion ChampionID `json:"championID"`
Level int `json:"championLevel"`
Points int `json:"championPoints"`
PointsSinceLastLevel int `json:"championPointsSinceLastLevel"`
PointsUntilNextLevel int `json:"championPointsUntilNextLevel"`
ChestGranted bool `json:"chestGranted"`
HighestGrade string `json:"highestGrade"`
LastPlayTime EpochMillisecond `json:"lastPlayTime"`
Player SummonerID `json:"playerId"`
}
func (a *APIEndpoint) formatChampionMasteryURL(url string, options map[string]string) string {
res := fmt.Sprintf("https://%s/championmastery/location/%s%s?api_key=%s", a.region.url, a.region.platformID, url, a.key)
for k, v := range options {
res = fmt.Sprintf("%s&%s=%s", res, k, v)
}
return res
}
// GetChampionMastery returns the champion mastery of a given player for a given champion, ni
func (a *APIEndpoint) GetChampionMastery(playerID SummonerID, championID ChampionID) (*ChampionMastery, error) {
res := &ChampionMastery{}
url := a.formatChampionMasteryURL(fmt.Sprintf("/player/%d/champion/%d", playerID, championID), nil)
err := a.g.Get(url, res)
if err != nil {
if rerr, ok := err.(RESTError); ok == true {
if rerr.Code == http.StatusNoContent {
return nil, nil
}
}
return nil, err
}
return res, nil
}
// GetChampionMasteries returns all of the ChampionMastery of a given
// player, ordered by decreasing points
func (a *APIEndpoint) GetChampionMasteries(playerID SummonerID) ([]ChampionMastery, error) {
res := []ChampionMastery{}
url := a.formatChampionMasteryURL(fmt.Sprintf("/player/%d/champions", playerID), nil)
err := a.g.Get(url, &res)
if err != nil {
return nil, err
}
return res, nil
}
// GetChampionMasteryScore return sthe total score of a player
func (a *APIEndpoint) GetChampionMasteryScore(playerID SummonerID) (int, error) {
res := -1
url := a.formatChampionMasteryURL(fmt.Sprintf("/player/%d/score", playerID), nil)
err := a.g.Get(url, &res)
if err != nil {
return -1, err
}
return res, nil
}
// GetChampionMasteryTopChampions returns the count top champion of a player
func (a *APIEndpoint) GetChampionMasteryTopChampions(playerID SummonerID, count int) ([]ChampionMastery, error) {
res := []ChampionMastery{}
options := map[string]string{}
if count != 3 {
options["count"] = fmt.Sprintf("%d", count)
}
url := a.formatChampionMasteryURL(fmt.Sprintf("/player/%d/topchampions", playerID), options)
log.Printf(url)
err := a.g.Get(url, &res)
if err != nil {
return nil, err
}
return res, nil
}