annotate Core.lua @ 8:0271e781b154

Working on converting the database to store items as links instead of names.
author Asa Ayers <Asa.Ayers@Gmail.com>
date Wed, 23 Jun 2010 23:47:48 -0700
parents bbba2fae0f69
children 374dd1a90d02
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@8 22 item_account = {},
Asa@8 23
Asa@8 24 items = {},
Asa@0 25 },
Asa@0 26 }
Asa@0 27 self.db = LibStub("AceDB-3.0"):New("ItemAuditorDB", DB_defaults, true)
Asa@8 28 addonTable.db= self.db
Asa@8 29 self.items = self.db.factionrealm.items
Asa@0 30
Asa@0 31 self:RegisterOptions()
Asa@0 32
Asa@3 33 self:RegisterEvent("PLAYER_ENTERING_WORLD")
Asa@0 34 end
Asa@0 35
Asa@8 36 function addon:ConvertItems()
Asa@8 37 for itemName, value in pairs(self.db.factionrealm.item_account) do
Asa@8 38 local itemID = utils:GetItemID(itemName)
Asa@8 39 if itemID ~= nil then
Asa@8 40 self:GetItem('item:' .. itemID)
Asa@8 41 end
Asa@8 42 if value == 0 then
Asa@8 43 self.db.factionrealm.item_account[itemName] = nil
Asa@8 44 end
Asa@8 45 end
Asa@8 46
Asa@8 47 for link, data in pairs(self.db.factionrealm.items) do
Asa@8 48 if self:GetItem(link).count == 0 or self:GetItem(link).invested == 0 then
Asa@8 49 self:RemoveItem(link)
Asa@8 50 end
Asa@8 51 end
Asa@8 52 end
Asa@8 53
Asa@0 54 function addon:GetCurrentInventory()
Asa@8 55 local i = {}
Asa@8 56 local bagID
Asa@8 57 local slotID
Asa@8 58
Asa@8 59 for bagID = 0, NUM_BAG_SLOTS do
Asa@8 60 bagSize=GetContainerNumSlots(bagID)
Asa@8 61 for slotID = 0, bagSize do
Asa@8 62 local link= GetContainerItemLink(bagID, slotID);
Asa@8 63
Asa@8 64 if link ~= nil and i[link] == nil then
Asa@8 65 i[link] = GetItemCount(link);
Asa@8 66 end
Asa@8 67 end
Asa@8 68
Asa@8 69 end
Asa@8 70 return {items = i, money = GetMoney()}
Asa@0 71 end
Asa@0 72
Asa@0 73 function addon:GetInventoryDiff(pastInventory, current)
Asa@8 74 if current == nil then
Asa@8 75 current = self:GetCurrentInventory()
Asa@8 76 end
Asa@8 77 local diff = {}
Asa@8 78
Asa@8 79 for link, count in pairs(current.items) do
Asa@8 80 if pastInventory.items[link] == nil then
Asa@8 81 diff[link] = count
Asa@8 82 -- self:Debug("1 diff[" .. name .. "]=" .. diff[name])
Asa@8 83 elseif count - pastInventory.items[link] ~= 0 then
Asa@8 84 diff[link] = count - pastInventory.items[link]
Asa@8 85 -- self:Debug("2 diff[" .. name .. "]=" .. diff[name])
Asa@8 86 end
Asa@8 87 end
Asa@8 88
Asa@8 89 for link, count in pairs(pastInventory.items) do
Asa@8 90 if current.items[link] == nil then
Asa@8 91 diff[link] = -count
Asa@8 92 -- self:Debug("3 diff[" .. name .. "]=" .. diff[name])
Asa@8 93 elseif current.items[link] - count ~= 0 then
Asa@8 94 diff[link] = current.items[link] - pastInventory.items[link]
Asa@8 95 -- self:Debug("4 diff[" .. name .. "]=" .. diff[name])
Asa@8 96 end
Asa@8 97 end
Asa@8 98
Asa@8 99 local moneyDiff = current.money - pastInventory.money
Asa@8 100
Asa@8 101 return {items = diff, money = moneyDiff}
Asa@0 102 end
Asa@0 103
Asa@0 104
Asa@6 105
Asa@0 106 function addon:ScanMail()
Asa@0 107 local results = {}
Asa@0 108 for mailIndex = 1, GetInboxNumItems() or 0 do
Asa@0 109 local sender, msgSubject, msgMoney, msgCOD, _, msgItem, _, _, msgText, _, isGM = select(3, GetInboxHeaderInfo(mailIndex))
Asa@6 110 local mailType = utils:GetMailType(msgSubject)
Asa@6 111
Asa@6 112 results[mailType] = (results[mailType] or {})
Asa@6 113
Asa@5 114 if mailType == "NonAHMail" and msgCOD > 0 then
Asa@6 115 mailType = 'COD'
Asa@6 116 results[mailType] = (results[mailType] or {})
Asa@5 117
Asa@5 118 local itemTypes = {}
Asa@5 119 for itemIndex = 1, ATTACHMENTS_MAX_RECEIVE do
Asa@5 120 local itemName, _, count, _, _= GetInboxItem(mailIndex, itemIndex)
Asa@5 121 if itemName ~= nil then
Asa@7 122 itemTypdes[itemName] = (itemTypes[itemName] or 0) + count
Asa@5 123 end
Asa@5 124 end
Asa@5 125
Asa@5 126 if utils:tcount(itemTypes) == 1 then
Asa@5 127 for itemName, count in pairs(itemTypes) do
Asa@6 128 results[mailType][itemName] = (results[mailType][itemName] or 0) - msgCOD
Asa@5 129 end
Asa@5 130 else
Asa@5 131 self:Debug("Don't know what to do with more than one item type on COD mail.")
Asa@5 132 end
Asa@6 133 elseif mailType == "CODPayment" then
Asa@6 134 itemName = msgSubject:gsub(utils.SubjectPatterns[mailType], function(item) return item end)
Asa@5 135
Asa@6 136 results[mailType][itemName] = (results[mailType][itemName] or 0) + msgMoney
Asa@5 137
Asa@0 138 elseif mailType == "AHSuccess" then
Asa@0 139 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@6 140 results[mailType][itemName] = (results[mailType][itemName] or 0) + deposit + buyout - consignment
Asa@0 141
Asa@0 142 elseif mailType == "AHWon" then
Asa@0 143 local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
Asa@6 144 results[mailType][itemName] = (results[mailType][itemName] or 0) - bid
Asa@5 145 elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
Asa@0 146 -- These should be handled when you pay the deposit at the AH
Asa@0 147 else
Asa@0 148 self:Debug("Unhandled mail type: " .. mailType)
Asa@0 149 self:Debug(msgSubject)
Asa@0 150 end
Asa@0 151
Asa@0 152 end
Asa@0 153 return results
Asa@0 154 end
Asa@0 155
Asa@8 156 function addon:GetItem(link)
Asa@8 157 link = utils:GetSafeLink(link)
Asa@8 158 DevTools_Dump(link)
Asa@8 159 if self.items[link] == nil then
Asa@8 160 local itemName = GetItemInfo(link)
Asa@8 161
Asa@8 162 self.items[link] = {
Asa@8 163 count = Altoholic:GetItemCount(utils:GetIDFromLink(link)),
Asa@8 164 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
Asa@8 165 }
Asa@8 166 self.db.factionrealm.item_account[itemName] = nil
Asa@8 167 end
Asa@8 168
Asa@8 169 return self.items[link]
Asa@8 170 end
Asa@8 171
Asa@8 172 function addon:RemoveItem(link)
Asa@8 173 link = utils:GetSafeLink(link)
Asa@8 174 self.items[link] = nil
Asa@8 175 end
Asa@8 176
Asa@8 177 function addon:SaveValue(link, value)
Asa@0 178 local item_account = self.db.factionrealm.item_account
Asa@6 179
Asa@8 180 local item = self:GetItem(link)
Asa@6 181
Asa@8 182 item.invested = item.invested + value
Asa@8 183
Asa@8 184 local itemName = GetItemInfo(link)
Asa@7 185 if abs(value) > 0 then
Asa@8 186 self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(item.invested) .. "(change: " .. utils:FormatMoney(value) .. ")")
Asa@7 187 end
Asa@0 188
Asa@8 189 if item.invested > 0 then
Asa@8 190 self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(0))
Asa@8 191 self:RemoveItem(link)
Asa@8 192 end
Asa@8 193
Asa@8 194 if item.count == 0 then
Asa@8 195 self:Print("You ran out of " .. itemName .. " and never recovered " .. utils:FormatMoney(item.invested))
Asa@8 196 self:RemoveItem(link)
Asa@0 197 end
Asa@0 198 end
Asa@0 199
Asa@4 200 local defaultBagDelay = 0.2
Asa@4 201
Asa@3 202 function addon:WatchBags(delay)
Asa@4 203 delay = delay or defaultBagDelay
Asa@4 204 if delay ~= self.currentBagDelay then
Asa@4 205 self:UnwatchBags()
Asa@4 206 end
Asa@4 207
Asa@4 208 if self.watch_handle == nil then
Asa@4 209 self.currentBagDelay = delay
Asa@4 210 self:Debug("currentBagDelay = " .. delay)
Asa@4 211 addon:UpdateCurrentInventory()
Asa@4 212 self.watch_handle = self:RegisterBucketEvent({"BAG_UPDATE", "PLAYER_MONEY"}, self.currentBagDelay, "UpdateAudit")
Asa@4 213 end
Asa@0 214 end
Asa@0 215
Asa@0 216 function addon:UnwatchBags()
Asa@4 217 if self.watch_handle ~= nil then
Asa@4 218 self:UnregisterBucket(self.watch_handle)
Asa@4 219 self.watch_handle = nil
Asa@4 220 end
Asa@0 221 end
Asa@0 222
Asa@8 223 function addon:GetItemCost(link, countModifier)
Asa@8 224 local item = self:GetItem(link)
Asa@8 225
Asa@8 226 local invested = item.invested
Asa@0 227
Asa@0 228 if invested > 0 then
Asa@8 229 local ItemID = utils:GetIDFromLink(link)
Asa@7 230 if ItemID ~= nil then
Asa@8 231 local count = self:GetItem(link).count
Asa@7 232
Asa@7 233 if countModifier ~= nil then
Asa@7 234 count = count - countModifier
Asa@7 235 end
Asa@7 236 if count > 0 then
Asa@7 237 return ceil(invested), ceil(invested/count), count
Asa@7 238 end
Asa@0 239 end
Asa@0 240 end
Asa@0 241 return 0, 0, 0
Asa@0 242 end