changeset 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
files Core.lua Modules/Events.lua Modules/Tooltip.lua Modules/Utils.lua
diffstat 4 files changed, 126 insertions(+), 70 deletions(-) [+]
line wrap: on
line diff
--- a/Core.lua	Tue Jun 08 11:19:16 2010 -0700
+++ b/Core.lua	Wed Jun 23 23:47:48 2010 -0700
@@ -19,69 +19,86 @@
 			debug = false
 		},
 		factionrealm = {
-			item_account = {}
+			item_account = {},
+			
+			items = {},
 		},
 	}
 	self.db = LibStub("AceDB-3.0"):New("ItemAuditorDB", DB_defaults, true)
+	addonTable.db= self.db
+	self.items = self.db.factionrealm.items
 	
 	self:RegisterOptions()
 	
 	self:RegisterEvent("PLAYER_ENTERING_WORLD")
 end
 
+function addon:ConvertItems()
+	for itemName, value in pairs(self.db.factionrealm.item_account) do
+		local itemID = utils:GetItemID(itemName)
+		if itemID ~= nil then
+			self:GetItem('item:' .. itemID)
+		end
+		if value == 0 then
+			self.db.factionrealm.item_account[itemName] = nil
+		end
+	end
+	
+	for link, data in pairs(self.db.factionrealm.items) do
+		if self:GetItem(link).count == 0 or self:GetItem(link).invested == 0 then
+			self:RemoveItem(link)
+		end
+	end
+end
+
 function addon:GetCurrentInventory()
-   local i = {}
-   local link
-   
-   for bagID = 0, NUM_BAG_SLOTS do
-      bagSize=GetContainerNumSlots(bagID)
-      for slotID = 0, bagSize do
-         itemID = GetContainerItemID(bagID, slotID);
-         
-         if itemID ~= nil then
-            _, itemCount, _, _, _= GetContainerItemInfo(bagID, slotID);
-            name = GetItemInfo(itemID)
-            if i[name] == nil then
-               i[name] = 0
-            end
-            i[name] = i[name] + (itemCount or 0)
-         end
-         
-      end
-      
-   end
-   return {items = i, money = GetMoney()}
+	local i = {}
+	local bagID
+	local slotID
+	
+	for bagID = 0, NUM_BAG_SLOTS do
+		bagSize=GetContainerNumSlots(bagID)
+		for slotID = 0, bagSize do
+			local link= GetContainerItemLink(bagID, slotID);
+
+			if link ~= nil and i[link] == nil then
+				i[link] = GetItemCount(link);
+			end
+		end
+
+	end
+	return {items = i, money = GetMoney()}
 end
 
 function addon:GetInventoryDiff(pastInventory, current)
-   if current == nil then
-      current = self:GetCurrentInventory()
-   end
-   local diff = {}
-   
-   for name, count in pairs(current.items) do
-      if pastInventory.items[name] == nil then
-         diff[name] = count
-         -- self:Debug("1 diff[" .. name .. "]=" .. diff[name])
-      elseif count - pastInventory.items[name] ~= 0 then
-         diff[name] = count - pastInventory.items[name]
-         -- self:Debug("2 diff[" .. name .. "]=" .. diff[name])        
-      end    
-   end
-   
-   for name, count in pairs(pastInventory.items) do
-      if current.items[name] == nil then
-         diff[name] = -count
-         -- self:Debug("3 diff[" .. name .. "]=" .. diff[name])                
-      elseif current.items[name] - count ~= 0 then
-         diff[name] = current.items[name] - pastInventory.items[name]
-         -- self:Debug("4 diff[" .. name .. "]=" .. diff[name])        
-      end
-   end
-   
-   local moneyDiff = current.money - pastInventory.money
-   
-   return {items = diff, money = moneyDiff}
+	if current == nil then
+		current = self:GetCurrentInventory()
+	end
+	local diff = {}
+
+	for link, count in pairs(current.items) do
+		if pastInventory.items[link] == nil then
+			diff[link] = count
+			-- self:Debug("1 diff[" .. name .. "]=" .. diff[name])
+		elseif count - pastInventory.items[link] ~= 0 then
+			diff[link] = count - pastInventory.items[link]
+			-- self:Debug("2 diff[" .. name .. "]=" .. diff[name])        
+		end    
+	end
+
+	for link, count in pairs(pastInventory.items) do
+		if current.items[link] == nil then
+			diff[link] = -count
+			-- self:Debug("3 diff[" .. name .. "]=" .. diff[name])                
+		elseif current.items[link] - count ~= 0 then
+			diff[link] = current.items[link] - pastInventory.items[link]
+			-- self:Debug("4 diff[" .. name .. "]=" .. diff[name])        
+		end
+	end
+
+	local moneyDiff = current.money - pastInventory.money
+
+	return {items = diff, money = moneyDiff}
 end
 
 
@@ -136,20 +153,47 @@
 	return results   
 end
 
-function addon:SaveValue(item, value)
+function addon:GetItem(link)
+	link = utils:GetSafeLink(link)
+	DevTools_Dump(link)
+	if self.items[link] == nil then
+		local itemName = GetItemInfo(link)
+	
+		self.items[link] = {
+			count = Altoholic:GetItemCount(utils:GetIDFromLink(link)),
+			invested = abs(self.db.factionrealm.item_account[itemName] or 0),
+		}
+		self.db.factionrealm.item_account[itemName] = nil
+	end
+	
+	return self.items[link]
+end
+
+function addon:RemoveItem(link)
+	link = utils:GetSafeLink(link)
+	self.items[link] = nil
+end
+
+function addon:SaveValue(link, value)
 	local item_account = self.db.factionrealm.item_account
 	
