annotate Core.lua @ 24:554b30908b33 v0.1

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