annotate Core.lua @ 6:5dddd73b2220

Removed dependency on postal. It was only being used to determine the mail type which has been moved into ItemAuditor so I can support CODPayments. Paying for COD items works, but getting the payments back can't reliably associate the payment with an item yet.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 26 May 2010 22:41:47 -0700
parents 7d0f4ebedf8c
children bbba2fae0f69
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@6 88
Asa@0 89 function addon:ScanMail()
Asa@0 90 local results = {}
Asa@0 91 for mailIndex = 1, GetInboxNumItems() or 0 do
Asa@0 92 local sender, msgSubject, msgMoney, msgCOD, _, msgItem, _, _, msgText, _, isGM = select(3, GetInboxHeaderInfo(mailIndex))
Asa@6 93 local mailType = utils:GetMailType(msgSubject)
Asa@6 94
Asa@6 95 results[mailType] = (results[mailType] or {})
Asa@6 96
Asa@5 97 if mailType == "NonAHMail" and msgCOD > 0 then
Asa@6 98 mailType = 'COD'
Asa@6 99 results[mailType] = (results[mailType] or {})
Asa@5 100
Asa@5 101 local itemTypes = {}
Asa@5 102 for itemIndex = 1, ATTACHMENTS_MAX_RECEIVE do
Asa@5 103 local itemName, _, count, _, _= GetInboxItem(mailIndex, itemIndex)
Asa@5 104 if itemName ~= nil then
Asa@5 105 itemTypes[itemName] = (itemTypes[itemName] or 0) + count
Asa@5 106 end
Asa@5 107 end
Asa@5 108
Asa@5 109 if utils:tcount(itemTypes) == 1 then
Asa@5 110 for itemName, count in pairs(itemTypes) do
Asa@6 111 results[mailType][itemName] = (results[mailType][itemName] or 0) - msgCOD
Asa@5 112 end
Asa@5 113 else
Asa@5 114 self:Debug("Don't know what to do with more than one item type on COD mail.")
Asa@5 115 end
Asa@6 116 elseif mailType == "CODPayment" then
Asa@6 117 itemName = msgSubject:gsub(utils.SubjectPatterns[mailType], function(item) return item end)
Asa@5 118
Asa@6 119 results[mailType][itemName] = (results[mailType][itemName] or 0) + msgMoney
Asa@5 120
Asa@0 121 elseif mailType == "AHSuccess" then
Asa@0 122 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@6 123 results[mailType][itemName] = (results[mailType][itemName] or 0) + deposit + buyout - consignment
Asa@0 124
Asa@0 125 elseif mailType == "AHWon" then
Asa@0 126 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@6 127 results[mailType][itemName] = (results[mailType][itemName] or 0) - bid
Asa@5 128 elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
Asa@0 129 -- These should be handled when you pay the deposit at the AH
Asa@0 130 else
Asa@0 131 self:Debug("Unhandled mail type: " .. mailType)
Asa@0 132 self:Debug(msgSubject)
Asa@0 133 end
Asa@0 134
Asa@0 135 end
Asa@0 136 return results
Asa@0 137 end
Asa@0 138
Asa@0 139 function addon:SaveValue(item, value)
Asa@0 140 local item_account = self.db.factionrealm.item_account
Asa@6 141
Asa@6 142 item_account[item] = (item_account[item] or 0) + value
Asa@6 143
Asa@6 144 self:Debug("Updated price of " .. item .. " to " .. utils:FormatMoney(item_account[item]) .. "(change: " .. utils:FormatMoney(value) .. ")")
Asa@0 145
Asa@0 146 if item_account[item] >= 0 then
Asa@6 147 self:Debug("Updated price of " .. item .. " to " .. utils:FormatMoney(0))
Asa@0 148 item_account[item] = nil
Asa@0 149 end
Asa@0 150 end
Asa@0 151
Asa@4 152 local defaultBagDelay = 0.2
Asa@4 153
Asa@3 154 function addon:WatchBags(delay)
Asa@4 155 delay = delay or defaultBagDelay
Asa@4 156 if delay ~= self.currentBagDelay then
Asa@4 157 self:UnwatchBags()
Asa@4 158 end
Asa@4 159
Asa@4 160 if self.watch_handle == nil then
Asa@4 161 self.currentBagDelay = delay
Asa@4 162 self:Debug("currentBagDelay = " .. delay)
Asa@4 163 addon:UpdateCurrentInventory()
Asa@4 164 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, self.currentBagDelay, "UpdateAudit")
Asa@4 165 end
Asa@0 166 end
Asa@0 167
Asa@0 168 function addon:UnwatchBags()
Asa@4 169 if self.watch_handle ~= nil then
Asa@4 170 self:UnregisterBucket(self.watch_handle)
Asa@4 171 self.watch_handle = nil
Asa@4 172 end
Asa@0 173 end
Asa@0 174
Asa@0 175 function addon:GetItemCost(itemName, countModifier)
Asa@0 176 local invested = abs(self.db.factionrealm.item_account[itemName] or 0)
Asa@0 177
Asa@0 178 if invested > 0 then
Asa@0 179 local _, itemLink = GetItemInfo (itemName);
Asa@0 180 local _, _, _, _, Id = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@0 181 local count = Altoholic:GetItemCount(tonumber(Id))
Asa@0 182 if countModifier ~= nil then
Asa@0 183 count = count - countModifier
Asa@0 184 end
Asa@0 185 if count == 0 then
Asa@0 186 self.db.factionrealm.item_account[itemName] = nil
Asa@3 187 self:Print("You ran out of " .. itemName .. "and never recovered " .. utils:FormatMoney(invested))
Asa@0 188 return 0, 0, 0
Asa@0 189 end
Asa@0 190 return ceil(invested), ceil(invested/count), count
Asa@0 191 end
Asa@0 192 return 0, 0, 0
Asa@0 193 end