annotate Core.lua @ 38:e27d13095b49

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