|
1
|
|
|
package albion |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"encoding/json" |
|
5
|
|
|
"errors" |
|
6
|
|
|
"fmt" |
|
7
|
|
|
"github.com/FlameInTheDark/dtbot/bot" |
|
8
|
|
|
"net/http" |
|
9
|
|
|
"strings" |
|
10
|
|
|
"time" |
|
11
|
|
|
) |
|
12
|
|
|
|
|
13
|
|
|
// SearchResult contains search response |
|
14
|
|
|
type SearchResult struct { |
|
15
|
|
|
Guilds []GuildSearch `json:"guilds"` |
|
16
|
|
|
Players []PlayerSearch `json:"players"` |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
// GuildSearch contains guild data from search response |
|
20
|
|
|
type GuildSearch struct { |
|
21
|
|
|
ID string `json:"Id"` |
|
22
|
|
|
Name string `json:"Name"` |
|
23
|
|
|
AllianceID string `json:"AllianceId"` |
|
24
|
|
|
AllianceName string `json:"AllianceName"` |
|
25
|
|
|
KillFame int `json:"KillFame"` |
|
26
|
|
|
DeathFame int `json:"DeathFame"` |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
// PlayerSearch contains player data from search response |
|
30
|
|
|
type PlayerSearch struct { |
|
31
|
|
|
ID string `json:"Id"` |
|
32
|
|
|
Name string `json:"Name"` |
|
33
|
|
|
AllianceID string `json:"AllianceId"` |
|
34
|
|
|
AllianceName string `json:"AllianceName"` |
|
35
|
|
|
KillFame int `json:"KillFame"` |
|
36
|
|
|
DeathFame int `json:"DeathFame"` |
|
37
|
|
|
Avatar string `json:"Avatar"` |
|
38
|
|
|
AvatarRing string `json:"AvatarRing"` |
|
39
|
|
|
FameRation float64 `json:"FameRatio"` |
|
40
|
|
|
TotalKills int `json:"totalKills"` |
|
41
|
|
|
GVGKills int `json:"gvgKills"` |
|
42
|
|
|
GVGWon int `json:"gvgWon"` |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
// Player data |
|
46
|
|
|
type Player struct { |
|
47
|
|
|
AverageItemPower float64 `json:"AverageItemPower"` |
|
48
|
|
|
Equipment Equipment `json:"Equipment"` |
|
49
|
|
|
Inventory []Item `json:"Inventory"` |
|
50
|
|
|
Name string `json:"Name"` |
|
51
|
|
|
Id string `json:"Id"` |
|
|
|
|
|
|
52
|
|
|
GuildName string `json:"GuildName"` |
|
53
|
|
|
GuildId string `json:"GuildId"` |
|
|
|
|
|
|
54
|
|
|
AllianceName string `json:"AllianceName"` |
|
55
|
|
|
AllianceId string `json:"AllianceId"` |
|
|
|
|
|
|
56
|
|
|
AllianceTag string `json:"AllianceTag"` |
|
57
|
|
|
Avatar string `json:"Avatar"` |
|
58
|
|
|
AvatarRing string `json:"AvatarRing"` |
|
59
|
|
|
DeathFame int `json:"DeathFame"` |
|
60
|
|
|
KillFame int `json:"KillFame"` |
|
61
|
|
|
FameRatio float64 `json:"FameRatio"` |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Equipment contains items in slots |
|
65
|
|
|
type Equipment struct { |
|
66
|
|
|
MainHand Item `json:"MainHand"` |
|
67
|
|
|
OffHand Item `json:"OffHand"` |
|
68
|
|
|
Head Item `json:"Head"` |
|
69
|
|
|
Armor Item `json:"Armor"` |
|
70
|
|
|
Shoes Item `json:"Shoes"` |
|
71
|
|
|
Bag Item `json:"Bag"` |
|
72
|
|
|
Cape Item `json:"Cape"` |
|
73
|
|
|
Mount Item `json:"Mount"` |
|
74
|
|
|
Potion Item `json:"Potion"` |
|
75
|
|
|
Food Item `json:"Food"` |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
// Item contains item data |
|
79
|
|
|
type Item struct { |
|
80
|
|
|
Type string `json:"Type"` |
|
81
|
|
|
Count int `json:"Count"` |
|
82
|
|
|
Quality int `json:"Quality"` |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
// Kill contains kill data |
|
86
|
|
|
type Kill struct { |
|
87
|
|
|
GroupMemberCount int `json:"groupMemberCount"` |
|
88
|
|
|
NumberOfParticipants int `json:"numberOfParticipants"` |
|
89
|
|
|
EventID int `json:"EventId"` |
|
90
|
|
|
TimeStamp string `json:"TimeStamp"` |
|
91
|
|
|
Version int `json:"Version"` |
|
92
|
|
|
Killer Player `json:"Killer"` |
|
93
|
|
|
Victim Player `json:"Victim"` |
|
94
|
|
|
TotalVictimKillFame int `json:"TotalVictimKillFame"` |
|
95
|
|
|
Location string `json:"Location"` |
|
96
|
|
|
Participants []Player `json:"Participants"` |
|
97
|
|
|
GroupMembers []Player `json:"GroupMembers"` |
|
98
|
|
|
BattleID int `json:"BattleId"` |
|
99
|
|
|
Type string `json:"Type"` |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
// SearchPlayers returns player list by name |
|
103
|
|
|
func SearchPlayers(name string) (result *SearchResult, err error) { |
|
104
|
|
|
var sResult SearchResult |
|
105
|
|
|
resp, err := http.Get(fmt.Sprintf("https://gameinfo.albiononline.com/api/gameinfo/search?q=%v", name)) |
|
106
|
|
|
if err != nil { |
|
107
|
|
|
return nil, err |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
if resp.StatusCode != 200 { |
|
111
|
|
|
return nil, errors.New("status " + resp.Status) |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
err = json.NewDecoder(resp.Body).Decode(&sResult) |
|
115
|
|
|
if err != nil { |
|
116
|
|
|
return nil, err |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return &sResult, nil |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
// GetPlayerKills returns array of kills by player id |
|
123
|
|
|
func GetPlayerKills(id string) (result []Kill, err error) { |
|
124
|
|
|
var kills []Kill |
|
125
|
|
|
resp, err := http.Get(fmt.Sprintf("https://gameinfo.albiononline.com/api/gameinfo/players/%v/topkills?range=lastWeek&offset=0&limit=11", id)) |
|
126
|
|
|
if err != nil { |
|
127
|
|
|
return nil, err |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
if resp.StatusCode != 200 { |
|
131
|
|
|
return nil, errors.New("status " + resp.Status) |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
err = json.NewDecoder(resp.Body).Decode(&kills) |
|
135
|
|
|
if err != nil { |
|
136
|
|
|
return nil, err |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return kills, nil |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
func GetKillID(id string) (kill *Kill, err error) { |
|
|
|
|
|
|
143
|
|
|
var result Kill |
|
144
|
|
|
resp, err := http.Get(fmt.Sprintf("https://gameinfo.albiononline.com/api/gameinfo/events/%v", id)) |
|
145
|
|
|
if err != nil { |
|
146
|
|
|
return nil, err |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
if resp.StatusCode != 200 { |
|
150
|
|
|
return nil, errors.New("status " + resp.Status) |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
err = json.NewDecoder(resp.Body).Decode(&result) |
|
154
|
|
|
if err != nil { |
|
155
|
|
|
return nil, err |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
return kill, nil |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
// ShowKills sends embed message in discord |
|
162
|
|
|
func ShowKills(ctx *bot.Context) { |
|
163
|
|
|
search, err := SearchPlayers(ctx.Args[1]) |
|
164
|
|
|
if err != nil { |
|
165
|
|
|
fmt.Println("Error:" + err.Error()) |
|
166
|
|
|
return |
|
167
|
|
|
} |
|
168
|
|
|
fmt.Println("Founded players") |
|
169
|
|
|
if len(search.Players) > 0 { |
|
170
|
|
|
fmt.Println("Players more then 0") |
|
171
|
|
|
kills, err := GetPlayerKills(search.Players[0].ID) |
|
172
|
|
|
fmt.Println("Searching kills of " + search.Players[0].Name + search.Players[0].ID) |
|
173
|
|
|
if err != nil { |
|
174
|
|
|
fmt.Println("Error: " + err.Error()) |
|
175
|
|
|
return |
|
176
|
|
|
} |
|
177
|
|
|
fmt.Println("Founded kills of " + search.Players[0].Name) |
|
178
|
|
|
if len(kills) > 0 { |
|
179
|
|
|
fmt.Println("Kills more then 0") |
|
180
|
|
|
embed := bot.NewEmbed("Albion Killboard") |
|
181
|
|
|
embed.Desc(fmt.Sprintf("[%v](https://albiononline.com/ru/killboard/player/%v)", search.Players[0].Name, search.Players[0].ID)) // "https://assets.albiononline.com/assets/images/icons/favicon.ico") |
|
182
|
|
|
embed.Color(ctx.GuildConf().EmbedColor) |
|
183
|
|
|
for _, k := range kills { |
|
184
|
|
|
fmt.Println("Killed " + k.Victim.Name) |
|
185
|
|
|
var timeString string |
|
186
|
|
|
t, err := time.Parse("2006-01-02T15:04:05.000000000Z", k.TimeStamp) |
|
187
|
|
|
if err == nil { |
|
188
|
|
|
timeString = fmt.Sprintf("%v.%v.%v %v:%v", t.Day(), t.Month().String(), t.Year(), t.Hour(), t.Minute()) |
|
189
|
|
|
} else { |
|
190
|
|
|
fmt.Println("Parse time: ", err.Error()) |
|
191
|
|
|
} |
|
192
|
|
|
embed.Field( |
|
193
|
|
|
k.Victim.Name, |
|
194
|
|
|
fmt.Sprintf("%v [[Link](https://albiononline.com/ru/killboard/kill/%v)]", |
|
195
|
|
|
fmt.Sprintf(ctx.Loc("albion_kill_short"), |
|
|
|
|
|
|
196
|
|
|
k.Victim.DeathFame, |
|
197
|
|
|
k.Victim.AverageItemPower, |
|
198
|
|
|
timeString), |
|
199
|
|
|
k.EventID), false) |
|
200
|
|
|
} |
|
201
|
|
|
embed.Send(ctx) |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
|
|
func ShowKill(ctx *bot.Context) { |
|
|
|
|
|
|
208
|
|
|
kill, err := GetKillID(ctx.Args[1]) |
|
209
|
|
|
if err != nil { |
|
210
|
|
|
fmt.Println("Error:" + err.Error()) |
|
211
|
|
|
return |
|
212
|
|
|
} |
|
213
|
|
|
if kill == nil { |
|
214
|
|
|
fmt.Println("Error! Kill is nil!") |
|
215
|
|
|
return |
|
216
|
|
|
} |
|
217
|
|
|
embed := bot.NewEmbed(fmt.Sprintf("Show on killboard #%v", kill.EventID)) |
|
218
|
|
|
embed.Desc(fmt.Sprintf("%v :crossed_swords: %v", kill.Killer.Name, kill.Victim.Name)) |
|
219
|
|
|
embed.Color(ctx.GuildConf().EmbedColor) |
|
220
|
|
|
embed.URL(fmt.Sprintf("https://albiononline.com/ru/killboard/kill/%v", kill.EventID)) |
|
221
|
|
|
embed.AttachThumbURL("https://assets.albiononline.com/assets/images/killboard/kill__date.png") |
|
222
|
|
|
embed.Author("Albion Killboard", "https://albiononline.com/ru/killboard", "https://assets.albiononline.com/assets/images/icons/favicon.ico") |
|
223
|
|
|
embed.TimeStamp(kill.TimeStamp) |
|
224
|
|
|
embed.Field(ctx.Loc("albion_guild"), kill.Victim.GuildName, true) |
|
225
|
|
|
embed.Field(ctx.Loc("albion_fame"), string(kill.Victim.DeathFame), true) |
|
226
|
|
|
embed.Field(ctx.Loc("albion_item_power"), fmt.Sprintf("%.3f", kill.Victim.AverageItemPower), true) |
|
227
|
|
|
if len(kill.Participants) > 0 { |
|
228
|
|
|
var names []string |
|
229
|
|
|
for _,p := range kill.Participants { |
|
230
|
|
|
names = append(names, p.Name) |
|
231
|
|
|
} |
|
232
|
|
|
embed.Field(ctx.Loc("albion_participants"), strings.Join(names, ", "), true) |
|
233
|
|
|
} |
|
234
|
|
|
embed.Send(ctx) |
|
235
|
|
|
} |
|
236
|
|
|
|