almost recursively works

Brad Gearon [04-09-14 - 12:35]
almost recursively works
Filename
.idea/workspace.xml
Stuff.lua
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index e534dd2..48de938 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,7 +5,7 @@
     <ignored path="stuff.iws" />
     <ignored path=".idea/workspace.xml" />
     <file path="/stuff.lua" changelist="1141a5ab-9990-4f4e-94b9-bcb14baef305" time="1397015183283" ignored="false" />
-    <file path="/Stuff.lua" changelist="1141a5ab-9990-4f4e-94b9-bcb14baef305" time="1397038320324" ignored="false" />
+    <file path="/Stuff.lua" changelist="1141a5ab-9990-4f4e-94b9-bcb14baef305" time="1397046838515" ignored="false" />
     <file path="/Stuff.txt" changelist="1141a5ab-9990-4f4e-94b9-bcb14baef305" time="1397019711692" ignored="false" />
     <file path="/inspect.lua" changelist="1141a5ab-9990-4f4e-94b9-bcb14baef305" time="1397019902866" ignored="false" />
     <option name="TRACKING_ENABLED" value="true" />
@@ -71,7 +71,7 @@
       <file leaf-file-name="Stuff.lua" pinned="false" current="true" current-in-tab="true">
         <entry file="file://$PROJECT_DIR$/Stuff.lua">
           <provider selected="true" editor-type-id="text-editor">
-            <state line="77" column="22" selection-start="2737" selection-end="2737" vertical-scroll-proportion="0.21492007">
+            <state line="63" column="6" selection-start="2096" selection-end="2096" vertical-scroll-proportion="0.19343066">
               <folding />
             </state>
           </provider>
@@ -412,7 +412,7 @@
       <window_info id="Changes" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
       <window_info id="JetGradle" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
       <window_info id="Palette" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
-      <window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32915717" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
+      <window_info id="Find" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.32990867" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
       <window_info id="Database" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
       <window_info id="Ant Build" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="1" side_tool="false" content_ui="tabs" />
       <window_info id="Debug" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.4" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
@@ -426,7 +426,7 @@
       <window_info id="Maven Projects" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
       <window_info id="Commander" active="false" anchor="right" auto_hide="false" internal_type="SLIDING" type="SLIDING" visible="false" weight="0.4" sideWeight="0.5" order="0" side_tool="false" content_ui="tabs" />
       <window_info id="Application Servers" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="7" side_tool="false" content_ui="tabs" />
-      <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.111534275" sideWeight="0.6708428" order="0" side_tool="false" content_ui="combo" />
+      <window_info id="Project" active="false" anchor="left" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="true" weight="0.111534275" sideWeight="0.67009133" order="0" side_tool="false" content_ui="combo" />
       <window_info id="Run" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="2" side_tool="false" content_ui="tabs" />
       <window_info id="LuaJ" active="false" anchor="right" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.33" sideWeight="0.5" order="3" side_tool="false" content_ui="tabs" />
       <window_info id="Cvs" active="false" anchor="bottom" auto_hide="false" internal_type="DOCKED" type="DOCKED" visible="false" weight="0.25" sideWeight="0.5" order="4" side_tool="false" content_ui="tabs" />
@@ -537,7 +537,7 @@
     </entry>
     <entry file="file://$PROJECT_DIR$/Stuff.lua">
       <provider selected="true" editor-type-id="text-editor">
-        <state line="77" column="22" selection-start="2737" selection-end="2737" vertical-scroll-proportion="0.21492007">
+        <state line="63" column="6" selection-start="2096" selection-end="2096" vertical-scroll-proportion="0.19343066">
           <folding />
         </state>
       </provider>
diff --git a/Stuff.lua b/Stuff.lua
index 8332236..c7f5e5e 100644
--- a/Stuff.lua
+++ b/Stuff.lua
@@ -18,7 +18,6 @@ function stackItem(fromBag, fromSlot, toBag, toSlot, quantity, name)
     return result
 end

-
 function insertItem(itemTable, bag, slot, stack)
     local item = {}
     item.bag = bag
@@ -27,6 +26,92 @@ function insertItem(itemTable, bag, slot, stack)
     table.insert(itemTable, item)
 end

