diff Scanner.lua @ 81:58617c7827fa

Item refilling should now be working. Probably very slow if your bags, bank or guild bank is filled with over 200 unique items or so (needs some real testing, but know that it is a known (possible) issue).
author Zerotorescue
date Thu, 06 Jan 2011 01:01:25 +0100
parents c0bf2ddb5288
children f885805da5d6
line wrap: on
line diff
--- a/Scanner.lua	Wed Jan 05 13:05:15 2011 +0100
+++ b/Scanner.lua	Thu Jan 06 01:01:25 2011 +0100
@@ -10,18 +10,6 @@
 local Mover, paused;
 local itemCache = {};
 
-local function _GetItemCount(itemId, location)
-	if location == addon.Locations.Bank then
-		-- No longer using GetItemCount as this includes equiped items and containers (e.g. bank bags)
-		--return (GetItemCount(itemId, true) - GetItemCount(itemId, false)); -- GetItemCount(X, true) provides count for bag+bank, GetItemCount(X, false) provides just bag, so (GetItemCount(X, true) - GetItemCount(X, false)) = just bank
-		return ((itemCache[itemId] and itemCache[itemId].totalCount) or 0);
-	elseif location == addon.Locations.Guild then
-		return ((itemCache[itemId] and itemCache[itemId].totalCount) or 0);
-	else
-		error("Invalid location provided for the local _GetItemCount. Must be Bag or Bank.");
-	end
-end
-
 local function GetItemID(link)
 	return tonumber(link:match("|Hitem:([-0-9]+):"));
 end
@@ -46,7 +34,7 @@
 		end
 		
 		-- Go through all our bags, including the backpack
-		for i = start, ((addon.Locations.Bag and stop) or (addon.Locations.Bank and (stop + 1))) do -- if scanning bags stop at normal bag slot, if scanning bank, stop one later to allow BANK_CONTAINER to be scanned too
+		for i = start, ((location == addon.Locations.Bag and stop) or (location == addon.Locations.Bank and (stop + 1))) do -- if scanning bags stop at normal bag slot, if scanning bank, stop one later to allow BANK_CONTAINER to be scanned too
 			-- Scan the default 100 slots whenever we're at a non-existing index
 			local bagId = (i == (stop + 1) and BANK_CONTAINER) or i;
 			local slotId = GetContainerNumSlots(bagId);
@@ -61,13 +49,14 @@
 					local itemMove;
 					if not itemCache[itemId] then
 						-- If this is the first time we see this item, make a new object
-						itemMove = addon.ItemMove:New();
+						itemMove = addon.ContainerItem:New();
+						itemCache[itemId] = itemMove;
 					else
 						-- If we had this item in another slot too
 						itemMove = itemCache[itemId];
 					end
 					
-					itemMove.AddLocation(bagId, slotId, itemCount);
+					itemMove:AddLocation(bagId, slotId, itemCount);
 				end
 			
 				-- Continue scanning a different slot
@@ -96,13 +85,14 @@
 				local itemMove;
 				if not itemCache[itemId] then
 					-- If this is the first time we see this item, make a new object
-					itemMove = addon.ItemMove:New();
+					itemMove = addon.ContainerItem:New();
+					itemCache[itemId] = itemMove;
 				else
 					-- If we had this item in another slot too
 					itemMove = itemCache[itemId];
 				end
 				
-				itemMove.AddLocation(tabId, slotId, itemCount);
+				itemMove:AddLocation(tabId, slotId, itemCount);
 			end
 			
 			-- Continue scanning a different slot
@@ -144,19 +134,19 @@
 			-- Go through all items
 			for itemId, _ in pairs(values.items) do
 				
-				-- Check if we have enough items local
-				local missingItems = (minLocalStock - addon:GetLocalItemCount(itemId, groupName));
+				-- Check if we have enough items local (but only do so if this location also has enough available)
+				local missingItems = itemCache[itemId] and (minLocalStock - addon:GetLocalItemCount(itemId, groupName));
 				
-				if missingItems > 0 then
+				if itemCache[itemId] and missingItems > 0 then
 					-- Check how many are available
