Repository navigation
Expand file tree
/
Copy pathMythicPlusPull.lua
More file actions
675 lines (580 loc) · 21.2 KB
/
Copy pathMythicPlusPull.lua
File metadata and controls
675 lines (580 loc) · 21.2 KB
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
local name, ns = ...
ns.data = {}
local DIFFICULTY_MYTHIC_PLUS = 8
--- @class MythicPlusPull: AceAddon, AceConsole-3.0, NumyAceEvent-3.0
local MPP = LibStub('AceAddon-3.0'):NewAddon(name, 'AceConsole-3.0', 'NumyAceEvent-3.0');
local L = ns.L
local LibGetFrame = LibStub('LibGetFrame-1.0');
--@debug@
_G.MythicPlusPull = MPP;
if not _G.MPP then _G.MPP = MPP; end
--@end-debug@
local nameplateAccessor = function(unit)
return LibGetFrame.GetUnitNameplate(unit);
end
do
local defaultAccessor = nameplateAccessor
local nameplateAddons = {
{
addonName = 'TidyPlates',
nameplateAccessor = function(unit)
local plate = defaultAccessor(unit);
return plate and plate.extended or plate;
end,
},
{
addonName = 'Platynator',
nameplateAccessor = function(unit)
local plate = C_NamePlate.GetNamePlateForUnit(unit);
if not plate then return; end
for _, child in ipairs({plate:GetChildren()}) do
if child:GetFrameStrata() == 'MEDIUM' and child:GetSourceLocation():match('Platynator') then
return child;
end
end
return plate;
end,
},
};
for _, info in ipairs(nameplateAddons) do
if C_AddOns.IsAddOnLoaded(info.addonName) then
nameplateAccessor = info.nameplateAccessor;
break;
end
end
end
ns.addon = MPP
MPP.ns = ns
MPP.exampleDisplayActive = false
MPP.loaded = false
MPP.previousQuantity = 0
--- @type table<string, FontString>
MPP.activeNameplates = {}
MPP.warnings = {}
local function GetAbsoluteFramePosition(frame)
return {
["anchorPoint"] = "TOPLEFT",
["relativeFrame"] = "UIParent",
["relativePoint"] = "BOTTOMLEFT",
["offX"] = frame:GetLeft(),
["offY"] = frame:GetTop(),
}
end
local function SetFramePoint(frame, pointInfo)
frame:ClearAllPoints()
frame:SetPoint(
pointInfo.anchorPoint,
pointInfo.relativeFrame,
pointInfo.relativePoint,
pointInfo.offX,
pointInfo.offY
);
end
function MPP:OnInitialize()
MMPEDB = MMPEDB or {}
self.DB = MMPEDB
local function init()
return UIParent:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
end
local function reset(_, obj)
if not obj then return end
obj:ClearAllPoints()
obj:SetText("")
obj:Hide()
end
self.fontStringPool = CreateObjectPool(init, reset) --[[@as ObjectPool<FontString>]]
local function deferToggle()
RunNextFrame(function() self:ToggleFunctionality() end)
end
self:RegisterEvent("SCENARIO_UPDATE", deferToggle)
self:RegisterEvent("PLAYER_ENTERING_WORLD", deferToggle)
deferToggle()
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, function(tooltip) self:OnUnitTooltip(tooltip) end)
self:VerifyDB()
self:CreatePullFrame()
self:InitConfig()
local openConfig = function() self:OpenConfig() end
self:RegisterChatCommand('mythicplusprogress', openConfig);
self:RegisterChatCommand('mythicpluspull', openConfig);
self:RegisterChatCommand('mypp', openConfig);
self:RegisterChatCommand('mpp', openConfig);
self:RegisterChatCommand('mppre', openConfig);
self:RegisterChatCommand('mppe', openConfig);
self.loaded = true
if NumyFunctionProfiler then
NumyFunctionProfiler:WrapModules('MythicPlusPull', 'Core', self);
end
end
function MPP:ToggleFunctionality()
local isActive = self:IsActiveScenario()
if isActive == self.wasActive then
return
end
if isActive then
for _, plate in ipairs(C_NamePlate.GetNamePlates()) do
local unit = plate.UnitFrame and plate.UnitFrame.unit
self:OnAddNameplate(unit)
end
self:RegisterEvent("NAME_PLATE_UNIT_ADDED", function(_, unit)
self:OnAddNameplate(unit)
self:CalculatePull()
end)
self:RegisterEvent("NAME_PLATE_UNIT_REMOVED", function(_, unit)
self:RemoveNameplateText(unit)
self:CalculatePull()
end)
self:RegisterEvent("SCENARIO_CRITERIA_UPDATE", function()
self:CalculatePull()
end)
self:RegisterEvent("UNIT_THREAT_LIST_UPDATE", function()
self:CalculatePull()
end)
self:CalculatePull()
else
self.currentPullFrame:Hide()
self:UnregisterEvent("NAME_PLATE_UNIT_ADDED")
self:UnregisterEvent("NAME_PLATE_UNIT_REMOVED")
self:UnregisterEvent("SCENARIO_CRITERIA_UPDATE")
self:UnregisterEvent("UNIT_THREAT_LIST_UPDATE")
end
self:FullUpdate()
self.wasActive = isActive
end
function MPP:FullUpdate()
local isActive = self:IsActiveScenario()
if isActive then
for _, plate in ipairs(C_NamePlate.GetNamePlates()) do
local unit = plate.UnitFrame and plate.UnitFrame.unit
self:OnAddNameplate(unit)
end
end
self:CalculatePull()
self:UpdateNameplateValues()
self:UpdateNameplates()
end
function MPP:HasWarned(message)
for _, warning in pairs(self.warnings) do
if warning == message then
return true
end
end
return false
end
function MPP:PrintWarning(message)
if not self:HasWarned(message) then
self:Print(message)
table.insert(self.warnings, message)
return true
end
return false
end
function MPP:VerifySettings(overwriteWithDefault)
for setting, value in pairs(self.defaultSettings) do
if self.DB.settings[setting] == nil or overwriteWithDefault then
self.DB.settings[setting] = value
end
end
if string.len(self.DB.settings["nameplateTextColor"]) == 6 then
-- alpha got added in a later version
self.DB.settings["nameplateTextColor"] = "FF" .. self.DB.settings["nameplateTextColor"]
end
self.DB.settings["offsetx"] = tonumber(self.DB.settings["offsetx"])
self.DB.settings["offsety"] = tonumber(self.DB.settings["offsety"])
end
function MPP:VerifyDB()
if not self.DB or not self.DB.settings then
self:Print(L["Running first time setup. This should only happen once. Enjoy! ;)"])
wipe(MMPEDB)
self.DB = MMPEDB
self.DB.settings = {}
end
self:VerifySettings()
end
function MPP:IsValidTarget(unit)
return UnitCanAttack("player", unit)
end
--- @return number numberOfSteps
function MPP:GetNumberOfScenarioSteps()
return select(3, C_Scenario.GetStepInfo()) or 0
end
function MPP:IsDungeonFinished()
if self.exampleDisplayActive then return false end
return self:GetNumberOfScenarioSteps() < 1
end
function MPP:IsActiveScenario()
if self.exampleDisplayActive then return true end
local difficulty = select(3, GetInstanceInfo()) or -1
return difficulty == DIFFICULTY_MYTHIC_PLUS and not self:IsDungeonFinished()
end
--- @return ScenarioCriteriaInfo? criteriaInfo
function MPP:GetProgressCriteriaInfo()
if not self:IsActiveScenario() then return nil end
local numSteps = self:GetNumberOfScenarioSteps()
for index = numSteps, 1, -1 do
local info = C_ScenarioInfo.GetCriteriaInfo(index)
if info.isWeightedProgress then
return info
end
end
end
function MPP:GetTotalCountRequired()
if self.exampleDisplayActive then return 120 end
local info = self:GetProgressCriteriaInfo()
if info then
return info.totalQuantity
end
return 0
end
function MPP:GetCurrentCount()
if self.exampleDisplayActive then return 42 end
local info = self:GetProgressCriteriaInfo()
if info and info.quantityString then
return tonumber((info.quantityString:match('%d+')))
end
return 0
end
--- @param unit UnitToken
--- @return number? count
--- @return number? countPercent
--- @return string? countPercentString
function MPP:GetUnitCount(unit)
if self.exampleDisplayActive then return 10, 0.0833, '8.33' end
return C_ScenarioInfo.GetUnitCriteriaProgressValues(unit)
end
---
--- TOOLTIPS
---
--- @param unit UnitToken
function MPP:ShouldAddToTooltip(unit)
return self.loaded and self:GetSetting("enableTooltip") and self:IsActiveScenario() and self:IsValidTarget(unit)
end
--- @param unit UnitToken
function MPP:GetTooltipMessage(unit)
local message = "|cFF82E0FF" .. L["M+Progress:"] .. " "
local count, _, countPercentString = self:GetUnitCount(unit)
if not countPercentString then
return message .. L["No Progress."]
end
local requiredCount = self:GetTotalCountRequired()
if self:GetSetting('includeCountInTooltip') then
message = string.format("%s%s%% %i/%i", message, countPercentString, count, requiredCount)
else
message = string.format("%s%s%%", message, countPercentString)
end
return message
end
--- @param tooltip GameTooltip
function MPP:OnUnitTooltip(tooltip)
local unit = "mouseover"
if not unit or not self:ShouldAddToTooltip(unit) then return end
local tooltipMessage = self:GetTooltipMessage(unit)
if tooltipMessage then
tooltip:AddLine(tooltipMessage)
tooltip:Show()
end
end
---
--- SHITTY CURRENT PULL FRAME
---
function MPP:CreatePullFrame()
self.currentPullFrame = CreateFrame("frame", nil, UIParent)
SetFramePoint(self.currentPullFrame, self.DB.settings.pullFramePoint)
self.currentPullFrame:SetMovable(true)
self.currentPullFrame:SetWidth(100)
self.currentPullFrame:SetHeight(30)
self.currentPullFrame:SetScale(self:GetSetting("pullFrameTextScale"))
local dragFrame = CreateFrame("Frame", nil, self.currentPullFrame)
self.currentPullFrame.DragFrame = dragFrame
dragFrame:EnableMouse(not self:GetSetting("lockPullFrame"))
dragFrame:RegisterForDrag("LeftButton")
dragFrame:SetScript("OnDragStart", function()
if self.DB.settings.lockPullFrame then return end
self.currentPullFrame:StartMoving()
end)
dragFrame:SetScript("OnDragStop", function()
self.currentPullFrame:StopMovingOrSizing()
self.DB.settings.pullFramePoint = GetAbsoluteFramePosition(self.currentPullFrame)
end)
self.currentPullString = self.currentPullFrame:CreateFontString(nil, "BACKGROUND", "GameFontHighlightLarge")
self.currentPullString:SetPoint("CENTER")
self.currentPullString:SetText("")
dragFrame:SetAllPoints(self.currentPullString)
end
---
--- NAMEPLATES
---
--- @return number currentPullCount # @todo: might be nil?
--- @return number currentPullPercent
--- @return string currentPullPercentString
--- @return number estimatedCount # @todo: might be nil?
--- @return number estimatedPercent
--- @return string estimatedPercentString
function MPP:GetCurrentPullCount()
return 0, 0, '?', 0, 0, '?'
--return C_Pony.GetCurrentPullCount() -- @todo: update once new API is documented
end
--- @return boolean shouldShow
--- @return boolean hideIfNoCount
function MPP:ShouldShowCurrentPullEstimate()
if self.exampleDisplayActive then return true, false end
-- if self:GetSetting("hidePullEstimateFrameWhenApiUnavailable") then
-- return false, false
-- end
if self:GetSetting("enablePullEstimate") and self:IsActiveScenario() then
return true, self:GetSetting("pullEstimateCombatOnly")
end
return false, false
end
function MPP:SetCurrentPullEstimateLabel(s)
self.currentPullString:SetText(s)
end
--- Secret safe placeholder replacer
--- @param formatString string
--- @param replacements table<string, string> # values are allowed to contain secrets
--- @return string # likely a secret string
function MPP:ReplacePlaceholders(formatString, replacements)
local placeholderOrder = {}
for x in string.gmatch(formatString, "%$[^%$]+%$") do
table.insert(placeholderOrder, x)
end
formatString = string.gsub(formatString, "%%", "%%%%")
formatString = string.gsub(formatString, "%$[^%$]+%$", "%%s")
local replacementValues = {}
for _, placeholder in ipairs(placeholderOrder) do
table.insert(replacementValues, replacements[placeholder] or placeholder)
end
return formatString:format(unpack(replacementValues))
end
function MPP:UpdateCurrentPullEstimate(pullCount, estimatedCount, pullPercentString, estimatedPercentString)
if self.exampleDisplayActive then
pullCount = 10
estimatedCount = pullCount + self:GetCurrentCount()
local requiredCount = self:GetTotalCountRequired()
pullPercentString = string.format("%.2f", (pullCount / requiredCount) * 100)
estimatedPercentString = string.format("%.2f", (estimatedCount / requiredCount) * 100)
end
local requiredCount = self:GetTotalCountRequired()
local shouldShow, hideIfNoCount = self:ShouldShowCurrentPullEstimate()
if not shouldShow or (hideIfNoCount and not pullCount) or requiredCount == 0 then
self.currentPullFrame:Hide()
return
end
self.currentPullFrame:Show()
local currentCount = self:GetCurrentCount()
local formatString = self:GetSetting('pullFrameTextFormat'); --[[@as string]]
local percentString = '%.2f%%';
local placeholderReplacements = {
['$current$'] = currentCount,
['$pull$'] = pullCount or 0,
['$estimated$'] = estimatedCount or 0,
['$required$'] = requiredCount,
['$current%$'] = percentString:format((currentCount / requiredCount) * 100),
['$pull%$'] = (pullPercentString or '?') .. '%',
['$estimated%$'] = (estimatedPercentString or '?') .. '%',
['$required%$'] = percentString:format(100),
};
local message = self:ReplacePlaceholders(formatString, placeholderReplacements)
self:SetCurrentPullEstimateLabel(message)
end
function MPP:CreateNameplateText(unit)
if self.activeNameplates[unit] then -- This should never happen
self:RemoveNameplateText(unit)
end
--- @type Frame?
local nameplate = nameplateAccessor(unit)
if nameplate then
if not nameplate:IsVisible() then
nameplate = C_NamePlate.GetNamePlateForUnit(unit)
end
self.activeNameplates[unit] = self.fontStringPool:Acquire()
self.activeNameplates[unit]:SetJustifyH("LEFT")
self.activeNameplates[unit]:SetParent(nameplate)
self.activeNameplates[unit]:SetText("+?%")
self.activeNameplates[unit]:SetScale(self:GetSetting('nameplateTextScale'))
end
end
function MPP:RemoveNameplateText(unit)
if self.activeNameplates[unit] ~= nil then
self.fontStringPool:Release(self.activeNameplates[unit])
self.activeNameplates[unit] = nil
end
end
function MPP:UpdateNameplateValue(unit)
local count, _, countPercentString = self:GetUnitCount(unit)
if count then
local message = "|c" .. self:GetSetting("nameplateTextColor")
local placeholderReplacements = {
['$percent$'] = countPercentString,
['$count$'] = count,
};
local formatString = self:GetSetting("nameplateTextFormat") --[[@as string]]
message = message .. self:ReplacePlaceholders(formatString, placeholderReplacements)
self.activeNameplates[unit]:SetText(message)
self.activeNameplates[unit]:Show()
return true
end
if self.activeNameplates[unit] then -- If mob dies, a new nameplate is created but not shown, and this ui widget will then not exist.
self.activeNameplates[unit]:SetText("")
self.activeNameplates[unit]:Hide()
end
return false
end
function MPP:UpdateNameplateValues()
for unit, _ in pairs(self.activeNameplates) do
self:UpdateNameplateValue(unit)
end
end
function MPP:UpdateNameplatePosition(unit)
local nameplate = C_NamePlate.GetNamePlateForUnit(unit)
if nameplate and nameplate.UnitFrame and nameplate.UnitFrame.unitExists and self.activeNameplates[unit] ~= nil then
local offsetx = self:GetSetting('offsetx')
local offsety = self:GetSetting('offsety')
self.activeNameplates[unit]:SetPoint("LEFT", self.activeNameplates[unit]:GetParent(), "RIGHT", offsetx, offsety)
else
self:RemoveNameplateText(unit)
end
end
function MPP:ShouldShowNameplateTexts()
return self:GetSetting("enableNameplateText") and self:IsActiveScenario()
end
function MPP:OnAddNameplate(unit)
if self:ShouldShowNameplateTexts() then
RunNextFrame(function() -- allow nameplate addons to create their frames first
self:CreateNameplateText(unit)
self:UpdateNameplateValue(unit)
self:UpdateNameplatePosition(unit)
end)
end
end
function MPP:UpdateNameplates()
local shouldShow = self:ShouldShowNameplateTexts()
for unit, _ in pairs(CopyTable(self.activeNameplates)) do
if shouldShow then
self:UpdateNameplatePosition(unit)
else
self:RemoveNameplateText(unit)
end
end
end
function MPP:IsUnitPulled(unit)
local threat = UnitThreatSituation("player", unit) or -1 -- Is nil if we're not on their aggro table, so make it -1 instead.
return self:IsValidTarget(unit) and (threat >= 0 or UnitPlayerControlled(unit .. "target"))
end
--- @return UnitTokenNamePlate[] pulledUnits
function MPP:GetPulledUnits()
local pulledUnits = {}
for _, nameplate in pairs(C_NamePlate.GetNamePlates()) do
if nameplate.UnitFrame.unitExists then
if self:IsUnitPulled(nameplate.UnitFrame.displayedUnit) then
tinsert(pulledUnits, nameplate.UnitFrame.displayedUnit)
end
end
end
return pulledUnits
end
--- @class MPP_statusBarPool
MPP.statusBarPool = {
index = 0,
--- @type table<number, StatusBar>
pool = {},
--- @param self MPP_statusBarPool
Acquire = function(self)
self.index = self.index + 1
if self.pool[self.index] then
return self.pool[self.index]
end
local bar = CreateFrame("StatusBar")
bar:SetAlpha(0)
self.pool[self.index] = bar
return bar
end,
--- @param self MPP_statusBarPool
ReleaseAll = function(self)
for i = 1, self.index do
if self.pool[i] then
local bar = self.pool[i]
bar:Hide()
bar:ClearAllPoints()
bar:SetValue(0)
end
end
self.index = 0
end,
}
MPP.percentCalculators = {}
function MPP:GetPercentCalculator(totalCount)
if totalCount == 0 then return end
if not self.percentCalculators[totalCount] then
local abbrevConfig = {
config = CreateAbbreviateConfig({
{
breakpoint = 0.00001,
abbreviation = "",
significandDivisor = totalCount / 10000,
fractionDivisor = 100,
abbreviationIsGlobal = false,
},
}),
}
self.percentCalculators[totalCount] = abbrevConfig
end
return self.percentCalculators[totalCount]
end
--- @return string? pullCount
--- @return string? estimatedCount
--- @return string? pullPercent
--- @return string? estimatedPercent
function MPP:CalculatePull()
local pulledUnits = self:GetPulledUnits()
local totalCount = self:GetTotalCountRequired()
if not next(pulledUnits) or totalCount == 0 then
self:UpdateCurrentPullEstimate(nil, nil, nil, nil)
return
end
local currentCount = self:GetCurrentCount()
self.statusBarPool:ReleaseAll()
local mainBar = self.statusBarPool:Acquire()
mainBar:SetSize(totalCount, 10)
mainBar:SetPoint("LEFT")
mainBar:SetStatusBarTexture("Interface/TargetingFrame/UI-StatusBar")
mainBar:SetMinMaxValues(0, totalCount)
mainBar:SetValue(0)
mainBar:Show()
local prevBar = mainBar:GetStatusBarTexture()
for _, unit in pairs(pulledUnits) do
local count = self:GetUnitCount(unit)
if count then
local bar = self.statusBarPool:Acquire()
bar:SetSize(totalCount, 10)
bar:SetPoint("LEFT", prevBar, "RIGHT", 0, 0)
bar:SetStatusBarTexture("Interface/TargetingFrame/UI-StatusBar")
bar:SetMinMaxValues(0, totalCount)
bar:SetValue(count)
bar:Show()
prevBar = bar:GetStatusBarTexture()
end
end
local currentCountBar = self.statusBarPool:Acquire()
currentCountBar:SetSize(totalCount, 10)
currentCountBar:SetPoint("LEFT", prevBar, "RIGHT", 0, 0)
currentCountBar:SetStatusBarTexture("Interface/TargetingFrame/UI-StatusBar")
currentCountBar:SetMinMaxValues(0, totalCount)
currentCountBar:SetValue(currentCount)
currentCountBar:Show()
RunNextFrame(function()
RunNextFrame(function()
if #pulledUnits ~= #self:GetPulledUnits() then return end
local pullCount = prevBar:GetRight()
local estimatedTotal = currentCountBar:GetStatusBarTexture():GetRight()
local percentCalculator = self:GetPercentCalculator(totalCount)
local pullPercent = AbbreviateNumbers(pullCount, percentCalculator)
local estimatedPercent = AbbreviateNumbers(estimatedTotal, percentCalculator)
self:UpdateCurrentPullEstimate(
C_StringUtil.RoundToNearestString(pullCount),
C_StringUtil.RoundToNearestString(estimatedTotal),
pullPercent,
estimatedPercent
)
end)
end)
end