annotate Core.lua @ 47:aae189bf32ce

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