Repository navigation
Expand file tree
/
Copy pathEasyTrackerCompact.lua
More file actions
249 lines (221 loc) · 8.45 KB
/
Copy pathEasyTrackerCompact.lua
File metadata and controls
249 lines (221 loc) · 8.45 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
local ADDON_NAME = ...
local compact = CreateFrame("Frame")
local COMPACT_HEIGHT = 100
local SAMPLE_WINDOW = 15 * 60
local MAX_MONSTER_SAMPLES = 20
local UPDATE_INTERVAL = 1
local panel
local lineOne
local lineTwo
local xpSamples = {}
local monsterSamples = {}
local lastXP = 0
local lastXPMax = 0
local lastLevel = 0
local lastSessionStart = 0
local lastSessionXP = 0
local updateElapsed = 0
local floor = math.floor
local max = math.max
local function FormatNumber(value)
value = floor(tonumber(value) or 0)
local formatted = tostring(value)
while true do
local replaced
formatted, replaced = string.gsub(formatted, "^(-?%d+)(%d%d%d)", "%1,%2")
if replaced == 0 then break end
end
return formatted
end
local function FormatDuration(seconds)
if not seconds or seconds < 0 or seconds == math.huge then return "--" end
seconds = floor(seconds + 0.5)
if seconds < 60 then return seconds .. "s" end
if seconds < 3600 then return floor(seconds / 60) .. "m" end
if seconds < 86400 then return floor(seconds / 3600) .. "h " .. floor((seconds % 3600) / 60) .. "m" end
return floor(seconds / 86400) .. "d " .. floor((seconds % 86400) / 3600) .. "h"
end
local function FormatMoney(copper)
copper = floor(tonumber(copper) or 0)
local gold = floor(copper / 10000)
local silver = floor((copper % 10000) / 100)
local coins = copper % 100
if gold > 0 then return FormatNumber(gold) .. "|cffffd36bg|r " .. silver .. "|cffc7c7c7s|r" end
if silver > 0 then return silver .. "|cffc7c7c7s|r " .. coins .. "|cffb87333c|r" end
return coins .. "|cffb87333c|r"
end
local function MakeText(parent, size, color)
local text = parent:CreateFontString(nil, "OVERLAY")
text:SetFont("Fonts\\FRIZQT__.TTF", size, "OUTLINE")
text:SetTextColor(color[1], color[2], color[3])
text:SetJustifyH("LEFT")
return text
end
local function AuctionPrice(item)
if not item then return nil end
if type(Atr_GetAuctionPrice) == "function" then
local price = Atr_GetAuctionPrice(item)
if price and price > 0 then return price end
end
if type(Atr_GetAuctionBuyout) == "function" then
local price = Atr_GetAuctionBuyout(item)
if price and price > 0 then return price end
end
return nil
end
local function TrimSamples(samples, now)
while #samples > 0 and now - samples[1].time > SAMPLE_WINDOW do
table.remove(samples, 1)
end
end
local function AddXPSample(amount)
if not amount or amount <= 0 then return end
local now = GetTime()
xpSamples[#xpSamples + 1] = { time = now, amount = amount }
TrimSamples(xpSamples, now)
end
local function AddMonsterSample(amount)
if not amount or amount <= 0 then return end
local now = GetTime()
monsterSamples[#monsterSamples + 1] = { time = now, amount = amount }
while #monsterSamples > MAX_MONSTER_SAMPLES do table.remove(monsterSamples, 1) end
TrimSamples(monsterSamples, now)
end
local function CalculateXPRate()
local now = GetTime()
TrimSamples(xpSamples, now)
if #xpSamples == 0 then return 0 end
local total = 0
for index = 1, #xpSamples do total = total + xpSamples[index].amount end
local elapsed = now - xpSamples[1].time
if elapsed < 30 then return 0 end
return total / elapsed * 3600
end
local function CalculateMonsterAverage()
local now = GetTime()
TrimSamples(monsterSamples, now)
if #monsterSamples == 0 then return 0 end
local total = 0
for index = 1, #monsterSamples do total = total + monsterSamples[index].amount end
return total / #monsterSamples
end
local function CalculateLootValue()
if not EasyTrackerDB or type(EasyTrackerDB.loot) ~= "table" then return 0 end
local total = 0
for index = 1, #EasyTrackerDB.loot do
local entry = EasyTrackerDB.loot[index]
local unitPrice = tonumber(entry.unitPrice) or 0
if unitPrice <= 0 then unitPrice = AuctionPrice(entry.link) or AuctionPrice(entry.name) or 0 end
total = total + unitPrice * (entry.count or 1)
end
return total
end
local function ParseCombatXP(message)
if type(message) ~= "string" then return nil end
local numberText = message:match("[Yy]ou gain%s+([%d%.,]+)%s+experience")
or message:match("gain%s+([%d%.,]+)%s+experience")
if not numberText then return nil end
return tonumber((numberText:gsub("[^%d]", "")))
end
local function ResetRecentTracking()
xpSamples = {}
monsterSamples = {}
lastXP = UnitXP("player") or 0
lastXPMax = UnitXPMax("player") or 0
lastLevel = UnitLevel("player") or 0
end
local function DetectSessionReset()
if not EasyTrackerDB or not EasyTrackerDB.session then return end
local sessionStart = EasyTrackerDB.session.startedAt or 0
local sessionXP = EasyTrackerDB.session.xpGained or 0
if sessionStart ~= lastSessionStart or (lastSessionXP > 0 and sessionXP == 0) then
ResetRecentTracking()
end
lastSessionStart = sessionStart
lastSessionXP = sessionXP
end
local function UpdateCompactText()
if not panel or not EasyTrackerDB then return end
DetectSessionReset()
local current = UnitXP("player") or 0
local maximum = UnitXPMax("player") or 0
local remaining = max(0, maximum - current)
local rate = CalculateXPRate()
local monsterAverage = CalculateMonsterAverage()
if maximum <= 0 then
lineOne:SetText("|cffffd36bMaximum level|r")
else
local eta = rate > 0 and FormatDuration(remaining / rate * 3600) or "--"
local monsters = monsterAverage > 0 and ("~" .. math.ceil(remaining / monsterAverage) .. " mobs") or "mobs --"
local average = monsterAverage > 0 and (" @ " .. FormatNumber(monsterAverage) .. " XP") or ""
lineOne:SetText("|cffffd36bETA " .. eta .. "|r | " .. monsters .. average)
end
local cash = EasyTrackerDB.session and EasyTrackerDB.session.cashGained or 0
local loot = CalculateLootValue()
lineTwo:SetText("Made +" .. FormatMoney(cash) .. " cash | Loot " .. FormatMoney(loot))
end
local function ApplyCompactState()
if not panel or not EasyTrackerDB then return end
if EasyTrackerDB.collapsed then
panel:SetHeight(COMPACT_HEIGHT)
lineOne:Show()
lineTwo:Show()
UpdateCompactText()
else
lineOne:Hide()
lineTwo:Hide()
end
end
local function InstallCompactMode()
panel = EasyTrackerFrame
if not panel or panel.easyTrackerCompactInstalled then return end
panel.easyTrackerCompactInstalled = true
lineOne = MakeText(panel, 10, {0.9, 0.85, 0.7})
lineOne:SetPoint("TOPLEFT", 16, -63)
lineOne:SetPoint("TOPRIGHT", -16, -63)
lineTwo = MakeText(panel, 10, {0.68, 0.82, 0.64})
lineTwo:SetPoint("TOPLEFT", 16, -81)
lineTwo:SetPoint("TOPRIGHT", -16, -81)
lastSessionStart = EasyTrackerDB.session and EasyTrackerDB.session.startedAt or 0
lastSessionXP = EasyTrackerDB.session and EasyTrackerDB.session.xpGained or 0
ResetRecentTracking()
if panel.collapse and panel.collapse.HookScript then
panel.collapse:HookScript("OnClick", ApplyCompactState)
end
ApplyCompactState()
end
compact:RegisterEvent("ADDON_LOADED")
compact:RegisterEvent("PLAYER_ENTERING_WORLD")
compact:RegisterEvent("PLAYER_XP_UPDATE")
compact:RegisterEvent("CHAT_MSG_COMBAT_XP_GAIN")
compact:SetScript("OnEvent", function(_, event, arg1)
if event == "ADDON_LOADED" and arg1 == ADDON_NAME then
InstallCompactMode()
elseif event == "PLAYER_ENTERING_WORLD" then
if not panel then InstallCompactMode() end
ResetRecentTracking()
UpdateCompactText()
elseif event == "PLAYER_XP_UPDATE" then
local current = UnitXP("player") or 0
local maximum = UnitXPMax("player") or 0
local level = UnitLevel("player") or 0
local gained = 0
if current >= lastXP and level == lastLevel then
gained = current - lastXP
elseif current < lastXP or level > lastLevel then
gained = max(0, lastXPMax - lastXP) + current
end
AddXPSample(gained)
lastXP, lastXPMax, lastLevel = current, maximum, level
UpdateCompactText()
elseif event == "CHAT_MSG_COMBAT_XP_GAIN" then
AddMonsterSample(ParseCombatXP(arg1))
UpdateCompactText()
end
end)
compact:SetScript("OnUpdate", function(_, elapsed)
updateElapsed = updateElapsed + elapsed
if updateElapsed < UPDATE_INTERVAL then return end
updateElapsed = 0
if EasyTrackerDB and EasyTrackerDB.collapsed then UpdateCompactText() end
end)