annotate Core.lua @ 26:75d917ccd942

Fixed Bug #9 - If existing items have 0c invested, increase them all to match the first transaction that changes their price.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 14 Jul 2010 00:28:44 -0700
parents 554b30908b33
children 34daa46b644a
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@26 169 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@26 170 results[mailType][itemName].total = results[mailType][itemName].total - msgMoney
Asa@5 171
Asa@0 172 elseif mailType == "AHSuccess" then
Asa@0 173 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@26 174 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@26 175 results[mailType][itemName].total = results[mailType][itemName].total - deposit - buyout + consignment
Asa@26 176
Asa@0 177
Asa@0 178 elseif mailType == "AHWon" then
Asa@0 179 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@26 180 results[mailType][itemName] = (results[mailType][itemName] or {total=0,count=0})
Asa@26 181 results[mailType][itemName].total = results[mailType][itemName].total + bid
Asa@26 182
Asa@26 183 local count = select(3, GetInboxItem(1,1))
Asa@26 184 results[mailType][itemName].count = results[mailType][itemName].count + count
Asa@5 185 elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
Asa@0 186 -- These should be handled when you pay the deposit at the AH
Asa@0 187 else
Asa@24 188 -- self:Debug("Unhandled mail type: " .. mailType)
Asa@24 189 -- self:Debug(msgSubject)
Asa@0 190 end
Asa@0 191
Asa@0 192 end
Asa@23 193
Asa@23 194 for mailType, collection in pairs(results) do
Asa@26 195 for item, data in pairs(collection) do
Asa@26 196 self:Debug(format("|cFF00FF00MailScan|r: %s - %s - %s x %s", mailType, item, data.total, data.count))
Asa@23 197 end
Asa@23 198 end
Asa@23 199
Asa@0 200 return results
Asa@0 201 end
Asa@0 202
Asa@9 203 function addon:GetItem(link, viewOnly)
Asa@9 204 if viewOnly == nil then
Asa@9 205 viewOnly = false
Asa@9 206 end
Asa@8 207
Asa@9 208 local itemName = nil
Asa@9 209 if self:GetSafeLink(link) == nil then
Asa@9 210 itemName = link
Asa@9 211 else
Asa@9 212 link = self:GetSafeLink(link)
Asa@9 213 itemName = GetItemInfo(link)
Asa@9 214 end
Asa@9 215
Asa@12 216
Asa@9 217 if self.db.factionrealm.item_account[itemName] ~= nil then
Asa@8 218 self.items[link] = {
Asa@12 219 count = Altoholic:GetItemCount(self:GetIDFromLink(link)),
Asa@8 220 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@8 221 }
Asa@8 222 self.db.factionrealm.item_account[itemName] = nil
Asa@8 223 end
Asa@8 224
Asa@9 225 if viewOnly == false and self.items[link] == nil then
Asa@24 226
Asa@9 227 self.items[link] = {
Asa@10 228 count = Altoholic:GetItemCount(self:GetIDFromLink(link)),
Asa@9 229 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@9 230 }
Asa@9 231
Asa@9 232 end
Asa@9 233
Asa@9 234
Asa@9 235
Asa@9 236 if viewOnly == true and self.items[link] == nil then
Asa@9 237 return {count = 0, invested = 0}
Asa@9 238 elseif viewOnly == true then
Asa@9 239 return {count = self.items[link].count, invested = self.items[link].invested}
Asa@9 240 end
Asa@10 241 self.items[link].count = Altoholic:GetItemCount(self:GetIDFromLink(link))
Asa@21 242 self.items[link].invested = tonumber(self.items[link].invested)
Asa@8 243 return self.items[link]
Asa@8 244 end
Asa@8 245
Asa@8 246 function addon:RemoveItem(link)
Asa@9 247 self.db.factionrealm.item_account[link] = nil
Asa@9 248 link = self:GetSafeLink(link)
Asa@9 249 if link ~= nil then
Asa@9 250 self.items[link] = nil
Asa@24 251 else
Asa@24 252 self:Debug('Failed to convert link' .. tostring(link))
Asa@9 253 end
Asa@8 254 end
Asa@8 255
Asa@26 256 function addon:SaveValue(link, value, countChange)
Asa@26 257 self:Debug("SaveValue(%s, %s, %s)", tostring(link), value, (countChange or 'default'))
Asa@26 258 countChange = countChange or 0
Asa@9 259 local item = nil
Asa@9 260 local realLink = self:GetSafeLink(link)
Asa@9 261 local itemName = nil
Asa@9 262 if realLink == nil then
Asa@26 263 itemName = link
Asa@23 264 self:Debug('SaveValue: GetSafeLink failed, falling back to storing by name: ' .. tostring(itemName))
Asa@9 265 self.db.factionrealm.item_account[itemName] = (self.db.factionrealm.item_account[itemName] or 0) + value
Asa@9 266 item = {invested = self.db.factionrealm.item_account[itemName], count = 1}
Asa@9 267 else
Asa@23 268
Asa@9 269 item = self:GetItem(realLink)
Asa@9 270 item.invested = item.invested + value
Asa@9 271 itemName = GetItemInfo(realLink)
Asa@9 272 end
Asa@8 273
Asa@26 274 if value > 0 and countChange > 0 and item.invested == value and item.count ~= countChange then
Asa@26 275 local costPerItem = value / countChange
Asa@26 276 value = costPerItem * item.count
Asa@26 277 item.invested = value
Asa@26 278 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 279 end
Asa@26 280
Asa@7 281 if abs(value) > 0 then
Asa@22 282 if item.invested < 0 then
Asa@16 283 if self.db.profile.messages.cost_updates then
Asa@16 284 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 285 end
Asa@12 286 self:RemoveItem(link)
Asa@12 287 -- This doesn't work when you mail the only copy of an item you have to another character.
Asa@12 288 --[[
Asa@12 289 elseif item.count == 0 and realLink and Altoholic:GetItemCount(self:GetIDFromLink(realLink)) then
Asa@15 290 self:Print("You ran out of " .. itemName .. " and never recovered " .. self:FormatMoney(item.invested))
Asa@12 291 self:RemoveItem(link)
Asa@12 292 ]]
Asa@16 293 else
Asa@16 294 if self.db.profile.messages.cost_updates then
Asa@16 295 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 296 end
Asa@12 297 end
Asa@0 298 end
Asa@10 299
Asa@10 300 if realLink ~= nil then
Asa@10 301 addon:UpdateQAThreshold(realLink)
Asa@10 302 end
Asa@10 303 end
Asa@12 304
Asa@0 305
Asa@23 306 function addon:WatchBags()
Asa@4 307 if self.watch_handle == nil then
Asa@4 308 addon:UpdateCurrentInventory()
Asa@23 309 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, 0.3, "UpdateAudit")
Asa@4 310 end
Asa@0 311 end
Asa@0 312
Asa@0 313 function addon:UnwatchBags()
Asa@4 314 if self.watch_handle ~= nil then
Asa@4 315 self:UnregisterBucket(self.watch_handle)
Asa@4 316 self.watch_handle = nil
Asa@4 317 end
Asa@0 318 end
Asa@0 319
Asa@9 320
Asa@9 321 function addon:GetSafeLink(link)
Asa@9 322 local newLink = nil
Asa@9 323
Asa@24 324 if link and link == string.match(link, '.-:[-0-9]+[:0-9]*') then
Asa@24 325 newLink = link
Asa@24 326 elseif link then
Asa@9 327 newLink = link and string.match(link, "|H(.-):([-0-9]+):([0-9]+)|h")
Asa@9 328 end
Asa@9 329 if newLink == nil then
Asa@9 330 local itemID = self:GetItemID(link)
Asa@9 331 if itemID ~= nil then
Asa@9 332 _, newLink = GetItemInfo(itemID)
Asa@9 333 return self:GetSafeLink(newLink)
Asa@9 334 end
Asa@9 335 end
Asa@9 336 return newLink and string.gsub(newLink, ":0:0:0:0:0:0", "")
Asa@9 337 end
Asa@9 338
Asa@9 339 function addon:GetIDFromLink(link)
Asa@9 340 local _, _, _, _, Id = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@9 341 return tonumber(Id)
Asa@9 342 end
Asa@9 343
Asa@8 344 function addon:GetItemCost(link, countModifier)
Asa@9 345 local item = self:GetItem(link, true)
Asa@8 346
Asa@9 347 if item.invested > 0 then
Asa@9 348 local count = item.count
Asa@9 349
Asa@9 350 if countModifier ~= nil then
Asa@9 351 count = count - countModifier
Asa@0 352 end
Asa@9 353 if count > 0 then
Asa@9 354 return ceil(item.invested), ceil(item.invested/item.count), count
Asa@9 355 end
Asa@9 356
Asa@0 357 end
Asa@0 358 return 0, 0, 0
Asa@0 359 end