-					local availableItems = _GetItemCount(itemId, location);
+					local availableItems = ((itemCache[itemId] and itemCache[itemId].totalCount) or 0);
 					
 					if availableItems > 0 then
 						print("Insufficient " .. select(2, GetItemInfo(itemId)) .. " but this location has " .. availableItems .. " (moving " .. missingItems .. ")");
 						
 						Mover:AddMove(itemId, missingItems);
 					else
-						print("Insufficient " .. select(2, GetItemInfo(itemId)));
+						print("Insufficient " .. IdToItemLink(itemId));
 					end
 				end
 			end
@@ -165,47 +155,72 @@
 	
 	self:ClearCache();
 	
-	self:Pause();
-	Mover:BeginMove(location, self.Unpause);
+	if Mover:HasMoves() then
+		StaticPopupDialogs["InventoriumRefill"] = {
+			text = "There are items that can be refilled from this location, do you wish to proceed?",
+			button1 = YES,
+			button2 = NO,
+			OnAccept = function()
+				mod:Pause();
+				Mover:BeginMove(location, self.Unpause);
+			end,
+			timeout = 0,
+			whileDead = 1,
+			hideOnEscape = 1,
+		};
+		StaticPopup_Show("InventoriumRefill");
+	end
+end
+
+local function BANKFRAME_CLOSED()
+	addon:Debug("Scanner:BANKFRAME_CLOSED");
+	
+	mod:UnregisterEvent("BANKFRAME_CLOSED");
+	
+	StaticPopup_Hide("InventoriumRefill");
 end
 
 local function BANKFRAME_OPENED()
+	addon:Debug("Scanner:BANKFRAME_OPENED");
+	
+	mod:RegisterEvent("BANKFRAME_CLOSED", BANKFRAME_CLOSED);
+	
 	-- Scan once when the bank is opened, but no need to scan after
 	mod:Scan(addon.Locations.Bank);
-	
-	addon:Debug("Scanner:BANKFRAME_OPENED");
 end
 
 -- Remember which tabs were scanned and don't scan them again
 local guildBankTabsScanned = {};
 
 local function GUILDBANKFRAME_CLOSED()
+	addon:Debug("Scanner:GUILDBANKFRAME_CLOSED");
+	
+	table.wipe(guildBankTabsScanned);
+	
 	mod:UnregisterEvent("GUILDBANKFRAME_CLOSED");
 	mod:UnregisterEvent("GUILDBANKBAGSLOTS_CHANGED");
 	
-	table.wipe(guildBankTabsScanned);
-	
-	addon:Debug("Scanner:GUILDBANKFRAME_CLOSED");
+	StaticPopup_Hide("InventoriumRefill");
 end
 
 local function GUILDBANKBAGSLOTS_CHANGED()
 	if not guildBankTabsScanned[GetCurrentGuildBankTab()] then
+		addon:Debug("Scanner:GUILDBANKBAGSLOTS_CHANGED (" .. GetCurrentGuildBankTab() .. ") - scanning");
+		
 		mod:Scan(addon.Locations.Guild);
 		guildBankTabsScanned[GetCurrentGuildBankTab()] = true;
-		
-		addon:Debug("Scanner:GUILDBANKBAGSLOTS_CHANGED (" .. GetCurrentGuildBankTab() .. ") - scanning");
 	else
 		addon:Debug("Scanner:GUILDBANKBAGSLOTS_CHANGED (" .. GetCurrentGuildBankTab() .. ") - not scanning");
 	end
 end
 
 local function GUILDBANKFRAME_OPENED()
+	addon:Debug("Scanner:GUILDBANKFRAME_OPENED");
+	
 	table.wipe(guildBankTabsScanned);
 	
 	mod:RegisterEvent("GUILDBANKFRAME_CLOSED", GUILDBANKFRAME_CLOSED);
 	mod:RegisterEvent("GUILDBANKBAGSLOTS_CHANGED", GUILDBANKBAGSLOTS_CHANGED);
-	
-	addon:Debug("Scanner:GUILDBANKFRAME_OPENED");
 end
 
 function mod:OnEnable()