comparison 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
comparison
equal deleted inserted replaced
7:bbba2fae0f69 8:0271e781b154
17 local DB_defaults = { 17 local DB_defaults = {
18 char = { 18 char = {
19 debug = false 19 debug = false
20 }, 20 },
21 factionrealm = { 21 factionrealm = {
22 item_account = {} 22 item_account = {},
23
24 items = {},
23 }, 25 },
24 } 26 }
25 self.db = LibStub("AceDB-3.0"):New("ItemAuditorDB", DB_defaults, true) 27 self.db = LibStub("AceDB-3.0"):New("ItemAuditorDB", DB_defaults, true)
28 addonTable.db= self.db
29 self.items = self.db.factionrealm.items
26 30
27 self:RegisterOptions() 31 self:RegisterOptions()
28 32
29 self:RegisterEvent("PLAYER_ENTERING_WORLD") 33 self:RegisterEvent("PLAYER_ENTERING_WORLD")
30 end 34 end
31 35
36 function addon:ConvertItems()
37 for itemName, value in pairs(self.db.factionrealm.item_account) do
38 local itemID = utils:GetItemID(itemName)
39 if itemID ~= nil then
40 self:GetItem('item:' .. itemID)
41 end
42 if value == 0 then
43 self.db.factionrealm.item_account[itemName] = nil
44 end
45 end
46
47 for link, data in pairs(self.db.factionrealm.items) do
48 if self:GetItem(link).count == 0 or self:GetItem(link).invested == 0 then
49 self:RemoveItem(link)
50 end
51 end
52 end
53
32 function addon:GetCurrentInventory() 54 function addon:GetCurrentInventory()
33 local i = {} 55 local i = {}
34 local link 56 local bagID
35 57 local slotID
36 for bagID = 0, NUM_BAG_SLOTS do 58
37 bagSize=GetContainerNumSlots(bagID) 59 for bagID = 0, NUM_BAG_SLOTS do
38 for slotID = 0, bagSize do 60 bagSize=GetContainerNumSlots(bagID)
39 itemID = GetContainerItemID(bagID, slotID); 61 for slotID = 0, bagSize do
40 62 local link= GetContainerItemLink(bagID, slotID);
41 if itemID ~= nil then 63
42 _, itemCount, _, _, _= GetContainerItemInfo(bagID, slotID); 64 if link ~= nil and i[link] == nil then
43 name = GetItemInfo(itemID) 65 i[link] = GetItemCount(link);
44 if i[name] == nil then 66 end
45 i[name] = 0 67 end
46 end 68
47 i[name] = i[name] + (itemCount or 0) 69 end
48 end 70 return {items = i, money = GetMoney()}
49
50 end
51
52 end
53 return {items = i, money = GetMoney()}
54 end 71 end
55 72
56 function addon:GetInventoryDiff(pastInventory, current) 73 function addon:GetInventoryDiff(pastInventory, current)
57 if current == nil then 74 if current == nil then
58 current = self:GetCurrentInventory() 75 current = self:GetCurrentInventory()
59 end 76 end
60 local diff = {} 77 local diff = {}
61 78
62 for name, count in pairs(current.items) do 79 for link, count in pairs(current.items) do
63 if pastInventory.items[name] == nil then 80 if pastInventory.items[link] == nil then
64 diff[name] = count 81 diff[link] = count
65 -- self:Debug("1 diff[" .. name .. "]=" .. diff[name]) 82 -- self:Debug("1 diff[" .. name .. "]=" .. diff[name])
66 elseif count - pastInventory.items[name] ~= 0 then 83 elseif count - pastInventory.items[link] ~= 0 then
67 diff[name] = count - pastInventory.items[name] 84 diff[link] = count - pastInventory.items[link]
68 -- self:Debug("2 diff[" .. name .. "]=" .. diff[name]) 85 -- self:Debug("2 diff[" .. name .. "]=" .. diff[name])
69 end 86 end
70 end 87 end
71 88
72 for name, count in pairs(pastInventory.items) do 89 for link, count in pairs(pastInventory.items) do
73 if current.items[name] == nil then 90 if current.items[link] == nil then
74 diff[name] = -count 91 diff[link] = -count
75 -- self:Debug("3 diff[" .. name .. "]=" .. diff[name]) 92 -- self:Debug("3 diff[" .. name .. "]=" .. diff[name])
76 elseif current.items[name] - count ~= 0 then 93 elseif current.items[link] - count ~= 0 then
77 diff[name] = current.items[name] - pastInventory.items[name] 94 diff[link] = current.items[link] - pastInventory.items[link]
78 -- self:Debug("4 diff[" .. name .. "]=" .. diff[name]) 95 -- self:Debug("4 diff[" .. name .. "]=" .. diff[name])
79 end 96 end
80 end 97 end
81 98
82 local moneyDiff = current.money - pastInventory.money 99 local moneyDiff = current.money - pastInventory.money
83 100
84 return {items = diff, money = moneyDiff} 101 return {items = diff, money = moneyDiff}
85 end 102 end
86 103
87 104
88 105
89 function addon:ScanMail() 106 function addon:ScanMail()
134 151
135 end 152 end
136 return results 153 return results
137 end 154 end
138 155
139 function addon:SaveValue(item, value) 156 function addon:GetItem(link)
157 link = utils:GetSafeLink(link)
158 DevTools_Dump(link)
159 if self.items[link] == nil then
160 local itemName = GetItemInfo(link)
161
162 self.items[link] = {
163 count = Altoholic:GetItemCount(utils:GetIDFromLink(link)),
164 invested = abs(self.db.factionrealm.item_account[itemName] or 0),
165 }
166 self.db.factionrealm.item_account[itemName] = nil
167 end
168
169 return self.items[link]
170 end
171
172 function addon:RemoveItem(link)
173 link = utils:GetSafeLink(link)
174 self.items[link] = nil
175 end
176
177 function addon:SaveValue(link, value)
140 local item_account = self.db.factionrealm.item_account 178 local item_account = self.db.factionrealm.item_account
141 179
142 item_account[item] = (item_account[item] or 0) + value 180 local item = self:GetItem(link)
143 181
182 item.invested = item.invested + value
183
184 local itemName = GetItemInfo(link)
144 if abs(value) > 0 then 185 if abs(value) > 0 then
145 self:Debug("Updated price of " .. item .. " to " .. utils:FormatMoney(item_account[item]) .. "(change: " .. utils:FormatMoney(value) .. ")") 186 self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(item.invested) .. "(change: " .. utils:FormatMoney(value) .. ")")
146 end 187 end
147 188
148 if item_account[item] > 0 then 189 if item.invested > 0 then
149 self:Debug("Updated price of " .. item .. " to " .. utils:FormatMoney(0)) 190 self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(0))
150 item_account[item] = nil 191 self:RemoveItem(link)
151 elseif item_account[item] < 0 then 192 end
152 addon:GetItemCost(itemName) 193
194 if item.count == 0 then
195 self:Print("You ran out of " .. itemName .. " and never recovered " .. utils:FormatMoney(item.invested))
196 self:RemoveItem(link)
153 end 197 end
154 end 198 end
155 199
156 local defaultBagDelay = 0.2 200 local defaultBagDelay = 0.2
157 201
174 self:UnregisterBucket(self.watch_handle) 218 self:UnregisterBucket(self.watch_handle)
175 self.watch_handle = nil 219 self.watch_handle = nil
176 end 220 end
177 end 221 end
178 222
179 function addon:GetItemCost(itemName, countModifier) 223 function addon:GetItemCost(link, countModifier)
180 local invested = abs(self.db.factionrealm.item_account[itemName] or 0) 224 local item = self:GetItem(link)
225
226 local invested = item.invested
181 227
182 if invested > 0 then 228 if invested > 0 then
183 local ItemID = utils:GetItemID(itemName) 229 local ItemID = utils:GetIDFromLink(link)
184 if ItemID ~= nil then 230 if ItemID ~= nil then
185 local count = Altoholic:GetItemCount(tonumber(ItemID)) 231 local count = self:GetItem(link).count
186 if count == 0 then
187 self.db.factionrealm.item_account[itemName] = nil
188 self:Print("You ran out of " .. itemName .. " and never recovered " .. utils:FormatMoney(invested))
189 end
190 232
191 if countModifier ~= nil then 233 if countModifier ~= nil then
192 count = count - countModifier 234 count = count - countModifier
193 end 235 end
194 if count > 0 then 236 if count > 0 then