Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
380 lines (289 loc) · 12.7 KB
/
Copy pathCore.lua
File metadata and controls
380 lines (289 loc) · 12.7 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
--[[
My Loot History addon
Copyright (C) 2026 RustyDaemon (https://github.com/RustyDaemon)
See License file for details.
--]]
local addonName, addon = ...
-- The addon object is the namespace every file is handed, so what the kit puts on it (ns.UI) is
-- MLH.UI.
local MLH = LibStub("AceAddon-3.0"):NewAddon(addon, addonName, "AceConsole-3.0", "AceEvent-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale(addonName)
MLH.website = "https://mlh.rustydaemon.com"
-- Test MULTIPLE forms first: single-item patterns also match multi-item messages.
local lootMessageForms = {
{ global = "LOOT_ITEM_SELF_MULTIPLE", hasQuantity = true },
{ global = "LOOT_ITEM_PUSHED_SELF_MULTIPLE", hasQuantity = true, kind = "pushed" },
{ global = "LOOT_ITEM_CREATED_SELF_MULTIPLE", hasQuantity = true, kind = "crafted" },
{ global = "LOOT_ITEM_SELF", hasQuantity = false },
{ global = "LOOT_ITEM_PUSHED_SELF", hasQuantity = false, kind = "pushed" },
{ global = "LOOT_ITEM_CREATED_SELF", hasQuantity = false, kind = "crafted" },
}
local currencyMessageForms = {
{ global = "CURRENCY_GAINED_MULTIPLE_BONUS", hasQuantity = true },
{ global = "CURRENCY_GAINED_MULTIPLE", hasQuantity = true },
{ global = "CURRENCY_GAINED", hasQuantity = false },
}
local lootPatterns = nil
local currencyPatterns = nil
-- Capture only quantity so localized argument order does not affect parsing.
local function toLootPattern(fmt, hasQuantity)
local pattern = fmt:gsub("%%%d%$", "%%") -- %1$s -> %s
pattern = pattern:gsub("([%^%$%(%)%.%[%]%*%+%-%?])", "%%%1") -- escape pattern magic
pattern = pattern:gsub("%%s", ".+")
pattern = pattern:gsub("%%d", hasQuantity and "(%%d+)" or "%%d+")
return "^"..pattern
end
local function buildPatterns(forms)
local patterns = {}
for i = 1, #forms do
local form = forms[i]
local fmt = _G[form.global]
if (fmt) then
patterns[#patterns+1] = {
pattern = toLootPattern(fmt, form.hasQuantity),
hasQuantity = form.hasQuantity,
kind = form.kind,
}
end
end
return patterns
end
local function getLootPatterns()
if (not lootPatterns) then
lootPatterns = buildPatterns(lootMessageForms)
end
return lootPatterns
end
local function getCurrencyPatterns()
if (not currencyPatterns) then
currencyPatterns = buildPatterns(currencyMessageForms)
end
return currencyPatterns
end
local function matchQuantity(message, patterns)
for i = 1, #patterns do
local form = patterns[i]
local match = message:match(form.pattern)
if (match) then
return form.hasQuantity and (tonumber(match) or 1) or 1, form.kind
end
end
return nil
end
function MLH:OnInitialize()
self:initDatabase()
-- Every window's title bar shows a map, not the sigil the AddOns list shows.
self.UI.logo = "Interface\\Icons\\inv_misc_map09"
self.UI:setFont(self.db.char.config.pixelFont and "pixel" or "blizzard")
local removedEntries, removedRecords = self:pruneHistory()
if (removedEntries > 0) then
print(L["M_HistoryPruned"](removedEntries, removedRecords, self.db.char.config.retentionDays))
end
self:addToGameOptions()
self:initMinimap()
self:RegisterChatCommand("mlh", "SlashCommandListener")
-- A reload or a quick relog carries on with the session; a longer break files it and starts anew.
self:continueOrBeginSession()
print(L["_IntroMessage"](addonName))
end
function MLH:OnEnable()
self:RegisterEvent("CHAT_MSG_LOOT")
self:RegisterEvent("CHAT_MSG_MONEY")
self:RegisterEvent("CHAT_MSG_CURRENCY")
-- Going away, coming back, and walking into or out of a resting area pause and resume the session
-- clock. Entering the world checks again, since the resting state may not be known at load time.
self:RegisterEvent("PLAYER_FLAGS_CHANGED", "onSessionPauseEvent")
self:RegisterEvent("PLAYER_UPDATE_RESTING", "onSessionPauseEvent")
self:RegisterEvent("PLAYER_ENTERING_WORLD", "onSessionPauseEvent")
-- A /reload logs out too: the time noted lets the next load carry the session on.
self:RegisterEvent("PLAYER_LOGOUT", "noteLogout")
self:applySourceTracking()
self:applyQuestTracking()
self:initTooltip()
self:applyHUD()
self:updateBrokerText()
end
function MLH:CHAT_MSG_LOOT(_, message, ...)
local itemLink, quantity, itemID, messageKind = self:getLootDetails(message)
if (not itemID) then return end
-- Capture zone and source before asynchronous item loading.
local zoneID = self:getZoneID()
local source = self:getQuestSource() or self:getCurrentSource(messageKind)
-- Load the link, not the id: an upgraded or bonus-rolled drop has its own quality and price.
Item:CreateFromItemLink(itemLink):ContinueOnItemLoad(function()
self:recordLoot(itemID, itemLink, quantity, zoneID, source, messageKind)
end)
end
-- Read from the link the chat message carried, so a 323 drop is not priced as the 302 base item.
local function readItemInfo(itemID, itemLink)
-- Called again rather than packed into a table: the returns have nils in the middle,
-- and unpack() on a table with holes may drop everything after the first one.
if (itemLink and C_Item.GetItemInfo(itemLink)) then
return C_Item.GetItemInfo(itemLink)
end
return C_Item.GetItemInfo(itemID)
end
function MLH:recordLoot(itemID, itemLink, quantity, zoneID, source, messageKind)
local itemName, cachedLink, itemQuality, _, _, _, _, _, _, itemTexture, sellPrice, classID, subClassID =
readItemInfo(itemID, itemLink)
if (self:isQuestItem(classID, subClassID)) then return end
sellPrice = sellPrice or 0
itemLink = itemLink or cachedLink
-- Alert before the zero-price check: mounts and pets often cannot be sold to a vendor.
-- A hidden item never alerts: the player said it does not matter.
local alert = not self:isItemHidden(itemID)
and self:getDropAlert(itemID, itemLink, itemQuality, quantity, sellPrice, classID, subClassID)
if (alert) then
self:alertDrop(itemLink, itemTexture, itemQuality, quantity, alert)
end
if (sellPrice == 0 and self.db.char.config.ignoreItemsWithZeroPrice) then return end
local entry = self:addItem(itemID, quantity, itemLink, itemTexture, itemQuality,
itemName, zoneID, sellPrice, source)
if (messageKind == "pushed") then self:noteQuestCandidate(entry) end
end
function MLH:CHAT_MSG_MONEY(_, message, ...)
local money = 0
local denominations = { { "GOLD_AMOUNT", 10000 }, { "SILVER_AMOUNT", 100 }, { "COPPER_AMOUNT", 1 } }
for _, denomination in ipairs(denominations) do
-- Only the first texture format argument is the coin amount; the rest are dimensions.
for _, suffix in ipairs({ "", "_TEXTURE" }) do
local fmt = _G[denomination[1]..suffix]
if (fmt) then
local pattern = fmt:gsub("%%%d%$", "%%")
pattern = pattern:gsub("%%[ds]", "MLHQUANTITY", 1)
pattern = pattern:gsub("([%^%$%(%)%.%[%]%*%+%-%?])", "%%%1")
pattern = pattern:gsub("%%d", "%%d+"):gsub("%%s", ".-")
pattern = pattern:gsub("MLHQUANTITY", "([%%d%%s,%%.]+)")
local amount = message:match(pattern)
if (amount) then
money = money + (tonumber((amount:gsub("%D", ""))) or 0) * denomination[2]
break
end
end
end
end
-- Quest reward money is already counted with the quest, so it is not added again.
if (money > 0 and not self:claimQuestMoney(money)) then
self:noteQuestCandidate(self:addGold(money, self:getZoneID()), money)
end
end
function MLH:CHAT_MSG_CURRENCY(_, message, ...)
if (not self.db.char.config.trackCurrency) then return end
local currencyID, quantity = self:getCurrencyDetails(message)
if (not currencyID) then return end
local info = C_CurrencyInfo.GetCurrencyInfo(currencyID)
local entry = self:addCurrency(currencyID, quantity, info and info.name,
info and info.iconFileID, info and info.quality, self:getZoneID(), self:getQuestSource())
self:noteQuestCandidate(entry)
end
function MLH:isQuestItem(classID, subClassID)
return (classID == Enum.ItemClass.Questitem) or (classID == Enum.ItemClass.Consumable and subClassID == 8)
end
function MLH:getLootDetails(message)
local quantity, kind = matchQuantity(message, getLootPatterns())
if (not quantity) then return nil end
local itemLink = message:match("|c.-|h|r")
if (not itemLink) then return nil end
return itemLink, quantity, C_Item.GetItemInfoInstant(itemLink), kind
end
function MLH:getCurrencyDetails(message)
local quantity = matchQuantity(message, getCurrencyPatterns())
if (not quantity) then return nil end
local currencyID = tonumber(message:match("|Hcurrency:(%d+)"))
if (not currencyID) then return nil end
return currencyID, quantity
end
function MLH:getZoneID()
return C_Map.GetBestMapForUnit("player")
end
-- Cache unknown map IDs as false to avoid repeated lookups.
local zoneNameCache = {}
function MLH:getZoneName(zoneID)
if (not zoneID) then return nil end
local cached = zoneNameCache[zoneID]
if (cached ~= nil) then
return cached or nil
end
local zoneInfo = C_Map.GetMapInfo(zoneID)
local zoneName = zoneInfo and zoneInfo.name
zoneNameCache[zoneID] = zoneName or false
return zoneName
end
-- Sort comparator: highest quantity first, ties broken by name.
function MLH.byQuantityThenName(l, r)
if (l.quantity == r.quantity) then return l.name < r.name end
return l.quantity > r.quantity
end
-- Sorts rows in place by value(row), either way, with ties broken by name(row) from A to Z.
function MLH.sortRows(rows, value, descending, name)
table.sort(rows, function(l, r)
local lv, rv = value(l), value(r)
if (lv == rv) then return name(l) < name(r) end
if (descending) then return lv > rv end
return lv < rv
end)
end
function MLH:aggregateLoot(entries, unknownZoneName)
local quantity, firstFound, lastFound = 0, nil, nil
local zoneCounts, zoneIds, zones = {}, {}, {}
for i = 1, #entries do
local entry = entries[i]
local entryQuantity = tonumber(entry.quantity) or 1
local knownName = self:getZoneName(entry.zoneID)
local zoneName = knownName or unknownZoneName
local foundOn = entry.foundOn
quantity = quantity + entryQuantity
if (zoneName) then
zoneCounts[zoneName] = (zoneCounts[zoneName] or 0) + entryQuantity
end
-- Only a nameable zone gets an id: the zone filter cannot select the unknown bucket.
if (knownName and not zoneIds[knownName]) then
zoneIds[knownName] = entry.zoneID
end
if (foundOn) then
if (firstFound == nil or foundOn < firstFound) then firstFound = foundOn end
if (lastFound == nil or foundOn > lastFound) then lastFound = foundOn end
end
end
for zoneName, zoneQuantity in pairs(zoneCounts) do
zones[#zones+1] = { name = zoneName, quantity = zoneQuantity, id = zoneIds[zoneName] }
end
table.sort(zones, MLH.byQuantityThenName)
return quantity, zones, firstFound, lastFound
end
-- /mlh share [say|party|raid|instance|guild]: the group's channel when none is named, or just the
-- player's own chat when there is no group to send it to.
function MLH:shareCommand(word)
local channel, unknown = self:resolveShareChannel(word)
if (unknown) then
print(L["M_ShareUnknownChannel"](word))
return
end
self:shareSession(channel)
if (not channel) then print(L["M_ShareOnlyYou"]) end
end
function MLH:SlashCommandListener(input)
local shareWord = type(input) == "string" and input:match("^share%s*(%S*)%s*$") or nil
if (shareWord) then
self:shareCommand(shareWord)
elseif (input == "config") then
self:openSettings()
elseif (input == "session") then
print(self:getSessionLine())
elseif (input == "session reset") then
self:resetSession()
print(self:getSessionLine())
elseif (input == "hud") then
print(self:toggleHUD() and L["M_HudShown"] or L["M_HudHidden"])
elseif (input == "hud lock") then
print(self:toggleHUDLock() and L["M_HudLocked"] or L["M_HudUnlocked"])
elseif (input == "gui") then
self:gui()
elseif (input == "help") then
print(L["M_Help"](self.website))
elseif (input == "web" or input == "website") then
print(L["M_Website"](self.website))
else
self:gui()
end
end