Skip to content

Commit 6e73d78

Browse files
vaisestLocalIdentity
andauthored
Fix buy similar popup not updating url and persist selections (#2339)
* Fix edit controls not updating buy similar url * Fix league/realm selectors not rebuilding url and make them persistent * Add basic tests for findTradeIdOption * Fix crash on missing socket in viewer * Fix changing dropdowns not updating trade URL Changing the dropdowns wasn't auto updating the trade URL --------- Co-authored-by: LocalIdentity <localidentity2@gmail.com>
1 parent 9065278 commit 6e73d78

4 files changed

Lines changed: 114 additions & 13 deletions

File tree

spec/System/TestCompareBuySimilar_spec.lua

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,73 @@ Implicits: 1
8686
assert.equal(50, entries[2].value)
8787
end)
8888
end)
89+
90+
describe("popup URL controls", function()
91+
local originalCopy
92+
local originalOpenURL
93+
local originalFetchLeagues
94+
local copiedUrl
95+
local searchEnv
96+
97+
before_each(function()
98+
newBuild()
99+
local requests = build.itemsTab.tradeQuery.tradeQueryRequests
100+
originalFetchLeagues = requests.FetchLeagues
101+
requests.FetchLeagues = function(_, realm, callback)
102+
callback({ "Test League", "Standard" })
103+
end
104+
end)
105+
106+
after_each(function()
107+
build.itemsTab.tradeQuery.tradeQueryRequests.FetchLeagues = originalFetchLeagues
108+
searchEnv.Copy = originalCopy
109+
searchEnv.OpenURL = originalOpenURL
110+
bs.lastRealmIdx = nil
111+
bs.lastLeagueIdx = nil
112+
bs.lastListedIndex = nil
113+
main:ClosePopup()
114+
end)
115+
116+
local function openPopup()
117+
local item = new("Item", "Rarity: Rare\nTest Ring\nRuby Ring\nImplicits: 0\n+50 to maximum Life")
118+
bs.openPopup(item, "Ring", build)
119+
local controls = main.popups[1].controls
120+
searchEnv = getfenv(controls.search.onClick)
121+
originalCopy = originalCopy or searchEnv.Copy
122+
originalOpenURL = originalOpenURL or searchEnv.OpenURL
123+
searchEnv.Copy = function(url) copiedUrl = url end
124+
searchEnv.OpenURL = function() end
125+
return controls
126+
end
127+
128+
it("rebuilds the URL when league and listed status change", function()
129+
local controls = openPopup()
130+
controls.search.onClick()
131+
local initialUrl = copiedUrl
132+
133+
controls.leagueDrop:SetSel(2)
134+
controls.search.onClick()
135+
assert.not_equal(initialUrl, copiedUrl)
136+
assert.is_truthy(copiedUrl:find("/Standard?", 1, true))
137+
local standardUrl = copiedUrl
138+
139+
controls.listedDrop:SetSel(4)
140+
controls.search.onClick()
141+
assert.not_equal(standardUrl, copiedUrl)
142+
assert.is_truthy(copiedUrl:find("any", 1, true))
143+
end)
144+
145+
it("persists popup selector choices", function()
146+
local controls = openPopup()
147+
controls.realmDrop.selFunc(1, "PoE2")
148+
controls.leagueDrop:SetSel(2)
149+
controls.listedDrop:SetSel(4)
150+
main:ClosePopup()
151+
152+
controls = openPopup()
153+
assert.equal(1, bs.lastRealmIdx)
154+
assert.equal("Standard", controls.leagueDrop:GetSelValue())
155+
assert.equal("Any", controls.listedDrop:GetSelValue())
156+
end)
157+
end)
89158
end)

spec/System/TestTradeHelpers_spec.lua

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,4 +109,21 @@ describe("TradeHelpers trade hash matching", function()
109109
assert.equal(7, value)
110110
end)
111111
end)
112+
describe("findTradeIdOption", function()
113+
it("matches a '#'-valued option and returns its value", function()
114+
local tradeId, value = tradeHelpers.findTradeIdOption("+2 to Level of all Arc Skills", "explicit")
115+
assert.equal("explicit.stat_448592698|98", tradeId)
116+
assert.equal(2, value)
117+
end)
118+
119+
it("matches an exact-text option and returns no value", function()
120+
local tradeId, value = tradeHelpers.findTradeIdOption("Allocates Abasement", "enchant")
121+
assert.equal("enchant.stat_2954116742|7338", tradeId)
122+
assert.is_nil(value)
123+
end)
124+
125+
it("returns nil for an unmatchable line", function()
126+
assert.is_nil(tradeHelpers.findTradeIdOption("+100 to IQ", "explicit"))
127+
end)
128+
end)
112129
end)

src/Classes/CompareBuySimilar.lua

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -330,14 +330,20 @@ function M.openPopup(item, slotName, primaryBuild)
330330
if not tradeQueryRequests then
331331
tradeQueryRequests = new("TradeQueryRequests")
332332
end
333+
local function rebuildUrl()
334+
local result = buildURL(item, slotName, controls, modEntries, defenceEntries, isUnique)
335+
uri = result
336+
end
333337

