Skip to content

Commit 8bdc4e7

Browse files
committed
Fix trader price sorting
1 parent a4b0a98 commit 8bdc4e7

2 files changed

Lines changed: 38 additions & 8 deletions

File tree

spec/System/TestTradeQueryCurrency_spec.lua

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,25 @@ describe("TradeQuery Currency Conversion", function()
5858
end)
5959

6060
describe("GetTotalPriceString", function()
61-
-- Pass: Sums and formats correctly (e.g., "5 chaos, 10 div")
61+
-- Pass: Sums and formats correctly (e.g., "5 chaos, 10 div", should be most valuable currency first)
6262
-- Fail: Wrong string (e.g., unsorted/missing sums), indicating aggregation bug, misleading users on totals
6363
it("aggregates prices", function()
64-
mock_tradeQuery.totalPrice = { { currency = "chaos", amount = 5 }, { currency = "div", amount = 10 } }
64+
-- check alphabetical sorting
65+
mock_tradeQuery.totalPrice = { { currency = "chaos", amount = 5 }, { currency = "div", amount = 10 }, {currency = "exalted", amount = 1} }
6566
local result = mock_tradeQuery:GetTotalPriceString()
66-
assert.are.equal(result, "5 chaos, 10 div")
67+
assert.are.equal(result, "1 exalted, 10 div, 5 chaos")
68+
69+
-- check if they're sorted according to currency value
70+
mock_tradeQuery.pbLeague = "league"
71+
mock_tradeQuery.pbCurrencyConversion = { league = { chaos = 0.1, exalted = 0.05, div = 1, mirror = 700} }
72+
local result = mock_tradeQuery:GetTotalPriceString()
73+
assert.are.equal(result, "10 div, 5 chaos, 1 exalted")
74+
75+
-- check that missing currency values don't crash
76+
mock_tradeQuery.pbLeague = "league"
77+
mock_tradeQuery.pbCurrencyConversion = { league = { chaos = 0.1, exalted = 0.05, mirror = 700 } }
78+
local result = mock_tradeQuery:GetTotalPriceString()
79+
assert.True(true)
6780
end)
6881
end)
6982
end)

src/Classes/TradeQuery.lua

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,15 +1298,32 @@ end
12981298
-- Method to update the Total Price string sum of all items
12991299
function TradeQueryClass:GetTotalPriceString()
13001300
local text = ""
1301-
local sorted_price = { }
1301+
-- sum up prices
1302+
local prices = {}
13021303
for _, entry in pairs(self.totalPrice) do
1303-
if sorted_price[entry.currency] then
1304-
sorted_price[entry.currency] = sorted_price[entry.currency] + entry.amount
1304+
if prices[entry.currency] then
1305+
prices[entry.currency] = prices[entry.currency] + entry.amount
13051306
else
1306-
sorted_price[entry.currency] = entry.amount
1307+
prices[entry.currency] = entry.amount
13071308
end
13081309
end
1309-
for currency, value in pairs(sorted_price) do
1310+
1311+
-- try to sort by the value of each currency, i.e. 1 mirror > 9999 div, 1 chaos > 123 ex
1312+
-- if currency data isn't available, just sort by currency name
1313+
local currencies = {}
1314+
for currency, _ in pairs(prices) do
1315+
table.insert(currencies, currency)
1316+
end
1317+
local currencyMap = self.pbCurrencyConversion[self.pbLeague] or {}
1318+
table.sort(currencies, function(a, b)
1319+
if currencyMap[a] and currencyMap[b] then
1320+
return currencyMap[a] > currencyMap[b]
1321+
else
1322+
return a > b
1323+
end
1324+
end)
1325+
for _, currency in ipairs(currencies) do
1326+
local value = prices[currency]
13101327
text = text .. tostring(value) .. " " .. currency .. ", "
13111328
end
13121329
if text ~= "" then

0 commit comments

Comments
 (0)