-
Notifications
You must be signed in to change notification settings - Fork 3
/
bnet_chieves.go
242 lines (224 loc) · 7.72 KB
/
bnet_chieves.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 main
import (
"fmt"
"strconv"
"strings"
"github.com/FuzzyStatic/blizzard/v2/wowgd"
"github.com/FuzzyStatic/blizzard/v2/wowp"
raven "github.com/getsentry/raven-go"
log "gopkg.in/inconshreveable/log15.v2"
)
//TODO: wow
//TODO: Multi-rank "Got My Mind On My Money"
//TODO: For chieve, fuzzy. Fuzzy for realm too.
func retrieveChievesForPlayer(realm, player string) (*wowp.CharacterAchievementsSummary, error) {
if c, ok := wowPlayerChieveCache.Get(realm + "-" + player); ok {
return c.(*wowp.CharacterAchievementsSummary), nil
}
cas, _, err := bNetClient.WoWCharacterAchievementsSummary(ctx, realm, player)
if err != nil {
return nil, err
}
wowPlayerChieveCache.SetDefault(realm+"-"+player, cas)
return cas, nil
}
func chieveForPlayer(realm, player, chieveName string) string {
if bNetClient == nil {
return "WOW API not available"
}
log.Debug("Handling Chieve Player", "Realm", realm, "Player", player, "ChieveName", chieveName)
cas, err := retrieveChievesForPlayer(realm, player)
if err != nil {
raven.CaptureError(err, nil)
return "Could not retrieve Chieves for Player"
}
return playerSingleChieveStatus(cas, chieveName)
}
// Parses the chieves of a player and returns a Slack formatted string
// as to whether they have received it or not.
func playerSingleChieveStatus(cas *wowp.CharacterAchievementsSummary, chieveName string) string {
log.Debug("Handling Chieve Player Status")
for _, a := range cas.Achievements {
if strings.EqualFold(strings.ToLower(a.Achievement.Name), strings.ToLower(chieveName)) {
log.Debug("playerSingleChieveStatus: Found chieve", "Chievo", a)
var ret []string
ret = append(ret, fmt.Sprintf("<http://www.wowhead.com/achievement=%d|%s>", a.ID, a.Achievement.Name))
if a.Criteria.IsCompleted || a.CompleteTimestamp != 0 {
ret = append(ret, ":fire: Achievement Unlocked! :fire:")
} else {
ret = append(ret, ":cry: Chievo not got :cry:")
}
/* SubChieves */
ac := chieveFromID(a.Achievement.ID)
if len(ac.Name) > 0 {
ret = append(ret, ac.Description)
}
accc := mapCriteriaToName(ac.Criteria.ChildCriteria)
// A bare chievo with a single child criterion
if len(ac.Criteria.ChildCriteria) == 1 && len(ac.Criteria.ChildCriteria[0].ChildCriteria) == 0 {
accc = singleBareChievoCriterion(ac)
}
// A bare chievo with a single amount and no children
if len(ac.Criteria.ChildCriteria) == 0 && ac.Criteria.Amount > 1 {
ret = append(ret, fmt.Sprintf("[Progress: %d/%d]", int64(a.Criteria.Amount), ac.Criteria.Amount))
}
log.Debug("Retrieved Chieve", "C", ac, "Map", accc)
if len(a.Criteria.ChildCriteria) > 0 {
ccret := recursePlayerCriteria(a.Criteria.ChildCriteria)
log.Debug("Looping CCs", "Map", ccret)
for k, v := range ccret {
if ad, ok := accc[k]; ok {
if strings.Contains(ad, "%s") {
ad = fmt.Sprintf(ad, v.amount)
}
if v.completed {
ret = append(ret, "[:white_check_mark:] "+ad)
} else {
ret = append(ret, "[:x:] "+ad)
}
}
}
}
return strings.Join(ret, "\n")
}
}
log.Debug("playerSingleChieveStatus: Not found")
return "Chieve not found :("
}
/* CHIEVE UTILITIES */
type playerCriteriaStuff struct {
completed bool
amount string
}
func formatChieveForSlack(a *wowgd.Achievement) string {
if a == nil {
return "Chieve not found :("
}
if len(a.Criteria.ChildCriteria) < 2 {
return fmt.Sprintf("<http://www.wowhead.com/achievement=%d|%s> - %s [:point_right: %d :point_left:]", a.ID, a.Name, a.Description, a.Points)
}
var ret []string
ret = append(ret, fmt.Sprintf("%s - %s\n", a.Name, a.Description))
ret = append(ret, mapCriteriaToStrings(a.Criteria.ChildCriteria)...)
if len(a.RewardDescription) > 0 {
ret = append(ret, fmt.Sprintf(":trophy: %s :trophy:", a.RewardDescription))
}
return ret[0] + strings.Join(ret[1:], "\n")
}
func handleChieveInput(input string) string {
tokens := strings.SplitN(input, " ", 3)
log.Debug("Handling Chieve Input", "Input", input, "Tokens", tokens)
realm, player, err := distinguishRealmFromPlayer(tokens[0], tokens[1])
if err != nil {
return formatChieveForSlack(chieveFromID(chieveNameToID(input)))
}
return chieveForPlayer(realm, player, tokens[2])
}
func chieveNameToID(chieveName string) int {
if bNetClient == nil || len(wowChieves.Achievements) == 0 {
log.Debug("Chieve Name to ID - no client or chieves")
return 0
}
for _, a := range wowChieves.Achievements {
if strings.EqualFold(strings.ToLower(a.Name), strings.ToLower(chieveName)) {
log.Debug("Chieve Name to ID", "Name", chieveName, "Chievo Found", a.ID)
return a.ID
}
}
log.Debug("Chieve Name to ID -- not found")
return 0
}
// Little wrapper to make the format function hermetic.
func chieveFromID(chieveID int) *wowgd.Achievement {
log.Debug("Chieve from ID", "ID", chieveID)
if chieveID == 0 {
return nil
}
c, _, err := bNetClient.WoWAchievement(ctx, chieveID)
if err != nil {
raven.CaptureError(err, nil)
return nil
}
log.Debug("Chieve from ID", "ID", chieveID, "Chievo Found", c.Name)
return c
}
func mapCriteriaToStrings(cc wowgd.ChildCriteria) []string {
var ret []string
for _, c := range cc {
if c.Achievement.ID == 0 {
if len(c.ChildCriteria) == 0 {
continue
}
if c.Operator.Name != "" && len(c.ChildCriteria) > 1 {
if c.Amount > 0 {
ret = append(ret, fmt.Sprintf("%s %d of:", c.Operator.Name, c.Amount))
} else {
ret = append(ret, fmt.Sprintf("%s of:", c.Operator.Name))
}
}
} else {
tryChieve := chieveFromID(c.Achievement.ID)
if tryChieve != nil && tryChieve.ID != 0 {
var faction string
if len(c.Faction.Name) > 1 {
faction = fmt.Sprintf(" [%s]", string(c.Faction.Name[0]))
}
ret = append(ret, fmt.Sprintf("<http://www.wowhead.com/achievement=%d|%s>%s - %s", tryChieve.ID, tryChieve.Name, faction, tryChieve.Description))
}
}
if len(c.ChildCriteria) > 0 {
ret = append(ret, mapCriteriaToStrings(c.ChildCriteria)...)
}
}
return ret
}
func singleBareChievoCriterion(c *wowgd.Achievement) map[int]string {
log.Debug("SBCC")
ret := make(map[int]string)
ret[c.Criteria.ChildCriteria[0].ID] = fmt.Sprintf("<http://www.wowhead.com/achievement=%d|%s> - %s", c.ID, c.Name, c.Description)
if c.Criteria.Amount > 1 {
ret[c.Criteria.ChildCriteria[0].ID] = ret[c.Criteria.ChildCriteria[0].ID] + fmt.Sprintf(" [%%s/%d]", c.Criteria.Amount)
}
return ret
}
func mapCriteriaToName(cc wowgd.ChildCriteria) map[int]string {
log.Debug("Recursing into Mapping Criteria to Name")
ret := make(map[int]string)
for _, c := range cc {
tryChieve := chieveFromID(c.Achievement.ID)
if tryChieve != nil && tryChieve.ID != 0 {
ret[c.ID] = fmt.Sprintf("<http://www.wowhead.com/achievement=%d|%s> - %s", tryChieve.ID, tryChieve.Name, tryChieve.Description)
if c.Amount > 1 {
ret[c.ID] = ret[c.ID] + fmt.Sprintf(" [%%s/%d]", c.Amount)
}
} else {
ret[c.ID] = c.Description
if c.Amount > 1 {
ret[c.ID] = ret[c.ID] + fmt.Sprintf(" [%%s/%d]", c.Amount)
}
}
if len(c.ChildCriteria) > 0 {
ret = mergeIntStringMaps(mapCriteriaToName(c.ChildCriteria), ret)
}
}
log.Debug("Recursing into Mapping Criteria to Name", "Ret", ret)
return ret
}
func recursePlayerCriteria(cc wowp.ChildCriteria) map[int]playerCriteriaStuff {
log.Debug("Recursing into Player Criteria")
ret := make(map[int]playerCriteriaStuff)
for _, c := range cc {
ret[c.ID] = playerCriteriaStuff{c.IsCompleted, strconv.FormatInt(int64(c.Amount), 10)}
if len(c.ChildCriteria) > 0 {
ret = mergeIntStuffMaps(recursePlayerCriteria(c.ChildCriteria), ret)
}
}
log.Debug("Recursing into Player Criteria", "Ret", ret)
return ret
}
func mergeIntStuffMaps(new map[int]playerCriteriaStuff, existing map[int]playerCriteriaStuff) map[int]playerCriteriaStuff {
for k, v := range new {
existing[k] = v
}
return existing
}