-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVGT-Utility.lua
695 lines (648 loc) · 17.7 KB
/
VGT-Utility.lua
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
---@param perc number
---@param ... number
---@return number r, number g, number b
local function colorGradient(perc, ...)
if perc >= 1 then
local r, g, b = select(select("#", ...) - 2, ...)
return r, g --[[@as number]], b --[[@as number]]
elseif perc <= 0 then
local r, g, b = ...
return r, g, b
end
local num = select("#", ...) / 3
local segment, relperc = math.modf(perc * (num - 1))
local r1, g1, b1, r2, g2, b2 = select((segment * 3) + 1, ...)
return r1 + (r2 - r1) * relperc, g1 + (g2 - g1) * relperc, b1 + (b2 - b1) * relperc
end
---Converts RGB magnitude valus to their hexadecimal representation
---@param r number 0-1 red value
---@param g number 0-1 green value
---@param b number 0-1 blue value
---@return string result Hexadecimal string representation of the color
function VGT.RGBToHex(r, g, b)
r = r <= 1 and r >= 0 and r or 0
g = g <= 1 and g >= 0 and g or 0
b = b <= 1 and b >= 0 and b or 0
return string.format("%02x%02x%02x", r * 255, g * 255, b * 255)
end
---Converts a percentage value to a hexadecimal representation of a color gradient from red to green
---@param percentage number
---@return string
function VGT.GetColorGradientHex(percentage)
return VGT.RGBToHex(colorGradient(percentage, 1, 0, 0, 1, 1, 0, 0, 1, 0))
end
---Rounds a number to a specified decimal
---@param number integer
---@param decimals integer|nil
---@return string
function VGT.Round(number, decimals)
local fmt
if decimals == nil or decimals <= 0 then
fmt = "%.0f"
elseif decimals == 1 then
fmt = "%.1f"
elseif decimals == 2 then
fmt = "%.2f"
else
fmt = string.format("%%.%df", decimals)
end
return string.format(fmt, number or 0)
end
---@alias ItemRestriction boolean|table<string, true>
---@type table<integer|string,ItemRestriction>
VGT.overrideEquipTable = {}
local classesPrefix = ITEM_CLASSES_ALLOWED:gsub("%%s", "(.*)")
---@private
---@param itemId integer|string
---@return ItemRestriction
function VGT:BuildRestrictions(itemId)
VGTScanningTooltip:ClearLines()
VGTScanningTooltip:SetHyperlink("item:" .. itemId .. ":0:0:0:0:0:0:0")
for i = 1, VGTScanningTooltip:NumLines() do
local line = _G["VGTScanningTooltipTextLeft" .. i]
local text = line and line:GetText() or ""
text = string.match(text, classesPrefix)
if text then
local overrides = {}
local classes = {}
for i = 1, GetNumClasses() do
local name, fileName = GetClassInfo(i)
if name and fileName then
classes[name] = fileName
end
end
for class in string.gmatch(text, "([%a]+[ ]?[%a]*)[, ]?") do
overrides[classes[class]] = true
end
return overrides
end
end
return true
end
---@private
---@param itemId integer|string
---@return ItemRestriction
function VGT:GetClassRestrictions(itemId)
local overrideResult = VGT.overrideEquipTable[itemId]
if not overrideResult then
overrideResult = self:BuildRestrictions(itemId)
VGT.overrideEquipTable[itemId] = overrideResult
end
return overrideResult
end
---@class RestrictionLevel
---@field [integer|string|"*"] RestrictionLevel|boolean
---@type table<integer, RestrictionLevel|boolean>
VGT.equipTable = {
[Enum.ItemClass.Consumable] = true,
[Enum.ItemClass.Container] = {
[0] = true, -- Bag
[1] = {
WARLOCK = true
}, -- Soul Bag
[2] = true, -- Herb Bag
[3] = true, -- Enchanting Bag
[4] = true, -- Engineering Bag
[5] = true, -- Gem Bag
[6] = true, -- Mining Bag
[7] = true, -- Leatherworking Bag
[8] = true, -- Inscription Bag
[9] = true, -- Tackle Box
[10] = true -- Cooking Bag
},
[Enum.ItemClass.Weapon] = {
[Enum.ItemWeaponSubclass.Axe1H] = {
["*"] = {
DEATHKNIGHT = true,
HUNTER = true,
PALADIN = true,
ROGUE = true,
SHAMAN = true,
WARRIOR = true
}, -- 1h axe
INVTYPE_WEAPONOFFHAND = {
DEATHKNIGHT = true,
HUNTER = true,
ROGUE = true,
SHAMAN = true,
WARRIOR = true
} -- oh axe
},
[Enum.ItemWeaponSubclass.Axe2H] = {
DEATHKNIGHT = true,
HUNTER = true,
PALADIN = true,
SHAMAN = true,
WARRIOR = true
}, -- 2h axe
[Enum.ItemWeaponSubclass.Bows] = {
HUNTER = true,
ROGUE = true,
WARRIOR = true
}, -- bow
[Enum.ItemWeaponSubclass.Guns] = {
HUNTER = true,
ROGUE = true,
WARRIOR = true
}, -- gun
[Enum.ItemWeaponSubclass.Mace1H] = {
["*"] = {
DEATHKNIGHT = true,
DRUID = true,
PALADIN = true,
PRIEST = true,
ROGUE = true,
SHAMAN = true,
WARRIOR = true
}, -- 1h mace
INVTYPE_WEAPONOFFHAND = {
DEATHKNIGHT = true,
ROGUE = true,
SHAMAN = true,
WARRIOR = true
} -- oh mace
},
[Enum.ItemWeaponSubclass.Mace2H] = {
DEATHKNIGHT = true,
DRUID = true,
PALADIN = true,
SHAMAN = true,
WARRIOR = true
}, -- 2h mace
[Enum.ItemWeaponSubclass.Polearm] = {
DEATHKNIGHT = true,
DRUID = true,
HUNTER = true,
PALADIN = true,
WARRIOR = true
}, -- polearm
[Enum.ItemWeaponSubclass.Sword1H] = {
["*"] = {
DEATHKNIGHT = true,
HUNTER = true,
MAGE = true,
PALADIN = true,
ROGUE = true,
WARLOCK = true,
WARRIOR = true
}, -- 1h sword
INVTYPE_WEAPONOFFHAND = {
DEATHKNIGHT = true,
HUNTER = true,
ROGUE = true,
WARRIOR = true
} -- oh sword
},
[Enum.ItemWeaponSubclass.Sword2H] = {
DEATHKNIGHT = true,
HUNTER = true,
PALADIN = true,
WARRIOR = true
}, -- 2h sword
[Enum.ItemWeaponSubclass.Warglaive] = false,
[Enum.ItemWeaponSubclass.Staff] = {
DRUID = true,
HUNTER = true,
MAGE = true,
PRIEST = true,
SHAMAN = true,
WARLOCK = true,
WARRIOR = true
}, -- staff
[Enum.ItemWeaponSubclass.Bearclaw] = false, -- bearclaw
[Enum.ItemWeaponSubclass.Catclaw] = false, -- catclaw
[Enum.ItemWeaponSubclass.Unarmed] = {
["*"] = {
DRUID = true,
HUNTER = true,
ROGUE = true,
SHAMAN = true,
WARRIOR = true
}, -- 1h unarmed
INVTYPE_WEAPONOFFHAND = {
HUNTER = true,
ROGUE = true,
SHAMAN = true,
WARRIOR = true
} -- oh unarmed
},
[Enum.ItemWeaponSubclass.Generic] = true, -- generic
[Enum.ItemWeaponSubclass.Dagger] = {
["*"] = {
DRUID = true,
HUNTER = true,
MAGE = true,
PRIEST = true,
ROGUE = true,
SHAMAN = true,
WARLOCK = true,
WARRIOR = true
}, -- 1h dagger
INVTYPE_WEAPONOFFHAND = {
HUNTER = true,
ROGUE = true,
SHAMAN = true,
WARRIOR = true
} -- oh dagger
},
[Enum.ItemWeaponSubclass.Thrown] = {
HUNTER = true,
ROGUE = true,
WARRIOR = true
}, -- thrown
[Enum.ItemWeaponSubclass.Obsolete3] = false, -- spear
[Enum.ItemWeaponSubclass.Crossbow] = {
HUNTER = true,
ROGUE = true,
WARRIOR = true
}, -- crossbow
[Enum.ItemWeaponSubclass.Wand] = {
MAGE = true,
PRIEST = true,
WARLOCK = true
}, -- wand
[Enum.ItemWeaponSubclass.Fishingpole] = true -- fishing pole
},
[Enum.ItemClass.Gem] = true,
[Enum.ItemClass.Armor] = {
[Enum.ItemArmorSubclass.Generic] = true,
[Enum.ItemArmorSubclass.Cloth] = true,
[Enum.ItemArmorSubclass.Leather] = {
DEATHKNIGHT = true,
DRUID = true,
HUNTER = true,
PALADIN = true,
ROGUE = true,
SHAMAN = true,
WARRIOR = true
},
[Enum.ItemArmorSubclass.Mail] = {
DEATHKNIGHT = true,
HUNTER = true,
PALADIN = true,
SHAMAN = true,
WARRIOR = true
},
[Enum.ItemArmorSubclass.Plate] = {
DEATHKNIGHT = true,
PALADIN = true,
WARRIOR = true
},
[Enum.ItemArmorSubclass.Cosmetic] = true,
[Enum.ItemArmorSubclass.Shield] = {
PALADIN = true,
SHAMAN = true,
WARRIOR = true
},
[Enum.ItemArmorSubclass.Libram] = {
PALADIN = true
},
[Enum.ItemArmorSubclass.Idol] = {
DRUID = true
},
[Enum.ItemArmorSubclass.Totem] = {
SHAMAN = true
},
[Enum.ItemArmorSubclass.Sigil] = {
DEATHKNIGHT = true
},
[Enum.ItemArmorSubclass.Relic] = {
DEATHKNIGHT = true,
DRUID = true,
PALADIN = true,
SHAMAN = true
}
},
[Enum.ItemClass.Reagent] = true,
[Enum.ItemClass.Projectile] = {
HUNTER = true,
ROGUE = true,
WARRIOR = true
},
[Enum.ItemClass.Tradegoods] = true,
[Enum.ItemClass.ItemEnhancement] = true,
[Enum.ItemClass.Recipe] = true,
[Enum.ItemClass.Gem] = true,
[Enum.ItemClass.CurrencyTokenObsolete] = false,
[Enum.ItemClass.Quiver] = {
HUNTER = true,
ROGUE = true,
WARRIOR = true
},
[Enum.ItemClass.Questitem] = true,
[Enum.ItemClass.Key] = true,
[Enum.ItemClass.Gem] = true,
[Enum.ItemClass.PermanentObsolete] = false,
[Enum.ItemClass.Miscellaneous] = true,
[Enum.ItemClass.Glyph] = {
[1] = {
WARRIOR = true
},
[2] = {
PALADIN = true
},
[3] = {
HUNTER = true
},
[4] = {
ROGUE = true
},
[5] = {
PRIEST = true
},
[6] = {
DEATHKNIGHT = true
},
[7] = {
SHAMAN = true
},
[8] = {
MAGE = true
},
[9] = {
WARLOCK = true
},
[11] = {
DRUID = true
}
},
[Enum.ItemClass.Battlepet] = true,
[Enum.ItemClass.WoWToken] = true
}
---@private
---@param source RestrictionLevel|boolean
---@param levels (string|integer)[]
---@param level integer
---@param playerClass string
---@return boolean|nil
local function GetNextLevel(source, levels, level, playerClass)
if type(source) ~= "table" then
VGT.LogTrace("Level %s was %s; returning as value", level, source)
return source --[[@as boolean]]
end
if source[playerClass] then
VGT.LogTrace("Level %s had %q class override; returning true", level, playerClass)
return true
end
if level > #levels then
VGT.LogTrace("Level %s exceeds the maximum; returning false", level)
return false
end
local nextLevel = levels[level]
local nextSource = source[nextLevel]
if nextSource then
VGT.LogTrace("Found %s match at level %s", type(nextSource), level)
else
nextSource = source["*"]
if nextSource then
VGT.LogTrace("Found wildcard %s match at level %s", type(nextSource), level)
else
VGT.LogTrace("No match found for level %s", level)
return
end
end
return GetNextLevel(nextSource, levels, level + 1, playerClass)
end
---Gets a value indicating whether an item is equippable by a class.
---@param item string|integer The id, name, or link for an item.
---@param playerClass string|nil
---@return boolean|nil
function VGT:Equippable(item, playerClass)
local itemId, _, _, equipLocId, _, classId, subclassId = GetItemInfoInstant(item)
playerClass = playerClass or UnitClassBase("player")
VGT.LogTrace("Equippable invoked. equipLocId = %s; classId = %s; subclassId = %s; playerClass = %s", equipLocId, classId, subclassId, playerClass)
local classRestrictions = self:GetClassRestrictions(itemId)
if classRestrictions == true then
return GetNextLevel(self.equipTable, {classId, subclassId, equipLocId}, 1, playerClass)
else
VGT.LogTrace("Found class restrictions for item #%s", itemId)
return classRestrictions[playerClass]
end
end
---Shows a confirmation dialog, then runs an action if it was confirmed.
---@param func fun() The function to run if an `ACCEPT` button is clicked.
---@param text string|nil The text to show in the dialog. If `nil`, the default confirmation text is used.
function VGT:Confirm(func, text)
local dialogId = "VLL_CONFIRM_DIALOG"
local dlg = StaticPopupDialogs[dialogId]
if not dlg then
dlg = {
text = CONFIRM_CONTINUE,
button1 = ACCEPT,
button2 = CANCEL,
hideOnEscape = true
}
StaticPopupDialogs[dialogId] = dlg
end
dlg.OnAccept = func
dlg.text = text or CONFIRM_CONTINUE
StaticPopup_Show(dialogId)
end
---Shows an input dialog
---@param title string The title of the dialog
---@param text string The initial text in the dialog
---@param callback fun(text:string)|nil The callback for when the dialog closes with an `OKAY` button. When nil, no `OKAY` button is shown.
function VGT:ShowInputDialog(title, text, callback)
local dialogId = "VLL_INPUT_DIALOG"
local dlg = StaticPopupDialogs[dialogId]
if not dlg then
local function NOP()
end
dlg = {
hasEditBox = 1,
hasWideEditBox = 1,
editBoxWidth = 350,
preferredIndex = 3,
OnHide = NOP,
OnAccept = NOP,
OnCancel = NOP,
timeout = 0,
whileDead = 1,
hideOnEscape = 1
}
function dlg:EditBoxOnEscapePressed()
self:GetParent():Hide()
end
function dlg:OnAccept()
local dlg = StaticPopupDialogs[dialogId]
if dlg.callback then
local editBox = _G[self:GetName() .. "WideEditBox"] or _G[self:GetName() .. "EditBox"]
dlg.callback(editBox:GetText())
end
end
function dlg:OnShow()
local dlg = StaticPopupDialogs[dialogId]
self:SetWidth(420)
local editBox = _G[self:GetName() .. "WideEditBox"] or _G[self:GetName() .. "EditBox"]
editBox:SetText(dlg.inputText or "")
editBox:SetFocus()
editBox:HighlightText(0, -1)
if not dlg.callback then
local button = _G[self:GetName() .. "Button2"]
button:ClearAllPoints()
button:SetWidth(200)
button:SetPoint("CENTER", editBox, "CENTER", 0, -30)
end
end
StaticPopupDialogs[dialogId] = dlg
end
dlg.text = title
dlg.inputText = text
if callback then
dlg.callback = callback
dlg.button1 = OKAY
dlg.button2 = CANCEL
else
dlg.callback = nil
dlg.button1 = nil
dlg.button2 = CLOSE
end
StaticPopup_Show(dialogId)
end
local classLookup = {
[1] = 1, -- Warrior
[2] = 2, -- Paladin
[3] = 4, -- Hunter
[4] = 8, -- Rogue
[5] = 16, -- Priest
[6] = 32, -- Death Knight
[7] = 64, -- Shaman
[8] = 128, -- Mage
[9] = 256, -- Warlock
[11] = 1024 -- Druid
}
---@type table<integer, integer>
local reverseClassLookup = {}
for k, v in pairs(classLookup) do
reverseClassLookup[v] = k
end
local raceLookup = {
[1] = 0, -- Human
[3] = 1, -- Dwarf
[4] = 2, -- NightElf
[7] = 3, -- Gnome
[11] = 4 -- Draenei
}
function VGT:Merge(table1, table2)
local newTable = {}
for key, value in pairs(table1) do
newTable[key] = value
end
for key, value in pairs(table2) do
newTable[key] = value
end
return newTable
end
function VGT:GetMinMaxLevelParenthesis(minLevel, maxLevel)
if minLevel == maxLevel then
return string.format("(%d)", maxLevel)
end
return string.format("(%d - %d)", minLevel, maxLevel)
end
local _, _, _, tocVersion = GetBuildInfo()
function VGT:IsSeason()
if tocVersion > 11500 and tocVersion < 19999 then
return true
end
return false
end
function VGT:ColorizeByLevel(name, playerLevel, minLevel, maxLevel)
local avgLevel = (minLevel + maxLevel) / 2
local levelDiff = playerLevel - avgLevel
if levelDiff >= 5 then
return "|cffC0C0C0"..name.."|r" -- Grey
elseif levelDiff >= 3 then
return "|cff42BF3C"..name.."|r" -- Green
elseif levelDiff >= -2 then
return "|cffFFFF00"..name.."|r" -- Yellow
elseif levelDiff >= -5 then
return "|cffFF803E"..name.."|r" -- Orange
else
return "|cffFF1819"..name.."|r" -- Red
end
end
function VGT:ColorizeCharacterName(name, class)
local _, _, _, color = GetClassColor(class)
if not color then
return name
else
return "|c" .. color .. name .. "|r"
end
end
---@param character JsonCharacter
---@return string
function VGT:ColorizeCharacter(character)
local _, _, _, color = GetClassColor(select(2, self:CharacterClassInfo(character)))
if not color then
return character.Name
else
return "|c" .. color .. character.Name .. "|r"
end
end
---@param character JsonCharacter
---@return string? className, string? classFile, number? classId
function VGT:CharacterClassInfo(character)
return GetClassInfo(reverseClassLookup[character.Class])
end
---@return JsonCharacter[]
function VGT:GetCharacters()
---@param characters JsonCharacter[]
---@param id string
local function insertCharacter(characters, id)
local name, _ = UnitName(id)
if (name) then
local _, _, raceId = UnitRace(id)
local _, _, classId = UnitClass(id)
if (raceId and classId) then
---@class JsonCharacter
local character = {
Name = name,
Class = classLookup[classId],
Race = raceLookup[raceId]
}
table.insert(characters, character)
end
end
end
---@type JsonCharacter[]
local characters = {}
if UnitInRaid("player") then
for i = 1, 40 do
insertCharacter(characters, "raid" .. i)
end
else
insertCharacter(characters, "player")
if UnitInParty("player") then
for i = 1, 4 do
insertCharacter(characters, "party" .. i)
end
end
end
return characters
end
function VGT:ExportRaidStart()
return json.encode({
Characters = self:GetCharacters()
})
end
function VGT:ShowRaidStartExport()
self:ShowInputDialog("Export Raid Start", VGT:ExportRaidStart())
end
function VGT:ExportKill(drops, characters, timestamp)
return json.encode({
Drops = drops,
Characters = characters or self:GetCharacters(),
Timestamp = timestamp
})
end
function VGT:ShowKillExport(drops, characters, timestamp)
self:ShowInputDialog("Export Kill", self:ExportKill(drops, characters, timestamp))
end
function VGT:TableContains(table, value)
for key, val in pairs(table) do
if val == value then
return true
end
end
return false
end