annotate Core.lua @ 20:ff9a698caebc

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