-
Notifications
You must be signed in to change notification settings - Fork 0
/
players.go
242 lines (211 loc) · 7.21 KB
/
players.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
package goclash
import (
"errors"
"fmt"
"net/http"
"strings"
"sync"
"github.com/bytedance/sonic"
)
// PlayerBase is embedded in Player and contains the most basic information about a player. May be used as DTO.
type PlayerBase struct {
League League `json:"league"`
BuilderBaseLeague BuilderBaseLeague `json:"builderBaseLeague"`
Clan PlayerClan `json:"clan"`
Role ClanRole `json:"role"`
AttackWins int `json:"attackWins"`
DefenseWins int `json:"defenseWins"`
TownHallLevel int `json:"townHallLevel"`
Tag string `json:"tag"`
Name string `json:"name"`
ExpLevel int `json:"expLevel"`
Trophies int `json:"trophies"`
BestTrophies int `json:"bestTrophies"`
Donations int `json:"donations"`
DonationsReceived int `json:"donationsReceived"`
BuilderHallLevel int `json:"builderHallLevel"`
BuilderBaseTrophies int `json:"builderBaseTrophies"`
BestBuilderBaseTrophies int `json:"bestBuilderBaseTrophies"`
WarStars int `json:"warStars"`
ClanCapitalContributions int `json:"clanCapitalContributions"`
}
// Player is a player returned by the API.
type Player struct {
*PlayerBase
WarPreference string `json:"warPreference"`
TownHallWeaponLevel int `json:"townHallWeaponLevel"`
LegendStatistics LegendStatistics `json:"legendStatistics"`
Troops []PlayerItemLevel `json:"troops"`
Heroes []PlayerItemLevel `json:"heroes,omitempty"`
HeroEquipment []PlayerItemLevel `json:"heroEquipment,omitempty"`
Spells []PlayerItemLevel `json:"spells"`
Labels []Label `json:"labels"`
Achievements []Achievement `json:"achievements"`
PlayerHouse PlayerHouse `json:"playerHouse"`
}
// InGameURL returns a link.clashofclans.com URL that can be used to open the player profile in game.
func (p *Player) InGameURL() string {
return "https://link.clashofclans.com?action=OpenPlayerProfile&tag=" + TagURLSafe(p.Tag)
}
// GetAchievement returns an IndexedAchievement by Achievement.Name and Achievement.Info. The index can be used to get the same achievement from other players, to make it more efficient.
func (p *Player) GetAchievement(achievement *Achievement) (*IndexedAchievement, error) {
for i, a := range p.Achievements {
switch a.Name {
case AchievementKeepYourAccountSafeOld.Name:
if a.Name != achievement.Name || a.Info != achievement.Info {
continue
}
default:
if a.Name != achievement.Name {
continue
}
}
return &IndexedAchievement{
Achievement: &a,
Index: i,
}, nil
}
return nil, errors.New("achievement not found")
}
type Players []*Player
func (p Players) Tags() []string {
tags := make([]string, len(p))
for i, player := range p {
tags[i] = player.Tag
}
return tags
}
// String implements fmt.Stringer, returning a comma-separated list of player names.
func (p Players) String() string {
str := make([]string, len(p))
for i, player := range p {
str[i] = player.Name
}
return strings.Join(str, ", ")
}
func (p Players) GetAchievement(achievement *Achievement) ([]*Achievement, error) {
if len(p) == 0 {
return nil, errors.New("no players were provided")
}
indexed, err := p[0].GetAchievement(achievement)
if err != nil {
return nil, err
}
achievements := make([]*Achievement, len(p))
for i, player := range p {
achievements[i] = &player.Achievements[indexed.Index]
}
return achievements, nil
}
type PlayerClan struct {
Tag string `json:"tag"`
Level int `json:"clanLevel"`
Name string `json:"name"`
BadgeURLs ImageURLs `json:"badgeUrls"`
}
type PlayerItemLevel struct {
Name string `json:"name"`
Village string `json:"village"`
Level int `json:"level"`
MaxLevel int `json:"maxLevel"`
SuperTroopIsActive bool `json:"superTroopIsActive,omitempty"`
Equipment []PlayerItemLevel `json:"equipment,omitempty"`
}
type LegendStatistics struct {
PreviousSeason Season `json:"previousSeason,omitempty"`
BestSeason Season `json:"bestSeason,omitempty"`
BestVersusSeason Season `json:"bestVersusSeason,omitempty"`
CurrentSeason CurrentSeason `json:"currentSeason,omitempty"`
LegendTrophies int `json:"legendTrophies,omitempty"`
}
type CurrentSeason struct {
Rank int `json:"rank,omitempty"`
Trophies int `json:"trophies,omitempty"`
}
type Season struct {
ID string `json:"id,omitempty"`
Rank int `json:"rank,omitempty"`
Trophies int `json:"trophies,omitempty"`
}
type BuilderBaseLeague struct {
ID int `json:"id"`
Name string `json:"name"`
}
type PlayerVerification struct {
Tag string `json:"tag"`
Token string `json:"token"`
Status string `json:"status"`
}
type PlayerHouse struct {
Elements []PlayerHouseElement `json:"elements"`
}
type PlayerHouseElement struct {
ID int `json:"id"`
Type string `json:"type"`
}
func (v *PlayerVerification) IsOk() bool {
return v.Status == PlayerVerificationStatusOk
}
const (
VillageHome = "home"
VillageBuilder = "builderBase"
PlayerVerificationStatusOk = "ok"
PlayerVerificationStatusInvalid = "invalid"
PlayerHouseElementTypeGround = "ground"
PlayerHouseElementTypeRoof = "roof"
PlayerHouseElementTypeFoot = "foot"
PlayerHouseElementTypeDeco = "deco"
)
// GetPlayer returns information about a single player by tag.
//
// GET /players/{playerTag}
func (h *Client) GetPlayer(tag string) (*Player, error) {
tag = TagURLSafe(CorrectTag(tag))
req := h.withAuth(h.newDefaultRequest())
data, err := h.do(http.MethodGet, PlayersEndpoint.Build(tag), req, true)
if err != nil {
return nil, err
}
var player *Player
err = sonic.Unmarshal(data, &player)
return player, err
}
// GetPlayers makes use of concurrency to get multiple players simultaneously.
func (h *Client) GetPlayers(tags ...string) (Players, error) {
var wg sync.WaitGroup
players := make(Players, len(tags))
errChan := make(chan error, len(tags))
for i, tag := range tags {
wg.Add(1)
go func(i int, tag string) {
defer wg.Done()
player, err := h.GetPlayer(tag)
if err != nil {
errChan <- err
return
}
players[i] = player
}(i, tag)
}
wg.Wait()
if len(errChan) > 0 {
return nil, <-errChan
}
return players, nil
}
// VerifyPlayer verifies a player token.
//
// POST /players/{playerTag}/verifytoken
func (h *Client) VerifyPlayer(tag, token string) (*PlayerVerification, error) {
tag = TagURLSafe(CorrectTag(tag))
req := h.withAuth(h.newDefaultRequest()).SetBody(map[string]string{
"token": token,
})
data, err := h.do(http.MethodPost, PlayersEndpoint.Build(fmt.Sprintf("%s/verifytoken", tag)), req, false)
if err != nil {
return nil, err
}
var verification *PlayerVerification
err = sonic.Unmarshal(data, &verification)
return verification, err
}