annotate Core.lua @ 4:c940b527ccab

Fixed Milling. Disenchating will probably have to be fixed the same way
author Asa Ayers <Asa.Ayers@Gmail.com>
date Sat, 22 May 2010 15:23:11 -0700
parents bbcf81868171
children 7d0f4ebedf8c
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.db.char.debug = true
Asa@0 28
Asa@0 29 self:RegisterOptions()
Asa@0 30
Asa@3 31 self:RegisterEvent("PLAYER_ENTERING_WORLD")
Asa@0 32 end
Asa@0 33
Asa@0 34 function addon:GetCurrentInventory()
Asa@0 35 local i = {}
Asa@0 36 local link
Asa@0 37
Asa@0 38 for bagID = 0, NUM_BAG_SLOTS do
Asa@0 39 bagSize=GetContainerNumSlots(bagID)
Asa@0 40 for slotID = 0, bagSize do
Asa@0 41 itemID = GetContainerItemID(bagID, slotID);
Asa@0 42
Asa@0 43 if itemID ~= nil then
Asa@0 44 _, itemCount, _, _, _= GetContainerItemInfo(bagID, slotID);
Asa@0 45 name = GetItemInfo(itemID)
Asa@0 46 if i[name] == nil then
Asa@0 47 i[name] = 0
Asa@0 48 end
Asa@0 49 i[name] = i[name] + (itemCount or 0)
Asa@0 50 end
Asa@0 51
Asa@0 52 end
Asa@0 53
Asa@0 54 end
Asa@0 55 return {items = i, money = GetMoney()}
Asa@0 56 end
Asa@0 57
Asa@0 58 function addon:GetInventoryDiff(pastInventory, current)
Asa@0 59 if current == nil then
Asa@0 60 current = self:GetCurrentInventory()
Asa@0 61 end
Asa@0 62 local diff = {}
Asa@0 63
Asa@0 64 for name, count in pairs(current.items) do
Asa@0 65 if pastInventory.items[name] == nil then
Asa@0 66 diff[name] = count
Asa@0 67 self:Debug("1 diff[" .. name .. "]=" .. diff[name])
Asa@0 68 elseif count - pastInventory.items[name] ~= 0 then
Asa@0 69 diff[name] = count - pastInventory.items[name]
Asa@0 70 self:Debug("2 diff[" .. name .. "]=" .. diff[name])
Asa@0 71 end
Asa@0 72 end
Asa@0 73
Asa@0 74 for name, count in pairs(pastInventory.items) do
Asa@0 75 if current.items[name] == nil then
Asa@0 76 diff[name] = -count
Asa@0 77 self:Debug("3 diff[" .. name .. "]=" .. diff[name])
Asa@0 78 elseif current.items[name] - count ~= 0 then
Asa@0 79 diff[name] = current.items[name] - pastInventory.items[name]
Asa@0 80 self:Debug("4 diff[" .. name .. "]=" .. diff[name])
Asa@0 81 end
Asa@0 82 end
Asa@0 83
Asa@0 84 local moneyDiff = current.money - pastInventory.money
Asa@0 85
Asa@0 86 return {items = diff, money = moneyDiff}
Asa@0 87 end
Asa@0 88
Asa@0 89
Asa@0 90 function addon:ScanMail()
Asa@0 91 local results = {}
Asa@0 92 for mailIndex = 1, GetInboxNumItems() or 0 do
Asa@0 93 local sender, msgSubject, msgMoney, msgCOD, _, msgItem, _, _, msgText, _, isGM = select(3, GetInboxHeaderInfo(mailIndex))
Asa@0 94 local mailType = Postal:GetMailType(msgSubject)
Asa@0 95
Asa@0 96 if mailType == "NonAHMail" then
Asa@0 97 -- Don't know how to handle these yet
Asa@0 98 elseif mailType == "AHSuccess" then
Asa@0 99 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@0 100 if results[itemName] == nil then
Asa@0 101 results[itemName] = 0
Asa@0 102 end
Asa@0 103 results[itemName] = results[itemName] + deposit + buyout - consignment
Asa@0 104
Asa@0 105 elseif mailType == "AHWon" then
Asa@0 106 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@0 107 if results[itemName] == nil then
Asa@0 108 results[itemName] = 0
Asa@0 109 end
Asa@0 110 results[itemName] = results[itemName] - bid
Asa@0 111 elseif mailType == "AHExpired" or mailType == "AHCancelled" then
Asa@0 112 -- These should be handled when you pay the deposit at the AH
Asa@0 113 else
Asa@0 114 self:Debug("Unhandled mail type: " .. mailType)
Asa@0 115 self:Debug(msgSubject)
Asa@0 116 end
Asa@0 117
Asa@0 118 end
Asa@0 119 return results
Asa@0 120 end
Asa@0 121
Asa@0 122 function addon:SaveValue(item, value)
Asa@0 123 local item_account = self.db.factionrealm.item_account
Asa@0 124 if item_account[item] == nil then
Asa@0 125 item_account[item] = 0
Asa@0 126 end
Asa@0 127 item_account[item] = item_account[item] + value
Asa@0 128
Asa@0 129 if item_account[item] >= 0 then
Asa@0 130 item_account[item] = nil
Asa@0 131 end
Asa@0 132 end
Asa@0 133
Asa@4 134 local defaultBagDelay = 0.2
Asa@4 135
Asa@3 136 function addon:WatchBags(delay)
Asa@4 137 delay = delay or defaultBagDelay
Asa@4 138 if delay ~= self.currentBagDelay then
Asa@4 139 self:UnwatchBags()
Asa@4 140 end
Asa@4 141
Asa@4 142 if self.watch_handle == nil then
Asa@4 143 self.currentBagDelay = delay
Asa@4 144 self:Debug("currentBagDelay = " .. delay)
Asa@4 145 addon:UpdateCurrentInventory()
Asa@4 146 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, self.currentBagDelay, "UpdateAudit")
Asa@4 147 end
Asa@0 148 end
Asa@0 149
Asa@0 150 function addon:UnwatchBags()
Asa@4 151 if self.watch_handle ~= nil then
Asa@4 152 self:UnregisterBucket(self.watch_handle)
Asa@4 153 self.watch_handle = nil
Asa@4 154 end
Asa@0 155 end
Asa@0 156
Asa@0 157 function addon:GetItemCost(itemName, countModifier)
Asa@0 158 local invested = abs(self.db.factionrealm.item_account[itemName] or 0)
Asa@0 159
Asa@0 160 if invested > 0 then
Asa@0 161 local _, itemLink = GetItemInfo (itemName);
Asa@0 162 local _, _, _, _, Id = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
Asa@0 163 local count = Altoholic:GetItemCount(tonumber(Id))
Asa@0 164 if countModifier ~= nil then
Asa@0 165 count = count - countModifier
Asa@0 166 end
Asa@0 167 if count == 0 then
Asa@0 168 self.db.factionrealm.item_account[itemName] = nil
Asa@3 169 self:Print("You ran out of " .. itemName .. "and never recovered " .. utils:FormatMoney(invested))
Asa@0 170 return 0, 0, 0
Asa@0 171 end
Asa@0 172 return ceil(invested), ceil(invested/count), count
Asa@0 173 end
Asa@0 174 return 0, 0, 0
Asa@0 175 end