Mercurial > wow > inventory
comparison 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 |
comparison
equal
deleted
inserted
replaced
80:c0bf2ddb5288 | 81:58617c7827fa |
---|---|
7 Guild = 2, | 7 Guild = 2, |
8 }; | 8 }; |
9 | 9 |
10 local Mover, paused; | 10 local Mover, paused; |
11 local itemCache = {}; | 11 local itemCache = {}; |
12 | |
13 local function _GetItemCount(itemId, location) | |
14 if location == addon.Locations.Bank then | |
15 -- No longer using GetItemCount as this includes equiped items and containers (e.g. bank bags) | |
16 --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 | |
17 return ((itemCache[itemId] and itemCache[itemId].totalCount) or 0); | |
18 elseif location == addon.Locations.Guild then | |
19 return ((itemCache[itemId] and itemCache[itemId].totalCount) or 0); | |
20 else | |
21 error("Invalid location provided for the local _GetItemCount. Must be Bag or Bank."); | |
22 end | |
23 end | |
24 | 12 |
25 local function GetItemID(link) | 13 local function GetItemID(link) |
26 return tonumber(link:match("|Hitem:([-0-9]+):")); | 14 return tonumber(link:match("|Hitem:([-0-9]+):")); |
27 end | 15 end |
28 | 16 |
44 start = ( NUM_BAG_SLOTS + 1 ); | 32 start = ( NUM_BAG_SLOTS + 1 ); |
45 stop = ( NUM_BAG_SLOTS + NUM_BANKBAGSLOTS ); | 33 stop = ( NUM_BAG_SLOTS + NUM_BANKBAGSLOTS ); |
46 end | 34 end |
47 | 35 |
48 -- Go through all our bags, including the backpack | 36 -- Go through all our bags, including the backpack |
49 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 | 37 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 |
50 -- Scan the default 100 slots whenever we're at a non-existing index | 38 -- Scan the default 100 slots whenever we're at a non-existing index |
51 local bagId = (i == (stop + 1) and BANK_CONTAINER) or i; | 39 local bagId = (i == (stop + 1) and BANK_CONTAINER) or i; |
52 local slotId = GetContainerNumSlots(bagId); | 40 local slotId = GetContainerNumSlots(bagId); |
53 | 41 |
54 while slotId ~= 0 do | 42 while slotId ~= 0 do |
59 | 47 |
60 if itemId and itemCount and itemCount > 0 then | 48 if itemId and itemCount and itemCount > 0 then |
61 local itemMove; | 49 local itemMove; |
62 if not itemCache[itemId] then | 50 if not itemCache[itemId] then |
63 -- If this is the first time we see this item, make a new object | 51 -- If this is the first time we see this item, make a new object |
64 itemMove = addon.ItemMove:New(); | 52 itemMove = addon.ContainerItem:New(); |
53 itemCache[itemId] = itemMove; | |
65 else | 54 else |
66 -- If we had this item in another slot too | 55 -- If we had this item in another slot too |
67 itemMove = itemCache[itemId]; | 56 itemMove = itemCache[itemId]; |
68 end | 57 end |
69 | 58 |
70 itemMove.AddLocation(bagId, slotId, itemCount); | 59 itemMove:AddLocation(bagId, slotId, itemCount); |
71 end | 60 end |
72 | 61 |
73 -- Continue scanning a different slot | 62 -- Continue scanning a different slot |
74 slotId = (slotId - 1); | 63 slotId = (slotId - 1); |
75 end | 64 end |
94 | 83 |
95 if itemLink and itemId and itemCount and itemCount > 0 then | 84 if itemLink and itemId and itemCount and itemCount > 0 then |
96 local itemMove; | 85 local itemMove; |
97 if not itemCache[itemId] then | 86 if not itemCache[itemId] then |
98 -- If this is the first time we see this item, make a new object | 87 -- If this is the first time we see this item, make a new object |
99 itemMove = addon.ItemMove:New(); | 88 itemMove = addon.ContainerItem:New(); |
89 itemCache[itemId] = itemMove; | |
100 else | 90 else |
101 -- If we had this item in another slot too | 91 -- If we had this item in another slot too |
102 itemMove = itemCache[itemId]; | 92 itemMove = itemCache[itemId]; |
103 end | 93 end |
104 | 94 |
105 itemMove.AddLocation(tabId, slotId, itemCount); | 95 itemMove:AddLocation(tabId, slotId, itemCount); |
106 end | 96 end |
107 | 97 |
108 -- Continue scanning a different slot | 98 -- Continue scanning a different slot |
109 slotId = (slotId - 1); | 99 slotId = (slotId - 1); |
110 end | 100 end |
142 local minLocalStock = addon:GetOptionByKey(groupName, "minLocalStock"); | 132 local minLocalStock = addon:GetOptionByKey(groupName, "minLocalStock"); |
143 | 133 |
144 -- Go through all items | 134 -- Go through all items |
145 for itemId, _ in pairs(values.items) do | 135 for itemId, _ in pairs(values.items) do |
146 | 136 |
147 -- Check if we have enough items local | 137 -- Check if we have enough items local (but only do so if this location also has enough available) |
148 local missingItems = (minLocalStock - addon:GetLocalItemCount(itemId, groupName)); | 138 local missingItems = itemCache[itemId] and (minLocalStock - addon:GetLocalItemCount(itemId, groupName)); |
149 | 139 |
150 if missingItems > 0 then | 140 if itemCache[itemId] and missingItems > 0 then |
151 -- Check how many are available | 141 -- Check how many are available |
152 local availableItems = _GetItemCount(itemId, location); | 142 local availableItems = ((itemCache[itemId] and itemCache[itemId].totalCount) or 0); |
153 | 143 |
154 if availableItems > 0 then | 144 if availableItems > 0 then |
155 print("Insufficient " .. select(2, GetItemInfo(itemId)) .. " but this location has " .. availableItems .. " (moving " .. missingItems .. ")"); | 145 print("Insufficient " .. select(2, GetItemInfo(itemId)) .. " but this location has " .. availableItems .. " (moving " .. missingItems .. ")"); |
156 | 146 |
157 Mover:AddMove(itemId, missingItems); | 147 Mover:AddMove(itemId, missingItems); |
158 else | 148 else |
159 print("Insufficient " .. select(2, GetItemInfo(itemId))); | 149 print("Insufficient " .. IdToItemLink(itemId)); |
160 end | 150 end |
161 end | 151 end |
162 end | 152 end |
163 end | 153 end |
164 end | 154 end |
165 | 155 |
166 self:ClearCache(); | 156 self:ClearCache(); |
167 | 157 |
168 self:Pause(); | 158 if Mover:HasMoves() then |
169 Mover:BeginMove(location, self.Unpause); | 159 StaticPopupDialogs["InventoriumRefill"] = { |
160 text = "There are items that can be refilled from this location, do you wish to proceed?", | |
161 button1 = YES, | |
162 button2 = NO, | |
163 OnAccept = function() | |
164 mod:Pause(); | |
165 Mover:BeginMove(location, self.Unpause); | |
166 end, | |
167 timeout = 0, | |
168 whileDead = 1, | |
169 hideOnEscape = 1, | |
170 }; | |
171 StaticPopup_Show("InventoriumRefill"); | |
172 end | |
173 end | |
174 | |
175 local function BANKFRAME_CLOSED() | |
176 addon:Debug("Scanner:BANKFRAME_CLOSED"); | |
177 | |
178 mod:UnregisterEvent("BANKFRAME_CLOSED"); | |
179 | |
180 StaticPopup_Hide("InventoriumRefill"); | |
170 end | 181 end |
171 | 182 |
172 local function BANKFRAME_OPENED() | 183 local function BANKFRAME_OPENED() |
184 addon:Debug("Scanner:BANKFRAME_OPENED"); | |
185 | |
186 mod:RegisterEvent("BANKFRAME_CLOSED", BANKFRAME_CLOSED); | |
187 | |
173 -- Scan once when the bank is opened, but no need to scan after | 188 -- Scan once when the bank is opened, but no need to scan after |
174 mod:Scan(addon.Locations.Bank); | 189 mod:Scan(addon.Locations.Bank); |
175 | |
176 addon:Debug("Scanner:BANKFRAME_OPENED"); | |
177 end | 190 end |
178 | 191 |
179 -- Remember which tabs were scanned and don't scan them again | 192 -- Remember which tabs were scanned and don't scan them again |
180 local guildBankTabsScanned = {}; | 193 local guildBankTabsScanned = {}; |
181 | 194 |
182 local function GUILDBANKFRAME_CLOSED() | 195 local function GUILDBANKFRAME_CLOSED() |
196 addon:Debug("Scanner:GUILDBANKFRAME_CLOSED"); | |
197 | |
198 table.wipe(guildBankTabsScanned); | |
199 | |
183 mod:UnregisterEvent("GUILDBANKFRAME_CLOSED"); | 200 mod:UnregisterEvent("GUILDBANKFRAME_CLOSED"); |
184 mod:UnregisterEvent("GUILDBANKBAGSLOTS_CHANGED"); | 201 mod:UnregisterEvent("GUILDBANKBAGSLOTS_CHANGED"); |
185 | 202 |
186 table.wipe(guildBankTabsScanned); | 203 StaticPopup_Hide("InventoriumRefill"); |
187 | |
188 addon:Debug("Scanner:GUILDBANKFRAME_CLOSED"); | |
189 end | 204 end |
190 | 205 |
191 local function GUILDBANKBAGSLOTS_CHANGED() | 206 local function GUILDBANKBAGSLOTS_CHANGED() |
192 if not guildBankTabsScanned[GetCurrentGuildBankTab()] then | 207 if not guildBankTabsScanned[GetCurrentGuildBankTab()] then |
208 addon:Debug("Scanner:GUILDBANKBAGSLOTS_CHANGED (" .. GetCurrentGuildBankTab() .. ") - scanning"); | |
209 | |
193 mod:Scan(addon.Locations.Guild); | 210 mod:Scan(addon.Locations.Guild); |
194 guildBankTabsScanned[GetCurrentGuildBankTab()] = true; | 211 guildBankTabsScanned[GetCurrentGuildBankTab()] = true; |
195 | |
196 addon:Debug("Scanner:GUILDBANKBAGSLOTS_CHANGED (" .. GetCurrentGuildBankTab() .. ") - scanning"); | |
197 else | 212 else |
198 addon:Debug("Scanner:GUILDBANKBAGSLOTS_CHANGED (" .. GetCurrentGuildBankTab() .. ") - not scanning"); | 213 addon:Debug("Scanner:GUILDBANKBAGSLOTS_CHANGED (" .. GetCurrentGuildBankTab() .. ") - not scanning"); |
199 end | 214 end |
200 end | 215 end |
201 | 216 |
202 local function GUILDBANKFRAME_OPENED() | 217 local function GUILDBANKFRAME_OPENED() |
218 addon:Debug("Scanner:GUILDBANKFRAME_OPENED"); | |
219 | |
203 table.wipe(guildBankTabsScanned); | 220 table.wipe(guildBankTabsScanned); |
204 | 221 |
205 mod:RegisterEvent("GUILDBANKFRAME_CLOSED", GUILDBANKFRAME_CLOSED); | 222 mod:RegisterEvent("GUILDBANKFRAME_CLOSED", GUILDBANKFRAME_CLOSED); |
206 mod:RegisterEvent("GUILDBANKBAGSLOTS_CHANGED", GUILDBANKBAGSLOTS_CHANGED); | 223 mod:RegisterEvent("GUILDBANKBAGSLOTS_CHANGED", GUILDBANKBAGSLOTS_CHANGED); |
207 | |
208 addon:Debug("Scanner:GUILDBANKFRAME_OPENED"); | |
209 end | 224 end |
210 | 225 |
211 function mod:OnEnable() | 226 function mod:OnEnable() |
212 -- Scan once when the bankframe is opened | 227 -- Scan once when the bankframe is opened |
213 mod:RegisterEvent("BANKFRAME_OPENED", BANKFRAME_OPENED); | 228 mod:RegisterEvent("BANKFRAME_OPENED", BANKFRAME_OPENED); |