annotate Core.lua @ 37:9bd18fce8498

Fixed a bug where sometimes the number owned was not being updated, so you might have 3 of an item but it was calculating as if you only had 1.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sun, 18 Jul 2010 21:32:15 -0700
parents aaa716c93fb2
children e27d13095b49
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@3 6 local utils = addonTable.utils
Asa@3 7
Asa@0 8
Asa@0 9 local WHITE = "|cFFFFFFFF"
Asa@0 10 local RED = "|cFFFF0000"
Asa@0 11 local GREEN = "|cFF00FF00"
Asa@0 12 local YELLOW = "|cFFFFFF00"
Asa@0 13 local ORANGE = "|cFFFF7F00"
Asa@0 14 local TEAL = "|cFF00FF9A"
Asa@0 15 local GOLD = "|cFFFFD700"
Asa@0 16
Asa@0 17 function addon:OnInitialize()
Asa@0 18 local DB_defaults = {
Asa@0 19 char = {
Asa@13 20 ah = 1,
Asa@13 21 use_quick_auctions = false,
Asa@20 22 crafting_threshold = 1,
Asa@20 23 auction_threshold = 0.15,
Asa@0 24 },
Asa@16 25 profile = {
Asa@16 26 messages = {
Asa@16 27 cost_updates = true,
Asa@20 28 queue_skip = false,
Asa@23 29 },
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@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@0 43
Asa@3 44 self:RegisterEvent("PLAYER_ENTERING_WORLD")
Asa@23 45
Asa@23 46 -- /run ItemAuditor.db.profile.show_debug_frame_on_startup = true
Asa@23 47 if self.db.profile.show_debug_frame_on_startup then
Asa@23 48 ItemAuditor_DebugFrame:Show()
Asa@28 49 self:CreateFrames()
Asa@23 50 end
Asa@0 51 end
Asa@0 52
Asa@8 53 function addon:ConvertItems()
Asa@8 54 for itemName, value in pairs(self.db.factionrealm.item_account) do
Asa@15 55 local itemID = self:GetItemID(itemName)
Asa@8 56 if itemID ~= nil then
Asa@8 57 self:GetItem('item:' .. itemID)
Asa@8 58 end
Asa@8 59 if value == 0 then
Asa@8 60 self.db.factionrealm.item_account[itemName] = nil
Asa@8 61 end
Asa@8 62 end
Asa@8 63
Asa@8 64 for link, data in pairs(self.db.factionrealm.items) do
Asa@8 65 if self:GetItem(link).count == 0 or self:GetItem(link).invested == 0 then
Asa@8 66 self:RemoveItem(link)
Asa@8 67 end
Asa@10 68 end
Asa@10 69
Asa@12 70 self:RefreshQAGroups()
Asa@12 71 end
Asa@12 72
Asa@24 73 local printPrefix = "|cFFA3CEFFItemAuditor|r: "
Asa@24 74 function addon:Print(message, ...)
Asa@24 75 message = format(message, ...)
Asa@24 76 DEFAULT_CHAT_FRAME:AddMessage( printPrefix .. tostring(message))
Asa@22 77 self:Log(message)
Asa@16 78 end
Asa@16 79
Asa@0 80 function addon:GetCurrentInventory()
Asa@8 81 local i = {}
Asa@8 82 local bagID
Asa@8 83 local slotID
Asa@8 84
Asa@8 85 for bagID = 0, NUM_BAG_SLOTS do
Asa@8 86 bagSize=GetContainerNumSlots(bagID)
Asa@8 87 for slotID = 0, bagSize do
Asa@8 88 local link= GetContainerItemLink(bagID, slotID);
Asa@10 89 link = link and self:GetSafeLink(link)
Asa@8 90
Asa@8 91 if link ~= nil and i[link] == nil then
Asa@8 92 i[link] = GetItemCount(link);
Asa@8 93 end
Asa@8 94 end
Asa@8 95
Asa@8 96 end
Asa@8 97 return {items = i, money = GetMoney()}
Asa@0 98 end
Asa@0 99
Asa@0 100 function addon:GetInventoryDiff(pastInventory, current)
Asa@8 101 if current == nil then
Asa@8 102 current = self:GetCurrentInventory()
Asa@8 103 end
Asa@8 104 local diff = {}
Asa@8 105
Asa@8 106 for link, count in pairs(current.items) do
Asa@8 107 if pastInventory.items[link] == nil then
Asa@8 108 diff[link] = count
Asa@23 109 self:Debug("1 diff[" .. link .. "]=" .. diff[link])
Asa@8 110 elseif count - pastInventory.items[link] ~= 0 then
Asa@8 111 diff[link] = count - pastInventory.items[link]
Asa@23 112 self:Debug("2 diff[" .. link .. "]=" .. diff[link])
Asa@8 113 end
Asa@8 114 end
Asa@8 115
Asa@8 116 for link, count in pairs(pastInventory.items) do
Asa@8 117 if current.items[link] == nil then
Asa@8 118 diff[link] = -count
Asa@23 119 self:Debug("3 diff[" .. link .. "]=" .. diff[link])
Asa@8 120 elseif current.items[link] - count ~= 0 then
Asa@8 121 diff[link] = current.items[link] - pastInventory.items[link]
Asa@23 122 self:Debug("4 diff[" .. link .. "]=" .. diff[link])
Asa@8 123 end
Asa@8 124 end
Asa@8 125
Asa@8 126 local moneyDiff = current.money - pastInventory.money
Asa@23 127 if abs(moneyDiff) > 0 then
Asa@23 128 self:Debug("moneyDiff: " .. moneyDiff)
Asa@23 129 end
Asa@8 130
Asa@8 131 return {items = diff, money = moneyDiff}
Asa@0 132 end
Asa@0 133
Asa@0 134
Asa@6 135
Asa@0 136 function addon:ScanMail()
Asa@0 137 local results = {}
Asa@0 138 for mailIndex = 1, GetInboxNumItems() or 0 do
Asa@0 139 local sender, msgSubject, msgMoney, msgCOD, _, msgItem, _, _, msgText, _, isGM = select(3, GetInboxHeaderInfo(mailIndex))
Asa@15 140 local mailType = self:GetMailType(msgSubject)
Asa@6 141
Asa@6 142 results[mailType] = (results[mailType] or {})
Asa@6 143
Asa@12 144 if mailType == "NonAHMail" then
Asa@9 145 --[[
Asa@12 146 and msgCOD > 0
Asa@12 147
Asa@6 148 mailType = 'COD'
Asa@6 149 results[mailType] = (results[mailType] or {})
Asa@5 150
Asa@5 151 local itemTypes = {}
Asa@5 152 for itemIndex = 1, ATTACHMENTS_MAX_RECEIVE do
Asa@5 153 local itemName, _, count, _, _= GetInboxItem(mailIndex, itemIndex)
Asa@5 154 if itemName ~= nil then
Asa@7 155 itemTypdes[itemName] = (itemTypes[itemName] or 0) + count
Asa@5 156 end
Asa@5 157 end
Asa@5 158
Asa@15 159 if self:tcount(itemTypes) == 1 then
Asa@5 160 for itemName, count in pairs(itemTypes) do
Asa@6 161 results[mailType][itemName] = (results[mailType][itemName] or 0) - msgCOD
Asa@5 162 end
Asa@5 163 else
Asa@5 164 self:Debug("Don't know what to do with more than one item type on COD mail.")
Asa@5 165 end
Asa@9 166 ]]
Asa@6 167 elseif mailType == "CODPayment" then
Asa@6 168 itemName = msgSubject:gsub(utils.SubjectPatterns[mailType], function(item) return item end)
Asa@5 169
Asa@26 170 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@26 171 results[mailType][itemName].total = results[mailType][itemName].total - msgMoney
Asa@5 172
Asa@0 173 elseif mailType == "AHSuccess" then
Asa@0 174 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@26 175 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@26 176 results[mailType][itemName].total = results[mailType][itemName].total - deposit - buyout + consignment
Asa@26 177
Asa@0 178
Asa@0 179 elseif mailType == "AHWon" then
Asa@0 180 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@26 181 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@26 182 results[mailType][itemName].total = results[mailType][itemName].total + bid
Asa@26 183
Asa@26 184 local count = select(3, GetInboxItem(1,1))
Asa@26 185 results[mailType][itemName].count = results[mailType][itemName].count + count
Asa@5 186 elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
Asa@0 187 -- These should be handled when you pay the deposit at the AH
Asa@0 188 else
Asa@24 189 -- self:Debug("Unhandled mail type: " .. mailType)
Asa@24 190 -- self:Debug(msgSubject)
Asa@0 191 end
Asa@0 192
Asa@0 193 end
Asa@23 194
Asa@23 195 for mailType, collection in pairs(results) do
Asa@26 196 for item, data in pairs(collection) do
Asa@26 197 self:Debug(format("|cFF00FF00MailScan|r: %s - %s - %s x %s", mailType, item, data.total, data.count))
Asa@23 198 end
Asa@23 199 end
Asa@23 200
Asa@0 201 return results
Asa@0 202 end
Asa@0 203
Asa@9 204 function addon:GetItem(link, viewOnly)
Asa@9 205 if viewOnly == nil then
Asa@9 206 viewOnly = false
Asa@9 207 end
Asa@8 208
Asa@9 209 local itemName = nil
Asa@9 210 if self:GetSafeLink(link) == nil then
Asa@9 211 itemName = link
Asa@9 212 else
Asa@9 213 link = self:GetSafeLink(link)
Asa@9 214 itemName = GetItemInfo(link)
Asa@9 215 end
Asa@9 216
Asa@12 217
Asa@9 218 if self.db.factionrealm.item_account[itemName] ~= nil then
Asa@8 219 self.items[link] = {
Asa@12 220 count = Altoholic:GetItemCount(self:GetIDFromLink(link)),
Asa@8 221 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@8 222 }
Asa@8 223 self.db.factionrealm.item_account[itemName] = nil
Asa@8 224 end
Asa@8 225
Asa@9 226 if viewOnly == false and self.items[link] == nil then
Asa@24 227
Asa@9 228 self.items[link] = {
Asa@10 229 count = Altoholic:GetItemCount(self:GetIDFromLink(link)),
Asa@9 230 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@9 231 }
Asa@9 232
Asa@9 233 end
Asa@9 234
Asa@37 235 if self.items[link] ~= nil then
Asa@37 236 self.items[link].count = Altoholic:GetItemCount(self:GetIDFromLink(link))
Asa@37 237 end
Asa@37 238
Asa@9 239 if viewOnly == true and self.items[link] == nil then
Asa@9 240 return {count = 0, invested = 0}
Asa@9 241 elseif viewOnly == true then
Asa@28 242
Asa@9 243 return {count = self.items[link].count, invested = self.items[link].invested}
Asa@9 244 end
Asa@37 245
Asa@28 246
Asa@28 247
Asa@8 248 return self.items[link]
Asa@8 249 end
Asa@8 250
Asa@8 251 function addon:RemoveItem(link)
Asa@9 252 self.db.factionrealm.item_account[link] = nil
Asa@9 253 link = self:GetSafeLink(link)
Asa@9 254 if link ~= nil then
Asa@35 255 local item = addon:GetItem(link)
Asa@35 256 item.invested = 0
Asa@24 257 else
Asa@24 258 self:Debug('Failed to convert link' .. tostring(link))
Asa@9 259 end
Asa@8 260 end
Asa@8 261
Asa@26 262 function addon:SaveValue(link, value, countChange)
Asa@26 263 self:Debug("SaveValue(%s, %s, %s)", tostring(link), value, (countChange or 'default'))
Asa@26 264 countChange = countChange or 0
Asa@9 265 local item = nil
Asa@9 266 local realLink = self:GetSafeLink(link)
Asa@9 267 local itemName = nil
Asa@9 268 if realLink == nil then
Asa@26 269 itemName = link
Asa@23 270 self:Debug('SaveValue: GetSafeLink failed, falling back to storing by name: ' .. tostring(itemName))
Asa@9 271 self.db.factionrealm.item_account[itemName] = (self.db.factionrealm.item_account[itemName] or 0) + value
Asa@9 272 item = {invested = self.db.factionrealm.item_account[itemName], count = 1}
Asa@9 273 else
Asa@23 274
Asa@9 275 item = self:GetItem(realLink)
Asa@9 276 item.invested = item.invested + value
Asa@9 277 itemName = GetItemInfo(realLink)
Asa@9 278 end
Asa@8 279
Asa@26 280 if value > 0 and countChange > 0 and item.invested == value and item.count ~= countChange then
Asa@26 281 local costPerItem = value / countChange
Asa@26 282 value = costPerItem * item.count
Asa@26 283 item.invested = value
Asa@26 284 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 285 end
Asa@26 286
Asa@7 287 if abs(value) > 0 then
Asa@22 288 if item.invested < 0 then
Asa@16 289 if self.db.profile.messages.cost_updates then
Asa@16 290 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 291 end
Asa@12 292 self:RemoveItem(link)
Asa@12 293 -- This doesn't work when you mail the only copy of an item you have to another character.
Asa@12 294 --[[
Asa@12 295 elseif item.count == 0 and realLink and Altoholic:GetItemCount(self:GetIDFromLink(realLink)) then
Asa@15 296 self:Print("You ran out of " .. itemName .. " and never recovered " .. self:FormatMoney(item.invested))
Asa@12 297 self:RemoveItem(link)
Asa@12 298 ]]
Asa@16 299 else
Asa@16 300 if self.db.profile.messages.cost_updates then
Asa@16 301 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 302 end
Asa@12 303 end
Asa@0 304 end
Asa@10 305
Asa@10 306 if realLink ~= nil then
Asa@10 307 addon:UpdateQAThreshold(realLink)
Asa@10 308 end
Asa@35 309 UpdateInvestedData()
Asa@10 310 end
Asa@12 311
Asa@0 312
Asa@23 313 function addon:WatchBags()
Asa@4 314 if self.watch_handle == nil then
Asa@4 315 addon:UpdateCurrentInventory()
Asa@23 316 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, 0.3, "UpdateAudit")
Asa@4 317 end
Asa@0 318 end
Asa@0 319
Asa@0 320 function addon:UnwatchBags()
Asa@4 321 if self.watch_handle ~= nil then
Asa@4 322 self:UnregisterBucket(self.watch_handle)
Asa@4 323 self.watch_handle = nil
Asa@4 324 end
Asa@0 325 end
Asa@0 326
Asa@9 327
Asa@9 328 function addon:GetSafeLink(link)
Asa@9 329 local newLink = nil
Asa@9 330
Asa@24 331 if link and link == string.match(link, '.-:[-0-9]+[:0-9]*') then
Asa@24 332 newLink = link
Asa@24 333 elseif link then
Asa@9 334 newLink = link and string.match(link, "|H(.-):([-0-9]+):([0-9]+)|h")
Asa@9 335 end
Asa@9 336 if newLink == nil then
Asa@9 337 local itemID = self:GetItemID(link)
Asa@9 338 if itemID ~= nil then
Asa@9 339 _, newLink = GetItemInfo(itemID)
Asa@9 340 return self:GetSafeLink(newLink)
Asa@9 341 end
Asa@9 342 end
Asa@9 343 return newLink and string.gsub(newLink, ":0:0:0:0:0:0", "")
Asa@9 344 end
Asa@9 345
Asa@9 346 function addon:GetIDFromLink(link)
Asa@9 347 local _, _, _, _, Id = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@9 348 return tonumber(Id)
Asa@9 349 end
Asa@9 350
Asa@8 351 function addon:GetItemCost(link, countModifier)
Asa@9 352 local item = self:GetItem(link, true)
Asa@8 353
Asa@9 354 if item.invested > 0 then
Asa@9 355 local count = item.count
Asa@9 356
Asa@9 357 if countModifier ~= nil then
Asa@9 358 count = count - countModifier
Asa@0 359 end
Asa@9 360 if count > 0 then
Asa@9 361 return ceil(item.invested), ceil(item.invested/item.count), count
Asa@9 362 end
Asa@9 363
Asa@0 364 end
Asa@35 365 return 0, 0, Altoholic:GetItemCount(ItemAuditor:GetIDFromLink(link))
Asa@0 366 end