Skip to content

Commit 7d2c883

Browse files
committed
quickbuy v1
1 parent a785d4f commit 7d2c883

File tree

3 files changed

+244
-2
lines changed

3 files changed

+244
-2
lines changed

Init/Init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,8 @@ function CraftSim.INIT:PLAYER_LOGIN()
483483
elseif command == "resetdb" then
484484
CraftSimDB = nil
485485
C_UI.Reload()
486+
elseif command == "quickbuy" then
487+
CraftSim.CRAFTQ:AuctionatorQuickBuy()
486488
else
487489
-- open options if any other command or no command is given
488490
Settings.OpenToCategory(CraftSim.OPTIONS.category.ID)

Modules/CraftQueue/CraftQueue.lua

+238-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,23 @@ local GUTIL = CraftSim.GUTIL
1010
local L = CraftSim.UTIL:GetLocalizer()
1111
local f = GUTIL:GetFormatter()
1212

13+
---@enum CraftSim.CRAFTQ.QB_STATUS
14+
local QB_STATUS = {
15+
INIT = "INIT",
16+
SEARCH_READY = "SEARCH_READY",
17+
RESULT_LIST_READY = "RESULT_LIST_READY",
18+
BUY_READY = "BUY_READY",
19+
SEARCH_STARTED = "SEARCH_STARTED",
20+
CONFIRM_START = "CONFIRM_START",
21+
CONFIRM_AWAIT = "CONFIRM_AWAIT",
22+
CONFIRM_READY = "CONFIRM_READY",
23+
PURCHASE_AWAIT = "PURCHASE_AWAIT",
24+
}
25+
26+
1327
---@class CraftSim.CRAFTQ : Frame
1428
CraftSim.CRAFTQ = GUTIL:CreateRegistreeForEvents({ "TRADE_SKILL_ITEM_CRAFTED_RESULT", "COMMODITY_PURCHASE_SUCCEEDED",
29+
"AUCTION_HOUSE_THROTTLED_SYSTEM_READY",
1530
"NEW_RECIPE_LEARNED", "CRAFTINGORDERS_CLAIMED_ORDER_UPDATED", "CRAFTINGORDERS_CLAIMED_ORDER_REMOVED" })
1631

1732
---@type CraftSim.CraftQueue
@@ -27,7 +42,21 @@ CraftSim.CRAFTQ.CraftSimCalledCraftRecipe = false
2742
--- if canCraft and such functions are not called by craftqueue it should be nil
2843
CraftSim.CRAFTQ.itemCountCache = nil
2944

30-
local systemPrint = print
45+
--- used to cache data for auctionator quick buy macro
46+
CraftSim.CRAFTQ.quickBuyCache = {
47+
---@type CraftSim.CRAFTQ.QB_STATUS
48+
status = QB_STATUS.INIT,
49+
---@type table<string, table> Auctionator Search String -> TableBuilder Row
50+
resultRows = {},
51+
---@type table<string, boolean>
52+
boughtSearchStrings = {},
53+
---@type string Auctionator SearchString
54+
currentSearchString = nil,
55+
purchasePending = false, -- listen for commoditybought event / item bought event
56+
purchaseConfirmed = false,
57+
}
58+
59+
local printQB = CraftSim.DEBUG:RegisterDebugID("Modules.CraftQueue.AuctionatorQuickBuy")
3160
local print = CraftSim.DEBUG:RegisterDebugID("Modules.CraftQueue")
3261

3362
--- cache for OnConfirmCommoditiesPurchase -> COMMODITY_PURCHASE_SUCCEEDED flow
@@ -47,9 +76,21 @@ function CraftSim.CRAFTQ:OnConfirmCommoditiesPurchase(itemID, boughtQuantity)
4776
item = Item:CreateFromItemID(itemID),
4877
quantity = boughtQuantity
4978
}
79+
80+
81+
-- -- continue with
82+
-- local qbCache = self.quickBuyCache
83+
-- if qbCache.status
84+
-- qbCache.status = QB_STATUS.CONFIRM_READY
85+
self.quickBuyCache.purchaseConfirmed = true
86+
--self:AuctionatorQuickBuy() -- wil trigger confirm await
5087
end
5188

5289
function CraftSim.CRAFTQ:COMMODITY_PURCHASE_SUCCEEDED()
90+
-- reset purchase pending in qbCache
91+
printQB("- " .. f.l("COMMODITY_PURCHASE_SUCCEEDED"))
92+
CraftSim.CRAFTQ.quickBuyCache.purchasePending = false
93+
5394
if not select(2, C_AddOns.IsAddOnLoaded(CraftSim.CONST.SUPPORTED_PRICE_API_ADDONS[2])) then
5495
return -- do not need if Auctionator not loaded
5596
end
@@ -63,7 +104,6 @@ function CraftSim.CRAFTQ:COMMODITY_PURCHASE_SUCCEEDED()
63104
print("commodity purchase successfull")
64105
print("item: " .. tostring(purchasedItem.item:GetItemLink()))
65106
print("quantity: " .. tostring(purchasedItem.quantity))
66-
67107
local success
68108
local result
69109
local shoppingListName = CraftSim.CONST.AUCTIONATOR_SHOPPING_LIST_QUEUE_NAME
@@ -620,6 +660,9 @@ function CraftSim.CRAFTQ.CreateAuctionatorShoppingList()
620660

