annotate Core.lua @ 138:7d258c041b11

Fixed an issue with /ia invested that caused all items to display twice. This was introduced by the fix in Ticket 42. At this point ItemAuditor watches mail for auctions sold or purchased, watches for buy/sell (money and 1 item type change) and conversions/tradeskills. Milling isn't working yet because there is too much time between the first event and the last event.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sat, 02 Oct 2010 20:34:07 -0700
parents 526036e4358f
children 7e1496b25311
rev   line source
Asa@63 1 local ItemAuditor = select(2, ...)
Asa@137 2 local Utils
Asa@63 3 ItemAuditor = LibStub("AceAddon-3.0"):NewAddon(ItemAuditor, "ItemAuditor", "AceEvent-3.0", "AceBucket-3.0")
Asa@65 4 --@debug@
Asa@65 5 _G['ItemAuditor'] = ItemAuditor
Asa@65 6 --@end-debug@
Asa@0 7
Asa@87 8 if not DevTools_Dump then
Asa@87 9 function DevTools_Dump()
Asa@87 10 end
Asa@87 11 end
Asa@87 12
Asa@98 13 local allMailboxes = {}
Asa@98 14 local myMailbox = {}
Asa@67 15
Asa@67 16 ItemAuditor.Options = {
Asa@67 17 handler = ItemAuditor,
Asa@67 18 name = "ItemAuditor @project-version@",
Asa@67 19 type = 'group',
Asa@67 20 args = {
Asa@67 21 options = {
Asa@67 22 type = "execute",
Asa@67 23 name = "options",
Asa@67 24 desc = "Show Blizzard's options GUI",
Asa@67 25 func = "ShowOptionsGUI",
Asa@67 26 guiHidden = true,
Asa@67 27 },
Asa@67 28 debug = {
Asa@67 29 type = "execute",
Asa@67 30 name = "debug",
Asa@67 31 desc = "Shows the debug frame",
Asa@67 32 func = function() ItemAuditor_DebugFrame:Show() end,
Asa@67 33 guiHidden = true,
Asa@67 34 },
Asa@67 35 suspend = {
Asa@67 36 type = "toggle",
Asa@67 37 name = "suspend",
Asa@67 38 desc = "Suspends ItemAuditor",
Asa@67 39 get = "IsEnabled",
Asa@67 40 set = "SetEnabled",
Asa@67 41 guiHidden = true,
Asa@67 42 },
Asa@67 43 },
Asa@67 44 }
Asa@67 45
Asa@100 46 ItemAuditor.DB_defaults = {
Asa@100 47 char = {
Asa@100 48 ah = 1,
Asa@100 49 use_quick_auctions = false,
Asa@101 50 profitable_threshold = 10000,
Asa@100 51 auction_threshold = 0.15,
Asa@111 52 auction_threshold_value = 0,
Asa@100 53 qa_extra = 0,
Asa@100 54 output_chat_frame = nil,
Asa@109 55 cod_warnings = true,
Asa@100 56 },
Asa@100 57 profile = {
Asa@100 58 messages = {
Asa@100 59 cost_updates = true,
Asa@100 60 queue_skip = false,
Asa@100 61 },
Asa@100 62 ItemAuditor_enabled = true,
Asa@100 63 queue_destination = nil,
Asa@100 64 disabled_deciders = {},
Asa@100 65 pricing_method = 'low',
Asa@100 66 },
Asa@100 67 factionrealm = {
Asa@100 68 item_account = {},
Asa@100 69 items = {},
Asa@100 70 outbound_cod = {},
Asa@100 71 mailbox = {},
Asa@132 72 enabled_guilds = {},
Asa@100 73 },
Asa@100 74 }
Asa@100 75
Asa@63 76 function ItemAuditor:OnInitialize()
Asa@137 77 Utils = self:GetModule("Utils")
Asa@100 78 self.db = LibStub("AceDB-3.0"):New("ItemAuditorDB", ItemAuditor.DB_defaults, true)
Asa@98 79
Asa@98 80 allMailboxes = self.db.factionrealm.mailbox
Asa@98 81 if not allMailboxes[UnitName("player")] then
Asa@98 82 allMailboxes[UnitName("player")] = {}
Asa@98 83 end
Asa@98 84 myMailbox = allMailboxes[UnitName("player")]
Asa@98 85
Asa@67 86 self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("ItemAuditor", "ItemAuditor")
Asa@133 87
Asa@67 88 LibStub("AceConfig-3.0"):RegisterOptionsTable("ItemAuditor", ItemAuditor.Options, {"ia"})
Asa@38 89 ItemAuditor:RegisterFrame(ItemAuditor_DebugFrame)
Asa@133 90
Asa@86 91 LibStub("AceConsole-3.0"):RegisterChatCommand('rl', ReloadUI)
Asa@100 92
Asa@101 93 if self.db.char.crafting_threshold then
Asa@101 94 local threshold = self.db.char.crafting_threshold
Asa@101 95 if threshold == 1 then
Asa@101 96 self.db.char.profitable_threshold = 5000
Asa@101 97 elseif threshold == 2 then
Asa@101 98 self.db.char.profitable_threshold = 10000
Asa@101 99 elseif threshold == 3 then
Asa@101 100 self.db.char.profitable_threshold = 50000
Asa@101 101 end
Asa@133 102
Asa@101 103 self.db.char.crafting_threshold = nil
Asa@101 104 end
Asa@101 105
Asa@119 106 ItemAuditor:IsQAEnabled()
Asa@112 107
Asa@65 108 --@debug@
Asa@59 109 -- ItemAuditor_DebugFrame:Show()
Asa@59 110 -- self:CreateFrame('tab_crafting')
Asa@74 111 self:RegisterEvent("TRADE_SKILL_SHOW", function()
Asa@93 112 ItemAuditor:DisplayCrafting()
Asa@74 113 end)
Asa@65 114 --@end-debug@
Asa@0 115 end
Asa@0 116
Asa@67 117
Asa@67 118
Asa@38 119 local registeredEvents = {}
Asa@133 120 local originalRegisterEvent = ItemAuditor.RegisterEvent
Asa@63 121 function ItemAuditor:RegisterEvent(event, callback, arg)
Asa@38 122 registeredEvents[event] = true
Asa@38 123 if arg ~= nil then
Asa@38 124 return originalRegisterEvent(self, event, callback, arg)
Asa@38 125 elseif callback ~= nil then
Asa@38 126 return originalRegisterEvent(self, event, callback)
Asa@38 127 else
Asa@38 128 return originalRegisterEvent(self, event)
Asa@38 129 end
Asa@38 130 end
Asa@38 131
Asa@63 132 local originalUnregisterEvent = ItemAuditor.UnregisterEvent
Asa@63 133 function ItemAuditor:UnregisterEvent(event)
Asa@38 134 registeredEvents[event] = nil
Asa@38 135 return originalUnregisterEvent(self, event)
Asa@38 136 end
Asa@38 137
Asa@63 138 function ItemAuditor:UnregisterAllEvents()
Asa@38 139 for event in pairs(registeredEvents) do
Asa@38 140 self:UnregisterEvent(event)
Asa@38 141 end
Asa@38 142 end
Asa@38 143
Asa@38 144 local registeredFrames = {}
Asa@63 145 function ItemAuditor:RegisterFrame(frame)
Asa@38 146 tinsert(registeredFrames, frame)
Asa@38 147 end
Asa@38 148
Asa@63 149 function ItemAuditor:HideAllFrames()
Asa@38 150 for key, frame in pairs(registeredFrames) do
Asa@38 151 if frame then
Asa@38 152 frame:Hide()
Asa@38 153 end
Asa@38 154 end
Asa@38 155 end
Asa@38 156
Asa@63 157 function ItemAuditor:ConvertItems()
Asa@8 158 for itemName, value in pairs(self.db.factionrealm.item_account) do
Asa@15 159 local itemID = self:GetItemID(itemName)
Asa@8 160 if itemID ~= nil then
Asa@8 161 self:GetItem('item:' .. itemID)
Asa@8 162 end
Asa@8 163 if value == 0 then
Asa@8 164 self.db.factionrealm.item_account[itemName] = nil
Asa@8 165 end
Asa@8 166 end
Asa@133 167
Asa@8 168 for link, data in pairs(self.db.factionrealm.items) do
Asa@8 169 if self:GetItem(link).count == 0 or self:GetItem(link).invested == 0 then
Asa@8 170 self:RemoveItem(link)
Asa@8 171 end
Asa@10 172 end
Asa@133 173
Asa@12 174 self:RefreshQAGroups()
Asa@12 175 end
Asa@12 176
Asa@133 177 -- Options doesn't exist when this file is created the first time, so getOptions will
Asa@133 178 -- make one call to :GetModule and return the result and replace itself with a
Asa@65 179 -- function that simply returns the same object. The permanent solution will probably be
Asa@65 180 -- to move :Print to a different module.
Asa@65 181 local function getOptions()
Asa@65 182 local Options = ItemAuditor:GetModule("Options")
Asa@65 183 getOptions = function() return Options end
Asa@65 184 return Options
Asa@65 185 end
Asa@65 186
Asa@24 187 local printPrefix = "|cFFA3CEFFItemAuditor|r: "
Asa@63 188 function ItemAuditor:Print(message, ...)
Asa@24 189 message = format(message, ...)
Asa@65 190 getOptions().GetSelectedChatWindow():AddMessage( printPrefix .. tostring(message))
Asa@16 191 end
Asa@16 192
Asa@81 193 local bankOpen = false
Asa@81 194
Asa@81 195 function ItemAuditor:BankFrameChanged(event)
Asa@81 196 bankOpen = (event == 'BANKFRAME_OPENED')
Asa@81 197 ItemAuditor:UpdateCurrentInventory()
Asa@81 198 end
Asa@81 199
Asa@80 200 local function scanBag(bagID, i)
Asa@80 201 bagSize=GetContainerNumSlots(bagID)
Asa@80 202 for slotID = 0, bagSize do
Asa@80 203 local link= GetContainerItemLink(bagID, slotID);
Asa@137 204 itemID = link and Utils.GetItemID(link)
Asa@80 205
Asa@137 206 if link and itemID and i[itemID] == nil then
Asa@137 207 i[itemID] = GetItemCount(itemID, bankOpen);
Asa@80 208 end
Asa@80 209 end
Asa@80 210 end
Asa@80 211
Asa@63 212 function ItemAuditor:GetCurrentInventory()
Asa@8 213 local i = {}
Asa@8 214 local bagID
Asa@8 215 local slotID
Asa@133 216
Asa@8 217 for bagID = 0, NUM_BAG_SLOTS do
Asa@80 218 scanBag(bagID, i)
Asa@8 219 end
Asa@133 220
Asa@81 221 if bankOpen then
Asa@81 222 scanBag(BANK_CONTAINER, i)
Asa@81 223 for bagID = NUM_BAG_SLOTS+1, NUM_BANKBAGSLOTS do
Asa@81 224 scanBag(bagID, i)
Asa@81 225 end
Asa@80 226 end
Asa@133 227
Asa@8 228 return {items = i, money = GetMoney()}
Asa@0 229 end
Asa@0 230
Asa@63 231 function ItemAuditor:GetInventoryDiff(pastInventory, current)
Asa@8 232 if current == nil then
Asa@8 233 current = self:GetCurrentInventory()
Asa@8 234 end
Asa@8 235 local diff = {}
Asa@8 236
Asa@137 237 for itemID, count in pairs(current.items) do
Asa@137 238 if pastInventory.items[itemID] == nil then
Asa@137 239 diff[itemID] = count
Asa@137 240 self:Debug("1 diff[" .. itemID .. "]=" .. diff[itemID])
Asa@137 241 elseif count - pastInventory.items[itemID] ~= 0 then
Asa@137 242 diff[itemID] = count - pastInventory.items[itemID]
Asa@137 243 self:Debug("2 diff[" .. itemID .. "]=" .. diff[itemID])
Asa@133 244 end
Asa@8 245 end
Asa@8 246
Asa@137 247 for itemID, count in pairs(pastInventory.items) do
Asa@137 248 if current.items[itemID] == nil then
Asa@137 249 diff[itemID] = -count
Asa@137 250 self:Debug("3 diff[" .. liitemIDnk .. "]=" .. diff[itemID])
Asa@137 251 elseif current.items[itemID] - count ~= 0 then
Asa@137 252 diff[itemID] = current.items[itemID] - pastInventory.items[itemID]
Asa@137 253 self:Debug("4 diff[" .. itemID .. "]=" .. diff[itemID])
Asa@8 254 end
Asa@8 255 end
Asa@8 256
Asa@8 257 local moneyDiff = current.money - pastInventory.money
Asa@23 258 if abs(moneyDiff) > 0 then
Asa@23 259 self:Debug("moneyDiff: " .. moneyDiff)
Asa@23 260 end
Asa@8 261
Asa@8 262 return {items = diff, money = moneyDiff}
Asa@0 263 end
Asa@0 264
Asa@39 265 local inboundCOD = {}
Asa@39 266 local skipMail = {}
Asa@63 267 function ItemAuditor:ScanMail()
Asa@0 268 local results = {}
Asa@39 269 local CODPaymentRegex = gsub(COD_PAYMENT, "%%s", "(.*)")
Asa@133 270
Asa@0 271 for mailIndex = 1, GetInboxNumItems() or 0 do
Asa@39 272 local sender, msgSubject, msgMoney, msgCOD, daysLeft, msgItem, _, _, msgText, _, isGM = select(3, GetInboxHeaderInfo(mailIndex))
Asa@15 273 local mailType = self:GetMailType(msgSubject)
Asa@133 274
Asa@39 275 local mailSignature = msgSubject .. '-' .. msgMoney .. '-' .. msgCOD .. '-' .. daysLeft
Asa@133 276
Asa@6 277 results[mailType] = (results[mailType] or {})
Asa@133 278
Asa@39 279 if skipMail[mailSignature] ~= nil then
Asa@39 280 -- do nothing
Asa@39 281 elseif mailType == "NonAHMail" and msgCOD > 0 then
Asa@6 282 mailType = 'COD'
Asa@6 283 results[mailType] = (results[mailType] or {})
Asa@133 284
Asa@5 285 local itemTypes = {}
Asa@5 286 for itemIndex = 1, ATTACHMENTS_MAX_RECEIVE do
Asa@5 287 local itemName, _, count, _, _= GetInboxItem(mailIndex, itemIndex)
Asa@5 288 if itemName ~= nil then
Asa@39 289 itemTypes[itemName] = (itemTypes[itemName] or 0) + count
Asa@5 290 end
Asa@5 291 end
Asa@133 292
Asa@15 293 if self:tcount(itemTypes) == 1 then
Asa@5 294 for itemName, count in pairs(itemTypes) do
Asa@39 295 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@39 296 results[mailType][itemName].total = results[mailType][itemName].total + msgCOD
Asa@133 297
Asa@39 298 if inboundCOD[mailSignature] == nil then
Asa@39 299 results[mailType][itemName].count = results[mailType][itemName].count + count
Asa@39 300 inboundCOD[mailSignature] = (inboundCOD[mailSignature] or 0) + count
Asa@39 301 else
Asa@39 302 results[mailType][itemName].count = inboundCOD[mailSignature]
Asa@39 303 end
Asa@133 304
Asa@133 305
Asa@5 306 end
Asa@5 307 else
Asa@5 308 self:Debug("Don't know what to do with more than one item type on COD mail.")
Asa@5 309 end
Asa@133 310 elseif mailType == "CODPayment" then
Asa@39 311 -- /dump ItemAuditor.db.factionrealm.outbound_cod
Asa@39 312 self:Debug(msgSubject)
Asa@39 313 self:Debug(CODPaymentRegex)
Asa@39 314 local outboundSubject = select(3, msgSubject:find(CODPaymentRegex))
Asa@39 315 local trackID
Asa@39 316 if outboundSubject ~= nil then
Asa@39 317 self:Debug(outboundSubject)
Asa@45 318 trackID = select(3, outboundSubject:find('[[]IA: (%d*)[]]'))
Asa@133 319
Asa@39 320 if trackID ~= nil then
Asa@45 321 trackID = tonumber(trackID)
Asa@45 322 self:Debug('COD ID: %s', trackID)
Asa@39 323 local cod = self.db.factionrealm.outbound_cod[trackID]
Asa@39 324 if cod == nil then
Asa@39 325 skipMail[mailSignature] = true
Asa@39 326 self:Print("WARNING: {%s} has an invalid ItemAuditor tracking number.", msgSubject)
Asa@39 327 else
Asa@39 328 itemName = trackID .. "|" .. cod['link']
Asa@133 329
Asa@133 330
Asa@39 331 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@39 332 results[mailType][itemName].total = results[mailType][itemName].total - msgMoney
Asa@39 333 results[mailType][itemName].count = results[mailType][itemName].count - cod.count
Asa@39 334 end
Asa@39 335 end
Asa@39 336 end
Asa@133 337
Asa@39 338 if trackID == nil then
Asa@39 339 skipMail[mailSignature] = true
Asa@39 340 self:Print("WARNING: {%s} is a COD payment but doesn't have an ItemAuditor tracking number.", msgSubject)
Asa@39 341 end
Asa@133 342
Asa@0 343 elseif mailType == "AHSuccess" then
Asa@0 344 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@26 345 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@26 346 results[mailType][itemName].total = results[mailType][itemName].total - deposit - buyout + consignment
Asa@133 347
Asa@0 348
Asa@0 349 elseif mailType == "AHWon" then
Asa@0 350 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@26 351 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@26 352 results[mailType][itemName].total = results[mailType][itemName].total + bid
Asa@133 353
Asa@98 354 local count = select(3, GetInboxItem(mailIndex,1))
Asa@26 355 results[mailType][itemName].count = results[mailType][itemName].count + count
Asa@5 356 elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
Asa@0 357 -- These should be handled when you pay the deposit at the AH
Asa@0 358 else
Asa@24 359 -- self:Debug("Unhandled mail type: " .. mailType)
Asa@24 360 -- self:Debug(msgSubject)
Asa@0 361 end
Asa@0 362
Asa@0 363 end
Asa@98 364
Asa@98 365 wipe(myMailbox)
Asa@23 366 for mailType, collection in pairs(results) do
Asa@98 367 myMailbox[mailType] = {}
Asa@26 368 for item, data in pairs(collection) do
Asa@98 369 myMailbox[mailType][item] = {
Asa@98 370 total = data.total,
Asa@98 371 count = data.count,
Asa@98 372 }
Asa@98 373 -- self:Print(format("|cFF00FF00MailScan|r: %s - %s - %s x %s", mailType, item, data.total, data.count))
Asa@23 374 end
Asa@23 375 end
Asa@133 376 return results
Asa@0 377 end
Asa@0 378
Asa@105 379 local realm = GetRealmName()
Asa@105 380 local ds_account = 'Default'
Asa@82 381 function ItemAuditor:GetItemCount(searchID)
Asa@105 382 local count = 0
Asa@105 383 for _, character in pairs(DataStore:GetCharacters(realm, ds_account)) do
Asa@136 384 if DataStore:GetCharacterFaction(character) == UnitFactionGroup("player") then
Asa@136 385 local bag, bank = DataStore:GetContainerItemCount(character, searchID)
Asa@136 386 count = count + (bag or 0) + (bank or 0)
Asa@136 387 count = count + (DataStore:GetAuctionHouseItemCount(character, searchID) or 0)
Asa@136 388 count = count + (DataStore:GetInventoryItemCount(character, searchID) or 0)
Asa@136 389 count = count + (DataStore:GetMailItemCount(character, searchID) or 0)
Asa@136 390 count = count + (DataStore:GetCurrencyItemCount(character, searchID) or 0)
Asa@136 391 end
Asa@105 392 end
Asa@132 393 for guildName in pairs(self.db.factionrealm.enabled_guilds) do
Asa@132 394 count = count + DataStore:GetGuildBankItemCount(DataStore:GetGuilds()[guildName], searchID)
Asa@132 395 end
Asa@132 396
Asa@98 397 local itemName = GetItemInfo(searchID)
Asa@98 398 for character, mailbox in pairs(allMailboxes) do
Asa@98 399 for type, items in pairs(mailbox) do
Asa@98 400 if type == 'AHWon' or type == 'COD' then
Asa@98 401 for name, data in pairs(items) do
Asa@98 402 if name == itemName then
Asa@98 403 count = count - data.count
Asa@82 404
Asa@98 405 end
Asa@98 406 end
Asa@98 407 end
Asa@98 408 end
Asa@82 409 end
Asa@82 410 return count
Asa@82 411 end
Asa@82 412
Asa@63 413 function ItemAuditor:GetItem(link, viewOnly)
Asa@9 414 if viewOnly == nil then
Asa@9 415 viewOnly = false
Asa@9 416 end
Asa@133 417
Asa@9 418 local itemName = nil
Asa@9 419 if self:GetSafeLink(link) == nil then
Asa@9 420 itemName = link
Asa@9 421 else
Asa@9 422 link = self:GetSafeLink(link)
Asa@9 423 itemName = GetItemInfo(link)
Asa@9 424 end
Asa@133 425
Asa@133 426
Asa@9 427 if self.db.factionrealm.item_account[itemName] ~= nil then
Asa@65 428 self.db.factionrealm.items[link] = {
Asa@82 429 count = ItemAuditor:GetItemCount(self:GetIDFromLink(link)),
Asa@8 430 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@8 431 }
Asa@8 432 self.db.factionrealm.item_account[itemName] = nil
Asa@8 433 end
Asa@133 434
Asa@65 435 if viewOnly == false and self.db.factionrealm.items[link] == nil then
Asa@133 436
Asa@65 437 self.db.factionrealm.items[link] = {
Asa@82 438 count = ItemAuditor:GetItemCount(self:GetIDFromLink(link)),
Asa@9 439 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@9 440 }
Asa@133 441
Asa@9 442 end
Asa@133 443
Asa@65 444 if self.db.factionrealm.items[link] ~= nil then
Asa@82 445 self.db.factionrealm.items[link].count = ItemAuditor:GetItemCount(self:GetIDFromLink(link))
Asa@133 446
Asa@65 447 if self.db.factionrealm.items[link].invested == nil then
Asa@65 448 self.db.factionrealm.items[link].invested = 0
Asa@45 449 end
Asa@37 450 end
Asa@133 451
Asa@65 452 if viewOnly == true and self.db.factionrealm.items[link] == nil then
Asa@9 453 return {count = 0, invested = 0}
Asa@9 454 elseif viewOnly == true then
Asa@133 455
Asa@65 456 return {count = self.db.factionrealm.items[link].count, invested = self.db.factionrealm.items[link].invested}
Asa@9 457 end
Asa@133 458
Asa@133 459
Asa@133 460
Asa@65 461 return self.db.factionrealm.items[link]
Asa@8 462 end
Asa@8 463
Asa@63 464 function ItemAuditor:RemoveItem(link)
Asa@9 465 self.db.factionrealm.item_account[link] = nil
Asa@9 466 link = self:GetSafeLink(link)
Asa@9 467 if link ~= nil then
Asa@63 468 local item = ItemAuditor:GetItem(link)
Asa@35 469 item.invested = 0
Asa@24 470 else
Asa@24 471 self:Debug('Failed to convert link' .. tostring(link))
Asa@9 472 end
Asa@8 473 end
Asa@8 474
Asa@63 475 function ItemAuditor:SaveValue(link, value, countChange)
Asa@26 476 self:Debug("SaveValue(%s, %s, %s)", tostring(link), value, (countChange or 'default'))
Asa@26 477 countChange = countChange or 0
Asa@9 478 local item = nil
Asa@9 479 local realLink = self:GetSafeLink(link)
Asa@9 480 local itemName = nil
Asa@9 481 if realLink == nil then
Asa@26 482 itemName = link
Asa@23 483 self:Debug('SaveValue: GetSafeLink failed, falling back to storing by name: ' .. tostring(itemName))
Asa@9 484 self.db.factionrealm.item_account[itemName] = (self.db.factionrealm.item_account[itemName] or 0) + value
Asa@9 485 item = {invested = self.db.factionrealm.item_account[itemName], count = 1}
Asa@9 486 else
Asa@133 487
Asa@9 488 item = self:GetItem(realLink)
Asa@9 489 item.invested = item.invested + value
Asa@9 490 itemName = GetItemInfo(realLink)
Asa@9 491 end
Asa@133 492
Asa@26 493 if value > 0 and countChange > 0 and item.invested == value and item.count ~= countChange then
Asa@26 494 local costPerItem = value / countChange
Asa@26 495 value = costPerItem * item.count
Asa@26 496 item.invested = value
Asa@26 497 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 498 end
Asa@133 499
Asa@7 500 if abs(value) > 0 then
Asa@22 501 if item.invested < 0 then
Asa@16 502 if self.db.profile.messages.cost_updates then
Asa@103 503 self:Print(format("Updated price of %s from %s to %s. |cFF00FF00You just made a profit of %s.", itemName, self:FormatMoney(item.invested - value), self:FormatMoney(0), self:FormatMoney(abs(item.invested))))
Asa@16 504 end
Asa@12 505 self:RemoveItem(link)
Asa@12 506 -- This doesn't work when you mail the only copy of an item you have to another character.
Asa@12 507 --[[
Asa@133 508 elseif item.count == 0 and realLink and ItemAuditor:GetItemCount(self:GetIDFromLink(realLink)) then
Asa@15 509 self:Print("You ran out of " .. itemName .. " and never recovered " .. self:FormatMoney(item.invested))
Asa@12 510 self:RemoveItem(link)
Asa@12 511 ]]
Asa@16 512 else
Asa@16 513 if self.db.profile.messages.cost_updates then
Asa@16 514 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 515 end
Asa@12 516 end
Asa@0 517 end
Asa@133 518
Asa@10 519 if realLink ~= nil then
Asa@63 520 ItemAuditor:UpdateQAThreshold(realLink)
Asa@112 521 self:SendMessage("IA_COST_CHANGED", realLink, unpack({ItemAuditor:GetItemCost(realLink)}))
Asa@10 522 end
Asa@35 523 UpdateInvestedData()
Asa@10 524 end
Asa@12 525
Asa@0 526
Asa@63 527 function ItemAuditor:WatchBags()
Asa@4 528 if self.watch_handle == nil then
Asa@63 529 ItemAuditor:UpdateCurrentInventory()
Asa@23 530 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, 0.3, "UpdateAudit")
Asa@4 531 end
Asa@0 532 end
Asa@0 533
Asa@63 534 function ItemAuditor:UnwatchBags()
Asa@4 535 if self.watch_handle ~= nil then
Asa@4 536 self:UnregisterBucket(self.watch_handle)
Asa@4 537 self.watch_handle = nil
Asa@4 538 end
Asa@0 539 end
Asa@0 540
Asa@9 541
Asa@63 542 function ItemAuditor:GetSafeLink(link)
Asa@9 543 local newLink = nil
Asa@9 544
Asa@24 545 if link and link == string.match(link, '.-:[-0-9]+[:0-9]*') then
Asa@24 546 newLink = link
Asa@24 547 elseif link then
Asa@9 548 newLink = link and string.match(link, "|H(.-):([-0-9]+):([0-9]+)|h")
Asa@9 549 end
Asa@9 550 if newLink == nil then
Asa@9 551 local itemID = self:GetItemID(link)
Asa@9 552 if itemID ~= nil then
Asa@9 553 _, newLink = GetItemInfo(itemID)
Asa@9 554 return self:GetSafeLink(newLink)
Asa@9 555 end
Asa@9 556 end
Asa@9 557 return newLink and string.gsub(newLink, ":0:0:0:0:0:0", "")
Asa@9 558 end
Asa@9 559
Asa@63 560 function ItemAuditor:GetIDFromLink(link)
Asa@9 561 local _, _, _, _, Id = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@9 562 return tonumber(Id)
Asa@9 563 end
Asa@9 564
Asa@63 565 function ItemAuditor:GetItemCost(link, countModifier)
Asa@9 566 local item = self:GetItem(link, true)
Asa@8 567
Asa@9 568 if item.invested > 0 then
Asa@9 569 local count = item.count
Asa@133 570
Asa@9 571 if countModifier ~= nil then
Asa@9 572 count = count - countModifier
Asa@0 573 end
Asa@133 574 if count > 0 then
Asa@45 575 return ceil(item.invested), ceil(item.invested/count), count
Asa@9 576 end
Asa@121 577 return ceil(item.invested), 0, count
Asa@0 578 end
Asa@82 579 return 0, 0, ItemAuditor:GetItemCount(ItemAuditor:GetIDFromLink(link))
Asa@0 580 end
Asa@132 581
Asa@132 582 ItemAuditor.Options.args.misc= {
Asa@132 583 name = "Misc",
Asa@132 584 type = 'group',
Asa@132 585 args = {
Asa@132 586 },
Asa@132 587 }
Asa@132 588 local function GetGuild(info)
Asa@132 589 local guildName = info[#(info)]
Asa@132 590 return (ItemAuditor.db.factionrealm.enabled_guilds[guildName] == true)
Asa@132 591 end
Asa@132 592
Asa@132 593 local function SetGuild(info, value)
Asa@132 594 local guildName = info[#(info)]
Asa@132 595 ItemAuditor.db.factionrealm.enabled_guilds[guildName] = value or nil
Asa@132 596 end
Asa@132 597
Asa@132 598 for guildName in pairs(DataStore:GetGuilds()) do
Asa@132 599 ItemAuditor.Options.args.misc.args[guildName] = {
Asa@132 600 type = "toggle",
Asa@132 601 name = "Count "..guildName.." Guild Bank",
Asa@132 602 get = GetGuild,
Asa@132 603 set = SetGuild,
Asa@132 604 order = 11,
Asa@132 605 }
Asa@132 606 end