View Single Post
04-02-20, 11:54 PM   #8
Vrul
A Scalebane Royal Guard
 
Vrul's Avatar
AddOn Author - Click to view addons
Join Date: Nov 2007
Posts: 404
You have a scope problem. The values of the variables inside the function you pass to C_Timer.After are still being modified by the main for-loop. You need to make local copies of each one used so that they cannot be modified. Basically you need to take a snapshot of their values at that time.

Some notes:
1. You seem to be leaking globals like crazy, but maybe they're declared at the top of the file.

2. I don't understand your use of the twelveCounter variable. It seems to be to limit the number of items/stacks you try and sell at once but you check it inside the function and not outside before creating the function. That seems very counter productive to me.

3. You are assuming you are selling items when that may not be the case. To keep track of each individual sell like you are trying you will almost need to make a list of items you can sell, try to sell them, and then see which items actually sold.

Try replacing your avkSell function with this (dry coded):
Code:
local avkSell
do
    local profitCounter = 0 -- Cash counting variable
    local sellList = { }    -- Array with entries: { bag, slot, itemLink, itemValue, itemCount }

    local function avkSellList()
        for index = #sellList, 1, -1 do -- Process backwards so we can remove items from list safely
            local bag, slot, itemLink, itemValue, itemCount = unpack(sellList[index])
            local itemID = GetContainerItemID(bag, slot)
            if itemID then -- Item still needs to be sold
                UseContainerItem(bag, slot)
            else -- Item has been sold
                tremove(sellList, index)
                local value = itemValue * itemCount
                if AVKGlobalSettings.Sell.sdsm then -- If messages are on
                    print((" - Sold: %s %s for %s"):format(itemCount, itemLink, GetCoinTextureString(value)))
                end
                profitCounter = profitCounter + value -- Add sale value to profit counter
            end
        end
        if #sellList == 0 then -- All items sold
            if profitCounter ~= 0 then -- If items were sold print out total -- this message cannot be disabled
                print(("%s%sTotal Profit :-|r %s"):format(ColourList.textBluePrefixSuffix, ColourList.textGreen, GetCoinTextureString(profitCounter)))
            elseif AVKGlobalSettings.Sell.sntsm then -- If no items sell print out grind message
                print(("%s%sNothing to sell. Better grind some more."):format(ColourList.textBluePrefixSuffix, ColourList.textGreen))
            end
        else -- Try to sell remaining items in 1-second
            C_Timer.After(1, avkSellList)
        end
    end

    function avkSell()
        local vendorGUID = UnitGUID('target')
        if not vendorGUID then -- Catches engineer repair anvils -- debug
            return
        end
        local _, _, _, _, _, npcID, _ = strsplit('-', vendorGUID)
        local vendorID = tonumber(npcID)
        if checkVendorBlacklist(vendorID) then -- Check if vendor ID is on blacklist
            print(ColourList.textBluePrefixSuffix .. ColourList.textGreen .. UnitName('target') .. ' refuses to buy your items.')
        else
            wipe(sellList)
            profitCounter = 0
            local numListed = 0
            for bag = 0, 4 do -- Check each bag
                for slot = 0, GetContainerNumSlots(bag) do -- Check each bag slot
                    local itemID = GetContainerItemID(bag, slot)
                    if itemID then -- If current bag slot has an item then get the item info
                        local _, itemLink, itemQuality, _, _, _, _, _, _, _, itemValue = GetItemInfo(itemID)
                        if
                            (checkItemOnGlobalProtectList(itemID) or checkItemOnCharacterProtectList(itemID)) and
                                itemValue > 0
                         then
                            if AVKGlobalSettings.Sell.sdsm and AVKGlobalSettings.Protect.pdpm then
                                print(' - Protected: ' .. itemLink .. ' not sold.')
                            end
                        elseif
                            checkItemOnGlobalSellList(itemID) or checkItemOnCharacterSellList(itemID) or
                                (AVKGlobalSettings.Sell.seasg and itemQuality == 0)
                         then
                            local _, itemCount = GetContainerItemInfo(bag, slot)
                            numListed = numListed + 1
                            sellList[numListed] = { bag, slot, itemLink, itemValue, itemCount }
                            if numListed == 12 and AVKGlobalSettings.Sell.stil then -- If twelve item limit
                                break
                            end
                        end
                    end
                end
            end
            avkSellList()
        end
    end
end
I removed the stuff at the end of the function as it was mostly repair stuff that was only messages and variable resets and not actually repairing.
  Reply With Quote