Repository navigation
Expand file tree
/
Copy pathProgress.lua
More file actions
292 lines (266 loc) · 11.3 KB
/
Copy pathProgress.lua
File metadata and controls
292 lines (266 loc) · 11.3 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
-- Keystone Percentile: rating history graph ("Progress" tab).
local ADDON, ns = ...
local L = ns.L
local DAY = 86400
local MAX_SNAPSHOTS = 400
---------------------------------------------------------------------------
-- Daily rating snapshots
---------------------------------------------------------------------------
local function History(key)
KP_DB.ratingHistory = KP_DB.ratingHistory or {}
key = key or ns.MyKey()
KP_DB.ratingHistory[key] = KP_DB.ratingHistory[key] or {}
return KP_DB.ratingHistory[key]
end
-- Overall and spec "Top X%" for a score, for the current player.
local function Percentiles(score)
local region = ns.GetRegion()
local id = ns.GetPlayerIdentity()
local overall = ns.EvaluateCohort(region, "overall", "overall", score, nil)
local spec = id.specID and ns.EvaluateCohort(region, "spec:" .. id.specID, "spec_" .. id.specID, score, nil)
return overall and overall.pct, spec and spec.pct, id
end
function ns.RecordSnapshot()
local score = ns.GetScore("player")
if not score or score <= 0 then return end
local top, specTop, id = Percentiles(score)
local hist = History()
local day = date("%Y-%m-%d", GetServerTime())
local last = hist[#hist]
local entry = { d = day, t = GetServerTime(), score = score, top = top, specTop = specTop, specID = id.specID,
season = KP_DATA.season }
if last and last.d == day then hist[#hist] = entry else hist[#hist + 1] = entry end
while #hist > MAX_SNAPSHOTS do table.remove(hist, 1) end
end
---------------------------------------------------------------------------
-- Whole season, also before the addon was installed: your rating rebuilt from
-- your runs. Rating is the sum of your best run's score in each dungeon; runs
-- imported from Warcraft Logs carry that score, keys finished with the addon
-- carry your new rating. Days with a real snapshot use the snapshot.
---------------------------------------------------------------------------
local function Rebuilt(season)
local list = {}
for _, r in ipairs(ns.AllRuns()) do
if r.t and r.season == season and (r.score or r.rating) then list[#list + 1] = r end
end
table.sort(list, function(a, b) return a.t < b.t end)
local best, out, prev = {}, {}, 0
for _, r in ipairs(list) do
if r.score and r.map and (not best[r.map] or r.score > best[r.map]) then best[r.map] = r.score end
local sum = 0
for _, v in pairs(best) do sum = sum + v end
local total = math.max(prev, r.rating or 0, sum)
prev = total
local day = date("%Y-%m-%d", r.t)
local e = { d = day, t = r.t, score = math.floor(total + 0.5), rebuilt = true, season = season }
if out[#out] and out[#out].d == day then out[#out] = e else out[#out + 1] = e end
end
return out
end
-- Real snapshots plus rebuilt days, current season, oldest first.
local series = {}
local function Series(season)
local byDay = {}
for _, e in ipairs(Rebuilt(season)) do byDay[e.d] = e end
for _, e in ipairs(History()) do
if e.season == season then byDay[e.d] = e end
end
local out = {}
for _, e in pairs(byDay) do out[#out + 1] = e end
table.sort(out, function(a, b) return a.t < b.t end)
-- Rating never drops in a season; a rebuilt day after a real snapshot can
-- miss keys that weren't logged, so it's never shown below the last real one.
local floor = 0
for _, e in ipairs(out) do
if e.rebuilt then
if e.score < floor then e.score = floor end
else
floor = math.max(floor, e.score)
end
end
return out
end
-- The point closest to (and not after) a time.
local function SnapshotAt(t)
local found
for _, e in ipairs(series) do
if e.t <= t then found = e else break end
end
return found
end
---------------------------------------------------------------------------
-- Progress view
---------------------------------------------------------------------------
local GW, GH = 540, 200
local range = 0 -- days; 0 = whole season
local view = ns.NewHistoryView("progress", L["Progress"], function() ns.RefreshProgress() end, 30)
ns.ProgressView = view
view.today = view:CreateFontString(nil, "OVERLAY", "GameFontHighlightLarge")
view.today:SetPoint("TOPLEFT", 14, -6)
view.change7 = view:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
view.change7:SetPoint("TOPLEFT", 14, -30)
view.change30 = view:CreateFontString(nil, "OVERLAY", "GameFontHighlight")
view.change30:SetPoint("TOPLEFT", 14, -48)
local graph = CreateFrame("Frame", nil, view, "BackdropTemplate")
graph:SetSize(GW, GH)
graph:SetPoint("TOPLEFT", 40, -80)
graph:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8X8" })
graph:SetBackdropColor(1, 1, 1, 0.03)
graph.maxLabel = view:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
graph.maxLabel:SetPoint("TOPRIGHT", graph, "TOPLEFT", -4, 0)
graph.minLabel = view:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
graph.minLabel:SetPoint("BOTTOMRIGHT", graph, "BOTTOMLEFT", -4, 0)
graph.startLabel = view:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
graph.startLabel:SetPoint("TOPLEFT", graph, "BOTTOMLEFT", 0, -4)
graph.endLabel = view:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
graph.endLabel:SetPoint("TOPRIGHT", graph, "BOTTOMRIGHT", 0, -4)
graph.empty = view:CreateFontString(nil, "OVERLAY", "GameFontDisable")
graph.empty:SetPoint("CENTER", graph)
graph.empty:SetWidth(GW - 40)
local rangeBtn = CreateFrame("Button", nil, view, "UIPanelButtonTemplate")
rangeBtn:SetSize(110, 20)
rangeBtn:SetPoint("TOPRIGHT", -16, -6)
rangeBtn:SetScript("OnClick", function()
range = (range == 30) and 0 or 30
ns.RefreshProgress()
end)
view.note = view:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall")
view.note:SetPoint("BOTTOMLEFT", 12, 10)
view.note:SetText(L["Before you installed the addon, your rating is rebuilt from your keys on Warcraft Logs. Hover a dot for details."])
local lines, dots, gridLines = {}, {}, {}
local function Line(i)
if not lines[i] then
local l = graph.CreateLine and graph:CreateLine(nil, "ARTWORK") or nil
if l then l:SetThickness(2) end
lines[i] = l or false
end
return lines[i] or nil
end
local function Dot(i)
if not dots[i] then
local b = CreateFrame("Button", nil, graph)
b:SetSize(10, 10)
b.tex = b:CreateTexture(nil, "OVERLAY")
b.tex:SetSize(5, 5); b.tex:SetPoint("CENTER")
b:SetScript("OnEnter", function(self)
local e = self.entry
GameTooltip:SetOwner(self, "ANCHOR_TOP")
GameTooltip:AddLine(e.d)
GameTooltip:AddDoubleLine(L["Rating"], ns.Num(e.score), 1, 1, 1, 1, 1, 1)
if e.rebuilt then GameTooltip:AddLine(L["Rebuilt from your keys that day"], 0.6, 0.6, 0.6) end
if e.top then GameTooltip:AddDoubleLine(L["Overall"], string.format(L["Top %s"], ns.FormatPct(e.top)), 1, 1, 1, ns.HexToRGB(ns.PctColor(e.top))) end
if e.specTop then
local info = ns.SpecInfo(e.specID)
GameTooltip:AddDoubleLine(info and info.name or L["Spec"], string.format(L["Top %s"], ns.FormatPct(e.specTop)),
1, 1, 1, ns.HexToRGB(ns.PctColor(e.specTop)))
end
GameTooltip:Show()
end)
b:SetScript("OnLeave", GameTooltip_Hide)
dots[i] = b
end
return dots[i]
end
local function ChangeText(label, old, now)
if not old then return string.format("|cff888888%s: %s|r", label, L["not enough history yet"]) end
local diff = now.score - old.score
local s = string.format("%s: |cff%s%s%d|r %s", label, diff >= 0 and "1eff00" or "ff5555", diff >= 0 and "+" or "",
diff, L["rating"])
if old.top and now.top then
s = s .. string.format(" · |c%s%s|r → |c%s%s|r", ns.PctColor(old.top), string.format(L["Top %s"], ns.FormatPct(old.top)),
ns.PctColor(now.top), string.format(L["Top %s"], ns.FormatPct(now.top)))
end
return s
end
function ns.RefreshProgress()
ns.RecordSnapshot()
local hist = History()
series = Series(KP_DATA.season)
local now = hist[#hist] or series[#series]
local first = series[1]
rangeBtn:SetText(range == 30 and L["Last 30 days"] or L["Whole season"])
if now then
local spec = now.specTop and string.format(" · %s |c%s%s|r", (ns.SpecInfo(now.specID) or {}).name or "",
ns.PctColor(now.specTop), string.format(L["Top %s"], ns.FormatPct(now.specTop))) or ""
view.today:SetText(string.format("|c%s%s|r |c%s%s|r %s%s", ns.PctColor(now.top or 100), ns.Num(now.score),
ns.PctColor(now.top or 100), now.top and string.format(L["Top %s"], ns.FormatPct(now.top)) or "",
now.top and L["overall"] or "", spec))
view.change7:SetText(ChangeText(L["Last 30 days"], SnapshotAt(now.t - 30 * DAY), now))
view.change30:SetText(first and first ~= now and ChangeText(L["Since your first key this season"], first, now) or "")
else
view.today:SetText(""); view.change7:SetText(""); view.change30:SetText("")
end
-- Points in range, current season only
local pts = {}
local from = (range > 0 and now) and (now.t - range * DAY) or 0
for _, e in ipairs(series) do
if e.t >= from then pts[#pts + 1] = e end
end
for _, l in pairs(lines) do if l then l:Hide() end end
for _, d in pairs(dots) do d:Hide() end
for _, g in pairs(gridLines) do g:Hide() end
if #pts < 2 then
graph.empty:SetText(L["Not enough history yet. Import your Warcraft Logs runs with tools/update_data.py to see the whole season."])
graph.empty:Show()
graph.maxLabel:SetText(""); graph.minLabel:SetText(""); graph.startLabel:SetText(""); graph.endLabel:SetText("")
return
end
graph.empty:Hide()
local lo, hi = math.huge, -math.huge
for _, e in ipairs(pts) do lo = math.min(lo, e.score); hi = math.max(hi, e.score) end
if hi - lo < 50 then lo, hi = lo - 25, hi + 25 end
local t0, t1 = pts[1].t, pts[#pts].t
if t1 == t0 then t1 = t0 + 1 end
graph.maxLabel:SetText(ns.Num(math.floor(hi))); graph.minLabel:SetText(ns.Num(math.floor(lo)))
graph.startLabel:SetText(pts[1].d); graph.endLabel:SetText(pts[#pts].d)
-- Faint horizontal guides at quarters
for i = 1, 3 do
local g = gridLines[i]
if not g then
g = graph:CreateTexture(nil, "BACKGROUND", nil, 1)
g:SetColorTexture(1, 1, 1, 0.06); g:SetHeight(1)
gridLines[i] = g
end
g:ClearAllPoints()
g:SetPoint("BOTTOMLEFT", graph, "BOTTOMLEFT", 0, GH * i / 4)
g:SetPoint("BOTTOMRIGHT", graph, "BOTTOMRIGHT", 0, GH * i / 4)
g:Show()
end
local function XY(e)
return (e.t - t0) / (t1 - t0) * (GW - 10) + 5, (e.score - lo) / (hi - lo) * (GH - 10) + 5
end
local px, py
for i, e in ipairs(pts) do
local x, y = XY(e)
local r, g, b
if e.top then r, g, b = ns.HexToRGB(ns.PctColor(e.top)) else r, g, b = 0.95, 0.78, 0.38 end
if px then
local l = Line(i)
if l then
l:SetColorTexture(r, g, b, 0.9)
l:SetStartPoint("BOTTOMLEFT", graph, px, py)
l:SetEndPoint("BOTTOMLEFT", graph, x, y)
l:Show()
end
end
local d = Dot(i)
d.entry = e
d.tex:SetColorTexture(r, g, b, 1)
d:ClearAllPoints(); d:SetPoint("CENTER", graph, "BOTTOMLEFT", x, y); d:Show()
px, py = x, y
end
end
---------------------------------------------------------------------------
-- When to take snapshots
---------------------------------------------------------------------------
ns.OnRunRecorded = function()
C_Timer.After(5, function()
ns.RecordSnapshot()
if ns.RefreshAll then ns.RefreshAll() end
end)
end
local ev = CreateFrame("Frame")
ev:RegisterEvent("PLAYER_ENTERING_WORLD")
ev:SetScript("OnEvent", function(_, _, isLogin)
if isLogin then C_Timer.After(8, ns.RecordSnapshot) end
end)