annotate Core.lua @ 136:d3d5e82043d8

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