Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
457 lines (415 loc) · 18 KB
/
Copy pathCore.lua
File metadata and controls
457 lines (415 loc) · 18 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
-- Keystone Percentile: "Top X%" for your Mythic+ rating and DPS.
-- Shows overall, role, class and spec percentiles from Raider.IO data.
local ADDON, ns = ...
local L = ns.L
local REGION_KEYS = { [1] = "us", [2] = "kr", [3] = "eu", [4] = "tw", [5] = "cn" }
local ROLE_KEYS = { DAMAGER = "dps", HEALER = "healer", TANK = "tank" }
local ROLE_NAMES = { dps = L["DPS"], healer = L["Healer"], tank = L["Tank"] }
local STALE_SECONDS = 7 * 24 * 3600
---------------------------------------------------------------------------
-- Helpers
---------------------------------------------------------------------------
local function Key(name, realm)
local n = (name or ""):gsub("%s", "")
local r = (realm or ""):gsub("[%s'%-%(%)]", "")
return (n .. "-" .. r):lower()
end
local function IsSecret(v)
return issecretvalue and issecretvalue(v) or false
end
local function GetRegion()
if KP_DB and KP_DB.region then return KP_DB.region end
return REGION_KEYS[GetCurrentRegion and GetCurrentRegion() or 0]
end
local function GetScore(unit)
local ok, summary = pcall(C_PlayerInfo.GetPlayerMythicPlusRatingSummary, unit)
if not ok or not summary then return nil end
local score = summary.currentSeasonScore
if score == nil or IsSecret(score) then return nil end
return math.floor(score + 0.5)
end
local function ScoreColor(score)
if C_ChallengeMode and C_ChallengeMode.GetDungeonScoreRarityColor then
local c = C_ChallengeMode.GetDungeonScoreRarityColor(score)
if c then return c:GenerateHexColor() end
end
return "ffffffff"
end
-- Colour the percentage itself: gold for top 1%, purple top 5%, blue top 25%...
local function PctColor(p)
if p <= 1 then return "ffff8000" end
if p <= 5 then return "ffa335ee" end
if p <= 25 then return "ff0070dd" end
if p <= 50 then return "ff1eff00" end
return "ffcccccc"
end
local function FormatPct(p)
if p < 1 then return string.format("%.2f%%", p) end
if p < 10 then return string.format("%.1f%%", p) end
return string.format("%d%%", math.floor(p + 0.5))
end
local function Num(n)
return BreakUpLargeNumbers and BreakUpLargeNumbers(n) or tostring(n)
end
ns.Key, ns.IsSecret, ns.FormatPct, ns.PctColor, ns.Num = Key, IsSecret, FormatPct, PctColor, Num
---------------------------------------------------------------------------
-- Player identity: role, class, spec, hero talent
---------------------------------------------------------------------------
local function GetHeroTalentName()
if not (C_ClassTalents and C_ClassTalents.GetActiveHeroTalentSpec) then return nil end
local ok, subTreeID = pcall(C_ClassTalents.GetActiveHeroTalentSpec)
if not ok or not subTreeID then return nil end
local configID = C_ClassTalents.GetActiveConfigID and C_ClassTalents.GetActiveConfigID()
if not configID then return nil end
local ok2, info = pcall(C_Traits.GetSubTreeInfo, configID, subTreeID)
return ok2 and info and info.name or nil
end
local function GetPlayerIdentity()
local className, _, classID = UnitClass("player")
local id = { className = className, classID = classID }
local idx = GetSpecialization and GetSpecialization()
if idx then
local specID, specName, _, _, role = GetSpecializationInfo(idx)
id.specID, id.specName, id.role = specID, specName, ROLE_KEYS[role]
end
id.hero = GetHeroTalentName()
return id
end
ns.GetPlayerIdentity = GetPlayerIdentity
---------------------------------------------------------------------------
-- Percentile maths
-- Each group has anchor points: Raider.IO's official brackets plus extra
-- brackets calculated from its daily score distribution (exact). Between two
-- anchors we interpolate on a log scale of the percentage.
---------------------------------------------------------------------------
local function Cohort(region, key)
local r = KP_DATA and KP_DATA.regions and KP_DATA.regions[region]
local c = r and r.cohorts and r.cohorts[key]
if c and c.cutoffs and #c.cutoffs > 0 then return c end
end
local function EstimatePct(score, cutoffs)
local c = cutoffs -- best first (smallest pct)
if score >= c[1].score then return c[1].pct, true end
for i = 1, #c - 1 do
local hi, lo = c[i], c[i + 1]
if score >= lo.score then
if hi.score == lo.score then return lo.pct end
local t = (score - lo.score) / (hi.score - lo.score)
return math.exp(math.log(lo.pct) + t * (math.log(hi.pct) - math.log(lo.pct)))
end
end
local last = c[#c]
if score <= 0 or last.score <= 0 then return 100 end
return last.pct + (100 - last.pct) * (1 - score / last.score)
end
-- Rating needed to be in the top `pct` percent (exact when that bracket is
-- one of the anchors, interpolated otherwise).
local function CutoffScore(cutoffs, pct)
local c = cutoffs
if not c or #c == 0 then return nil end
for i = 1, #c do
if math.abs(c[i].pct - pct) < 1e-6 then return c[i].score end
end
if pct < c[1].pct then return nil end -- better than the best anchor: unknown
for i = 1, #c - 1 do
local hi, lo = c[i], c[i + 1]
if pct > hi.pct and pct < lo.pct then
local t = (math.log(pct) - math.log(lo.pct)) / (math.log(hi.pct) - math.log(lo.pct))
return lo.score + t * (hi.score - lo.score)
end
end
local last = c[#c]
return last.score * (1 - (pct - last.pct) / (100 - last.pct))
end
local function NextGoal(score, cutoffs)
for i = #cutoffs, 1, -1 do
if score < cutoffs[i].score then
return cutoffs[i].pct, math.ceil(cutoffs[i].score - score)
end
end
end
-- Result for one group: { pct, rank, pop, exact, capped, goalPct, need }
local function Evaluate(region, cohortKey, rankKey, score, char)
local co = Cohort(region, cohortKey)
if not co then return nil end
local res = { pop = co.pop }
local rank = char and char.ranks and char.ranks[rankKey]
if rank and co.pop and co.pop > 0 then
res.rank, res.exact = rank, true
res.pct = math.max(rank / co.pop * 100, 0.001)
else
res.pct, res.capped = EstimatePct(score, co.cutoffs)
if co.pop and co.pop > 0 then res.rank = math.max(1, math.ceil(res.pct / 100 * co.pop)) end
end
res.goalPct, res.need = NextGoal(score, co.cutoffs)
return res
end
-- Stored Raider.IO ranks only count while your live score still matches them.
local function ExactChar(name, realm, score)
local chars = (KP_PERSONAL and KP_PERSONAL.characters) or (KP_DATA and KP_DATA.characters)
local c = chars and chars[Key(name, realm)]
if c and c.score and math.abs(c.score - score) < 1 then return c end
end
ns.EvaluateCohort, ns.ExactChar, ns.GetRegion, ns.GetScore = Evaluate, ExactChar, GetRegion, GetScore
ns.Cohort, ns.EstimatePct, ns.CutoffScore, ns.ScoreColor = Cohort, EstimatePct, CutoffScore, ScoreColor
-- Rows shown for the player: { label, result } (result may be nil)
local function BuildRows(score)
local region = GetRegion()
local id = GetPlayerIdentity()
local char = ExactChar(UnitName("player"), GetRealmName(), score)
local rows = {}
rows[#rows + 1] = { L["Overall"], Evaluate(region, "overall", "overall", score, char) }
if id.role then
rows[#rows + 1] = { ROLE_NAMES[id.role], Evaluate(region, id.role, id.role, score, char) }
end
if id.classID then
rows[#rows + 1] = { id.className, Evaluate(region, "class:" .. id.classID, "class", score, char) }
end
if id.specID then
rows[#rows + 1] = { id.specName, Evaluate(region, "spec:" .. id.specID, "spec_" .. id.specID, score, char) }
end
if id.hero then
rows[#rows + 1] = { id.hero, nil, L["no ranking data"] }
end
return rows, region
end
ns.BuildRows = BuildRows
local function PctText(res)
if not res then return "|cff777777—|r" end
local s = string.format(L["Top %s"], FormatPct(res.pct) .. (res.capped and "+" or ""))
return string.format("|c%s%s|r", PctColor(res.pct), s)
end
local function RankText(res, fallback)
if not res then return "|cff777777" .. (fallback or L["no data"]) .. "|r" end
if not res.rank or not res.pop then return "" end
if res.exact then
return string.format("|cff00ff96#%s|r |cff999999/ %s|r", Num(res.rank), Num(res.pop))
elseif res.capped then
return string.format("|cff999999%s %s / %s|r", L["top"], Num(res.rank), Num(res.pop))
end
return string.format("|cff999999~#%s / %s|r", Num(res.rank), Num(res.pop))
end
ns.PctText, ns.RankText = PctText, RankText
---------------------------------------------------------------------------
-- Optional floating panel (off by default; Settings or /kp panel).
-- Click to open the window, drag to move, drag the corner to resize.
---------------------------------------------------------------------------
local panel = CreateFrame("Frame", "KeystonePercentilePanel", UIParent, "BackdropTemplate")
panel:SetSize(240, 100)
panel:SetFrameStrata("MEDIUM")
panel:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8X8", edgeFile = "Interface\\Buttons\\WHITE8X8", edgeSize = 1 })
panel:SetBackdropColor(0, 0, 0, 0.6)
panel:SetBackdropBorderColor(0, 0, 0, 0.9)
panel:SetMovable(true)
panel:SetClampedToScreen(true)
panel:EnableMouse(true)
panel:RegisterForDrag("LeftButton")
panel:Hide()
panel.header = panel:CreateFontString(nil, "OVERLAY", "GameFontNormalLarge")
panel.header:SetPoint("TOPLEFT", 10, -8)
panel.footer = panel:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
panel.footer:SetJustifyH("LEFT")
local ROW_H, PAD = 16, 10
local rowFS = {}
local function RowStrings(i)
if not rowFS[i] then
local a = panel:CreateFontString(nil, "OVERLAY", "GameFontNormal")
local b = panel:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
local c = panel:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
a:SetJustifyH("LEFT"); b:SetJustifyH("LEFT"); c:SetJustifyH("LEFT")
rowFS[i] = { a, b, c }
end
return rowFS[i]
end
panel:SetScript("OnDragStart", function(self)
self.dragged = true
if not KP_DB.locked then self:StartMoving() end
end)
panel:SetScript("OnMouseDown", function(self) self.dragged = false end)
panel:SetScript("OnMouseUp", function(self, button)
if button == "LeftButton" and not self.dragged and ns.ToggleHistory then ns.ToggleHistory() end
end)
panel:SetScript("OnDragStop", function(self)
self:StopMovingOrSizing()
local p, _, rp, x, y = self:GetPoint()
KP_DB.pos = { p, rp, x, y }
end)
-- Resize from the bottom-right corner: scales the whole panel, keeping its
-- top-left corner in place.
local grip = CreateFrame("Button", nil, panel)
grip:SetSize(14, 14)
grip:SetPoint("BOTTOMRIGHT", -1, 1)
grip:SetNormalTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Up")
grip:SetHighlightTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Highlight")
grip:SetPushedTexture("Interface\\ChatFrame\\UI-ChatIM-SizeGrabber-Down")
grip:SetScript("OnMouseDown", function()
if KP_DB.locked then return end
local s0 = panel:GetScale()
local left, top = (panel:GetLeft() or 0) * s0, (panel:GetTop() or 0) * s0
local x0 = GetCursorPosition()
local w0 = panel:GetWidth() * s0
grip:SetScript("OnUpdate", function()
local x = GetCursorPosition() / UIParent:GetEffectiveScale()
local x0s = x0 / UIParent:GetEffectiveScale()
local s = math.max(0.6, math.min(2.0, s0 * (1 + (x - x0s) / math.max(w0, 1))))
panel:SetScale(s)
panel:ClearAllPoints()
panel:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", left / s, top / s)
end)
end)
grip:SetScript("OnMouseUp", function()
grip:SetScript("OnUpdate", nil)
local s = panel:GetScale()
KP_DB.scale = s
KP_DB.pos = { "TOPLEFT", "BOTTOMLEFT", panel:GetLeft(), panel:GetTop() }
end)
local function RefreshPanel()
if ns.RefreshDock then ns.RefreshDock() end
if not KP_DB or not KP_DB.floating then panel:Hide() return end
local score = GetScore("player")
if not score then return end
local rows, region = BuildRows(score)
panel.header:SetText(string.format("|c%s%s|r |cff999999%s, %s|r",
ScoreColor(score), Num(score), L["M+ rating"], (region or "?"):upper()))
-- Fill the three columns, then measure them to line everything up.
local w1, w2, w3 = 0, 0, 0
for i, row in ipairs(rows) do
local fs = RowStrings(i)
fs[1]:SetText(row[1]); fs[2]:SetText(PctText(row[2])); fs[3]:SetText(RankText(row[2], row[3]))
w1 = math.max(w1, fs[1]:GetStringWidth())
w2 = math.max(w2, fs[2]:GetStringWidth())
w3 = math.max(w3, fs[3]:GetStringWidth())
for c = 1, 3 do fs[c]:Show() end
end
for i = #rows + 1, #rowFS do for c = 1, 3 do rowFS[i][c]:Hide() end end
local top = -8 - panel.header:GetStringHeight() - 6
for i = 1, #rows do
local fs, y = rowFS[i], top - (i - 1) * ROW_H
fs[1]:ClearAllPoints(); fs[1]:SetPoint("TOPLEFT", PAD, y)
fs[2]:ClearAllPoints(); fs[2]:SetPoint("TOPLEFT", PAD + w1 + 12, y)
fs[3]:ClearAllPoints(); fs[3]:SetPoint("TOPLEFT", PAD + w1 + 12 + w2 + 12, y - 1)
end
local footer
local goal = ns.GoalLine and ns.GoalLine()
if not rows[1][2] then
footer = "|cffff5555" .. string.format(L["No ladder data for %s yet."], (region or "?"):upper()) .. "|r"
elseif (KP_DATA.updatedEpoch or 0) > 0 and GetServerTime() - KP_DATA.updatedEpoch > STALE_SECONDS then
footer = "|cffff5555" .. L["Data is over 7 days old."] .. "|r"
elseif goal then
footer = goal
end
local height = 8 + panel.header:GetStringHeight() + 6 + #rows * ROW_H
if footer then
panel.footer:SetText(footer)
panel.footer:ClearAllPoints(); panel.footer:SetPoint("TOPLEFT", PAD, -height - 2)
panel.footer:Show()
height = height + panel.footer:GetStringHeight() + 4
else
panel.footer:Hide()
end
local width = math.max(PAD + w1 + 12 + w2 + 12 + w3 + PAD, panel.header:GetStringWidth() + 2 * PAD,
footer and (panel.footer:GetStringWidth() + 2 * PAD) or 0)
panel:SetSize(width + 6, height + 10)
panel:Show()
end
ns.RefreshPanel = RefreshPanel
---------------------------------------------------------------------------
-- Unit tooltips: overall and class percentile for other players
-- (works when the client knows their rating, e.g. group members)
---------------------------------------------------------------------------
local function OnUnitTooltip(tooltip)
if tooltip ~= GameTooltip or not KP_DB.tooltips or InCombatLockdown() then return end
local _, unit = tooltip:GetUnit()
if not unit or IsSecret(unit) or not UnitIsPlayer(unit) then return end
local score = GetScore(unit)
if not score or score <= 0 then return end
local region = GetRegion()
local name, realm = UnitName(unit)
local char = ExactChar(name, realm or GetRealmName(), score)
local overall = Evaluate(region, "overall", "overall", score, char)
if not overall then return end
local text = "M+ " .. PctText(overall)
local className, _, classID = UnitClass(unit)
local cls = classID and Evaluate(region, "class:" .. classID, "class", score, char)
if cls then text = text .. " · " .. className .. " " .. PctText(cls) end
tooltip:AddLine(text)
end
---------------------------------------------------------------------------
-- Events & slash command
---------------------------------------------------------------------------
local ev = CreateFrame("Frame")
ev:RegisterEvent("ADDON_LOADED")
ev:RegisterEvent("PLAYER_ENTERING_WORLD")
ev:RegisterEvent("CHALLENGE_MODE_MAPS_UPDATE")
ev:RegisterEvent("CHALLENGE_MODE_COMPLETED")
ev:RegisterEvent("PLAYER_SPECIALIZATION_CHANGED")
ev:RegisterEvent("TRAIT_CONFIG_UPDATED")
ev:SetScript("OnEvent", function(_, event, arg1)
if event == "ADDON_LOADED" then
if arg1 ~= ADDON then return end
KP_DB = KP_DB or {}
if KP_DB.tooltips == nil then KP_DB.tooltips = true end
if KP_DB.scale then panel:SetScale(KP_DB.scale) end
local pos = KP_DB.pos
panel:ClearAllPoints()
if pos then panel:SetPoint(pos[1], UIParent, pos[2], pos[3], pos[4])
else panel:SetPoint("TOP", UIParent, "TOP", 0, -120) end
if TooltipDataProcessor and Enum.TooltipDataType then
TooltipDataProcessor.AddTooltipPostCall(Enum.TooltipDataType.Unit, OnUnitTooltip)
end
elseif event == "PLAYER_ENTERING_WORLD" or event == "CHALLENGE_MODE_COMPLETED" then
if C_MythicPlus and C_MythicPlus.RequestMapInfo then C_MythicPlus.RequestMapInfo() end
C_Timer.After(2, RefreshPanel)
elseif event == "PLAYER_SPECIALIZATION_CHANGED" and arg1 ~= "player" then
return
else
C_Timer.After(0.2, RefreshPanel)
end
end)
SLASH_KEYSTONEPERCENTILE1 = "/kp"
SLASH_KEYSTONEPERCENTILE2 = "/keystone"
SlashCmdList.KEYSTONEPERCENTILE = function(msg)
msg = (msg or ""):lower():match("^%s*(.-)%s*$")
local cmd, arg = msg:match("^(%S*)%s*(.*)$")
local tag = "|cff00ff96Keystone Percentile|r: "
if cmd == "" or cmd == "overview" then
ns.ToggleHistory("overview")
elseif cmd == "runs" or cmd == "history" then
ns.ToggleHistory("runs")
elseif cmd == "progress" then
ns.ToggleHistory("progress")
elseif cmd == "goals" or cmd == "next" then
ns.ToggleHistory("goals")
elseif cmd == "last" then
ns.ShowLastRun()
elseif cmd == "config" or cmd == "options" or cmd == "settings" then
ns.OpenSettings()
elseif cmd == "panel" or cmd == "show" or cmd == "hide" then
if cmd == "panel" then KP_DB.floating = not KP_DB.floating else KP_DB.floating = (cmd == "show") end
print(tag .. L["floating panel"] .. " " .. (KP_DB.floating and L["on"] or L["off"]))
RefreshPanel()
elseif cmd == "lock" then
KP_DB.locked = not KP_DB.locked
print(tag .. L["floating panel"] .. " " .. (KP_DB.locked and L["locked"] or L["unlocked"]))
elseif cmd == "reset" then
KP_DB.pos, KP_DB.scale = nil, nil
panel:SetScale(1)
panel:ClearAllPoints(); panel:SetPoint("TOP", UIParent, "TOP", 0, -120)
elseif cmd == "minimap" then
KP_DB.minimap = KP_DB.minimap or {}
KP_DB.minimap.hide = not KP_DB.minimap.hide
if ns.RefreshMinimap then ns.RefreshMinimap() end
elseif cmd == "tooltips" then
KP_DB.tooltips = not KP_DB.tooltips
print(tag .. L["tooltips"] .. " " .. (KP_DB.tooltips and L["on"] or L["off"]))
elseif cmd == "popup" then
KP_DB.popup = (KP_DB.popup == false)
print(tag .. L["end-of-key pop-up"] .. " " .. (KP_DB.popup and L["on"] or L["off"]))
elseif cmd == "region" then
KP_DB.region = (arg ~= "" and arg ~= "auto") and arg or nil
print(tag .. "region = " .. (GetRegion() or "?"))
RefreshPanel()
else
print(tag .. "/kp, /kp runs, /kp progress, /kp goals, /kp last, /kp config, /kp panel, /kp minimap")
end
end