+
+function moveItem(fromItem, toItem, maxStack, itemName)
+    -- d(key .. inspect(item))
+    -- the most we can move
+    local quantity = math.min(maxStack - toItem.stack, fromItem.stack)
+    -- if we can move any
+    if (quantity > 0) then
+        d(" moving " .. quantity .. " " .. itemName .. "from bag: " .. fromItem.bag .. " slot: " .. fromItem.slot .. " to bag: " .. toItem.bag .. " slot: " .. toItem.slot .. " with: " .. toItem.stack)
+        -- move them
+        result = stackItem(fromItem.bag, fromItem.slot, toItem.bag, toItem.slot, quantity, itemName)
+        if (result) then
+            fromItem.stack = fromItem.stack - quantity
+            toItem.stack = toItem.stack + quantity
+            d("moved " .. quantity .. " " .. itemName .. "from bag: " .. fromItem.bag .. " slot: " .. fromItem.slot .. " to bag: " .. toItem.bag .. " slot: " .. toItem.slot .. " with: " .. toItem.stack)
+        end
+    end
+end
+
+function sortStack (first, second)
+    return first.stack > second.stack
+end
+
+function reverseStack (first, second)
+    return first.stack < second.stack
+end
+
+
+function moveItems(bags, fromBag, toBag)
+    d("call")
+    for itemName, bagItem in pairs(bags) do
+        local fromItems = bagItem[fromBag]
+        local toItems = bagItem[toBag]
+        loopItems(fromItems, toItems, bagItem.maxStack, itemName)
+    end
+end
+
+function keepMovingItems(fromItems, toItems, bagItem, itemName)
+    local maxIterations = 15
+    local iterations = 1
+    while iterations < maxIterations and checkItems(fromItems, toItems, bagItem.maxStack, itemName) do
+        loopItems(fromItems, toItems, bagItem.maxStack, itemName)
+        table.sort(toItems, sortStack)
+        table.sort(fromItems, reverseStack)
+        iterations = iterations + 1
+    end
+end
+
+function checkItems(fromItems, toItems, maxStack)
+    local itemsLeft = false
+    for toIndex, toItem in ipairs(toItems) do
+        if(maxStack - toItem.stack > 0) then
+            for fromIndex, fromItem in ipairs(fromItems) do
+                local sameBag = fromItem.bag == toItem.bag
+                local sameSlot = fromItem.slot == toItem.slot
+                itemsLeft = not (sameBag and sameSlot) and fromItem.stack > 0
+                if(itemsLeft) then
+                    break
+                end
+            end
+        end
+    end
+    return itemsLeft
+end
+
+function loopItems(fromItems, toItems, maxStack, itemName)
+    table.sort(toItems, sortStack)
+    table.sort(fromItems, reverseStack)
+
+    for fromIndex, fromItem in ipairs(fromItems) do
+        if (fromItem.stack > 0 and fromItem.stack < maxStack) then
+            for toIndex, toItem in ipairs(toItems) do
+                if(toItem.stack > 0 and toItem.stack < maxStack) then
+                    local sameBag = fromItem.bag == toItem.bag
+                    local sameSlot = fromItem.slot == toItem.slot
+                    if (not (sameBag and sameSlot)) then
+                        moveItem(fromItem, toItem, maxStack, itemName)
+                        table.sort(toItems, sortStack)
+                        table.sort(fromItems, reverseStack)
+                    end
+                end
+            end
+        end
+    end
+end
+
+
 function HandleOpenBank(eventCode, addOnName, isManual)
     local maxBags = GetMaxBags()
     local bags = {}
@@ -42,46 +127,39 @@ function HandleOpenBank(eventCode, addOnName, isManual)
             itemName = GetItemName(bag, slot)

             if (stack > 0 and itemName ~= nil) then
-                -- right now this only works with the bank (will add support for ui based
-                -- guild bank deposit / join
-                local isBank = BAG_BANK == bag
+                -- right now this only works with the bank
+                -- will add support for ui based guild bank deposit / join
                 bagItem = bags[itemName] or {}
-                bagItem.bank = bagItem.bank or {}
-                bagItem.item = bagItem.item or {}
+
+                bagItem[BAG_BANK] = bagItem[BAG_BANK] or {}
+                bagItem[BAG_BACKPACK] = bagItem[BAG_BACKPACK] or {}

                 bagItem.maxStack = maxStack
                 bagItem.name = itemName

-                local itemTable = isBank and bagItem.bank or bagItem.item
+                local itemTable = bagItem[bag]

                 -- insert the slot item in the appropriate table
                 insertItem(itemTable, bag, slot, stack)
-
                 bags[itemName] = bagItem
-
-                if (not isManual and isBank) then
-                    for key, item in pairs(bagItem.item) do
-                        -- the most we can move
-                        local quantity = math.min(maxStack - stack, item.stack)
-                        -- if we can move any
-                        if (quantity > 0) then
-                            d("moving " .. quantity .. " " .. itemName .. " to bagId: " .. bag)
-                            -- move them
-                            result = stackItem(item.bag, item.slot, bag, slot, quantity, itemName)
-                            if(result) then
-                                item.stack = item.stack - quantity
-                                d("moved " .. quantity .. " " .. itemName .. " to bagId: " .. bag)
-                            end
-                        end
-                    end
-                end
             end
         end
     end
+
+    if (not isManual) then
+        -- consolidate source
+        moveItems(bags, BAG_BACKPACK, BAG_BACKPACK)
+        -- consolidate destination
+        moveItems(bags, BAG_BANK, BAG_BANK)
+        -- move to bank
+        moveItems(bags, BAG_BACKPACK, BAG_BANK)
+    end
 end

+
 function HandleAddOnLoaded(eventCode, addOnName)
-    if addOnName ~= STUFF_NAME then return end
+    if addOnName ~= STUFF_NAME then return
+    end
     Stuff.Defaults = {}

     Stuff.Saved = ZO_SavedVars:New(STUFF_NAME, 5, nil, Stuff.Defaults, nil)