Repository navigation
Expand file tree
/
Copy pathCore.lua
More file actions
executable file
·501 lines (454 loc) · 20.8 KB
/
Copy pathCore.lua
File metadata and controls
executable file
·501 lines (454 loc) · 20.8 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
-- MarketWall: core, settings, helpers
local ADDON, ns = ...
MarketWall = ns
ns.cfgDefaults = {
wallShare = 0.05, -- share of total supply a price level needs (cumulative) to count as the wall
wallMinUnits = 5, -- minimum units for a wall on thin items
ahCut = 0.05, -- auction house cut on sales
minSupply = 20, -- full-scan snipes/dips need at least this many units listed
minSnipeProfit = 100, -- copper, total profit after cut
minSnipeDiscount = 0.10, -- snipe units must average this far under the wall
dipPct = 0.10, -- wall must be this far under its median to be a dip
minDisenchantProfit = 5000, -- copper per item, net disenchant value (after cut and deposits) minus price
minDisenchantPct = 0.20, -- that profit as a share of the price
disenchantShowAll = false, -- Disenchant list also shows items above your Enchanting skill
salvagerMode = "auto", -- Salvager's disenchant bonus: "auto" (while the buff is up), "on", "off"
salvagerBonus = 0, -- that bonus, in percent; both start from Disenchantry's on first import
minHistoryDays = 5, -- "Days before data is reliable": fewer days of data flag an item as low-data
baselineDays = 14, -- days used for the normal price
retainDays = 30, -- daily history kept for unwatched items
retainWeeks = 52, -- weekly points kept for unwatched items, folded from their expired days
minRecordSupply = 5, -- unwatched items with fewer units are not recorded
maxSnaps = 400, -- per-scan snapshots kept per watched item
sound = true,
autoScan = true, -- start the daily full scan when the auction house opens
staleMinutes = 60, -- watchlist prices older than this are greyed out
sellDuration = 2, -- last auction duration picked on the Sell page: 1 = 12h, 2 = 24h, 3 = 48h
baitSearchGap = 1.0, -- seconds between Bait Detect searches; never less than 1
baitConfirmPress = true, -- Bait Detect confirms each purchase with its own press
baitMinUnder = 0.50, -- bait must sit at least this far under the wall; held to 10% to 95%
minimapButton = true, -- show the MW button on the minimap
mobDisenchant = "auto", -- disenchanting in a mob's Optimal value: "auto" (with Enchanting), "always", "never"
mobSkinning = "auto", -- skinning in a mob's Optimal value: "auto" (with Skinning), "always", "never"
mobRareShare = 0.02, -- drops seen in fewer than this share of kills count as rare
mobMinKills = 10, -- seen kills under this flag a mob's values as rough
}
-- Settings Reset to defaults leaves alone: imported once from Disenchantry, which never imports again
ns.KEPT_ON_RESET = { salvagerMode = true, salvagerBonus = true }
function ns.ResetSettings(cfg)
for key in pairs(cfg) do
if not ns.KEPT_ON_RESET[key] then cfg[key] = nil end
end
end
-- The last bag index to scan for "all my bags": the backpack, the equipped bags and the reagent bag. Never the bank.
function ns.LastBagIndex()
return NUM_TOTAL_EQUIPPED_BAG_SLOTS or ((NUM_BAG_SLOTS or 4) + (NUM_REAGENTBAG_SLOTS or 0))
end
-- A value the game returned, or nil when it is secret (Forever hides some values from add-ons)
function ns.Readable(v)
local check = _G.issecretvalue
if check and check(v) then return nil end
return v
end
-- Colour codes, one job each. Item names keep their quality colour instead.
ns.COLOR = {
LABEL = "|cffffd100", -- titles and labels only
VALUE = "|cffffffff", -- values
GREY = "|cffa3acac", -- secondary detail
GAIN = "|cff4fc36b", -- profit or gain
WARN = "|cffffb057", -- warnings only: low data, stale, rough, suspected bait
LOSS = "|cffe06060", -- loss
BUY = "|cff7cc0ff", -- your buy price and what is under it, like the chart's buy line
BRAND = "|cff2fa39d", -- the MarketWall name, as the item tooltip's heading shows it
}
local function Print(...)
print(ns.COLOR.BRAND .. "MarketWall|r:", ...)
end
ns.Print = Print
function ns.Round(x)
return math.floor(x + 0.5)
end
-- A share as a percent, with one decimal under 10%: "4.2%", "31%"
function ns.ShareText(share)
local pct = share * 100
if pct < 10 then return string.format("%.1f%%", pct) end
return string.format("%d%%", ns.Round(pct))
end
-- The coins shown for a copper amount, largest first, and whether it was negative. A zero coin
-- is left out ("2g", not "2g 0s 0c"); zero itself shows as 0c.
local function CoinParts(c)
c = ns.Round(c)
local neg = c < 0
if neg then c = -c end
local parts = {}
for _, coin in ipairs({ { math.floor(c / 10000), "g" }, { math.floor(c / 100) % 100, "s" }, { c % 100, "c" } }) do
if coin[1] > 0 then parts[#parts + 1] = coin end
end
if #parts == 0 then parts[1] = { 0, "c" } end
return parts, neg
end
local COIN_ICON = {
g = "|TInterface\\MoneyFrame\\UI-GoldIcon:0:0:0:0|t",
s = "|TInterface\\MoneyFrame\\UI-SilverIcon:0:0:0:0|t",
c = "|TInterface\\MoneyFrame\\UI-CopperIcon:0:0:0:0|t",
}
local function JoinCoins(c, mark, compact)
if not c then return "-" end
if compact and math.abs(c) >= 10000 then
c = (c < 0 and -1 or 1) * ns.Round(math.abs(c) / 100) * 100 -- to the nearest silver, either sign
end
local parts, neg = CoinParts(c)
local out = {}
for i, part in ipairs(parts) do out[i] = part[1] .. mark(part[2]) end
return (neg and "-" or "") .. table.concat(out, " ")
end
-- For display: the number then its coin icon, in the colour of the surrounding text
function ns.Money(c)
return JoinCoins(c, function(unit) return COIN_ICON[unit] end)
end
-- Coin icons tinted darker, so they fade with grey text. A tint needs the full texture form:
-- size, offset, texture size, coordinates, then r:g:b out of 255.
local FADED_COIN_ICON = {}
for unit, icon in pairs(COIN_ICON) do
FADED_COIN_ICON[unit] = icon:gsub(":0:0:0:0|t$", ":0:0:0:0:64:64:0:64:0:64:128:128:128|t")
end
-- ns.Money for faded text: the same numbers, with the coin icons darkened to match
function ns.MoneyFaded(c)
return JoinCoins(c, function(unit) return FADED_COIN_ICON[unit] end)
end
-- For table cells and chart labels: at most two coins, from the largest, so gold amounts are
-- rounded to the nearest silver (5g 4s 73c shows 5g 5s, 9g 99s 88c shows 10g). A list's money
-- cell shows the exact ns.Money amount on hover; chart labels have no hover.
function ns.MoneyShort(c)
return JoinCoins(c, function(unit) return COIN_ICON[unit] end, true)
end
-- Letters instead of icons ("1g 41s 23c"), for edit boxes and anything ParseMoney reads back
function ns.MoneyText(c)
return JoinCoins(c, function(unit) return unit end)
end
local MONEY_PART = "(%d*%.?%d+)%s*([gsc])"
local COPPER_PER = { g = 10000, s = 100, c = 1 }
-- Accepts "80c", "1s 20c", "2g", "1g5s", "1.5g", or a plain copper number. Fractions of a
-- copper are dropped. Anything else, including negative amounts, is nil.
function ns.ParseMoney(text)
if not text then return nil end
text = string.lower((text:gsub("|c%x%x%x%x%x%x%x%x", ""):gsub("|r", "")))
local plain = tonumber(text)
if plain then
if plain < 0 then return nil end
return math.floor(plain)
end
local leftover, parts = string.gsub(text, MONEY_PART, "")
if parts == 0 or string.find(leftover, "%S") then return nil end
local total = 0
for num, unit in string.gmatch(text, MONEY_PART) do
total = total + tonumber(num) * COPPER_PER[unit]
end
return math.floor(total + 1e-9)
end
-- Days are counted in local time, so "today" flips at your midnight, not at midnight UTC
local tzOffset
local function Offset()
if tzOffset then return tzOffset end
local ok, value = pcall(function()
local now = time()
return time(date("*t", now)) - time(date("!*t", now))
end)
tzOffset = (ok and type(value) == "number") and value or 0
return tzOffset
end
function ns.DayOf(t)
return math.floor((t + Offset()) / 86400)
end
function ns.DayStart(day)
return day * 86400 - Offset()
end
function ns.Today()
return ns.DayOf(time())
end
function ns.Ago(t)
if not t then return "never" end
local s = time() - t
if s < 90 then return "just now" end
if s < 3600 then return math.floor(s / 60) .. " min ago" end
if s < 86400 then return math.floor(s / 3600) .. "h ago" end
return math.floor(s / 86400) .. "d ago"
end
function ns.Icon(id)
if C_Item and C_Item.GetItemIconByID then
local icon = C_Item.GetItemIconByID(id)
if icon then return icon end
end
if C_Item and C_Item.GetItemInfoInstant then
local _, _, _, _, icon = C_Item.GetItemInfoInstant(id)
if icon then return icon end
end
return 134400
end
function ns.ColorName(id)
local name = ns.Name(id)
local quality = C_Item and C_Item.GetItemQualityByID and C_Item.GetItemQualityByID(id)
local color = quality and ITEM_QUALITY_COLORS and ITEM_QUALITY_COLORS[quality]
if color and color.hex then return color.hex .. name .. "|r" end
if color and color.r then
return string.format("|cff%02x%02x%02x%s|r", color.r * 255, color.g * 255, color.b * 255, name)
end
return name
end
ns.session = { qty = 0, cost = 0, resale = 0, buys = 0 }
local function SessionSummary()
local s = ns.session
if s.buys > 0 then
local text = string.format("This visit: %d purchase(s), %d units, %s spent.", s.buys, s.qty, ns.Money(s.cost))
if s.resale > 0 then
text = text .. " Expected profit at your sell prices: " .. ns.Money(s.resale - s.cost) .. "."
end
Print(text)
end
ns.session = { qty = 0, cost = 0, resale = 0, buys = 0 }
end
function ns.Realm()
return (GetNormalizedRealmName and GetNormalizedRealmName()) or (GetRealmName and GetRealmName()) or "Unknown"
end
-- The faction whose Auction House the character uses: "Alliance" or "Horde", else "Neutral"
function ns.Faction()
local faction = UnitFactionGroup and ns.Readable(UnitFactionGroup("player"))
if faction == "Alliance" or faction == "Horde" then return faction end
return "Neutral"
end
-- The server ID in a player GUID: "6796" from "Player-6796-002683B6", nil for anything else
function ns.ServerID(guid)
return type(guid) == "string" and string.match(guid, "^Player%-(%d+)%-") or nil
end
-- Whose Auction House history this character reads: its realm and faction, as "6796-Alliance".
-- WoW Forever gives every character on one realm the same server ID in its GUID, so that
-- names the realm; the realm name stands in while the GUID is unreadable.
function ns.RealmKey()
local guid = UnitGUID and ns.Readable(UnitGUID("player"))
return (ns.ServerID(guid) or ns.Realm()) .. "-" .. ns.Faction()
end
-- Whether a realm bucket holds anything: any value, or any table with an entry
local function HasHistory(bucket)
for _, value in pairs(bucket or {}) do
if type(value) ~= "table" or next(value) then return true end
end
return false
end
-- Before realm keys had a faction, all history sat under the realm name alone. Moves it, once,
-- to key, unless key already has history; the old bucket then stays where it is. Returns true
-- when it moved.
function ns.AdoptRealmHistory(realms, legacyKey, key)
local old = realms[legacyKey]
if not old or legacyKey == key or HasHistory(realms[key]) then return false end
realms[key], realms[legacyKey] = old, nil
return true
end
-- Auctioneers friendly to both factions, so they serve the neutral Auction House (from the
-- installed QuestieDB for WoW Forever, which flags them as auctioneers)
local NEUTRAL_AUCTIONEERS = {
[8661] = true, -- Beardo, Gadgetzan
[9858] = true, [15677] = true, [15681] = true, -- Kresky, Graves and O'reely, Booty Bay
[9857] = true, -- Grizzlin, Everlook
[258878] = true, -- Quickcoin, added by WoW Forever
[257037] = true, -- Zelena Favorbreeze, Zephras Isle, added by WoW Forever
}
-- The NPC ID in a creature GUID ("Creature-0-6796-0-1-8661-0000ABCDEF" gives 8661), nil for anything else
function ns.NpcID(guid)
if type(guid) ~= "string" then return nil end
return tonumber(string.match(guid, "^%a+%-%d+%-%d+%-%d+%-%d+%-(%d+)%-"))
end
-- Whether the auctioneer you opened the Auction House at serves the neutral one. An unreadable
-- GUID counts as your faction's.
function ns.IsNeutralAuctioneer(guid)
return NEUTRAL_AUCTIONEERS[ns.NpcID(guid) or 0] == true
end
ns.NEUTRAL_REFUSAL = "Neutral auction house: MarketWall doesn't scan, buy or post here."
-- Why MarketWall can't search, buy or post right now, or nil when it can
function ns.TradeBlocked()
if not ns.ahOpen then return "Open the auction house first." end
if ns.neutralAH then return ns.NEUTRAL_REFUSAL end
return nil
end
function ns.CanTrade()
return ns.TradeBlocked() == nil
end
-- Per-realm, per-faction data table
function ns.R()
if ns.realmData then return ns.realmData end
local db = MarketWallDB
local key = ns.RealmKey()
db.realms[key] = db.realms[key] or {}
local r = db.realms[key]
r.names = r.names or {} -- [itemID] = name
r.watch = r.watch or {} -- [itemID] = { buy=, sell=, added=, cur={t,w,m,q,under,cost} }
r.days = r.days or {} -- [itemID] = { [day] = { wall, lowWall, highWall, units, minPrice } }
r.weeks = r.weeks or {} -- [itemID] = packed weekly history, see ns.PackWeeks in Data.lua
r.snaps = r.snaps or {} -- [itemID] = { {t, wall, minPrice, units}, ... } watched items only
r.pos = r.pos or {} -- [itemID] = { qty=, cost= } bought through the addon
r.buys = r.buys or {} -- { {t, id, qty, unit, total}, ... } every purchase through the addon
r.bait = r.bait or {} -- [itemID] = bait record, see Bait.lua; overwritten each scan
r.recent = r.recent or {} -- item IDs looked up in the search window, newest first
r.supplied = r.supplied or {} -- [itemID] = true with minSupply units at the latest search or full scan, which replaces it
ns.realmData = r
return r
end
function ns.Cfg(key)
local v = MarketWallDB and MarketWallDB.cfg and MarketWallDB.cfg[key]
if v == nil then v = ns.cfgDefaults[key] end
return v
end
function ns.Name(id)
local r = ns.R()
if r.names[id] then return r.names[id] end
local name
if C_Item and C_Item.GetItemInfo then name = C_Item.GetItemInfo(id) elseif GetItemInfo then name = GetItemInfo(id) end
if name then
r.names[id] = name
return name
end
if C_Item and C_Item.RequestLoadItemDataByID then C_Item.RequestLoadItemDataByID(id) end
return "item:" .. id
end
-- The item ID in an item link, nil for anything else
function ns.ItemID(link)
return type(link) == "string" and tonumber(string.match(link, "item:(%d+)")) or nil
end
-- Resolve user text to an item ID: link, number, or name known from scans
function ns.ResolveItem(text)
if not text or text == "" then return nil end
local id = ns.ItemID(text) or tonumber(text)
if id then return id end
local want = string.lower(strtrim(text))
local r = ns.R()
local exact, partial, count = nil, nil, 0
local matches = {}
for itemID, name in pairs(r.names) do
local lname = string.lower(name)
if lname == want then exact = itemID end
if string.find(lname, want, 1, true) then
partial = itemID
count = count + 1
if #matches < 8 then matches[#matches + 1] = name end
end
end
if exact then return exact end
if count == 1 then return partial end
if C_Item and C_Item.GetItemInfoInstant then
local instant = C_Item.GetItemInfoInstant(text)
if instant then return instant end
end
if count > 1 then
table.sort(matches)
return nil, "Several items match: " .. table.concat(matches, ", ") .. (count > #matches and ", ..." or "")
end
return nil, "Item not found. Shift-click the item into the box, or run a full scan first so names are known."
end
local frame = CreateFrame("Frame")
frame:RegisterEvent("ADDON_LOADED")
frame:RegisterEvent("PLAYER_LOGIN")
frame:RegisterEvent("AUCTION_HOUSE_SHOW")
frame:RegisterEvent("AUCTION_HOUSE_CLOSED")
frame:SetScript("OnEvent", function(_, event, arg1)
if event == "ADDON_LOADED" and arg1 == ADDON then
MarketWallDB = MarketWallDB or {}
MarketWallDB.version = MarketWallDB.version or 1
MarketWallDB.cfg = MarketWallDB.cfg or {}
MarketWallDB.realms = MarketWallDB.realms or {}
MarketWallDB.mobs = MarketWallDB.mobs or {} -- kill records per mob, see Mobs.lua
MarketWallDB.disenchants = MarketWallDB.disenchants or {} -- your disenchant results, see DisenchantLog.lua
MarketWallDB.containers = MarketWallDB.containers or {} -- what containers opened into, see Containers.lua
MarketWallDB.ledger = MarketWallDB.ledger or {} -- cash ledger per character, see Ledger.lua
MarketWallDB.sessions = MarketWallDB.sessions or {} -- farm sessions, see Sessions.lua
MarketWallDB.sessionGroups = MarketWallDB.sessionGroups or {}
MarketWallDB.creatures = MarketWallDB.creatures or {}
MarketWallDB.depositRate = MarketWallDB.depositRate or {} -- learned auction deposit rates, see Outlet.lua
MarketWallDB.crafting = MarketWallDB.crafting or {} -- known recipes per character, see Professions.lua
elseif event == "PLAYER_LOGIN" then
ns.realmData = nil
local faction = ns.Faction()
-- an unknown faction would hide the history in a bucket no character reads, so it waits
if faction ~= "Neutral" and ns.AdoptRealmHistory(MarketWallDB.realms, ns.Realm(), ns.RealmKey()) then
Print("Your auction house history so far now belongs to " .. faction .. " on this realm. Characters of the other faction start their own.")
end
ns.R()
if ns.InitTooltip then ns.InitTooltip() end
if ns.InitMinimapButton then ns.InitMinimapButton() end
if ns.Kills then ns.Kills.Start() end
if ns.DisenchantLog then ns.DisenchantLog.Start() end
elseif event == "AUCTION_HOUSE_SHOW" then
ns.ahOpen = true
ns.neutralAH = ns.IsNeutralAuctioneer(UnitGUID and ns.Readable(UnitGUID("npc")))
if ns.OnAuctionHouseShow then ns.OnAuctionHouseShow() end
elseif event == "AUCTION_HOUSE_CLOSED" then
ns.ahOpen = false
ns.neutralAH = false
if ns.OnAuctionHouseClosed then ns.OnAuctionHouseClosed() end
SessionSummary()
end
end)
SLASH_MARKETWALL1 = "/mw"
SLASH_MARKETWALL2 = "/marketwall"
SlashCmdList["MARKETWALL"] = function(msg)
msg = strtrim(msg or "")
local cmd, rest = string.match(msg, "^(%S*)%s*(.-)$")
cmd = string.lower(cmd or "")
if cmd == "add" then
local id, err = ns.ResolveItem(rest)
if id then ns.Watch(id) Print("Watching " .. ns.Name(id)) else Print(err or "Item not found.") end
elseif cmd == "remove" then
local id = ns.ResolveItem(rest)
if id then ns.Unwatch(id) Print("Removed " .. ns.Name(id)) end
elseif cmd == "scan" then
ns.StartWatchScan()
elseif cmd == "full" then
ns.StartFullScan()
elseif cmd == "set" then
local key, value = string.match(rest, "^(%S+)%s+(%S+)$")
if key and ns.cfgDefaults[key] ~= nil then
local default = ns.cfgDefaults[key]
if type(default) == "boolean" then
MarketWallDB.cfg[key] = (value == "true" or value == "on" or value == "1")
elseif type(default) == "string" then
MarketWallDB.cfg[key] = string.lower(value)
else
MarketWallDB.cfg[key] = tonumber(value) or default
end
Print(key .. " = " .. tostring(MarketWallDB.cfg[key]))
ns.UpdateMinimapButton()
ns.Mobs.Invalidate() -- the cut and the mob settings feed mob values
else
Print("Settings:")
for k, d in pairs(ns.cfgDefaults) do
print(" " .. k .. " = " .. tostring(ns.Cfg(k)) .. " (default " .. tostring(d) .. ")")
end
end
elseif cmd == "minimap" then
MarketWallDB.cfg.minimapButton = not ns.Cfg("minimapButton")
ns.UpdateMinimapButton()
Print(ns.Cfg("minimapButton") and "Minimap button shown." or "Minimap button hidden. /mw minimap shows it again.")
elseif cmd == "search" then
ns.ToggleSearch(true)
elseif cmd == "resetpos" then
local id = ns.ResolveItem(rest)
if id then ns.R().pos[id] = nil Print("Position cleared for " .. ns.Name(id)) end
elseif cmd == "probe" then
ns.ProbeCommand(rest)
elseif cmd == "session" then
ns.Sessions.Command(rest)
elseif cmd == "professions" then
ns.Professions.Diagnose()
else
Print("Open the auction house and use the Market tab. Commands:")
print(" /mw add <item link, id or name> watch an item")
print(" /mw remove <item> stop watching")
print(" /mw scan scan the watchlist")
print(" /mw full full market scan")
print(" /mw set <setting> <value> change a setting, /mw set lists them")
print(" /mw resetpos <item> clear your tracked position")
print(" /mw probe start|stop|status record what the client exposes for kills, loot, money and trainers")
print(" /mw search open the item search window")
print(" /mw minimap hide or show the minimap button")
print(" /mw session start <label>|stop|pause|resume a farm session; /mw session shows the running one")
print(" /mw professions check reading recipes from the open profession window")
end
end