Skip to content

Commit d795c9f

Browse files
committed
Buy similar: fix matching mods with dropdown options, and fix inverted mod matching
1 parent ea6a331 commit d795c9f

2 files changed

Lines changed: 53 additions & 32 deletions

File tree

src/Classes/CompareBuySimilar.lua

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ local function buildURL(item, slotName, controls, modEntries, defenceEntries, is
129129
if entry.tradeId and controls[prefix .. "Check"] and controls[prefix .. "Check"].state then
130130

131131
local filter = { id = entry.tradeId }
132-
if entry.valueIsOption then
132+
if entry.options then
133133
filter.value = { option = entry.value }
134134
elseif entry.value then
135135
local minVal = tonumber(controls[prefix .. "Min"].buf)
@@ -215,26 +215,9 @@ function M.openPopup(item, slotName, primaryBuild)
215215
-- Use range-resolved text for matching
216216
local resolvedLine = (modLine.range and itemLib.applyRange(modLine.line, modLine.range, modLine.valueScalar)) or modLine.line
217217

218-
-- special case: cluster enchantment
219-
local identifier, value
220-
-- whether to add a min and max, or an option field
221-
local valueIsOption = false
222-
local clusterMatch = resolvedLine:match("Added Small Passive Skills grant: (.+)")
223-
if clusterMatch then
224-
identifier = "enchant.stat_3948993189"
225-
for _, v in pairs(item.clusterJewel.skills) do
226-
for _, stat in ipairs(v.stats) do
227-
if stat == clusterMatch then
228-
value = v.id
229-
valueIsOption = true
230-
goto outer
231-
end
232-
end
233-
end
234-
::outer::
235-
else
236-
local tradeHash = tradeHelpers.findTradeHash(item, resolvedLine, source.type)
237-
identifier = tradeHash and string.format("%s.stat_%s", source.type, tradeHash)
218+
local tradeHash, options, value = tradeHelpers.findTradeHash(item, resolvedLine, source.type)
219+
local identifier = tradeHash and string.format("%s.stat_%s", source.type, tradeHash)
220+
if not options then
238221
value = tradeHelpers.modLineValue(resolvedLine)
239222
end
240223

@@ -244,7 +227,7 @@ function M.openPopup(item, slotName, primaryBuild)
244227
formatted = formatted,
245228
tradeId = identifier,
246229
value = value,
247-
valueIsOption = valueIsOption,
230+
options = options,
248231
modType = source.type,
249232
type = source.type,
250233
invert = tradeHelpers.shouldBeInverted(identifier, resolvedLine, source.type)
@@ -419,15 +402,15 @@ function M.openPopup(item, slotName, primaryBuild)
419402
-- strip color codes and replace with gray
420403
displayText = "^8" .. displayText:gsub("%^x%x%x%x%x%x%x", ""):gsub("%^%x", "")
421404
end
422-
if #displayText > (#colorCodeLength + 65) then
423-
displayText = displayText:sub(1, #colorCodeLength + 55) .. "..."
405+
if #displayText > (#colorCodeLength + 60) then
406+
displayText = displayText:sub(1, #colorCodeLength + 54) .. "..."
424407
end
425408

426409
controls[prefix .. "Label"] = new("LabelControl", { "LEFT", controls[prefix .. "Check"], "RIGHT" }, { 4, 0, 0, 16 },
427410
displayText)
428411
-- when the trade site has a dropdown for the value, we opt to disable
429412
-- the inputs as they are numeric
430-
if not entry.valueIsOption and entry.value then
413+
if not entry.options and entry.value then
431414
controls[prefix .. "Min"] = newPlainNumericEdit(nil, {minFieldX - popupWidth/2, ctrlY, fieldW, fieldH}, entry.value ~= 0 and tostring(m_floor(entry.value)) or "", "Min", 8)
432415
controls[prefix .. "Max"] = newPlainNumericEdit(nil, {maxFieldX - popupWidth/2, ctrlY, fieldW, fieldH}, "", "Max", 8)
433416
if not canSearch then
@@ -448,7 +431,6 @@ function M.openPopup(item, slotName, primaryBuild)
448431
controls.search.enabled = function()
449432
return uri and uri ~= ""
450433
end
451-
-- ctrlY = ctrlY + rowHeight + 4
452434

453435
controls.close = new("ButtonControl", nil, {popupWidth/2 - 50, ctrlY, 60, 20}, "Close", function()
454436
main:ClosePopup()

src/Classes/TradeHelpers.lua

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ end
3535
-- Helper: fetch and cache the trade API stats
3636
local _tradeStats = nil
3737
local _tradeStatsFetched = false
38+
-- contains data for stats which have options, like allocates #
39+
local optionTradeStatMap = {}
3840
--- @return table
3941
local function getTradeStatsLookup()
4042
if _tradeStats then return _tradeStats end
@@ -52,6 +54,21 @@ local function getTradeStatsLookup()
5254
if not ok or tradeStats == "" then return {} end
5355
local parsed = dkjson.decode(tradeStats)
5456
_tradeStats = parsed.result
57+
58+
for _, cat in ipairs(_tradeStats) do
59+
if cat.id == "enchant" or cat.id == "explicit" or cat.id == "implicit" then
60+
for _, entry in ipairs(cat.entries) do
61+
if entry.option and entry.text:match("#") then
62+
-- pob parses the passage part as a separate mod line, which
63+
-- causes trouble
64+
local matchKey = entry.text:gsub("#", "(.*)"):gsub("\nPassage", "")
65+
local hash = tonumber(entry.id:match("%a+.stat_(%d+)"))
66+
optionTradeStatMap[matchKey] = { type = cat.id, options = entry.option.options, tradeHash = hash }
67+
end
68+
end
69+
end
70+
end
71+
5572
return _tradeStats
5673
end
5774

@@ -96,6 +113,10 @@ end
96113
function M.shouldBeInverted(tradeId, modLine, modType)
97114
local formattedLine = M.formatDatabaseText(M.formatDatabaseText(modLine))
98115
local invertedLine, inverseKey = M.swapInverse(formattedLine)
116+
invertedLine = invertedLine:gsub("^%-", "")
117+
if not inverseKey then
118+
return false
119+
end
99120
for _, category in ipairs(getTradeStatsLookup()) do
100121
if category.id == modType then
101122
for _, stat in ipairs(category.entries) do
@@ -140,8 +161,15 @@ end
140161
--- @param item table
141162
--- @param modLine string
142163
--- @param modType string
143-
--- @return number?
164+
--- the hash that was found
165+
--- @return number? hash, table[]? optionsIfApplicable, number[]? matchedOptionId
144166
function M.findTradeHash(item, modLine, modType)
167+
-- these mod results in false positives and are either useless, or
168+
-- effectively found by searching for the unique item's name
169+
if modLine == "Passage" or modLine == "Passive Skills in Radius can be Allocated without being connected to your tree" then
170+
return nil
171+
end
172+
145173
local formattedLine = M.formatDatabaseText(modLine)
146174
-- the data export splits some mods into different parts, even though they
147175
-- are technically just one stat. we handle that here
@@ -157,9 +185,6 @@ function M.findTradeHash(item, modLine, modType)
157185
end
158186
for tradeHash, description in pairs(dbMod.tradeHashes) do
159187
local tradeLine = table.concat(description, "\n")
160-
if tradeLine:match("increased Critical Strike Chance against Shocked Enemies") then
161-
ConPrintf("help")
162-
end
163188
if formattedLine == M.formatDatabaseText(tradeLine) then
164189
return tradeHash
165190
end
@@ -172,11 +197,25 @@ function M.findTradeHash(item, modLine, modType)
172197
return tradeHash
173198
end
174199
end
175-
176-
177200
end
178201
end
179202

203+
-- initialise optionTradeStatMap
204+
if not _tradeStats then
205+
getTradeStatsLookup()
206+
end
207+
-- reformat double-line cluster enchants
208+
modLine = modLine:gsub("\nAdded Small Passive Skills grant: ", "\n")
209+
for pat, entry in pairs(optionTradeStatMap) do
210+
local match = modLine:match(pat)
211+
if entry.type == modType and match then
212+
for _, option in ipairs(entry.options) do
213+
if option.text == match then
214+
return entry.tradeHash, entry.options, option.id
215+
end
216+
end
217+
end
218+
end
180219
if item.foulborn then
181220
for _, dbMod in pairs(data.itemMods.Foulborn) do
182221
local tradeHashMaybe = findStat(dbMod)

0 commit comments

Comments
 (0)