-
Notifications
You must be signed in to change notification settings - Fork 1
/
d2lb.go
379 lines (344 loc) · 9.04 KB
/
d2lb.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (c) 2020-2022, The OneBot Contributors. All rights reserved.
package main
import (
"fmt"
"os"
"strconv"
"strings"
"golang.org/x/text/language"
"golang.org/x/text/message"
"github.com/TheDiscordian/onebot/onelib"
"github.com/lunixbochs/struc"
)
const (
// NAME is same as filename, minus extension
NAME = "d2lb"
// LONGNAME is what's presented to the user
LONGNAME = "Diablo II Leaderboards"
// VERSION of the plugin
VERSION = "v0.0.1"
)
var (
// Path to ladder.D2DV
d2lbLadderPath string
)
// Load returns the Plugin object.
func Load() onelib.Plugin {
d2lbLadderPath = onelib.GetTextConfig(NAME, "ladder_path")
return new(D2LBPlugin)
}
func formatXp(xp int) string {
outMsg := "%d"
if xp >= 1000000 {
outMsg = "%dK"
xp /= 1000
}
p := message.NewPrinter(language.English)
return p.Sprintf(outMsg, xp)
}
func createTable(title string, chars []*CharInfo) (text, formattedText string) {
text = fmt.Sprintf("Diablo II %s Ladder:\n", title)
formattedText = fmt.Sprintf("<strong>Diablo II %s Ladder:</strong><br />\n<table><tr><th> # </th><th> Name </th><th> Class </th><th> Level </th><th> XP </th></tr><br />\n", title)
for i, char := range chars { // getTitle(c Class, expansion bool, difficulty int, hardcore bool)
text += fmt.Sprintf(" %d. %s [%s] Lvl.%d (%sxp)\n", i+1, getTitle(char.Class, char.expansion, char.difficulty, char.hardcore)+char.CharName, classToString(char.Class), char.Level, formatXp(char.Experience))
var highlight, highlightCloser string
switch i {
case 0:
highlight = `<font color="#C9B037">`
highlightCloser = "</font>"
case 1:
highlight = `<font color="#D7D7D7">`
highlightCloser = "</font>"
case 2:
highlight = `<font color="#6A3805">`
highlightCloser = "</font>"
}
formattedText += fmt.Sprintf("<tr><td> %s%d%s </td><th> <strong>%s%s%s</strong> </th><td> %s </td><td> %d </td><td> %s<br /></td></tr>\n", highlight, i+1, highlightCloser, highlight, getTitle(char.Class, char.expansion, char.difficulty, char.hardcore)+char.CharName, highlightCloser, classToString(char.Class), char.Level, formatXp(char.Experience))
if i <= 2 {
formattedText += "</font>"
}
}
formattedText += "</table>"
return
}
func d2xpLb(msg onelib.Message, sender onelib.Sender) {
lbn, err := strconv.Atoi(msg.Text())
if err != nil {
lbn = 10
}
_, _, exp, _ := getLeaderboards(lbn)
text, formattedText := createTable("Expansion", exp)
sender.Location().SendFormattedText(text, formattedText)
}
func d2xphcLb(msg onelib.Message, sender onelib.Sender) {
lbn, err := strconv.Atoi(msg.Text())
if err != nil {
lbn = 10
}
_, _, _, exphc := getLeaderboards(lbn)
text, formattedText := createTable("Expansion Hardcore", exphc)
sender.Location().SendFormattedText(text, formattedText)
}
func d2Lb(msg onelib.Message, sender onelib.Sender) {
lbn, err := strconv.Atoi(msg.Text())
if err != nil {
lbn = 10
}
d2, _, _, _ := getLeaderboards(lbn)
text, formattedText := createTable("Standard", d2)
sender.Location().SendFormattedText(text, formattedText)
}
func d2hcLb(msg onelib.Message, sender onelib.Sender) {
lbn, err := strconv.Atoi(msg.Text())
if err != nil {
lbn = 10
}
_, hc, _, _ := getLeaderboards(lbn)
text, formattedText := createTable("Standard Hardcore", hc)
sender.Location().SendFormattedText(text, formattedText)
}
func d2AllLb(msg onelib.Message, sender onelib.Sender) {
lbn, err := strconv.Atoi(msg.Text())
if err != nil {
lbn = 3
}
d2, hc, exp, exphc := getLeaderboards(lbn)
text, formattedText := createTable("Standard", d2)
text2, formattedText2 := createTable("Standard Hardcore", hc)
text3, formattedText3 := createTable("Expansion", exp)
text4, formattedText4 := createTable("Expansion Hardcore", exphc)
sender.Location().SendFormattedText(text+"\n"+text2+"\n"+text3+"\n"+text4, formattedText+"<br>"+formattedText2+"<br>"+formattedText3+"<br>"+formattedText4)
}
// D2LBPlugin is an object for satisfying the Plugin interface.
type D2LBPlugin int
// Name returns the name of the plugin, usually the filename.
func (dp *D2LBPlugin) Name() string {
return NAME
}
// LongName returns the display name of the plugin.
func (dp *D2LBPlugin) LongName() string {
return LONGNAME
}
// Version returns the version of the plugin, usually in the format of "v0.0.0".
func (dp *D2LBPlugin) Version() string {
return VERSION
}
// Implements returns a map of commands and monitor the plugin implements.
func (dp *D2LBPlugin) Implements() (map[string]onelib.Command, *onelib.Monitor) {
return map[string]onelib.Command{"d2xp": d2xpLb, "d2": d2Lb, "d2hc": d2hcLb, "d2xphc": d2xphcLb, "d2all": d2AllLb}, nil
}
// Remove is necessary to satisfy the Plugin interface, it does nothing.
func (dp *D2LBPlugin) Remove() {
}
type LadderHeader struct {
MaxType int `struc:"int32,little"`
Checksum int `struc:"int32,little"`
}
type CharInfo struct {
Experience int `struc:"int32,little"`
Status int `struc:"int8,little"`
ActsCompleted int `struc:"int8,little"` // this doesn't seem to update after completing act IV : /
Level int `struc:"int8,little"`
Class Class `struc:"int8,little"`
CharName string `struc:"[16]int8,little"`
// statuses
expansion bool
difficulty int
hardcore bool
dead bool
}
type LadderIndex struct {
Type int `struc:"int32,little"`
Offset int `struc:"int32,little"`
Number int `struc:"int32,little"`
}
const (
ExpansionBit = 1 << 5
HardcoreBit = 1 << 2
DeadBit = 1 << 3
)
type Class byte
const (
Amazon Class = iota
Sorceress
Necromancer
Paladin
Barbarian
Druid
Assassin
)
func classToString(c Class) string {
switch c {
case Amazon:
return "ama"
case Sorceress:
return "sor"
case Necromancer:
return "nec"
case Paladin:
return "pal"
case Barbarian:
return "bar"
case Druid:
return "dru"
case Assassin:
return "asn"
}
return ""
}
func getGender(c Class) string {
switch c {
case Amazon, Sorceress, Assassin:
return "f"
default:
return "m"
}
}
func getTitle(c Class, expansion bool, difficulty int, hardcore bool) string {
gender := getGender(c)
if expansion {
switch difficulty {
case 1:
if !hardcore {
return "Slayer "
} else {
return "Destroyer "
}
case 2:
if !hardcore {
return "Champion "
} else {
return "Conqueror "
}
case 3:
if hardcore {
return "Guardian "
} else {
if gender == "f" {
return "Matriarch "
} else {
return "Patriarch "
}
}
}
} else {
if gender == "f" {
switch difficulty {
case 1:
if !hardcore {
return "Dame "
} else {
return "Countess "
}
case 2:
if !hardcore {
return "Lady "
} else {
return "Duchess "
}
case 3:
if !hardcore {
return "Baroness "
} else {
return "Queen "
}
}
} else {
switch difficulty {
case 1:
if !hardcore {
return "Sir "
} else {
return "Count "
}
case 2:
if !hardcore {
return "Lord "
} else {
return "Duke "
}
case 3:
if !hardcore {
return "Baron "
} else {
return "King "
}
}
}
}
return ""
}
// count must be positive and non-zero, or you won't get any results
func getLeaderboards(count int) (norm, hc, exp, expHc []*CharInfo) {
if count > 50 {
count = 50
}
f, err := os.Open(d2lbLadderPath)
if err != nil {
onelib.Error.Println(err)
return
}
ladderHeader := new(LadderHeader)
err = struc.Unpack(f, ladderHeader)
if err != nil {
onelib.Error.Println(err)
return
}
// Iterate past ... whatever this data is
for i := 0; i < ladderHeader.MaxType; i++ {
ladderIndex := new(LadderIndex)
err = struc.Unpack(f, ladderIndex)
if err != nil {
onelib.Error.Println(err)
return
}
}
norm = make([]*CharInfo, 0, count)
hc = make([]*CharInfo, 0, count)
exp = make([]*CharInfo, 0, count)
expHc = make([]*CharInfo, 0, count)
// Throw all the chars into a map, because there will be duplicates
chars := make(map[string]*CharInfo)
charInfo := new(CharInfo)
var i int
for ; err == nil; err = struc.Unpack(f, charInfo) {
charInfo.CharName = strings.Replace(charInfo.CharName, "\x00", "", -1)
if charInfo.CharName == "" || charInfo.CharName[0] == 0 || chars[charInfo.CharName] != nil {
continue
}
if err == nil {
i++
if charInfo.Status&ExpansionBit == ExpansionBit {
charInfo.expansion = true
}
if charInfo.Status&HardcoreBit == HardcoreBit {
charInfo.hardcore = true
if charInfo.Status&DeadBit == DeadBit {
charInfo.dead = true
}
}
//fmt.Printf("%+v\n", charInfo)
if !charInfo.expansion {
charInfo.difficulty = charInfo.ActsCompleted / 4
if !charInfo.hardcore && len(norm) < count {
norm = append(norm, charInfo)
} else if charInfo.hardcore && len(hc) < count {
hc = append(hc, charInfo)
}
} else {
charInfo.difficulty = charInfo.ActsCompleted / 5
if !charInfo.hardcore && len(exp) < count {
exp = append(exp, charInfo)
} else if charInfo.hardcore {
if len(expHc) >= count {
return
}
expHc = append(expHc, charInfo)
}
}
chars[charInfo.CharName] = charInfo
charInfo = new(CharInfo)
}
}
return
}