Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 63 additions & 25 deletions src/Classes/CompareBuySimilar.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ local t_insert = table.insert
local m_floor = math.floor
local dkjson = require "dkjson"
local tradeHelpers = LoadModule("Classes/TradeHelpers")
local tradeStats = tradeHelpers.getTradeStats()

-- used to check what stats actually exist on the trade site.
local existingStats = {}
for _, cat in ipairs(tradeStats or {}) do
for _, entry in ipairs(cat.entries) do
existingStats[entry.id] = true
end
end

local M = {}

Expand Down Expand Up @@ -128,13 +137,12 @@ local function buildURL(item, slotName, controls, modEntries, defenceEntries, is
-- Mod filters
for i, entry in ipairs(modEntries) do
local prefix = "mod" .. i
if entry.tradeId and controls[prefix .. "Check"] and controls[prefix .. "Check"].state then
local filter = { id = entry.tradeId }
local function getFilter(tradeId)
local filter = { id = tradeId }
if entry.isOption then
filter.value = { min = entry.value, max = entry.value }
elseif entry.value then
local minVal = tonumber(controls[prefix .. "Min"].buf)

local maxVal = tonumber(controls[prefix .. "Max"].buf)
local value = {}
if minVal then
Expand All @@ -152,7 +160,20 @@ local function buildURL(item, slotName, controls, modEntries, defenceEntries, is
filter.value = value
end
end
t_insert(queryTable.query.stats[1].filters, filter)
return filter
end
if controls[prefix .. "Check"] and controls[prefix .. "Check"].state then
if #entry.tradeIds == 1 then
-- 1 id entries are added to the stat filters section
t_insert(queryTable.query.stats[1].filters, getFilter(entry.tradeIds[1]))
elseif #entry.tradeIds > 1 then
-- ambiguous entries are added as a separate count filter
local countFilter = { type = "count", value = { min = 1 }, filters = {} }
for _, tradeId in ipairs(entry.tradeIds) do
t_insert(countFilter.filters, getFilter(tradeId))
end
t_insert(queryTable.query.stats, countFilter)
end
end
end

Expand Down Expand Up @@ -202,16 +223,19 @@ function M.openPopup(item, slotName, primaryBuild)
-- this adds a single aggregated entry for matching stats (e.g. transformed flat dmg mods) which avoids issues with confusing results. different types are not summed as e.g. implicit and explicit mods are separate in the search. options are also avoided as they don't represent values that can be added combined
local function insertOrAddToExisting(entry)
for _, existingFilter in ipairs(modEntries) do
if (not existingFilter.isOption) and entry.value
and existingFilter.tradeId and existingFilter.tradeId == entry.tradeId
and existingFilter.type == entry.type
then
-- check if all result trade ids are equal
local sameHashes = #entry.tradeIds > 0 and tableDeepEquals(entry.tradeIds, existingFilter.tradeIds)
if sameHashes and existingFilter.type == entry.type then
-- count of combined lines
existingFilter.count = existingFilter.count + 1
local value = (entry.invert ~= existingFilter.invert) and -entry.value or entry.value
existingFilter.value = (existingFilter.value or 0) + value
if entry.value then
local value = (entry.invert ~= existingFilter.invert) and -entry.value or entry.value or 0
existingFilter.value = (existingFilter.value or 0) + value
end
t_insert(existingFilter.formattedLines, entry.formattedLines[1])
return
end
::continue::
end
t_insert(modEntries, entry)
end
Expand All @@ -228,24 +252,38 @@ function M.openPopup(item, slotName, primaryBuild)
-- Use range-resolved text for matching
local resolvedLine = (modLine.range and itemLib.applyRange(modLine.line, modLine.range, modLine.valueScalar)) or
modLine.line
local tradeHash, identifier, value = tradeHelpers.findTradeHash(item, resolvedLine, source.type, modLine.desecrated)
local isOption = not not identifier
if not identifier then
identifier = tradeHash and string.format("%s.stat_%s", source.type, tradeHash)
value = tradeHelpers.modLineValue(resolvedLine)
end
local invert = (not isOption) and tradeHelpers.shouldBeInverted(identifier, resolvedLine, source.type)
insertOrAddToExisting({
-- check option first, because even if we match a line via the descriptors, the trade id formatting is different for options. e.g.: explicit.stat_345345|33
local tradeId, value = tradeHelpers.findTradeIdOption(resolvedLine, source.type)

local entry = {
-- this array will always start with one line, but if multiple mods are
-- aggregated together it will contain the original mod lines for each
formattedLines = {formatted},
tradeId = identifier,
value = value,
isOption = isOption,
formattedLines = { formatted },
type = source.type,
invert = invert,
isOption = true,
invert = false,
count = 1,
})
tradeIds = { tradeId },
value = value,
}

if not tradeId then
local resultHashes, value, invert = tradeHelpers.findTradeHash(resolvedLine)
-- convert hashes to string ids
local resultIds = {}
if resultHashes then
for idx = 1, #resultHashes do
local id = string.format("%s.stat_%s", source.type, resultHashes[idx])
if existingStats[id] then
resultIds[idx] = id
end
end
end
entry.tradeIds = resultHashes
entry.value = value
entry.invert = invert
end
insertOrAddToExisting(entry)
end
end
end
Expand Down Expand Up @@ -407,7 +445,7 @@ function M.openPopup(item, slotName, primaryBuild)
end
prevType = entry.type
local prefix = "mod" .. i
local canSearch = entry.tradeId ~= nil
local canSearch = #entry.tradeIds > 0

local rows = #entry.formattedLines

Expand Down
10 changes: 10 additions & 0 deletions src/Classes/ItemsTab.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ local m_min = math.min
local m_ceil = math.ceil
local m_floor = math.floor
local m_modf = math.modf
local buySimilar = LoadModule("Classes/CompareBuySimilar")

local gemTooltip = LoadModule("Classes/GemTooltip")
local rarityDropList = {
Expand Down Expand Up @@ -410,6 +411,15 @@ holding Shift will put it in the second.]])
self:SetDisplayItem()
end)

self.controls.displayItemBuySimilar = new("ButtonControl",
{ "LEFT", self.controls.removeDisplayItem, "RIGHT", true },
{ 8, 0, 100, 20 }, "Buy similar", function()
local itemSlot = self:GetComparisonSlotNameForItem(self.displayItem)
buySimilar.openPopup(self.displayItem, itemSlot, self.build)
end)
self.controls.displayItemBuySimilar.shown = function()
return self.displayItem
end
-- Section: Variant(s)

self.controls.displayItemSectionVariant = new("Control", {"TOPLEFT",self.controls.addDisplayItem,"BOTTOMLEFT"}, {0, 8, 0, function()
Expand Down
Loading
Loading