621661
CraftSim.CRAFTQ:DeleteAllCraftSimShoppingLists()
622662

663+
-- reset quick buy
664+
CraftSim.CRAFTQ:ResetQuickBuyCache()
665+
623666
CraftSim.DEBUG:StartProfiling("CreateAuctionatorShopping")
624667
local reagentMap = {}
625668
-- create a map of all used reagents in the queue and their quantity
@@ -984,3 +1027,196 @@ function CraftSim.CRAFTQ:NEW_RECIPE_LEARNED(recipeID)
9841027

9851028
self.UI:UpdateDisplay()
9861029
end
1030+
1031+
--- Magic Command for one-button shopping list buying
1032+
--- Currently only works for craftsim shopping list due to relying on bought item removal
1033+
--- TODO: fix auto removal for reagents like darkmoon decks
1034+
function CraftSim.CRAFTQ:AuctionatorQuickBuy()
1035+
local print = printQB
1036+
1037+
print("AuctionatorQuickBuy", false, true)
1038+
1039+
local qbCache = self.quickBuyCache
1040+
1041+
if not AuctionatorShoppingFrame then
1042+
CraftSim.DEBUG:SystemPrint(f.l("CraftSim: ") .. f.r("Quick Buy only available for Auctionator Shopping Lists"))
1043+
return
1044+
end
1045+
1046+
local listManager = Auctionator.Shopping.ListManager
1047+
local listName = "CraftSim CraftQueue"
1048+
1049+
local listsContainer = AuctionatorShoppingFrame.ListsContainer
1050+
local resultsList = AuctionatorShoppingFrame.ResultsListing
1051+
local buyButton = AuctionatorBuyCommodityFrame.DetailsContainer.BuyButton
1052+
1053+
local commodityFrame = AuctionatorBuyCommodityFrame
1054+
local confirmationDialog = commodityFrame.FinalConfirmationDialog
1055+
1056+
---@param value CraftSim.CRAFTQ.QB_STATUS
1057+
---@return boolean
1058+
local function status(value)
1059+
return qbCache.status == value
1060+
end
1061+
1062+
1063+
---@param value CraftSim.CRAFTQ.QB_STATUS
1064+
local function set(value)
1065+
print("- Setting Status: " .. tostring(value))
1066+
qbCache.status = value
1067+
end
1068+
1069+
local function getResultSearchString(itemID)
1070+
local item = Item:CreateFromItemID(itemID)
1071+
local itemLink = item:GetItemLink()
1072+
local qualityID = GUTIL:GetQualityIDFromLink(itemLink)
1073+
local searchTerm = {
1074+
searchString = item:GetItemName(),
1075+
isExact = true,
1076+
tier = qualityID
1077+
}
1078+
return Auctionator.API.v1.ConvertToSearchString("CraftSim", searchTerm)
1079+
end
1080+
1081+
local function mapSearchResultRows(itemSearchStrings)
1082+
wipe(qbCache.resultRows)
1083+
-- map rows to shopping list items
1084+
for _, searchString in ipairs(itemSearchStrings) do
1085+
local row = GUTIL:Find(resultsList.tableBuilder.rows, function(row)
1086+
local itemID = row.rowData.itemKey.itemID
1087+
return GUTIL:StringStartsWith(searchString, getResultSearchString(itemID))
1088+
end)
1089+
1090+
if row then
1091+
qbCache.resultRows[searchString] = row
1092+
end
1093+
end
1094+
end
1095+
1096+
local function matchSearchResultRows(itemSearchStrings)
1097+
return GUTIL:Every(itemSearchStrings, function(searchString)
1098+
return qbCache.resultRows[searchString] ~= nil
1099+
end)
1100+
end
1101+
1102+
local listIndex = listManager:GetIndexForName(listName)
1103+
1104+
if not listIndex then
1105+
set(QB_STATUS.INIT)
1106+
return
1107+
end
1108+
1109+
local list = listManager:GetByIndex(listIndex)
1110+
local allItemSearchStrings = list:GetAllItems()
1111+
local numItems = allItemSearchStrings
1112+
1113+
if numItems == 0 then
1114+
set(QB_STATUS.INIT)
1115+
print("- No Items Left")
1116+
return
1117+
end
1118+
1119+
-- get item that was not bought yet
1120+
local firstShoppingListSearchString = GUTIL:Find(list.data.items, function(searchString)
1121+
return not qbCache.boughtSearchStrings[searchString]
1122+
end)
1123+
1124+
print("- STATUS: " .. tostring(qbCache.status))
1125+
--print("- firstShoppingListSearchString: " .. tostring(firstShoppingListSearchString))
1126+
1127+
1128+
-- if qbCache.purchasePending then
1129+
-- print(f.r("- Purchase Pending"))
1130+
-- return
1131+
-- end
1132+
1133+
1134+
if not firstShoppingListSearchString then
1135+
print("- All bought")
1136+
self:ResetQuickBuyCache()
1137+
return
1138+
end
1139+
1140+
if status(QB_STATUS.INIT) then
1141+
set(QB_STATUS.SEARCH_READY)
1142+
if not listsContainer:IsListExpanded(list) then
1143+
listsContainer:ExpandList(list)
1144+
end
1145+
end
1146+
1147+
if status(QB_STATUS.SEARCH_READY) then
1148+
mapSearchResultRows(allItemSearchStrings)
1149+
if not matchSearchResultRows(allItemSearchStrings) then
1150+
print("- No Match with Results: DoSearch")
1151+
AuctionatorShoppingFrame:DoSearch(allItemSearchStrings)
1152+
end
1153+
set(QB_STATUS.SEARCH_STARTED)
1154+
end
1155+
1156+
if status(QB_STATUS.SEARCH_STARTED) then
1157+
-- check if result row items match with shopping list if no do nothing, if yes continue
1158+
mapSearchResultRows(allItemSearchStrings)
1159+
1160+
if matchSearchResultRows(allItemSearchStrings) then
1161+
set(QB_STATUS.RESULT_LIST_READY)
1162+
end
1163+
end
1164+
1165+
if firstShoppingListSearchString and status(QB_STATUS.RESULT_LIST_READY) then
1166+
local resultRow = qbCache.resultRows[firstShoppingListSearchString]
1167+
1168+
if not resultRow then
1169+
print("Result Row not found in result list: " .. tostring(firstShoppingListSearchString))
1170+
return
1171+
end
1172+
1173+
resultRow:OnClick()
1174+
qbCache.currentSearchString = firstShoppingListSearchString
1175+
set(QB_STATUS.BUY_READY)
1176+
return
1177+
end
1178+
1179+
if status(QB_STATUS.BUY_READY) then
1180+
if buyButton:IsVisible() and buyButton:IsEnabled() then
1181+
buyButton:Click()
1182+
set(QB_STATUS.CONFIRM_START)
1183+
return
1184+
else
1185+
return
1186+
end
1187+
end
1188+
1189+
if status(QB_STATUS.CONFIRM_START) and confirmationDialog:IsVisible() and not confirmationDialog.purchasePending then
1190+
print("- " .. f.g("Confirm Purchase"))
1191+
qbCache.purchasePending = true
1192+
qbCache.purchaseConfirmed = false
1193+
1194+
confirmationDialog:ConfirmPurchase() -- will trigger event
1195+
set(QB_STATUS.CONFIRM_AWAIT)
1196+
end
1197+
1198+
if status(QB_STATUS.CONFIRM_AWAIT) and qbCache.purchaseConfirmed then
1199+
qbCache.boughtSearchStrings[qbCache.currentSearchString] = true
1200+
set(QB_STATUS.PURCHASE_AWAIT)
1201+
return
1202+
end
1203+
1204+
if status(QB_STATUS.PURCHASE_AWAIT) and not qbCache.purchasePending then
1205+
set(QB_STATUS.RESULT_LIST_READY)
1206+
self:AuctionatorQuickBuy() -- to instantly click next item
1207+
end
1208+
end
1209+
1210+
function CraftSim.CRAFTQ:AUCTION_HOUSE_THROTTLED_SYSTEM_READY()
1211+
1212+
end
1213+
1214+
function CraftSim.CRAFTQ:ResetQuickBuyCache()
1215+
local qbCache = self.quickBuyCache
1216+
qbCache.status = QB_STATUS.INIT
1217+
qbCache.currentSearchString = nil
1218+
qbCache.purchasePending = false
1219+
qbCache.purchaseConfirmed = false
1220+
wipe(qbCache.boughtSearchStrings)
1221+
wipe(qbCache.resultRows)
1222+
end

Modules/Debug/UI.lua

+4
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ function CraftSim.DEBUG.UI:InitControlPanel(debugFrame)
212212
CraftSim.DEBUG:InspectTable(nameMap, "CraftQueueItems", true)
213213
end)
214214

215+
craftQueue:CreateButton("Inspect CraftQueue.QuickBuyCache", function()
216+
CraftSim.DEBUG:InspectTable(CraftSim.CRAFTQ.quickBuyCache, "QuickBuyCache", true)
217+
end)
218+
215219
local craftLog = rootDescription:CreateButton("CraftLog")
216220

217221
craftLog:CreateButton("Inspect CraftSessionData", function()

0 commit comments

Comments
 (0)