-	item_account[item] = (item_account[item] or 0) + value
+	local item = self:GetItem(link)
 	
+	item.invested = item.invested + value
+	
+	local itemName = GetItemInfo(link)
 	if abs(value) > 0 then
-		self:Debug("Updated price of " .. item .. " to " .. utils:FormatMoney(item_account[item]) .. "(change: " .. utils:FormatMoney(value) .. ")")
+		self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(item.invested) .. "(change: " .. utils:FormatMoney(value) .. ")")
 	end
 	
-	if item_account[item] > 0 then
-		self:Debug("Updated price of " .. item .. " to " .. utils:FormatMoney(0))
-		item_account[item] = nil
-	elseif item_account[item] < 0 then
-		addon:GetItemCost(itemName)
+	if item.invested > 0 then
+		self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(0))
+		self:RemoveItem(link)
+	end
+	
+	if item.count == 0 then 
+		self:Print("You ran out of " .. itemName .. " and never recovered " .. utils:FormatMoney(item.invested))
+		self:RemoveItem(link)
 	end
 end
 
@@ -176,17 +220,15 @@
 	end
 end
 
-function addon:GetItemCost(itemName, countModifier)
-	local invested = abs(self.db.factionrealm.item_account[itemName] or 0)
+function addon:GetItemCost(link, countModifier)
+	local item = self:GetItem(link)
+
+	local invested = item.invested
 	
 	if invested > 0 then
-		local ItemID = utils:GetItemID(itemName)
+		local ItemID = utils:GetIDFromLink(link)
 		if ItemID ~= nil then
-			local count = Altoholic:GetItemCount(tonumber(ItemID))
-			if count == 0 then 
-				self.db.factionrealm.item_account[itemName] = nil
-				self:Print("You ran out of " .. itemName .. " and never recovered " .. utils:FormatMoney(invested))
-			end
+			local count = self:GetItem(link).count
 			
 			if countModifier ~= nil then
 				count = count - countModifier
--- a/Modules/Events.lua	Tue Jun 08 11:19:16 2010 -0700
+++ b/Modules/Events.lua	Wed Jun 23 23:47:48 2010 -0700
@@ -4,6 +4,9 @@
 local utils = addonTable.utils
 
 function addon:PLAYER_ENTERING_WORLD()
+	addon:ConvertItems()
+	DevTools_Dump(ItemAuditor.db.factionrealm.items)
+
 	self:RegisterEvent("MAIL_SHOW")
 	self:RegisterEvent("UNIT_SPELLCAST_START")
 	self:WatchBags()
@@ -99,7 +102,7 @@
 	if diff.money > 0 and utils:tcount(positive) > 0 and utils:tcount(negative) == 0 then
 		-- self:Debug("loot")
 	elseif utils:tcount(diff.items) == 1 then
-		-- self:Debug("purchase or sale")
+		self:Debug("purchase or sale")
 		
 		for itemName, count in pairs(diff.items) do
 			self:SaveValue(itemName, diff.money)
--- a/Modules/Tooltip.lua	Tue Jun 08 11:19:16 2010 -0700
+++ b/Modules/Tooltip.lua	Wed Jun 23 23:47:48 2010 -0700
@@ -8,10 +8,10 @@
 		return;
 	end
 
-	local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, _, _, _, _, itemVendorPrice = GetItemInfo (link);
+	-- local itemName, itemLink, itemRarity, itemLevel, itemMinLevel, itemType, _, _, _, _, itemVendorPrice = GetItemInfo (link);
 	-- local _, _, Color, Ltype, Id, Enchant, Gem1, Gem2, Gem3, Gem4, Suffix, Unique, LinkLvl, Name = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
 
-	local investedTotal, investedPerItem, count = ItemAuditor:GetItemCost(itemName)
+	local investedTotal, investedPerItem, count = ItemAuditor:GetItemCost(link)
 	
 	local AHCut = 0.05
 	local keep = 1 - AHCut
--- a/Modules/Utils.lua	Tue Jun 08 11:19:16 2010 -0700
+++ b/Modules/Utils.lua	Wed Jun 23 23:47:48 2010 -0700
@@ -14,7 +14,7 @@
 	return prefix .. Altoholic:GetMoneyString(abs(money), WHITE, false)
 end
 
-                   -- This is only here to make sure this doesn't blow up if ReplaceItemCache is never called
+-- This is only here to make sure this doesn't blow up if ReplaceItemCache is never called
 local item_db = {}
 
 function addon:ReplaceItemCache(new_cache)
@@ -71,6 +71,17 @@
    return n
 end
 
+function addon:GetSafeLink(link)
+	if link ~= string.match(link, '.-:[-0-9]+[:0-9]*') then
+		link = link and string.match(link, "|H(.-):([-0-9]+):([0-9]+)|h")
+	end
+	return link and string.gsub(link, ":0:0:0:0:0:0", "")
+end
+
+function addon:GetIDFromLink(link)
+	local _, _, _, _, Id = string.find(link, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
+	return tonumber(Id)
+end
 
 function addon:GetDebug(info)
 	return true