-
Notifications
You must be signed in to change notification settings - Fork 0
/
summoner.go
191 lines (179 loc) · 8.22 KB
/
summoner.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
package storage
import (
"fmt"
"time"
"github.com/torlenor/alolstats/utils"
"github.com/torlenor/alolstats/riotclient"
)
// Summoner is the storage type used for Summoner Data
type Summoner struct {
SummonerDTO riotclient.SummonerDTO
SummonerName string
SummonerID string
AccountID string
PUUID string
}
func (s *Storage) storeSummoner(summoner riotclient.SummonerDTO) error {
return s.backend.StoreSummoner(&Summoner{
SummonerDTO: summoner,
SummonerName: utils.CleanUpSummonerName(summoner.Name),
SummonerID: summoner.ID,
AccountID: summoner.AccountID,
PUUID: summoner.PuuID,
})
}
// getSummonerByNameFromClient returns a Summoner identified by name
// forceUpdate will try to update the champion, if it is false the config settings will be considered if update is required
func (s *Storage) getSummonerByNameFromClient(client riotclient.Client, name string, forceUpdate bool) (riotclient.SummonerDTO, error) {
name = utils.CleanUpSummonerName(name)
duration := time.Since(s.backend.GetSummonerByNameTimeStamp(name))
if (duration.Minutes() > float64(s.config.MaxAgeSummoner)) || forceUpdate {
summoner, err := s.riotClient.SummonerByName(name)
if err != nil {
s.log.Warnln("Could not get new data from Client, trying to get it from Storage instead", err)
summoner, err := s.backend.GetSummonerByName(name)
if err != nil {
s.log.Warnln("Could not get data from either Storage nor Client:", err)
return riotclient.SummonerDTO{}, err
}
s.log.Debugf("Returned Summoner %s from Storage", name)
return summoner.SummonerDTO, nil
}
err = s.storeSummoner(*summoner)
if err != nil {
s.log.Warnln("Could not store Summoner in storage backend:", err)
}
s.log.Debugf("Returned Summoner %s from Riot API", name)
return *summoner, nil
}
summoner, err := s.backend.GetSummonerByName(name)
if err != nil {
summoner, errClient := s.riotClient.SummonerByName(name)
if errClient != nil {
s.log.Warnln("Could not get data from either Storage nor Client:", errClient)
return riotclient.SummonerDTO{}, errClient
}
s.log.Warnln("Could not get Summoner from storage backend, returning from Client instead:", err)
err = s.storeSummoner(*summoner)
if err != nil {
s.log.Warnln("Could not store Summoner in storage backend:", err)
}
s.log.Debugf("Returned Summoner %s from Riot API", name)
return *summoner, nil
}
s.log.Debugf("Returned Summoner %s from Storage", name)
return summoner.SummonerDTO, nil
}
// GetSummonerByName returns a Summoner identified by name
// forceUpdate will try to update the champion, if it is false the config settings will be considered if update is required
func (s *Storage) GetSummonerByName(name string, forceUpdate bool) (riotclient.SummonerDTO, error) {
return s.getSummonerByNameFromClient(s.riotClient, name, forceUpdate)
}
// GetRegionalSummonerByName returns a Summoner identified by name for a specific region
// forceUpdate will try to update the champion, if it is false the config settings will be considered if update is required
func (s *Storage) GetRegionalSummonerByName(region string, name string, forceUpdate bool) (riotclient.SummonerDTO, error) {
if client, ok := s.riotClients[region]; ok {
return s.getSummonerByNameFromClient(client, name, forceUpdate)
}
return riotclient.SummonerDTO{}, fmt.Errorf("Invalid region specified: %s", region)
}
// getSummonerBySummonerIDFromClient returns a Summoner identified by Summoner ID
// forceUpdate will try to update the champion, if it is false the config settings will be considered if update is required
func (s *Storage) getSummonerBySummonerIDFromClient(client riotclient.Client, summonerID string, forceUpdate bool) (riotclient.SummonerDTO, error) {
if len(summonerID) == 0 {
return riotclient.SummonerDTO{}, fmt.Errorf("Summoner ID cannot be empty")
}
duration := time.Since(s.backend.GetSummonerBySummonerIDTimeStamp(summonerID))
if (duration.Minutes() > float64(s.config.MaxAgeSummoner)) || forceUpdate {
summoner, err := client.SummonerBySummonerID(summonerID)
if err != nil {
s.log.Warnln("Could not get new data from Client, trying to get it from Storage instead", err)
summoner, err := s.backend.GetSummonerBySummonerID(summonerID)
if err != nil {
s.log.Warnln("Could not get data from either Storage nor Client:", err)
return riotclient.SummonerDTO{}, err
}
s.log.Debugf("Returned Summoner with SummonerID %s from Storage", summonerID)
return summoner.SummonerDTO, nil
}
err = s.storeSummoner(*summoner)
if err != nil {
s.log.Warnln("Could not store Summoner in storage backend:", err)
}
s.log.Debugf("Returned Summoner with SummonerID %s from Riot API", summonerID)
return *summoner, nil
}
summoner, err := s.backend.GetSummonerBySummonerID(summonerID)
if err != nil {
summoner, errClient := s.riotClient.SummonerBySummonerID(summonerID)
if errClient != nil {
s.log.Warnln("Could not get data from either Storage nor Client:", errClient)
return riotclient.SummonerDTO{}, errClient
}
s.log.Warnln("Could not get Summoner from storage backend, returning from Client instead:", err)
err = s.storeSummoner(*summoner)
if err != nil {
s.log.Warnln("Could not store Summoner in storage backend:", err)
}
s.log.Debugf("Returned Summoner with SummonerID %s from Riot API", summonerID)
return *summoner, nil
}
s.log.Debugf("Returned Summoner with SummonerID %s from Storage", summonerID)
return summoner.SummonerDTO, nil
}
// GetSummonerBySummonerID returns a Summoner identified by Summoner ID
// forceUpdate will try to update the champion, if it is false the config settings will be considered if update is required
func (s *Storage) GetSummonerBySummonerID(summonerID string, forceUpdate bool) (riotclient.SummonerDTO, error) {
return s.getSummonerBySummonerIDFromClient(s.riotClient, summonerID, forceUpdate)
}
// GetRegionalSummonerBySummonerID returns a Summoner identified by Summoner ID for a specific region
// forceUpdate will try to update the champion, if it is false the config settings will be considered if update is required
func (s *Storage) GetRegionalSummonerBySummonerID(region string, summonerID string, forceUpdate bool) (riotclient.SummonerDTO, error) {
if client, ok := s.riotClients[region]; ok {
return s.getSummonerBySummonerIDFromClient(client, summonerID, forceUpdate)
}
return riotclient.SummonerDTO{}, fmt.Errorf("Invalid region specified: %s", region)
}
// GetSummonerByAccountID returns a Summoner identified by Account ID
func (s *Storage) GetSummonerByAccountID(accountID string, forceUpdate bool) (riotclient.SummonerDTO, error) {
if len(accountID) == 0 {
return riotclient.SummonerDTO{}, fmt.Errorf("Account ID cannot be empty")
}
duration := time.Since(s.backend.GetSummonerByAccountIDTimeStamp(accountID))
if (duration.Minutes() > float64(s.config.MaxAgeSummoner)) || forceUpdate {
summoner, err := s.riotClient.SummonerByAccountID(accountID)
if err != nil {
s.log.Warnln("Could not get new data from Client, trying to get it from Storage instead", err)
summoner, err := s.backend.GetSummonerByAccountID(accountID)
if err != nil {
s.log.Warnln("Could not get data from either Storage nor Client:", err)
return riotclient.SummonerDTO{}, err
}
s.log.Debugf("Returned Summoner with AccountID %s from Storage", accountID)
return summoner.SummonerDTO, nil
}
err = s.storeSummoner(*summoner)
if err != nil {
s.log.Warnln("Could not store Summoner in storage backend:", err)
}
s.log.Debugf("Returned Summoner with AccountID %s from Riot API", accountID)
return *summoner, nil
}
summoner, err := s.backend.GetSummonerByAccountID(accountID)
if err != nil {
summoner, errClient := s.riotClient.SummonerByAccountID(accountID)
if errClient != nil {
s.log.Warnln("Could not get data from either Storage nor Client:", errClient)
return riotclient.SummonerDTO{}, errClient
}
s.log.Warnln("Could not get Summoner from storage backend, returning from Client instead:", err)
err = s.storeSummoner(*summoner)
if err != nil {
s.log.Warnln("Could not store Summoner in storage backend:", err)
}
s.log.Debugf("Returned Summoner with AccountID %s from Riot API", accountID)
return *summoner, nil
}
s.log.Debugf("Returned Summoner with AccountID %s from Storage", accountID)
return summoner.SummonerDTO, nil
}