334338
-- Helper to fetch and populate leagues for a given realm API id
335339
local function fetchLeaguesForRealm(realmApiId)
340+
local lastIdx = M.lastLeagueIdx
336341
controls.leagueDrop:SetList({"Loading..."})
337342
controls.leagueDrop.selIndex = 1
338343
tradeQueryRequests:FetchLeagues(realmApiId, function(leagues, errMsg)
339344
if errMsg then
340345
controls.leagueDrop:SetList({"Standard"})
346+
rebuildUrl()
341347
return
342348
end
343349
local leagueList = {}
@@ -362,6 +368,10 @@ function M.openPopup(item, slotName, primaryBuild)
362368
break
363369
end
364370
end
371+
if lastIdx then
372+
controls.leagueDrop:SetSel(lastIdx)
373+
end
374+
rebuildUrl()
365375
end)
366376
end
367377

@@ -370,31 +380,36 @@ function M.openPopup(item, slotName, primaryBuild)
370380
controls.realmDrop = new("DropDownControl", {"LEFT", controls.realmLabel, "RIGHT"}, {4, 0, 80, 20}, {"PoE2"}, function(index, value)
371381
local realmApiId = REALM_API_IDS[value] or "poe2"
372382
fetchLeaguesForRealm(realmApiId)
383+
rebuildUrl()
384+
M.lastRealmIdx = index
373385
end)
386+
if M.lastRealmIdx then
387+
controls.realmDrop:SetSel(M.lastRealmIdx, true)
388+
end
374389
controls.realmDrop.disabled = true
375390

376391
-- League dropdown
377392
controls.leagueLabel = new("LabelControl", {"LEFT", controls.realmDrop, "RIGHT"}, {12, 0, 0, 16}, "^7League:")
378393
controls.leagueDrop = new("DropDownControl", {"LEFT", controls.leagueLabel, "RIGHT"}, {4, 0, 160, 20}, {"Loading..."}, function(index, value)
379-
-- League selection stored in the dropdown itself
394+
M.lastLeagueIdx = index
395+
rebuildUrl()
380396
end)
381397
controls.leagueDrop.enabled = function() return #controls.leagueDrop.list > 0 and controls.leagueDrop.list[1] ~= "Loading..." end
382398

383399
-- Listed status dropdown
384400
controls.listedDrop = new("DropDownControl", {"TOPRIGHT", nil, "TOPRIGHT"}, {-leftMargin, ctrlY, 242, 20}, LISTED_STATUS_LABELS, function(index, value)
385-
-- Listed status selection stored in the dropdown itself
401+
M.lastListedIndex = index
402+
rebuildUrl()
386403
end)
404+
if M.lastListedIndex then
405+
controls.listedDrop:SetSel(M.lastListedIndex, true)
406+
end
387407
controls.listedLabel = new("LabelControl", {"RIGHT", controls.listedDrop, "LEFT"}, {-4, 0, 0, 16}, "^7Listed:")
388408

389-
-- Fetch initial leagues for default realm
390-
fetchLeaguesForRealm("poe2")
409+
-- Fetch initial leagues for the selected realm
410+
fetchLeaguesForRealm(REALM_API_IDS[controls.realmDrop:GetSelValue()] or "poe2")
391411
ctrlY = ctrlY + rowHeight + 4
392412

393-
local function rebuildUrl()
394-
local result = buildURL(item, slotName, controls, modEntries, defenceEntries, isUnique)
395-
uri = result
396-
end
397-
398413

399414
if isUnique then
400415
-- Unique item name label
@@ -483,8 +498,8 @@ function M.openPopup(item, slotName, primaryBuild)
483498
-- when the trade site has a dropdown for the value, we opt to disable
484499
-- the inputs as they are numeric
485500
if not (entry.isOption or entry.needsExactValue) and entry.value then
486-
controls[prefix .. "Min"] = tradeHelpers.newPlainNumericEdit(nil, { minFieldX - popupWidth / 2, controlYPos, fieldW, fieldH }, entry.value ~= 0 and tostring(entry.value) or "", "Min", 8, rebuildUrl)
487-
controls[prefix .. "Max"] = tradeHelpers.newPlainNumericEdit(nil, { maxFieldX - popupWidth / 2, controlYPos, fieldW, fieldH }, "", "Max", 8, rebuildUrl)
501+
controls[prefix .. "Min"] = tradeHelpers.newPlainNumericEdit(nil, { minFieldX - popupWidth / 2, controlYPos, fieldW, fieldH }, entry.value ~= 0 and tostring(entry.value) or "", "Min", 8, nil, rebuildUrl)
502+
controls[prefix .. "Max"] = tradeHelpers.newPlainNumericEdit(nil, { maxFieldX - popupWidth / 2, controlYPos, fieldW, fieldH }, "", "Max", 8, nil, rebuildUrl)
488503
if not canSearch then
489504
controls[prefix .. "Min"].enabled = function() return false end
490505
controls[prefix .. "Max"].enabled = function() return false end

src/Modules/ItemSlotHelper.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ local M = {}
88
---@param w integer
99
---@param h integer
1010
function M.DrawViewer(itemsTab, nodeId, x, y, w, h)
11+
local node = itemsTab.build.spec.nodes[nodeId]
12+
if not node then return end
1113
SetDrawLayer(nil, 15)
1214
SetDrawColor(1, 1, 1)
1315

1416
local borderWidth = 1
1517
DrawImage(nil, x, y, w + 2 * borderWidth, h + 2 * borderWidth)
1618

1719
local viewer = itemsTab.socketViewer
18-
local node = itemsTab.build.spec.nodes[nodeId]
19-
2020
viewer.zoom = 17
2121

2222
local viewPortSize = math.min(w, h)

0 commit comments

Comments
 (0)