annotate Core.lua @ 59:4ec321eb0dfe

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