annotate Core.lua @ 14:6fc9fbaa94b3

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