



Dreys wrote:I'd love to see an auction house addon working...

|  |   | 

|  |   | 

function run(request)
   if (request.state == READY_STATE) then
      -- Locate a stack of the items. If the request has a stack associated
      -- with it, that's a hint to try and use it. Otherwise we'll search
      -- for a stack of the exact size. Failing that, we'll start with the
      -- first stack we find.
      local stack1 = nil;
      if (request.stack and request.itemSignature == getContainerItemSignature(request.stack.bag, request.stack.slot)) then
         -- Use the stack hint.
         stack1 = request.stack;
      else
         -- Find the first stack.
         stack1 = findStackBySignature(request.itemSignature);
         -- Now look for a stack of the exact size to use instead.
         if (stack1) then
            local stack2 = { bag = stack1.bag, slot = stack1.slot };
            local _, stack2Size = GetContainerItemInfo(stack2.bag, stack2.slot);
            while (stack2 and stack2Size ~= request.stackSize) do
               stack2 = findStackBySignature(request.itemSignature, stack2.bag, stack2.slot + 1);
               if (stack2) then
                  _, stack2Size = GetContainerItemInfo(stack2.bag, stack2.slot);
               end
            end
            if (stack2) then
               stack1 = stack2;
            end
         end
      end
      -- If we have found a stack, figure out what we should do with it.
      if (stack1) then
         local _, stack1Size = GetContainerItemInfo(stack1.bag, stack1.slot);
         if (stack1Size == request.stackSize) then
            -- We've done it! Now move the stack to the auction house.
            request.stack = stack1;
            setState(request, AUCTIONING_STACK_STATE);
            pickupContainerItem(stack1.bag, stack1.slot);
            ClickAuctionSellItemButton();
            -- Start the auction if requested.
            if (request.bid and request.buyout and request.duration) then
               StartAuction(request.bid, request.buyout, request.duration);
            else
               removeRequestFromQueue();
            end
         elseif (stack1Size < request.stackSize) then
            -- The stack we have is less than needed. Locate more of the item.
            local stack2 = findStackBySignature(request.itemSignature, stack1.bag, stack1.slot + 1);
            if (stack2) then
               local _, stack2Size = GetContainerItemInfo(stack2.bag, stack2.slot);
               if (stack1Size + stack2Size <= request.stackSize) then
                  -- Combine all of stack2 with stack1.
                  setState(request, COMBINING_STACK_STATE);
                  pickupContainerItem(stack2.bag, stack2.slot);
                  pickupContainerItem(stack1.bag, stack1.slot);
                  request.stack = stack1;
               else
                  -- Combine part of stack2 with stack1.
                  setState(request, SPLITTING_AND_COMBINING_STACK_STATE);
                  splitContainerItem(stack2.bag, stack2.slot, request.stackSize - stack1Size);
                  pickupContainerItem(stack1.bag, stack1.slot);
                  request.stack = stack1;
               end
            else
               -- Not enough of the item found!
               chatPrint(_AUCT('FrmtNoEmptyPackSpace'));
               removeRequestFromQueue();
            end
         else
            -- The stack we have is more than needed. Locate an empty slot.
            local stack2 = findEmptySlot();
            if (stack2) then
               setState(request, SPLITTING_STACK_STATE);
               splitContainerItem(stack1.bag, stack1.slot, request.stackSize);
               pickupContainerItem(stack2.bag, stack2.slot);
               request.stack = stack2;
            else
               -- Not enough of the item!
               local output = string.format(_AUCT('FrmtNotEnoughOfItem'), request.name);
               chatPrint(output);
               removeRequestFromQueue();
            end
         end
      else
         -- Item not found!
         local output = string.format(_AUCT('FrmtNotEnoughOfItem'), request.name);
         chatPrint(output);
         removeRequestFromQueue();
      end
   end
end