annotate Core.lua @ 10:c79ede3c7b82

Updated version and added a dependency on my modified API for QuickAuctions. Next I need to make that dependency optional
author Asa Ayers <Asa.Ayers@Gmail.com>
date Fri, 25 Jun 2010 22:03:44 -0700
parents 374dd1a90d02
children 6a6296dd249f
rev   line source
Asa@3 1 local addonName, addonTable = ...;
Asa@3 2 _G[addonName] = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "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@0 20 debug = false
Asa@0 21 },
Asa@0 22 factionrealm = {
Asa@8 23 item_account = {},
Asa@8 24 items = {},
Asa@10 25 AHCut = 0.05,
Asa@0 26 },
Asa@0 27 }
Asa@0 28 self.db = LibStub("AceDB-3.0"):New("ItemAuditorDB", DB_defaults, true)
Asa@8 29 addonTable.db= self.db
Asa@8 30 self.items = self.db.factionrealm.items
Asa@0 31
Asa@0 32 self:RegisterOptions()
Asa@0 33
Asa@3 34 self:RegisterEvent("PLAYER_ENTERING_WORLD")
Asa@0 35 end
Asa@0 36
Asa@8 37 function addon:ConvertItems()
Asa@8 38 for itemName, value in pairs(self.db.factionrealm.item_account) do
Asa@8 39 local itemID = utils:GetItemID(itemName)
Asa@8 40 if itemID ~= nil then
Asa@8 41 self:GetItem('item:' .. itemID)
Asa@8 42 end
Asa@8 43 if value == 0 then
Asa@8 44 self.db.factionrealm.item_account[itemName] = nil
Asa@8 45 end
Asa@8 46 end
Asa@8 47
Asa@8 48 for link, data in pairs(self.db.factionrealm.items) do
Asa@8 49 if self:GetItem(link).count == 0 or self:GetItem(link).invested == 0 then
Asa@8 50 self:RemoveItem(link)
Asa@8 51 end
Asa@10 52 -- addon:UpdateQAThreshold(link)
Asa@10 53 end
Asa@10 54
Asa@10 55 for groupName in pairs(QAAPI:GetGroups()) do
Asa@10 56 self:UpdateQAGroup(groupName)
Asa@8 57 end
Asa@8 58 end
Asa@8 59
Asa@0 60 function addon:GetCurrentInventory()
Asa@8 61 local i = {}
Asa@8 62 local bagID
Asa@8 63 local slotID
Asa@8 64
Asa@8 65 for bagID = 0, NUM_BAG_SLOTS do
Asa@8 66 bagSize=GetContainerNumSlots(bagID)
Asa@8 67 for slotID = 0, bagSize do
Asa@8 68 local link= GetContainerItemLink(bagID, slotID);
Asa@10 69 link = link and self:GetSafeLink(link)
Asa@8 70
Asa@8 71 if link ~= nil and i[link] == nil then
Asa@8 72 i[link] = GetItemCount(link);
Asa@8 73 end
Asa@8 74 end
Asa@8 75
Asa@8 76 end
Asa@8 77 return {items = i, money = GetMoney()}
Asa@0 78 end
Asa@0 79
Asa@0 80 function addon:GetInventoryDiff(pastInventory, current)
Asa@8 81 if current == nil then
Asa@8 82 current = self:GetCurrentInventory()
Asa@8 83 end
Asa@8 84 local diff = {}
Asa@8 85
Asa@8 86 for link, count in pairs(current.items) do
Asa@8 87 if pastInventory.items[link] == nil then
Asa@8 88 diff[link] = count
Asa@8 89 -- self:Debug("1 diff[" .. name .. "]=" .. diff[name])
Asa@8 90 elseif count - pastInventory.items[link] ~= 0 then
Asa@8 91 diff[link] = count - pastInventory.items[link]
Asa@8 92 -- self:Debug("2 diff[" .. name .. "]=" .. diff[name])
Asa@8 93 end
Asa@8 94 end
Asa@8 95
Asa@8 96 for link, count in pairs(pastInventory.items) do
Asa@8 97 if current.items[link] == nil then
Asa@8 98 diff[link] = -count
Asa@8 99 -- self:Debug("3 diff[" .. name .. "]=" .. diff[name])
Asa@8 100 elseif current.items[link] - count ~= 0 then
Asa@8 101 diff[link] = current.items[link] - pastInventory.items[link]
Asa@8 102 -- self:Debug("4 diff[" .. name .. "]=" .. diff[name])
Asa@8 103 end
Asa@8 104 end
Asa@8 105
Asa@8 106 local moneyDiff = current.money - pastInventory.money
Asa@8 107
Asa@8 108 return {items = diff, money = moneyDiff}
Asa@0 109 end
Asa@0 110
Asa@0 111
Asa@6 112
Asa@0 113 function addon:ScanMail()
Asa@0 114 local results = {}
Asa@0 115 for mailIndex = 1, GetInboxNumItems() or 0 do
Asa@0 116 local sender, msgSubject, msgMoney, msgCOD, _, msgItem, _, _, msgText, _, isGM = select(3, GetInboxHeaderInfo(mailIndex))
Asa@6 117 local mailType = utils:GetMailType(msgSubject)
Asa@6 118
Asa@6 119 results[mailType] = (results[mailType] or {})
Asa@6 120
Asa@5 121 if mailType == "NonAHMail" and msgCOD > 0 then
Asa@9 122 --[[
Asa@6 123 mailType = 'COD'
Asa@6 124 results[mailType] = (results[mailType] or {})
Asa@5 125
Asa@5 126 local itemTypes = {}
Asa@5 127 for itemIndex = 1, ATTACHMENTS_MAX_RECEIVE do
Asa@5 128 local itemName, _, count, _, _= GetInboxItem(mailIndex, itemIndex)
Asa@5 129 if itemName ~= nil then
Asa@7 130 itemTypdes[itemName] = (itemTypes[itemName] or 0) + count
Asa@5 131 end
Asa@5 132 end
Asa@5 133
Asa@5 134 if utils:tcount(itemTypes) == 1 then
Asa@5 135 for itemName, count in pairs(itemTypes) do
Asa@6 136 results[mailType][itemName] = (results[mailType][itemName] or 0) - msgCOD
Asa@5 137 end
Asa@5 138 else
Asa@5 139 self:Debug("Don't know what to do with more than one item type on COD mail.")
Asa@5 140 end
Asa@9 141 ]]
Asa@6 142 elseif mailType == "CODPayment" then
Asa@6 143 itemName = msgSubject:gsub(utils.SubjectPatterns[mailType], function(item) return item end)
Asa@5 144
Asa@9 145 results[mailType][itemName] = (results[mailType][itemName] or 0) - msgMoney
Asa@5 146
Asa@0 147 elseif mailType == "AHSuccess" then
Asa@0 148 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@9 149 results[mailType][itemName] = (results[mailType][itemName] or 0) - deposit - buyout + consignment
Asa@0 150
Asa@0 151 elseif mailType == "AHWon" then
Asa@0 152 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@9 153 results[mailType][itemName] = (results[mailType][itemName] or 0) + bid
Asa@5 154 elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
Asa@0 155 -- These should be handled when you pay the deposit at the AH
Asa@0 156 else
Asa@0 157 self:Debug("Unhandled mail type: " .. mailType)
Asa@0 158 self:Debug(msgSubject)
Asa@0 159 end
Asa@0 160
Asa@0 161 end
Asa@0 162 return results
Asa@0 163 end
Asa@0 164
Asa@9 165 function addon:GetItem(link, viewOnly)
Asa@9 166 if viewOnly == nil then
Asa@9 167 viewOnly = false
Asa@9 168 end
Asa@8 169
Asa@9 170 local itemName = nil
Asa@9 171 if self:GetSafeLink(link) == nil then
Asa@9 172 itemName = link
Asa@9 173 else
Asa@9 174 link = self:GetSafeLink(link)
Asa@9 175 itemName = GetItemInfo(link)
Asa@9 176 end
Asa@9 177
Asa@9 178 if self.db.factionrealm.item_account[itemName] ~= nil then
Asa@8 179 self.items[link] = {
Asa@8 180 count = Altoholic:GetItemCount(utils:GetIDFromLink(link)),
Asa@8 181 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@8 182 }
Asa@8 183 self.db.factionrealm.item_account[itemName] = nil
Asa@8 184 end
Asa@8 185
Asa@9 186 if viewOnly == false and self.items[link] == nil then
Asa@9 187 local itemName = GetItemInfo(link)
Asa@9 188
Asa@9 189 self.items[link] = {
Asa@10 190 count = Altoholic:GetItemCount(self:GetIDFromLink(link)),
Asa@9 191 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@9 192 }
Asa@9 193
Asa@9 194 end
Asa@9 195
Asa@9 196
Asa@9 197
Asa@9 198 if viewOnly == true and self.items[link] == nil then
Asa@9 199 return {count = 0, invested = 0}
Asa@9 200 elseif viewOnly == true then
Asa@9 201 return {count = self.items[link].count, invested = self.items[link].invested}
Asa@9 202 end
Asa@10 203 self.items[link].count = Altoholic:GetItemCount(self:GetIDFromLink(link))
Asa@8 204 return self.items[link]
Asa@8 205 end
Asa@8 206
Asa@8 207 function addon:RemoveItem(link)
Asa@9 208 self.db.factionrealm.item_account[link] = nil
Asa@9 209 link = self:GetSafeLink(link)
Asa@9 210 if link ~= nil then
Asa@9 211 self.items[link] = nil
Asa@9 212 end
Asa@8 213 end
Asa@9 214 --[[
Asa@9 215 ItemAuditor:SaveValue('Scroll of Enchant Weapon - Exceptional Spellpower', 1)
Asa@8 216
Asa@9 217 DevTools_Dump(ItemAuditor.db.factionrealm.item_account)
Asa@9 218
Asa@9 219 = ItemAuditor:GetItem('Scroll of Enchant Weapon - Exceptional Spellpower', true).invested
Asa@9 220 ]]
Asa@8 221 function addon:SaveValue(link, value)
Asa@9 222 local item = nil
Asa@9 223 local realLink = self:GetSafeLink(link)
Asa@9 224 local itemName = nil
Asa@9 225 if realLink == nil then
Asa@9 226 itemName = link
Asa@9 227 self.db.factionrealm.item_account[itemName] = (self.db.factionrealm.item_account[itemName] or 0) + value
Asa@9 228 item = {invested = self.db.factionrealm.item_account[itemName], count = 1}
Asa@9 229 else
Asa@9 230 item = self:GetItem(realLink)
Asa@9 231 item.invested = item.invested + value
Asa@9 232 itemName = GetItemInfo(realLink)
Asa@9 233 end
Asa@8 234
Asa@7 235 if abs(value) > 0 then
Asa@8 236 self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(item.invested) .. "(change: " .. utils:FormatMoney(value) .. ")")
Asa@7 237 end
Asa@0 238
Asa@10 239 if abs(value) > 0 and item.invested <= 0 then
Asa@8 240 self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(0))
Asa@8 241 self:RemoveItem(link)
Asa@10 242 elseif item.count == 0 and ItemAuditor:GetCurrentInventory() > 0 then
Asa@8 243 self:Print("You ran out of " .. itemName .. " and never recovered " .. utils:FormatMoney(item.invested))
Asa@8 244 self:RemoveItem(link)
Asa@0 245 end
Asa@10 246
Asa@10 247 if realLink ~= nil then
Asa@10 248 addon:UpdateQAThreshold(realLink)
Asa@10 249 end
Asa@10 250 end
Asa@10 251 --[[
Asa@10 252
Asa@10 253 ItemAuditor:UpdateQAThreshold("item:42646")
Asa@10 254 ]]
Asa@10 255 function addon:UpdateQAThreshold(link)
Asa@10 256 _, link= GetItemInfo(link)
Asa@10 257
Asa@10 258 self:UpdateQAGroup(QAAPI:GetItemGroup(link))
Asa@10 259 end
Asa@10 260
Asa@10 261 function addon:UpdateQAGroup(groupName)
Asa@10 262 if groupName then
Asa@10 263 local threshold = 10000
Asa@10 264
Asa@10 265 for link in pairs(QAAPI:GetItemsInGroup(groupName)) do
Asa@10 266 local totalCost, itemCost, itemCount = ItemAuditor:GetItemCost(link, 0)
Asa@10 267
Asa@10 268 if itemCost > threshold then
Asa@10 269 threshold = itemCost
Asa@10 270 end
Asa@10 271 end
Asa@10 272
Asa@10 273 -- Adding the cost of mailing every item once.
Asa@10 274 threshold = threshold + 30
Asa@10 275
Asa@10 276 -- add my minimum profit margin 15%
Asa@10 277 threshold = threshold * 1.15
Asa@10 278
Asa@10 279 -- add AH Cut
Asa@10 280 local keep = 1 - self.db.factionrealm.AHCut
Asa@10 281 threshold = threshold/keep
Asa@10 282
Asa@10 283 QAAPI:SetGroupThreshold(groupName, ceil(threshold))
Asa@10 284 end
Asa@0 285 end
Asa@0 286
Asa@4 287 local defaultBagDelay = 0.2
Asa@4 288
Asa@3 289 function addon:WatchBags(delay)
Asa@4 290 delay = delay or defaultBagDelay
Asa@4 291 if delay ~= self.currentBagDelay then
Asa@4 292 self:UnwatchBags()
Asa@4 293 end
Asa@4 294
Asa@4 295 if self.watch_handle == nil then
Asa@4 296 self.currentBagDelay = delay
Asa@4 297 self:Debug("currentBagDelay = " .. delay)
Asa@4 298 addon:UpdateCurrentInventory()
Asa@4 299 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, self.currentBagDelay, "UpdateAudit")
Asa@4 300 end
Asa@0 301 end
Asa@0 302
Asa@0 303 function addon:UnwatchBags()
Asa@4 304 if self.watch_handle ~= nil then
Asa@4 305 self:UnregisterBucket(self.watch_handle)
Asa@4 306 self.watch_handle = nil
Asa@4 307 end
Asa@0 308 end
Asa@0 309
Asa@9 310 function addon:GetItemID(itemName)
Asa@9 311 return utils:GetItemID(itemName)
Asa@9 312 end
Asa@9 313
Asa@9 314 function addon:GetSafeLink(link)
Asa@9 315 local newLink = nil
Asa@9 316
Asa@10 317 if link and link ~= string.match(link, '.-:[-0-9]+[:0-9]*') then
Asa@9 318 newLink = link and string.match(link, "|H(.-):([-0-9]+):([0-9]+)|h")
Asa@9 319 end
Asa@9 320 if newLink == nil then
Asa@9 321 local itemID = self:GetItemID(link)
Asa@9 322 if itemID ~= nil then
Asa@9 323 _, newLink = GetItemInfo(itemID)
Asa@9 324 return self:GetSafeLink(newLink)
Asa@9 325 end
Asa@9 326 end
Asa@9 327 return newLink and string.gsub(newLink, ":0:0:0:0:0:0", "")
Asa@9 328 end
Asa@9 329
Asa@9 330 function addon:GetIDFromLink(link)
Asa@9 331 local _, _, _, _, Id = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@9 332 return tonumber(Id)
Asa@9 333 end
Asa@9 334
Asa@8 335 function addon:GetItemCost(link, countModifier)
Asa@9 336 local item = self:GetItem(link, true)
Asa@8 337
Asa@9 338 if item.invested > 0 then
Asa@9 339 local count = item.count
Asa@9 340
Asa@9 341 if countModifier ~= nil then
Asa@9 342 count = count - countModifier
Asa@0 343 end
Asa@9 344 if count > 0 then
Asa@9 345 return ceil(item.invested), ceil(item.invested/item.count), count
Asa@9 346 end
Asa@9 347
Asa@0 348 end
Asa@0 349 return 0, 0, 0
Asa@0 350 end