forked from nekoh/Numeration
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCore.lua
667 lines (592 loc) · 16.6 KB
/
Core.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
local addonname, addon = ...
Numeration = addon
local L = addon.locale
local C = addon.core
local boss = LibStub("LibBossIDs")
addon.events = CreateFrame("Frame")
addon.events:SetScript("OnEvent", function(self, event, ...)
addon[event](addon, event, ...)
end)
addon.views = {}
-- important GUIDs
addon.guidToClass = {}
addon.guidToName = {}
-- Workaround EasyMenu
local function EasyMenu_Initialize(frame, level, menuList)
for index = 1, #menuList do
local value = menuList[index]
if value.text then
value.index = index
UIDropDownMenu_AddButton(value, level)
end
end
end
function EasyMenu(menuList, menuFrame, anchor, x, y, displayMode, autoHideDelay)
if displayMode == "MENU" then
menuFrame.displayMode = displayMode
end
UIDropDownMenu_Initialize(menuFrame, EasyMenu_Initialize, displayMode, nil, menuList)
ToggleDropDownMenu(1, nil, menuFrame, anchor, x, y, menuList, nil, autoHideDelay)
end
-- Keybindings
BINDING_NAME_NUMERATION_VISIBILITY = L.binding_visibility
BINDING_NAME_NUMERATION_RESET = L.binding_reset
-- used colors
addon.color = {
PET = {0.09, 0.61, 0.55},
}
addon.colorhex = {}
do
local colors = CUSTOM_CLASS_COLORS or RAID_CLASS_COLORS
for class, c in pairs(colors) do
addon.color[class] = {c.r, c.g, c.b}
addon.colorhex[class] = string.format("%02X%02X%02X", c.r * 255, c.g * 255, c.b * 255)
end
addon.colorhex["PET"] = string.format("%02X%02X%02X", addon.color.PET[1] * 255, addon.color.PET[2] * 255, addon.color.PET[3] * 255)
end
addon.spellIcon = setmetatable({[75] = "", [88163] = ""}, {__index = function(tbl, i)
local spellInfo = C_Spell.GetSpellInfo(i)
if spellInfo then
addon.spellName[i] = spellInfo.name
tbl[i] = spellInfo.iconID
return spellInfo.iconID
end
end})
addon.spellName = setmetatable({}, {__index = function(tbl, i)
local spellInfo = C_Spell.GetSpellInfo(i)
if spellInfo then
addon.spellIcon[i] = spellInfo.iconID
tbl[i] = spellInfo.name
return spellInfo.name or UNKNOWN
end
end})
local newSet = function()
return {
unit = {},
}
end
local icon = LibStub("LibDBIcon-1.0")
local ldb = LibStub("LibDataBroker-1.1"):NewDataObject("Numeration", {
type = "data source",
text = "Numeration",
icon = [[Interface\Icons\Ability_Warrior_WeaponMastery]],
})
local current
addon.events:RegisterEvent("ADDON_LOADED")
function addon:ADDON_LOADED(event, addon)
if addon ~= addonname then return end
self.events:UnregisterEvent("ADDON_LOADED")
self:InitOptions()
if Numeration.windows.title_hide then
NumerationCharOptions.minimap.hide = false
end
icon:Register("Numeration", ldb, NumerationCharOptions.minimap)
self.window:OnInitialize()
if NumerationCharOptions.forcehide or NumerationCharOptions.combatshow then
self.window:Hide()
end
if not NumerationCharDB then
self:Reset()
end
current = self:GetSet(1) or newSet()
self.collect:RemoveUnneededEvents()
self.events:RegisterEvent("ZONE_CHANGED_NEW_AREA")
if GetRealZoneText() ~= "" then
self:ZONE_CHANGED_NEW_AREA(event)
end
end
local function abrNumber(self, num)
if num >= 1e6 then
return ("%.1fm"):format(num / 1e6)
elseif num >= 1e3 then
return ("%.1fk"):format(num / 1e3)
else
return ("%i"):format(num)
end
end
local function fullNumber(self, num)
return ("%i"):format(num)
end
function addon:InitOptions()
self.ids = {}
do
for i, tbl in ipairs(self.types) do
self.ids[tbl.id] = true
if tbl.id2 then
self.ids[tbl.id2] = true
end
end
end
if not NumerationCharOptions then
NumerationCharOptions = {}
end
if NumerationCharOptions.keeponlybosses == nil then
NumerationCharOptions.keeponlybosses = false
end
if NumerationCharOptions.petsmerged == nil then
NumerationCharOptions.petsmerged = true
end
if NumerationCharOptions.onlyinstance == nil then
NumerationCharOptions.onlyinstance = false
end
if NumerationCharOptions.combathide == nil then
NumerationCharOptions.combathide = false
end
if NumerationCharOptions.combatshow == nil then
NumerationCharOptions.combatshow = false
end
if NumerationCharOptions.combatnone == nil then
NumerationCharOptions.combatnone = true
end
if not NumerationCharOptions.minimap then
NumerationCharOptions.minimap = {
hide = false,
}
end
if not NumerationCharOptions.nav then
NumerationCharOptions.nav = {
view = "Units",
set = "current",
type = 1,
}
end
self.nav = NumerationCharOptions.nav
self.ModNumber = C.shortnumbers and abrNumber or fullNumber
end
function ldb:OnTooltipShow()
icon.tooltip:AddLine("Numeration", 1, 1, 1)
icon.tooltip:AddLine(L.toggle)
icon.tooltip:AddLine(L.reset)
if addon.windows.title_hide then
icon.tooltip:AddLine(L.menu)
end
end
function ldb:OnClick(button)
if button == "LeftButton" then
if IsShiftKeyDown() then
if C.silent_reset then
addon:Reset()
else
StaticPopup_Show("RESET_DATA")
end
else
addon:ToggleVisibility()
end
elseif addon.windows.title_hide then
addon:DropdownMenu()
end
end
function addon:ToggleVisibility()
NumerationCharOptions.forcehide = not NumerationCharOptions.forcehide
if NumerationCharOptions.forcehide then
self.window:Hide()
else
self.window:Show()
self:RefreshDisplay()
end
end
function addon:MinimapIconShow(show)
if addon.windows.title_hide then
NumerationCharOptions.minimap.hide = false
return
end
NumerationCharOptions.minimap.hide = not show
if show then
icon:Show("Numeration")
else
icon:Hide("Numeration")
end
end
function addon:CombatShow(option)
NumerationCharOptions.combathide = false
NumerationCharOptions.combatshow = false
NumerationCharOptions.combatnone = false
NumerationCharOptions[option] = true
end
function addon:SetOption(option, value)
NumerationCharOptions[option] = value
if option == "onlyinstance" then
self:ZONE_CHANGED_NEW_AREA(true)
elseif option == "petsmerged" then
self:RefreshDisplay(true)
end
end
function addon:GetOption(option)
return NumerationCharOptions[option]
end
function addon:Reset()
local lastZone = NumerationCharDB and NumerationCharDB.zone
NumerationCharDB = {
[0] = newSet(),
zone = lastZone,
}
NumerationCharDB[0].name = L.overall
current = newSet()
if self.nav.set and self.nav.set ~= "total" and self.nav.set ~= "current" then
self.nav.set = "current"
end
self:RefreshDisplay()
collectgarbage("collect")
end
local updateTimer = CreateFrame("Frame")
updateTimer:Hide()
updateTimer:SetScript("OnUpdate", function(self, elapsed)
self.timer = self.timer - elapsed
if self.timer > 0 then return end
self.timer = C.refreshinterval
if current.changed then
ldb.text = addon.views["Units"]:GetXps(current, UnitName("player"), "dd", NumerationCharOptions.petsmerged)
end
local set = addon.nav.set and addon:GetSet(addon.nav.set) or current
if not set or not set.changed then return end
set.changed = nil
addon:RefreshDisplay(true)
end)
function updateTimer:Activate()
self.timer = C.refreshinterval
self:Show()
end
function updateTimer:Refresh()
self.timer = C.refreshinterval
end
function addon:RefreshDisplay(update)
if self.window:IsShown() then
self.window:Clear()
if not update then
self.views[self.nav.view]:Init()
local segment = self.nav.set == "total" and "O" or self.nav.set == "current" and "C" or self.nav.set
self.window:UpdateSegment(segment)
end
self.views[self.nav.view]:Update(NumerationCharOptions.petsmerged)
end
if not update then
ldb.text = self.views["Units"]:GetXps(current, UnitName("player"), "dd", NumerationCharOptions.petsmerged)
end
updateTimer:Refresh()
end
local useChatType, useChannel
function addon:Report(lines, chatType, channel)
useChatType, useChannel = chatType, channel
if chatType == "WHISPER" then
whispname = StaticPopup1EditBox:GetText()
if whispname == nil or whispname == "" then
print("|cffffff00Numeration|r: "..L.bad_whisp)
return
end
end
local view = self.views[self.nav.view]
if view.Report and lines then
view:Report(NumerationCharOptions.petsmerged, lines)
else
print("|cffffff00Numeration|r: "..L.bad_report)
end
end
function addon:PrintHeaderLine(set)
local datetext, timetext = self:GetDuration(set)
self:PrintLine("Numeration: %s - %s%s", self.window:GetTitle(), set.name, datetext and format(" [%s %s]", datetext, timetext) or "")
end
function addon:PrintLine(...)
SendChatMessage(format(...), useChatType, nil, useChannel)
end
function addon:Scroll(dir)
local view = self.views[self.nav.view]
if dir > 0 and view.first > 1 then
if IsShiftKeyDown() then
view.first = 1
else
view.first = view.first - 1
end
elseif dir < 0 then
if IsShiftKeyDown() then
view.first = 9999
else
view.first = view.first + 1
end
end
self:RefreshDisplay(true)
end
function addon:GetArea(start, total)
if total == 0 then return start end
local first = start
local last = start + self.window.maxlines - 1
if last > total then
first = first - last + total
last = total
end
if first < 1 then
first = 1
end
self.window:SetScrollPosition(first, total)
return first, last
end
function addon:GetSet(id)
if not id then return end
if id == "current" then
return current
elseif id == "total" then
id = 0
end
return NumerationCharDB[id]
end
function addon:GetSets()
return NumerationCharDB[0], current.active and current
end
function addon:GetDuration(set)
if not set.start or not set.now then return end
local duration = math.ceil(set.now - set.start)
local durationtext = duration < 60 and format("%i"..L.s.."", duration%60) or format("%i"..L.m.."%i"..L.s.."", math.floor(duration/60), duration%60)
return date("%H:%M", set.start), durationtext
end
function addon:GetUnitClass(playerID)
if not playerID then return end
local class = self.guidToClass[playerID]
return self.guidToName[class] and "PET" or class
end
function addon:GetUnit(set, id)
local name, class = self.guidToName[id], self.guidToClass[id]
local owner = self.guidToName[class]
if not owner then
-- unit
local u = set.unit[name]
if not u then
u = {
name = name,
class = class,
}
set.unit[name] = u
end
return u
else
-- pet
local key = format("%s:%s", owner, name or UNKNOWN)
local p = set.unit[key]
if not p then
local ownertable = self:GetUnit(set, class)
if not ownertable.pets then
ownertable.pets = {}
end
ownertable.pets[key] = true
p = {
name = name,
class = "PET",
owner = owner,
}
set.unit[key] = p
end
return p
end
end
local summonOwner, summonName = {}, {}
do
local addPlayerPet = function(unit, pet)
local unitID = UnitGUID(unit)
if not unitID then return end
local unitName, unitRealm = UnitName(unit)
local _, unitClass = UnitClass(unit)
local petID = UnitGUID(pet)
addon.guidToClass[unitID] = unitClass
addon.guidToName[unitID] = unitRealm and unitRealm ~= "" and format("%s-%s", unitName, unitRealm) or unitName
if petID then
addon.guidToClass[petID] = unitID
addon.guidToName[petID] = UnitName(pet)
end
end
function addon:UpdateGUIDS()
self.guidToName = wipe(self.guidToName)
self.guidToClass = wipe(self.guidToClass)
local num = GetNumGroupMembers()
if num > 5 then
for i = 1, num do
addPlayerPet("raid"..i, "raid"..i.."pet")
end
else
addPlayerPet("player", "pet")
if num > 0 then
for i = 1, num do
addPlayerPet("party"..i, "party"..i.."pet")
end
end
end
-- remove summons from guid list, if owner is gone
for pid, uid in pairs(summonOwner) do
if self.guidToClass[uid] then
self.guidToClass[pid] = uid
self.guidToName[pid] = summonName[pid]
else
summonOwner[pid] = nil
summonName[pid] = nil
end
end
self:GUIDsUpdated()
end
end
function addon:COMBAT_LOG_EVENT_UNFILTERED(e)
local timestamp, eventtype, _, srcGUID, _, _, _, dstGUID, dstName = CombatLogGetCurrentEventInfo()
if self.collect[eventtype] then
self.collect[eventtype](timestamp, select(4, CombatLogGetCurrentEventInfo()))
end
local ClassOrOwnerGUID = self.guidToClass[srcGUID]
if eventtype == "SPELL_SUMMON" and ClassOrOwnerGUID then
local realSrcGUID = self.guidToClass[ClassOrOwnerGUID] and ClassOrOwnerGUID or srcGUID
summonOwner[dstGUID] = realSrcGUID
summonName[dstGUID] = dstName
self.guidToClass[dstGUID] = realSrcGUID
self.guidToName[dstGUID] = dstName
elseif eventtype == "UNIT_DIED" and summonOwner[srcGUID] then
summonOwner[srcGUID] = nil
summonName[srcGUID] = nil
self.guidToClass[srcGUID] = nil
self.guidToName[srcGUID] = nil
end
end
addon.PLAYER_ENTERING_WORLD = addon.UpdateGUIDS
addon.GROUP_ROSTER_UPDATE = addon.UpdateGUIDS
addon.UNIT_PET = addon.UpdateGUIDS
addon.UNIT_NAME_UPDATE = addon.UpdateGUIDS
function addon:ZONE_CHANGED_NEW_AREA(force)
local _, instanceType, difficultyID = GetInstanceInfo()
if force == true or instanceType ~= self.instanceType then
self.instanceType = instanceType
if not NumerationCharOptions.onlyinstance or (IsInInstance() and difficultyID ~= 0) then
if IsInInstance() and difficultyID ~= 0 then
local curZone = GetRealZoneText()
if curZone ~= NumerationCharDB.zone then
NumerationCharDB.zone = curZone
if C.silent_reset then
addon:Reset()
else
StaticPopup_Show("RESET_DATA")
end
end
end
self:UpdateGUIDS()
self.events:RegisterEvent("PLAYER_ENTERING_WORLD")
self.events:RegisterEvent("GROUP_ROSTER_UPDATE")
self.events:RegisterEvent("UNIT_PET")
self.events:RegisterEvent("UNIT_NAME_UPDATE")
self.events:RegisterEvent("PLAYER_REGEN_DISABLED")
self.events:RegisterEvent("PLAYER_REGEN_ENABLED")
self.events:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self.events:RegisterEvent("ENCOUNTER_START")
updateTimer:Activate()
if not NumerationCharOptions.forcehide and not NumerationCharOptions.combatshow then
self:RefreshDisplay()
self.window:Show()
end
else
self.events:UnregisterEvent("PLAYER_ENTERING_WORLD")
self.events:UnregisterEvent("GROUP_ROSTER_UPDATE")
self.events:UnregisterEvent("UNIT_PET")
self.events:UnregisterEvent("UNIT_NAME_UPDATE")
self.events:UnregisterEvent("PLAYER_REGEN_DISABLED")
self.events:UnregisterEvent("PLAYER_REGEN_ENABLED")
self.events:UnregisterEvent("COMBAT_LOG_EVENT_UNFILTERED")
self.events:UnregisterEvent("ENCOUNTER_START")
updateTimer:Hide()
if instanceType == "none" then
if not NumerationCharOptions.forcehide then
self:RefreshDisplay()
self.window:Show()
end
else
self.window:Hide()
end
end
end
end
local IsGroupInCombat = function()
if GetNumGroupMembers() > 0 then
local unit = IsInRaid() and "raid" or "party"
for i = 1, GetNumGroupMembers(), 1 do
if UnitExists(unit..i) and UnitAffectingCombat(unit..i) then
return true
end
end
end
return false
end
local inCombat = nil
local combatTimer = CreateFrame("Frame")
combatTimer:Hide()
combatTimer:SetScript("OnUpdate", function(self, elapsed)
self.timer = self.timer - elapsed
if self.timer > 0 then return end
if IsGroupInCombat() then self.timer = C.combatseconds return end
addon:LeaveCombatEvent()
self:Hide()
end)
function combatTimer:Activate()
self.timer = C.combatseconds
self:Show()
end
function addon:PLAYER_REGEN_DISABLED()
inCombat = true
combatTimer:Hide()
if NumerationCharOptions.combathide then
self.window:Hide()
end
if not NumerationCharOptions.forcehide then
if NumerationCharOptions.combatshow then
self.window:Show()
self:RefreshDisplay()
end
end
end
function addon:PLAYER_REGEN_ENABLED()
inCombat = nil
combatTimer:Activate()
if not NumerationCharOptions.forcehide then
if NumerationCharOptions.combathide then
self.window:Show()
self:RefreshDisplay()
end
end
if NumerationCharOptions.combatshow then
self.window:Hide()
end
end
function addon:ENCOUNTER_START(_, _, encounterName)
addon.encounterName = encounterName
end
function addon:EnterCombatEvent(timestamp, guid, name)
if not current.active then
current = newSet()
current.start = timestamp
addon.start = timestamp
current.active = true
end
current.now = timestamp
addon.now = timestamp
if not current.boss then
local mobid = boss.BossIDs[tonumber(({('-'):split(guid)})[6])]
if not name then name = UNKNOWN end
if mobid then
current.name = mobid == true and name or mobid
current.boss = true
addon.encounterName = nil
elseif addon.encounterName then
current.name = addon.encounterName
current.boss = true
addon.encounterName = nil
elseif not current.name then
current.name = name
end
end
if not inCombat then
combatTimer:Activate()
end
end
function addon:LeaveCombatEvent()
if current.active then
current.active = nil
if ((current.now - current.start) < C.minfightlength) or (NumerationCharOptions.keeponlybosses and not current.boss) then
return
end
tinsert(NumerationCharDB, 1, current)
if type(self.nav.set) == "number" then
self.nav.set = self.nav.set + 1
end
-- Refresh View
self:RefreshDisplay(true)
end
end