@ -1,20 +1,64 @@
local TSM = select ( 2 , ... )
local TSM = select ( 2 , ... )
local L = LibStub ( " AceLocale-3.0 " ) : GetLocale ( " TradeSkillMaster_Shopping " ) -- loads the localization table
local L = LibStub ( " AceLocale-3.0 " ) : GetLocale ( " TradeSkillMaster_Shopping " ) -- loads the localization table
local private = { itemOperations = { } , purchasedCounts = { } }
local private = { itemOperations = { } , purchasedCounts = { } , itemQuantities = { } , operationSettings = { } , itemOperationSettings = { } , itemOperationMaxPrices = { } }
local function GetCachedItemQuantity ( itemString )
local quantity = private.itemQuantities [ itemString ]
if quantity == nil then
local totalQty = TSM : GetTotalQuantity ( itemString ) or 0
totalQty = totalQty + ( private.purchasedCounts [ itemString ] or 0 )
private.itemQuantities [ itemString ] = totalQty
quantity = totalQty
end
return quantity
end
local function GetOperationSettings ( opName )
local settings = private.operationSettings [ opName ]
if settings == nil then
TSMAPI : UpdateOperation ( " Shopping " , opName )
settings = TSM.operations [ opName ]
private.operationSettings [ opName ] = settings or false
end
return settings ~= false and settings or nil
end
local function GetOperationMaxPrice ( itemString , opName , opSettings )
private.itemOperationMaxPrices [ itemString ] = private.itemOperationMaxPrices [ itemString ] or { }
local cached = private.itemOperationMaxPrices [ itemString ] [ opName ]
if cached ~= nil then
return cached ~= false and cached or nil
end
local operationPrice = TSM : GetMaxPrice ( opSettings.maxPrice , itemString )
private.itemOperationMaxPrices [ itemString ] [ opName ] = operationPrice or false
return operationPrice
end
local function GetItemOperationSettings ( itemString , operations )
local settingsList = private.itemOperationSettings [ itemString ]
if settingsList then return settingsList end
settingsList = { }
for _ , opName in ipairs ( operations or { } ) do
local opSettings = GetOperationSettings ( opName )
if opSettings then
tinsert ( settingsList , { name = opName , settings = opSettings } )
end
end
private.itemOperationSettings [ itemString ] = settingsList
return settingsList
end
local function GetActiveOperations ( itemString , operations )
local function GetActiveOperations ( itemString , operations )
if not operations or # operations == 0 then return end
if not operations or # operations == 0 then return end
local totalQty = TSM : GetTotalQuantity ( itemString ) or 0
local totalQty = GetCachedItemQuantity ( itemString )
totalQty = totalQty + ( private.purchasedCounts [ itemString ] or 0 )
local active = { }
local active = { }
for _ , opName in ipairs ( operations ) do
for _ , opEntry in ipairs ( GetItemOperationSettings ( itemString , operations ) ) do
TSMAPI : UpdateOperation ( " Shopping " , opName )
local opSettings = opEntry.settings
local opSettings = TSM.operations [ opName ]
if opSettings then
if opSettings then
local maxRestock = tonumber ( opSettings.maxRestock ) or 0
local maxRestock = tonumber ( opSettings.maxRestock ) or 0
if maxRestock <= 0 or totalQty < maxRestock then
if maxRestock <= 0 or totalQty < maxRestock then
tinsert ( active , opSettings )
tinsert ( active , opEntry )
end
end
end
end
end
end
@ -60,12 +104,13 @@ function private.ScanCallback(event, ...)
local operations = GetActiveOperations ( itemString , private.itemOperations [ itemString ] )
local operations = GetActiveOperations ( itemString , private.itemOperations [ itemString ] )
if not operations or # operations == 0 then return end
if not operations or # operations == 0 then return end
local itemMaxPrice
local itemMaxPrice
for _ , operation in ipairs ( operations ) do
for _ , opEntry in ipairs ( operations ) do
if operation.showAboveMaxPrice then
local opSettings = opEntry.settings
if opSettings.showAboveMaxPrice then
itemMaxPrice = nil
itemMaxPrice = nil
break
break
end
end
local operationPrice = TSM : GetMaxPrice ( operation.maxPrice , itemString )
local operationPrice = GetOperationMaxPrice ( itemString , opEntry.name , opSettings )
if operationPrice then
if operationPrice then
itemMaxPrice = itemMaxPrice and max ( itemMaxPrice , operationPrice ) or operationPrice
itemMaxPrice = itemMaxPrice and max ( itemMaxPrice , operationPrice ) or operationPrice
end
end
@ -84,20 +129,20 @@ function private.ScanCallback(event, ...)
itemString = TSMAPI : GetBaseItemString ( itemString , true )
itemString = TSMAPI : GetBaseItemString ( itemString , true )
local operations = GetActiveOperations ( itemString , private.itemOperations [ itemString ] )
local operations = GetActiveOperations ( itemString , private.itemOperations [ itemString ] )
if not operations or # operations == 0 then return end
if not operations or # operations == 0 then return end
local totalQty = GetCachedItemQuantity ( itemString )
auctionItem : FilterRecords ( function ( record )
auctionItem : FilterRecords ( function ( record )
local buyout = record : GetItemBuyout ( ) or 0
local buyout = record : GetItemBuyout ( ) or 0
local totalQty = TSM : GetTotalQuantity ( itemString ) or 0
for _ , opEntry in ipairs ( operations ) do
totalQty = totalQty + ( private.purchasedCounts [ itemString ] or 0 )
local opSettings = opEntry.settings
for _ , operation in ipairs ( operations ) do
local maxRestock = tonumber ( opSettings.maxRestock ) or 0
local maxRestock = tonumber ( operation.maxRestock ) or 0
if maxRestock > 0 and ( totalQty + record.count ) > maxRestock then
if maxRestock > 0 and ( totalQty + record.count ) > maxRestock then
-- this operation can't accept more of this item
-- this operation can't accept more of this item
elseif operation .evenStacks and record.count % 5 ~= 0 then
elseif opSettings .evenStacks and record.count % 5 ~= 0 then
-- not an even stack for this operation
-- not an even stack for this operation
elseif operation .showAboveMaxPrice then
elseif opSettings .showAboveMaxPrice then
return false
return false
else
else
local operationPrice = TSM : GetMaxPrice ( operation.maxPrice , itemString )
local operationPrice = GetOperationMaxPrice ( itemString , opEntry.name , opSettings )
if operationPrice and buyout <= operationPrice then
if operationPrice and buyout <= operationPrice then
return false
return false
end
end
@ -106,8 +151,8 @@ function private.ScanCallback(event, ...)
return true
return true
end )
end )
local marketValue
local marketValue
for _ , operation in ipairs ( operations ) do
for _ , opEntry in ipairs ( operations ) do
local operationPrice = TSM : GetMaxPrice ( operation.maxPrice , itemString )
local operationPrice = GetOperationMaxPrice ( itemString , opEntry.name , opEntry.settings )
if operationPrice then
if operationPrice then
marketValue = marketValue and max ( marketValue , operationPrice ) or operationPrice
marketValue = marketValue and max ( marketValue , operationPrice ) or operationPrice
end
end
@ -125,12 +170,17 @@ function private.StartScan()
TSM.shoppingGroupSearchActive = true
TSM.shoppingGroupSearchActive = true
wipe ( private.itemOperations )
wipe ( private.itemOperations )
wipe ( private.purchasedCounts )
wipe ( private.purchasedCounts )
private.itemQuantities = { }
private.operationSettings = { }
private.itemOperationSettings = { }
private.itemOperationMaxPrices = { }
TSM.searchCallback = function ( event , auction )
TSM.searchCallback = function ( event , auction )
if event == " OnBuyout " and auction then
if event == " OnBuyout " and auction then
local linkItemString = auction.link and TSMAPI : GetItemString ( auction.link )
local linkItemString = auction.link and TSMAPI : GetItemString ( auction.link )
if linkItemString then
if linkItemString then
local itemString = TSMAPI : GetBaseItemString ( linkItemString , true )
local itemString = TSMAPI : GetBaseItemString ( linkItemString , true )
private.purchasedCounts [ itemString ] = ( private.purchasedCounts [ itemString ] or 0 ) + ( auction.count or 0 )
private.purchasedCounts [ itemString ] = ( private.purchasedCounts [ itemString ] or 0 ) + ( auction.count or 0 )
private.itemQuantities [ itemString ] = nil
end
end
end
end
end
end
@ -138,18 +188,18 @@ function private.StartScan()
itemString = TSMAPI : GetBaseItemString ( itemString , true )
itemString = TSMAPI : GetBaseItemString ( itemString , true )
local operations = GetActiveOperations ( itemString , private.itemOperations [ itemString ] )
local operations = GetActiveOperations ( itemString , private.itemOperations [ itemString ] )
if not operations or # operations == 0 then return false end
if not operations or # operations == 0 then return false end
for _ , operation in ipairs ( operations ) do
local totalQty = GetCachedItemQuantity ( itemString )
local maxRestock = tonumber ( operation.maxRestock ) or 0
for _ , opEntry in ipairs ( operations ) do
local totalQty = TSM : GetTotalQuantity ( itemString ) or 0
local opSettings = opEntry.settings
totalQty = totalQty + ( private.purchasedCounts [ itemString ] or 0 )
local maxRestock = tonumber ( opSettings.maxRestock ) or 0
if maxRestock > 0 and ( totalQty + count ) > maxRestock then
if maxRestock > 0 and ( totalQty + count ) > maxRestock then
-- doesn't fit this operation
-- doesn't fit this operation
elseif operation .evenStacks and count % 5 ~= 0 then
elseif opSettings .evenStacks and count % 5 ~= 0 then
-- not an even stack for this operation
-- not an even stack for this operation
elseif operation .showAboveMaxPrice then
elseif opSettings .showAboveMaxPrice then
return true
return true
else
else
local operationPrice = TSM : GetMaxPrice ( operation.maxPrice , itemString )
local operationPrice = GetOperationMaxPrice ( itemString , opEntry.name , opSettings )
if operationPrice and buyoutPerItem and buyoutPerItem > 0 and buyoutPerItem <= operationPrice then
if operationPrice and buyoutPerItem and buyoutPerItem > 0 and buyoutPerItem <= operationPrice then
return true
return true
end
end
@ -173,7 +223,7 @@ function private.StartScan()
if err then
if err then
TSM : Printf ( L [ " Invalid custom price source for %s. %s " ] , TSMAPI : GetSafeItemInfo ( itemString ) or itemString , err )
TSM : Printf ( L [ " Invalid custom price source for %s. %s " ] , TSMAPI : GetSafeItemInfo ( itemString ) or itemString , err )
else
else
local totalQty = TSM : GetTotal Quantity( itemString ) or 0
local totalQty = GetCachedItem Quantity( itemString )
local maxRestock = tonumber ( opSettings.maxRestock ) or 0
local maxRestock = tonumber ( opSettings.maxRestock ) or 0
if maxRestock <= 0 or totalQty < maxRestock then
if maxRestock <= 0 or totalQty < maxRestock then
private.itemOperations [ baseItemString ] = private.itemOperations [ baseItemString ] or { }
private.itemOperations [ baseItemString ] = private.itemOperations [ baseItemString ] or { }