annotate Core.lua @ 22:66b7c3f5937e

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