annotate Core.lua @ 9:374dd1a90d02

Changed the way things are stored so that items known only by name, usually from AH mail, will be stored by their name, but will get converted if the link is discovered through a tooltip. This version is funcioning again
author Asa Ayers <Asa.Ayers@Gmail.com>
date Fri, 25 Jun 2010 01:17:58 -0700
parents 0271e781b154
children c79ede3c7b82
rev   line source
Asa@3 1 local addonName, addonTable = ...;
Asa@3 2 _G[addonName] = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0", "AceBucket-3.0")
Asa@3 3 local addon = _G[addonName]
Asa@9 4 addonTable.ItemAuditor = addon
Asa@0 5
Asa@3 6 local utils = addonTable.utils
Asa@3 7
Asa@0 8
Asa@0 9 local WHITE = "|cFFFFFFFF"
Asa@0 10 local RED = "|cFFFF0000"
Asa@0 11 local GREEN = "|cFF00FF00"
Asa@0 12 local YELLOW = "|cFFFFFF00"
Asa@0 13 local ORANGE = "|cFFFF7F00"
Asa@0 14 local TEAL = "|cFF00FF9A"
Asa@0 15 local GOLD = "|cFFFFD700"
Asa@0 16
Asa@0 17 function addon:OnInitialize()
Asa@0 18 local DB_defaults = {
Asa@0 19 char = {
Asa@0 20 debug = false
Asa@0 21 },
Asa@0 22 factionrealm = {
Asa@8 23 item_account = {},
Asa@8 24
Asa@8 25 items = {},
Asa@0 26 },
Asa@0 27 }
Asa@0 28 self.db = LibStub("AceDB-3.0"):New("ItemAuditorDB", DB_defaults, true)
Asa@8 29 addonTable.db= self.db
Asa@8 30 self.items = self.db.factionrealm.items
Asa@0 31
Asa@0 32 self:RegisterOptions()
Asa@0 33
Asa@3 34 self:RegisterEvent("PLAYER_ENTERING_WORLD")
Asa@0 35 end
Asa@0 36
Asa@8 37 function addon:ConvertItems()
Asa@8 38 for itemName, value in pairs(self.db.factionrealm.item_account) do
Asa@8 39 local itemID = utils:GetItemID(itemName)
Asa@8 40 if itemID ~= nil then
Asa@8 41 self:GetItem('item:' .. itemID)
Asa@8 42 end
Asa@8 43 if value == 0 then
Asa@8 44 self.db.factionrealm.item_account[itemName] = nil
Asa@8 45 end
Asa@8 46 end
Asa@8 47
Asa@8 48 for link, data in pairs(self.db.factionrealm.items) do
Asa@8 49 if self:GetItem(link).count == 0 or self:GetItem(link).invested == 0 then
Asa@8 50 self:RemoveItem(link)
Asa@8 51 end
Asa@8 52 end
Asa@8 53 end
Asa@8 54
Asa@0 55 function addon:GetCurrentInventory()
Asa@8 56 local i = {}
Asa@8 57 local bagID
Asa@8 58 local slotID
Asa@8 59
Asa@8 60 for bagID = 0, NUM_BAG_SLOTS do
Asa@8 61 bagSize=GetContainerNumSlots(bagID)
Asa@8 62 for slotID = 0, bagSize do
Asa@8 63 local link= GetContainerItemLink(bagID, slotID);
Asa@8 64
Asa@8 65 if link ~= nil and i[link] == nil then
Asa@8 66 i[link] = GetItemCount(link);
Asa@8 67 end
Asa@8 68 end
Asa@8 69
Asa@8 70 end
Asa@8 71 return {items = i, money = GetMoney()}
Asa@0 72 end
Asa@0 73
Asa@0 74 function addon:GetInventoryDiff(pastInventory, current)
Asa@8 75 if current == nil then
Asa@8 76 current = self:GetCurrentInventory()
Asa@8 77 end
Asa@8 78 local diff = {}
Asa@8 79
Asa@8 80 for link, count in pairs(current.items) do
Asa@8 81 if pastInventory.items[link] == nil then
Asa@8 82 diff[link] = count
Asa@8 83 -- self:Debug("1 diff[" .. name .. "]=" .. diff[name])
Asa@8 84 elseif count - pastInventory.items[link] ~= 0 then
Asa@8 85 diff[link] = count - pastInventory.items[link]
Asa@8 86 -- self:Debug("2 diff[" .. name .. "]=" .. diff[name])
Asa@8 87 end
Asa@8 88 end
Asa@8 89
Asa@8 90 for link, count in pairs(pastInventory.items) do
Asa@8 91 if current.items[link] == nil then
Asa@8 92 diff[link] = -count
Asa@8 93 -- self:Debug("3 diff[" .. name .. "]=" .. diff[name])
Asa@8 94 elseif current.items[link] - count ~= 0 then
Asa@8 95 diff[link] = current.items[link] - pastInventory.items[link]
Asa@8 96 -- self:Debug("4 diff[" .. name .. "]=" .. diff[name])
Asa@8 97 end
Asa@8 98 end
Asa@8 99
Asa@8 100 local moneyDiff = current.money - pastInventory.money
Asa@8 101
Asa@8 102 return {items = diff, money = moneyDiff}
Asa@0 103 end
Asa@0 104
Asa@0 105
Asa@6 106
Asa@0 107 function addon:ScanMail()
Asa@0 108 local results = {}
Asa@0 109 for mailIndex = 1, GetInboxNumItems() or 0 do
Asa@0 110 local sender, msgSubject, msgMoney, msgCOD, _, msgItem, _, _, msgText, _, isGM = select(3, GetInboxHeaderInfo(mailIndex))
Asa@6 111 local mailType = utils:GetMailType(msgSubject)
Asa@6 112
Asa@6 113 results[mailType] = (results[mailType] or {})
Asa@6 114
Asa@5 115 if mailType == "NonAHMail" and msgCOD > 0 then
Asa@9 116 --[[
Asa@6 117 mailType = 'COD'
Asa@6 118 results[mailType] = (results[mailType] or {})
Asa@5 119
Asa@5 120 local itemTypes = {}
Asa@5 121 for itemIndex = 1, ATTACHMENTS_MAX_RECEIVE do
Asa@5 122 local itemName, _, count, _, _= GetInboxItem(mailIndex, itemIndex)
Asa@5 123 if itemName ~= nil then
Asa@7 124 itemTypdes[itemName] = (itemTypes[itemName] or 0) + count
Asa@5 125 end
Asa@5 126 end
Asa@5 127
Asa@5 128 if utils:tcount(itemTypes) == 1 then
Asa@5 129 for itemName, count in pairs(itemTypes) do
Asa@6 130 results[mailType][itemName] = (results[mailType][itemName] or 0) - msgCOD
Asa@5 131 end
Asa@5 132 else
Asa@5 133 self:Debug("Don't know what to do with more than one item type on COD mail.")
Asa@5 134 end
Asa@9 135 ]]
Asa@6 136 elseif mailType == "CODPayment" then
Asa@6 137 itemName = msgSubject:gsub(utils.SubjectPatterns[mailType], function(item) return item end)
Asa@5 138
Asa@9 139 results[mailType][itemName] = (results[mailType][itemName] or 0) - msgMoney
Asa@5 140
Asa@0 141 elseif mailType == "AHSuccess" then
Asa@0 142 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@9 143 results[mailType][itemName] = (results[mailType][itemName] or 0) - deposit - buyout + consignment
Asa@0 144
Asa@0 145 elseif mailType == "AHWon" then
Asa@0 146 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@9 147 results[mailType][itemName] = (results[mailType][itemName] or 0) + bid
Asa@5 148 elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
Asa@0 149 -- These should be handled when you pay the deposit at the AH
Asa@0 150 else
Asa@0 151 self:Debug("Unhandled mail type: " .. mailType)
Asa@0 152 self:Debug(msgSubject)
Asa@0 153 end
Asa@0 154
Asa@0 155 end
Asa@0 156 return results
Asa@0 157 end
Asa@0 158
Asa@9 159 function addon:GetItem(link, viewOnly)
Asa@9 160 if viewOnly == nil then
Asa@9 161 viewOnly = false
Asa@9 162 end
Asa@8 163
Asa@9 164 local itemName = nil
Asa@9 165 if self:GetSafeLink(link) == nil then
Asa@9 166 itemName = link
Asa@9 167 else
Asa@9 168 link = self:GetSafeLink(link)
Asa@9 169 itemName = GetItemInfo(link)
Asa@9 170 end
Asa@9 171
Asa@9 172 if self.db.factionrealm.item_account[itemName] ~= nil then
Asa@8 173 self.items[link] = {
Asa@8 174 count = Altoholic:GetItemCount(utils:GetIDFromLink(link)),
Asa@8 175 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@8 176 }
Asa@8 177 self.db.factionrealm.item_account[itemName] = nil
Asa@8 178 end
Asa@8 179
Asa@9 180 if viewOnly == false and self.items[link] == nil then
Asa@9 181 local itemName = GetItemInfo(link)
Asa@9 182
Asa@9 183 self.items[link] = {
Asa@9 184 count = Altoholic:GetItemCount(self:GetIDFromLink(link)),
Asa@9 185 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@9 186 }
Asa@9 187
Asa@9 188 end
Asa@9 189
Asa@9 190
Asa@9 191
Asa@9 192 if viewOnly == true and self.items[link] == nil then
Asa@9 193 return {count = 0, invested = 0}
Asa@9 194 elseif viewOnly == true then
Asa@9 195 return {count = self.items[link].count, invested = self.items[link].invested}
Asa@9 196 end
Asa@8 197 return self.items[link]
Asa@8 198 end
Asa@8 199
Asa@8 200 function addon:RemoveItem(link)
Asa@9 201 self.db.factionrealm.item_account[link] = nil
Asa@9 202 link = self:GetSafeLink(link)
Asa@9 203 if link ~= nil then
Asa@9 204 self.items[link] = nil
Asa@9 205 end
Asa@8 206 end
Asa@9 207 --[[
Asa@9 208 ItemAuditor:SaveValue('Scroll of Enchant Weapon - Exceptional Spellpower', 1)
Asa@8 209
Asa@9 210 DevTools_Dump(ItemAuditor.db.factionrealm.item_account)
Asa@9 211
Asa@9 212 = ItemAuditor:GetItem('Scroll of Enchant Weapon - Exceptional Spellpower', true).invested
Asa@9 213 ]]
Asa@8 214 function addon:SaveValue(link, value)
Asa@9 215 local item = nil
Asa@9 216 local realLink = self:GetSafeLink(link)
Asa@9 217 local itemName = nil
Asa@9 218 if realLink == nil then
Asa@9 219 itemName = link
Asa@9 220
Asa@9 221 self.db.factionrealm.item_account[itemName] = (self.db.factionrealm.item_account[itemName] or 0) + value
Asa@9 222
Asa@9 223 item = {invested = self.db.factionrealm.item_account[itemName], count = 1}
Asa@9 224 else
Asa@6 225
Asa@9 226 item = self:GetItem(realLink)
Asa@6 227
Asa@9 228 item.invested = item.invested + value
Asa@9 229
Asa@9 230 itemName = GetItemInfo(realLink)
Asa@9 231 end
Asa@8 232
Asa@7 233 if abs(value) > 0 then
Asa@8 234 self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(item.invested) .. "(change: " .. utils:FormatMoney(value) .. ")")
Asa@7 235 end
Asa@0 236
Asa@9 237 if item.invested <= 0 then
Asa@8 238 self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(0))
Asa@8 239 self:RemoveItem(link)
Asa@9 240 elseif item.count == 0 then
Asa@8 241 self:Print("You ran out of " .. itemName .. " and never recovered " .. utils:FormatMoney(item.invested))
Asa@8 242 self:RemoveItem(link)
Asa@0 243 end
Asa@0 244 end
Asa@0 245
Asa@4 246 local defaultBagDelay = 0.2
Asa@4 247
Asa@3 248 function addon:WatchBags(delay)
Asa@4 249 delay = delay or defaultBagDelay
Asa@4 250 if delay ~= self.currentBagDelay then
Asa@4 251 self:UnwatchBags()
Asa@4 252 end
Asa@4 253
Asa@4 254 if self.watch_handle == nil then
Asa@4 255 self.currentBagDelay = delay
Asa@4 256 self:Debug("currentBagDelay = " .. delay)
Asa@4 257 addon:UpdateCurrentInventory()
Asa@4 258 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, self.currentBagDelay, "UpdateAudit")
Asa@4 259 end
Asa@0 260 end
Asa@0 261
Asa@0 262 function addon:UnwatchBags()
Asa@4 263 if self.watch_handle ~= nil then
Asa@4 264 self:UnregisterBucket(self.watch_handle)
Asa@4 265 self.watch_handle = nil
Asa@4 266 end
Asa@0 267 end
Asa@0 268
Asa@9 269 function addon:GetItemID(itemName)
Asa@9 270 return utils:GetItemID(itemName)
Asa@9 271 end
Asa@9 272
Asa@9 273 function addon:GetSafeLink(link)
Asa@9 274 local newLink = nil
Asa@9 275
Asa@9 276 if link ~= string.match(link, '.-:[-0-9]+[:0-9]*') then
Asa@9 277 newLink = link and string.match(link, "|H(.-):([-0-9]+):([0-9]+)|h")
Asa@9 278 end
Asa@9 279 if newLink == nil then
Asa@9 280 local itemID = self:GetItemID(link)
Asa@9 281 if itemID ~= nil then
Asa@9 282 _, newLink = GetItemInfo(itemID)
Asa@9 283 return self:GetSafeLink(newLink)
Asa@9 284 end
Asa@9 285 end
Asa@9 286 return newLink and string.gsub(newLink, ":0:0:0:0:0:0", "")
Asa@9 287 end
Asa@9 288
Asa@9 289 function addon:GetIDFromLink(link)
Asa@9 290 local _, _, _, _, Id = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@9 291 return tonumber(Id)
Asa@9 292 end
Asa@9 293
Asa@8 294 function addon:GetItemCost(link, countModifier)
Asa@9 295 local item = self:GetItem(link, true)
Asa@8 296
Asa@9 297 if item.invested > 0 then
Asa@9 298 local count = item.count
Asa@9 299
Asa@9 300 if countModifier ~= nil then
Asa@9 301 count = count - countModifier
Asa@0 302 end
Asa@9 303 if count > 0 then
Asa@9 304 return ceil(item.invested), ceil(item.invested/item.count), count
Asa@9 305 end
Asa@9 306
Asa@0 307 end
Asa@0 308 return 0, 0, 0
Asa@0 309 end