annotate Core.lua @ 23:819bfdc5d73c

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