annotate Core.lua @ 12:6a6296dd249f

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