annotate Core.lua @ 5:7d0f4ebedf8c

work in progress: I added Prospecting and Disenchanting and AHOutbid mail and I also changed the initialization to quit forcing debug every time ItemAuditor loads and have begun to implement the COD mail scanning.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 26 May 2010 21:26:24 -0700
parents c940b527ccab
children 5dddd73b2220
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@0 4
Asa@3 5 local utils = addonTable.utils
Asa@3 6
Asa@0 7
Asa@0 8 local WHITE = "|cFFFFFFFF"
Asa@0 9 local RED = "|cFFFF0000"
Asa@0 10 local GREEN = "|cFF00FF00"
Asa@0 11 local YELLOW = "|cFFFFFF00"
Asa@0 12 local ORANGE = "|cFFFF7F00"
Asa@0 13 local TEAL = "|cFF00FF9A"
Asa@0 14 local GOLD = "|cFFFFD700"
Asa@0 15
Asa@0 16 function addon:OnInitialize()
Asa@0 17 local DB_defaults = {
Asa@0 18 char = {
Asa@0 19 debug = false
Asa@0 20 },
Asa@0 21 factionrealm = {
Asa@0 22 item_account = {}
Asa@0 23 },
Asa@0 24 }
Asa@0 25 self.db = LibStub("AceDB-3.0"):New("ItemAuditorDB", DB_defaults, true)
Asa@0 26
Asa@0 27 self:RegisterOptions()
Asa@0 28
Asa@3 29 self:RegisterEvent("PLAYER_ENTERING_WORLD")
Asa@0 30 end
Asa@0 31
Asa@0 32 function addon:GetCurrentInventory()
Asa@0 33 local i = {}
Asa@0 34 local link
Asa@0 35
Asa@0 36 for bagID = 0, NUM_BAG_SLOTS do
Asa@0 37 bagSize=GetContainerNumSlots(bagID)
Asa@0 38 for slotID = 0, bagSize do
Asa@0 39 itemID = GetContainerItemID(bagID, slotID);
Asa@0 40
Asa@0 41 if itemID ~= nil then
Asa@0 42 _, itemCount, _, _, _= GetContainerItemInfo(bagID, slotID);
Asa@0 43 name = GetItemInfo(itemID)
Asa@0 44 if i[name] == nil then
Asa@0 45 i[name] = 0
Asa@0 46 end
Asa@0 47 i[name] = i[name] + (itemCount or 0)
Asa@0 48 end
Asa@0 49
Asa@0 50 end
Asa@0 51
Asa@0 52 end
Asa@0 53 return {items = i, money = GetMoney()}
Asa@0 54 end
Asa@0 55
Asa@0 56 function addon:GetInventoryDiff(pastInventory, current)
Asa@0 57 if current == nil then
Asa@0 58 current = self:GetCurrentInventory()
Asa@0 59 end
Asa@0 60 local diff = {}
Asa@0 61
Asa@0 62 for name, count in pairs(current.items) do
Asa@0 63 if pastInventory.items[name] == nil then
Asa@0 64 diff[name] = count
Asa@0 65 self:Debug("1 diff[" .. name .. "]=" .. diff[name])
Asa@0 66 elseif count - pastInventory.items[name] ~= 0 then
Asa@0 67 diff[name] = count - pastInventory.items[name]
Asa@0 68 self:Debug("2 diff[" .. name .. "]=" .. diff[name])
Asa@0 69 end
Asa@0 70 end
Asa@0 71
Asa@0 72 for name, count in pairs(pastInventory.items) do
Asa@0 73 if current.items[name] == nil then
Asa@0 74 diff[name] = -count
Asa@0 75 self:Debug("3 diff[" .. name .. "]=" .. diff[name])
Asa@0 76 elseif current.items[name] - count ~= 0 then
Asa@0 77 diff[name] = current.items[name] - pastInventory.items[name]
Asa@0 78 self:Debug("4 diff[" .. name .. "]=" .. diff[name])
Asa@0 79 end
Asa@0 80 end
Asa@0 81
Asa@0 82 local moneyDiff = current.money - pastInventory.money
Asa@0 83
Asa@0 84 return {items = diff, money = moneyDiff}
Asa@0 85 end
Asa@0 86
Asa@0 87
Asa@0 88 function addon:ScanMail()
Asa@0 89 local results = {}
Asa@0 90 for mailIndex = 1, GetInboxNumItems() or 0 do
Asa@0 91 local sender, msgSubject, msgMoney, msgCOD, _, msgItem, _, _, msgText, _, isGM = select(3, GetInboxHeaderInfo(mailIndex))
Asa@0 92 local mailType = Postal:GetMailType(msgSubject)
Asa@0 93
Asa@5 94 if mailType == "NonAHMail" and msgCOD > 0 then
Asa@0 95 -- Don't know how to handle these yet
Asa@5 96
Asa@5 97 local itemTypes = {}
Asa@5 98 for itemIndex = 1, ATTACHMENTS_MAX_RECEIVE do
Asa@5 99 local itemName, _, count, _, _= GetInboxItem(mailIndex, itemIndex)
Asa@5 100 if itemName ~= nil then
Asa@5 101 itemTypes[itemName] = (itemTypes[itemName] or 0) + count
Asa@5 102 end
Asa@5 103 end
Asa@5 104
Asa@5 105 if utils:tcount(itemTypes) == 1 then
Asa@5 106 for itemName, count in pairs(itemTypes) do
Asa@5 107 results[itemName] = (results[itemName] or 0) - msgCOD
Asa@5 108 end
Asa@5 109 else
Asa@5 110 self:Debug("Don't know what to do with more than one item type on COD mail.")
Asa@5 111 end
Asa@5 112
Asa@5 113
Asa@0 114 elseif mailType == "AHSuccess" then
Asa@0 115 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@5 116 results[itemName] = (results[itemName] or 0) + deposit + buyout - consignment
Asa@0 117
Asa@0 118 elseif mailType == "AHWon" then
Asa@0 119 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@5 120 results[itemName] = (results[itemName] or 0) - bid
Asa@5 121 elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
Asa@0 122 -- These should be handled when you pay the deposit at the AH
Asa@0 123 else
Asa@0 124 self:Debug("Unhandled mail type: " .. mailType)
Asa@0 125 self:Debug(msgSubject)
Asa@0 126 end
Asa@0 127
Asa@0 128 end
Asa@0 129 return results
Asa@0 130 end
Asa@0 131
Asa@0 132 function addon:SaveValue(item, value)
Asa@0 133 local item_account = self.db.factionrealm.item_account
Asa@0 134 if item_account[item] == nil then
Asa@0 135 item_account[item] = 0
Asa@0 136 end
Asa@0 137 item_account[item] = item_account[item] + value
Asa@0 138
Asa@0 139 if item_account[item] >= 0 then
Asa@0 140 item_account[item] = nil
Asa@0 141 end
Asa@0 142 end
Asa@0 143
Asa@4 144 local defaultBagDelay = 0.2
Asa@4 145
Asa@3 146 function addon:WatchBags(delay)
Asa@4 147 delay = delay or defaultBagDelay
Asa@4 148 if delay ~= self.currentBagDelay then
Asa@4 149 self:UnwatchBags()
Asa@4 150 end
Asa@4 151
Asa@4 152 if self.watch_handle == nil then
Asa@4 153 self.currentBagDelay = delay
Asa@4 154 self:Debug("currentBagDelay = " .. delay)
Asa@4 155 addon:UpdateCurrentInventory()
Asa@4 156 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, self.currentBagDelay, "UpdateAudit")
Asa@4 157 end
Asa@0 158 end
Asa@0 159
Asa@0 160 function addon:UnwatchBags()
Asa@4 161 if self.watch_handle ~= nil then
Asa@4 162 self:UnregisterBucket(self.watch_handle)
Asa@4 163 self.watch_handle = nil
Asa@4 164 end
Asa@0 165 end
Asa@0 166
Asa@0 167 function addon:GetItemCost(itemName, countModifier)
Asa@0 168 local invested = abs(self.db.factionrealm.item_account[itemName] or 0)
Asa@0 169
Asa@0 170 if invested > 0 then
Asa@0 171 local _, itemLink = GetItemInfo (itemName);
Asa@0 172 local _, _, _, _, Id = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@0 173 local count = Altoholic:GetItemCount(tonumber(Id))
Asa@0 174 if countModifier ~= nil then
Asa@0 175 count = count - countModifier
Asa@0 176 end
Asa@0 177 if count == 0 then
Asa@0 178 self.db.factionrealm.item_account[itemName] = nil
Asa@3 179 self:Print("You ran out of " .. itemName .. "and never recovered " .. utils:FormatMoney(invested))
Asa@0 180 return 0, 0, 0
Asa@0 181 end
Asa@0 182 return ceil(invested), ceil(invested/count), count
Asa@0 183 end
Asa@0 184 return 0, 0, 0
Asa@0 185 end