-
Notifications
You must be signed in to change notification settings - Fork 3
/
bnet_char.go
289 lines (278 loc) · 8.69 KB
/
bnet_char.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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package main
import (
"fmt"
"math/rand"
"strings"
"time"
"github.com/FuzzyStatic/blizzard/v2/wowp"
raven "github.com/getsentry/raven-go"
log "gopkg.in/inconshreveable/log15.v2"
)
type wowDude struct {
cps *wowp.CharacterProfileSummary
css *wowp.CharacterStatisticsSummary
ces *wowp.CharacterEquipmentSummary
cas *wowp.CharacterAchievementsStatistics
}
func getDudeRaid(input1, input2, expn, tier string) string {
log.Debug("GDR", "Player", input1, "Realm", input2, "Expn", expn, "Tier", tier)
if bNetClient == nil {
return "WOW API not available"
}
realm, player, err := distinguishRealmFromPlayer(input1, input2)
if err != nil {
return "Could not distinguish realm"
}
var ret []string
cr, _, err := bNetClient.WoWCharacterRaids(ctx, realm, player)
if err != nil {
log.Warn("GDR", "Err", err)
raven.CaptureError(err, nil)
return "Could not retrieve raids"
}
for _, ex := range cr.Expansions {
if strings.EqualFold(strings.ToLower(ex.Expansion.Name), strings.ToLower(expn)) {
for _, i := range ex.Instances {
if strings.EqualFold(strings.ToLower(i.Instance.Name), strings.ToLower(tier)) {
ret = append(ret, fmt.Sprintf("%s - %s", ex.Expansion.Name, i.Instance.Name))
for _, m := range i.Modes {
t := ""
if m.Status.Name == "Complete" {
t = ":trophy: "
}
ret = append(ret, fmt.Sprintf("Difficulty: %s -- %s%d/%d", m.Difficulty.Name, t, m.Progress.CompletedCount, m.Progress.TotalCount))
}
return strings.Join(ret, "\n")
}
}
return "Raid found, instance not found"
}
}
return "Raid not found"
}
func retrieveDude(player, realm string) (wowDude, error) {
log.Debug("RD", "Player", player, "Realm", realm)
if c, ok := wowPlayerCache.Get(realm + "-" + player); ok {
return c.(wowDude), nil
}
var ret wowDude
cps, _, err := bNetClient.WoWCharacterProfileSummary(ctx, realm, player)
if err != nil {
log.Warn("RD", "CPS", err)
raven.CaptureError(err, nil)
return ret, err
}
css, _, err := bNetClient.WoWCharacterStatisticsSummary(ctx, realm, player)
if err != nil {
log.Warn("RD", "CSS", err)
raven.CaptureError(err, nil)
return ret, err
}
ces, _, err := bNetClient.WoWCharacterEquipmentSummary(ctx, realm, player)
if err != nil {
log.Warn("RD", "CES", err)
raven.CaptureError(err, nil)
return ret, err
}
cas, _, err := bNetClient.WoWCharacterAchievementsStatistics(ctx, realm, player)
if err != nil {
log.Warn("RD", "CAS", err)
raven.CaptureError(err, nil)
return ret, err
}
ret = wowDude{
cps: cps,
css: css,
ces: ces,
cas: cas,
}
return ret, nil
}
func printWoWDude(input1, input2 string) string {
log.Debug("PWD", "Player", input1, "Realm", input2)
if bNetClient == nil {
return "WOW API not available"
}
realm, player, err := distinguishRealmFromPlayer(input1, input2)
if err != nil {
return "Could not distinguish realm"
}
wd, err := retrieveDude(player, realm)
if err != nil {
return "Problem retrieving player"
}
var ret []string
var name = wd.cps.Name
if len(wd.cps.ActiveTitle.DisplayString) > 0 {
name = strings.Replace(wd.cps.ActiveTitle.DisplayString, "{name}", wd.cps.Name, -1)
}
ret = append(ret, fmt.Sprintf("%s - Level %d %s %s", name, wd.cps.Level, wd.cps.ActiveSpec.Name, wd.cps.CharacterClass.Name))
ret = append(ret, fmt.Sprintf("iLvl %d -- :trophy: %d", wd.cps.AverageItemLevel, wd.cps.AchievementPoints))
for _, ei := range wd.ces.EquippedItems {
emoji := fmt.Sprintf(":wow-%s:", strings.ToLower(string(ei.Quality.Name[0])))
sText := ""
for _, s := range ei.Spells {
// Corruption purple
if s.DisplayColor.R == corruptionR && s.DisplayColor.G == corruptionG && s.DisplayColor.B == corruptionB {
sText = "[Corruption: " + s.Spell.Name + "]"
}
}
ret = append(ret, fmt.Sprintf("%s <http://www.wowhead.com/item=%d|%s> (%d) %s", emoji, ei.Item.ID, ei.Name, ei.Level.Value, sText))
// TODO: Context (i.e Mythic, WQ, etc)
}
return strings.Join(ret, "\n")
}
func handleStatInput(input string) string {
tokens := strings.SplitN(input, " ", 3)
log.Debug("Handling Stat Input", "Input", input, "Tokens", tokens)
realm, player, err := distinguishRealmFromPlayer(tokens[0], tokens[1])
if err != nil {
return err.Error()
}
var statName string
if len(tokens) < 3 {
statName = "random"
} else {
statName = tokens[2]
}
p, err := retrieveDude(player, realm)
if err != nil {
return "Problem retrieving player"
}
statName, statDesc, statQty, err := getDudeStat(p, statName)
if err != nil {
return err.Error()
}
return fmt.Sprintf("%s : %s (%v)", statName, statDesc, statQty)
}
func handleStatFightInput(input string) string {
tokens := strings.SplitN(input, " ", 5)
if len(tokens) < 4 {
return "Invalid command"
}
var statName string
if len(tokens) == 4 {
statName = "random"
} else {
statName = tokens[4]
}
log.Debug("Handling Stat Fight Input", "Input", input, "Tokens", tokens)
if bNetClient == nil {
return "WOW API not available"
}
realm1, player1, err := distinguishRealmFromPlayer(tokens[0], tokens[1])
if err != nil {
return "Could not distinguish realm for Player 1"
}
p1, err := retrieveDude(player1, realm1)
if err != nil {
return "Problem retrieving player 1"
}
realm2, player2, err := distinguishRealmFromPlayer(tokens[2], tokens[3])
if err != nil {
return "Could not distinguish realm for Player 2"
}
p2, err := retrieveDude(player2, realm2)
if err != nil {
return "Problem retrieving player 2"
}
return statFight(p1, p2, statName)
}
func statFight(p1, p2 wowDude, statName string) string {
log.Debug("StatFight", "p1", p1.cps.Name, "p2", p2.cps.Name, "Name", statName)
if statName == "random" {
p1stats := populateWoWStats(p1)
p2stats := populateWoWStats(p2)
var commonStats []string
for _, s := range p1stats {
if stringSliceContains(p2stats, s) {
commonStats = append(commonStats, s)
}
}
rand.Seed(time.Now().Unix())
statName = commonStats[rand.Intn(len(commonStats))]
log.Debug("StatFight Randomised", "p1 Stat Len", len(p1stats), "p2 Stat Len ", len(p2stats), "Common len", len(commonStats), "Name", statName)
}
p1name, p1desc, p1qty, err := getDudeStat(p1, statName)
if err != nil {
return err.Error()
}
_, p2desc, p2qty, err := getDudeStat(p2, p1name)
if err != nil {
return err.Error()
}
if p1qty > p2qty {
return fmt.Sprintf("%s\n[:white_check_mark:] %s : %s (%v) vs %s : %s (%v) [:x:]", statName, p1.cps.Name, p1desc, p1qty, p2.cps.Name, p2desc, p2qty)
} else if p1qty < p2qty {
return fmt.Sprintf("%s\n[:x:] %s : %s (%v) vs %s : %s (%v) [:white_check_mark:]", p1name, p1.cps.Name, p1desc, p1qty, p2.cps.Name, p2desc, p2qty)
} else {
return fmt.Sprintf("%s\n[:interrobang:] TIE!! Both on %v", p1name, p1qty)
}
}
// Returns Stat Name, Stat Description, Stat Quantity and Error
func getDudeStat(player wowDude, statName string) (string, string, float64, error) {
log.Debug("Get stat", "Player", player.cps.Name, "Stat", statName)
if bNetClient == nil {
return "", "", 0, fmt.Errorf("WOW API not available")
}
if statName == "random" {
stats := populateWoWStats(player)
rand.Seed(time.Now().Unix())
statName = stats[rand.Intn(len(stats))]
}
for _, cat := range player.cas.Categories {
for _, sc := range cat.SubCategories {
for _, stat := range sc.Statistics {
if strings.EqualFold(strings.ToLower(stat.Name), strings.ToLower(statName)) {
return stat.Name, stat.Description, stat.Quantity, nil
}
}
}
for _, stat := range cat.Statistics {
if strings.EqualFold(strings.ToLower(stat.Name), strings.ToLower(statName)) {
return stat.Name, stat.Description, stat.Quantity, nil
}
}
}
return "", "", 0, fmt.Errorf("Stat not found")
}
func getDudeReps(input1, input2 string) string {
log.Debug("Get reps", "Player", input1, "Realm", input2)
if bNetClient == nil {
return "WOW API not available"
}
realm, player, err := distinguishRealmFromPlayer(input1, input2)
if err != nil {
return "Could not distinguish realm"
}
var ret []string
reps, _, err := bNetClient.WoWCharacterReputationsSummary(ctx, realm, player)
if err != nil {
log.Warn("GDRep", "Err", err)
raven.CaptureError(err, nil)
return "Could not retrieve reputations"
}
for _, r := range reps.Reputations {
if stringSliceContains(conf.BattleNet.Reputations, r.Faction.Name) {
emoji := ":question_man:"
switch r.Standing.Name {
case "Hated":
emoji = ":angry:"
case "Stranger":
emoji = ":thinking_tom:"
case "Neutral":
emoji = ":neutral_face:"
case "Friendly":
emoji = ":fry_real:"
case "Honored":
emoji = ":ok_hand:"
case "Revered":
emoji = ":bflove:"
case "Exalted":
emoji = ":angrylaugh:"
}
ret = append(ret, fmt.Sprintf("%s %s - %s [%d/%d]", emoji, r.Faction.Name, r.Standing.Name, r.Standing.Value, r.Standing.Max))
}
}
return strings.Join(ret, "\n")
}