annotate Core.lua @ 19:67f4151d535c

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