annotate Core.lua @ 65:32d53abee666

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