wow-addons/QuestShoppingList/QuestShoppingList.lua
rope 9d30c8fed8 Add Quest Shopping List
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 13:58:06 +00:00

463 lines
15 KiB
Lua

-- Quest Shopping List: a panel beside the auction house listing items your
-- active quests still need, with what they'd cost against what the quest pays.
local _, ns = ...
local CALLER = "QuestShoppingList"
local AUCTION_KEEP = 0.95 -- what's left of a sale after the auction house cut
local BIND_ON_PICKUP, BIND_QUEST = 1, 4
local PANEL_WIDTH = 350
local QUEST_ROW_HEIGHT = 36
local ITEM_ROW_HEIGHT = 18
local PRICE_REFRESH_SECONDS = 3
local GREEN, RED, WHITE = "|cff40ff40", "|cffff5050", "|cffffffff"
-- List of { questID, title, level, items = { { id, name, need } } }.
local quests = {}
local panel, content, searchAll
local rowPool = {}
local waitingForItem = {}
local allMissing = {}
-- "Linen Cloth: 3/5" or "3/5 Linen Cloth (Optional)" -> "Linen Cloth"
local function ObjectiveItemName(text)
local name = text:match("^%d+/%d+%s+(.+)$") or text:match("^(.-):?%s*%d+/%d+")
if name then
name = name:gsub("%s+%(Optional%)$", "")
name = name:match("^%s*(.-)%s*$")
if name ~= "" then
return name
end
end
end
local function ReadEntries()
local entries = {}
for i = 1, (C_QuestLog.GetNumQuestLogEntries()) do
local info = C_QuestLog.GetInfo(i)
if info then
entries[#entries + 1] = info
end
end
return entries
end
-- Collapsed headers can hide their quests, so open those long enough to read.
local function ExpandHiddenHeaders(entries)
local reopened
for i = #entries, 1, -1 do
local info, nextInfo = entries[i], entries[i + 1]
if info.isHeader and info.isCollapsed and (not nextInfo or nextInfo.isHeader) then
reopened = reopened or {}
reopened[info.title] = true
ExpandQuestHeader(i)
end
end
return reopened
end
local function CollapseHeaders(titles)
local entries = ReadEntries()
for i = #entries, 1, -1 do
local info = entries[i]
if info.isHeader and titles[info.title] then
CollapseQuestHeader(i)
end
end
end
local function ReadQuestItems(questID)
local items = {}
for _, objective in ipairs(C_QuestLog.GetQuestObjectives(questID) or {}) do
local name = objective.type == "item" and objective.text and ObjectiveItemName(objective.text)
local id = name and ns.itemIDs[name:lower()]
if id and objective.numRequired and objective.numRequired > 0 then
items[#items + 1] = { id = id, name = name, need = objective.numRequired }
end
end
return items
end
local function ScanQuests()
local entries = ReadEntries()
local reopened = ExpandHiddenHeaders(entries)
if reopened then
entries = ReadEntries()
end
local found = {}
for _, info in ipairs(entries) do
if not info.isHeader and not info.isHidden and info.questID and info.questID > 0 then
local items = ReadQuestItems(info.questID)
if #items > 0 then
found[#found + 1] = { questID = info.questID, title = info.title, level = info.level, items = items }
end
end
end
if reopened then
CollapseHeaders(reopened)
end
quests = found
end
local function AuctionPrice(id)
return Auctionator.API.v1.GetAuctionPriceByItemID(CALLER, id)
end
-- Sell price and bind type, or nil until the client has loaded the item.
local function ItemFacts(id)
local sellPrice, _, _, bindType = select(11, C_Item.GetItemInfo(id))
if bindType == nil then
if not waitingForItem[id] then
waitingForItem[id] = true
C_Item.RequestLoadItemDataByID(id)
end
return nil
end
return sellPrice or 0, bindType
end
-- Items not yet loaded stay listed until we know they can't be traded.
local function CanBuy(id)
local _, bindType = ItemFacts(id)
return bindType ~= BIND_ON_PICKUP and bindType ~= BIND_QUEST
end
-- What a reward is worth to you: vendor it, or auction it if it can be traded.
local function RewardItemValue(id, quantity)
local sellPrice, bindType = ItemFacts(id)
if not sellPrice then
return nil
end
local value = sellPrice * quantity
if bindType ~= BIND_ON_PICKUP and bindType ~= BIND_QUEST then
local price = AuctionPrice(id)
if price then
value = math.max(value, math.floor(price * quantity * AUCTION_KEEP))
end
end
return value
end
local function ReadRewards(questID)
local rewards = { money = GetQuestLogRewardMoney(questID) or 0, xp = GetQuestLogRewardXP(questID) or 0, complete = true, details = {} }
rewards.total = rewards.money
if rewards.money > 0 then
rewards.details[#rewards.details + 1] = { "Gold", rewards.money }
end
for i = 1, GetNumQuestLogRewards(questID) or 0 do
local name, _, quantity, _, _, itemID = GetQuestLogRewardInfo(i, questID)
local value = itemID and RewardItemValue(itemID, quantity or 1)
if value then
rewards.total = rewards.total + value
rewards.details[#rewards.details + 1] = { name, value }
else
rewards.complete = false
end
end
-- You only get one of the choices, so count the most valuable.
local bestName, bestValue
for i = 1, GetNumQuestLogChoices(questID) or 0 do
local name, _, quantity, _, _, itemID = GetQuestLogChoiceInfo(i, questID)
local value = itemID and RewardItemValue(itemID, quantity or 1)
if not value then
rewards.complete = false
elseif not bestValue or value > bestValue then
bestName, bestValue = name, value
end
end
if bestValue then
rewards.total = rewards.total + bestValue
rewards.details[#rewards.details + 1] = { "Best choice: " .. bestName, bestValue }
end
return rewards
end
-- Items still to buy for a quest, and what they'd cost. `owned` is shared by
-- all quests in a pass, so one stack isn't counted towards two of them.
local function ReadShopping(quest, owned)
local shopping = { items = {}, cost = 0, costKnown = true }
for _, item in ipairs(quest.items) do
owned[item.id] = owned[item.id] or C_Item.GetItemCount(item.id, true) or 0
local have = math.min(owned[item.id], item.need)
owned[item.id] = owned[item.id] - have
local missing = item.need - have
if missing > 0 and CanBuy(item.id) then
local price = AuctionPrice(item.id)
local itemCost = price and price * missing
if itemCost then
shopping.cost = shopping.cost + itemCost
else
shopping.costKnown = false
end
shopping.items[#shopping.items + 1] = {
id = item.id, name = item.name, have = have, need = item.need, missing = missing, cost = itemCost,
}
end
end
return shopping
end
local function Money(copper)
return GetMoneyString(copper)
end
local function Summary(shopping, rewards)
local costText = shopping.costKnown and Money(shopping.cost)
or (shopping.cost > 0 and Money(shopping.cost) .. " + ?" or "?")
local parts = {}
if rewards.total > 0 then
parts[#parts + 1] = Money(rewards.total)
end
if rewards.xp > 0 then
parts[#parts + 1] = rewards.xp .. " XP"
end
local rewardText = #parts > 0 and table.concat(parts, " + ") or "none"
local color = WHITE
if shopping.costKnown and rewards.complete then
color = rewards.total >= shopping.cost and GREEN or RED
end
return color .. "Buy " .. costText .. " | Reward " .. rewardText .. "|r"
end
local function Search(items)
local byName, terms = {}, {}
for _, item in ipairs(items) do
local term = byName[item.name]
if term then
term.quantity = term.quantity + item.missing
else
term = { searchString = item.name, isExact = true, quantity = item.missing }
byName[item.name] = term
terms[#terms + 1] = term
end
end
if #terms > 0 then
Auctionator.API.v1.MultiSearchAdvanced(CALLER, terms)
end
end
local function OnRowEnter(row)
if not row.details then
return
end
GameTooltip:SetOwner(row, "ANCHOR_RIGHT")
GameTooltip:SetText(row.tooltipTitle)
for _, detail in ipairs(row.details) do
GameTooltip:AddDoubleLine(detail[1], Money(detail[2]), 1, 1, 1, 1, 1, 1)
end
GameTooltip:Show()
end
local function OnRowLeave()
GameTooltip:Hide()
end
local function OnRowClick(row)
Search(row.items)
end
local function GetRow(index)
local row = rowPool[index]
if row then
return row
end
row = CreateFrame("Button", nil, content)
row:SetHighlightTexture("Interface\\QuestFrame\\UI-QuestTitleHighlight", "ADD")
row.right = row:CreateFontString(nil, "ARTWORK", "GameFontNormalSmall")
row.right:SetPoint("TOPRIGHT", row, "TOPRIGHT", -4, -3)
row.right:SetJustifyH("RIGHT")
row.left = row:CreateFontString(nil, "ARTWORK", "GameFontNormalSmall")
row.left:SetPoint("TOPLEFT", row, "TOPLEFT", 4, -3)
row.left:SetPoint("RIGHT", row.right, "LEFT", -4, 0)
row.left:SetJustifyH("LEFT")
row.sub = row:CreateFontString(nil, "ARTWORK", "GameFontNormalSmall")
row.sub:SetPoint("TOPLEFT", row.left, "BOTTOMLEFT", 0, -3)
row.sub:SetPoint("RIGHT", row, "RIGHT", -4, 0)
row.sub:SetJustifyH("LEFT")
row:SetScript("OnClick", OnRowClick)
row:SetScript("OnEnter", OnRowEnter)
row:SetScript("OnLeave", OnRowLeave)
rowPool[index] = row
return row
end
local function PlaceRow(row, y, height)
row:ClearAllPoints()
row:SetPoint("TOPLEFT", content, "TOPLEFT", 0, -y)
row:SetPoint("RIGHT", content, "RIGHT", 0, 0)
row:SetHeight(height)
row:Show()
return y + height
end
local function Render()
if not panel then
return
end
local used, y = 0, 0
allMissing = {}
local owned = {}
for _, quest in ipairs(quests) do
local shopping = ReadShopping(quest, owned)
if #shopping.items > 0 then
local rewards = ReadRewards(quest.questID)
used = used + 1
local header = GetRow(used)
header.rowKind = "quest"
header.items = shopping.items
header.details = rewards.details
header.tooltipTitle = quest.title
header.left:SetText((quest.level and "[" .. quest.level .. "] " or "") .. quest.title)
header.right:SetText("")
header.sub:SetText(Summary(shopping, rewards))
header.sub:Show()
y = PlaceRow(header, y, QUEST_ROW_HEIGHT)
for _, item in ipairs(shopping.items) do
allMissing[#allMissing + 1] = item
used = used + 1
local row = GetRow(used)
row.rowKind = "item"
row.items = { item }
row.details = nil
row.left:SetText(WHITE .. " " .. item.name .. "|r")
row.right:SetText(WHITE .. item.have .. " / " .. item.need .. " " .. (item.cost and Money(item.cost) or "no price") .. "|r")
row.sub:Hide()
y = PlaceRow(row, y, ITEM_ROW_HEIGHT)
end
end
end
for i = used + 1, #rowPool do
rowPool[i]:Hide()
end
content:SetHeight(math.max(y, 1))
if used == 0 then
panel:Hide()
end
return used
end
local function CreatePanel(parent)
panel = CreateFrame("Frame", "QuestShoppingListPanel", parent, "BackdropTemplate")
panel:SetPoint("TOPLEFT", parent, "TOPRIGHT", 2, 0)
panel:SetPoint("BOTTOMLEFT", parent, "BOTTOMRIGHT", 2, 0)
panel:SetWidth(PANEL_WIDTH)
panel:SetBackdrop({
bgFile = "Interface\\Tooltips\\UI-Tooltip-Background",
edgeFile = "Interface\\Tooltips\\UI-Tooltip-Border",
tile = true, tileSize = 16, edgeSize = 16,
insets = { left = 4, right = 4, top = 4, bottom = 4 },
})
panel:SetBackdropColor(0, 0, 0, 0.92)
local title = panel:CreateFontString(nil, "ARTWORK", "GameFontNormalLarge")
title:SetPoint("TOPLEFT", panel, "TOPLEFT", 12, -10)
title:SetText("Quest Shopping")
local close = CreateFrame("Button", nil, panel, "UIPanelCloseButton")
close:SetPoint("TOPRIGHT", panel, "TOPRIGHT", 0, 0)
searchAll = CreateFrame("Button", nil, panel, "UIPanelButtonTemplate")
searchAll:SetSize(100, 22)
searchAll:SetPoint("TOPRIGHT", close, "TOPLEFT", -2, -4)
searchAll:SetText("Search all")
searchAll:SetScript("OnClick", function()
Search(allMissing)
end)
local scroll = CreateFrame("ScrollFrame", nil, panel, "UIPanelScrollFrameTemplate")
scroll:SetPoint("TOPLEFT", panel, "TOPLEFT", 8, -36)
scroll:SetPoint("BOTTOMRIGHT", panel, "BOTTOMRIGHT", -28, 8)
content = CreateFrame("Frame", nil, scroll)
content:SetSize(PANEL_WIDTH - 36, 1)
scroll:SetScrollChild(content)
C_Timer.NewTicker(PRICE_REFRESH_SECONDS, function()
if panel:IsVisible() then
Render()
end
end)
end
local function OpenPanel()
local parent = AuctionHouseFrame or AuctionFrame
if not parent then
return
end
if not panel then
CreatePanel(parent)
end
ScanQuests()
if Render() > 0 then
panel:Show()
end
end
local queuedScan, queuedRender = false, false
local function QueueRender()
if not queuedRender then
queuedRender = true
C_Timer.After(0.2, function()
queuedRender = false
if panel and panel:IsVisible() then
Render()
end
end)
end
end
local function QueueScan()
if not queuedScan then
queuedScan = true
C_Timer.After(0.5, function()
queuedScan = false
if panel and panel:IsVisible() then
ScanQuests()
Render()
end
end)
end
end
local frame = CreateFrame("Frame")
for _, event in ipairs({ "AUCTION_HOUSE_SHOW", "BAG_UPDATE_DELAYED", "ITEM_DATA_LOAD_RESULT", "QUEST_ACCEPTED", "QUEST_REMOVED", "QUEST_TURNED_IN" }) do
if not C_EventUtils or C_EventUtils.IsEventValid(event) then
frame:RegisterEvent(event)
end
end
frame:SetScript("OnEvent", function(_, event, arg1)
if event == "AUCTION_HOUSE_SHOW" then
-- The auction window may not exist until the next frame.
C_Timer.After(0, OpenPanel)
elseif event == "ITEM_DATA_LOAD_RESULT" then
if waitingForItem[arg1] then
waitingForItem[arg1] = nil
QueueRender()
end
elseif event == "BAG_UPDATE_DELAYED" then
QueueRender()
else
QueueScan()
end
end)
-- /qsl reopens the panel after closing it.
SLASH_QUESTSHOPPINGLIST1 = "/qsl"
SlashCmdList.QUESTSHOPPINGLIST = function()
if (AuctionHouseFrame and AuctionHouseFrame:IsVisible()) or (AuctionFrame and AuctionFrame:IsVisible()) then
OpenPanel()
else
DEFAULT_CHAT_FRAME:AddMessage("|cffffd100QuestShoppingList:|r open the auction house first")
end
end