changeset 9:374dd1a90d02

Changed the way things are stored so that items known only by name, usually from AH mail, will be stored by their name, but will get converted if the link is discovered through a tooltip. This version is funcioning again
author Asa Ayers <Asa.Ayers@Gmail.com>
date Fri, 25 Jun 2010 01:17:58 -0700
parents 0271e781b154
children c79ede3c7b82
files Core.lua Modules/Events.lua Modules/Utils.lua
diffstat 3 files changed, 132 insertions(+), 54 deletions(-) [+]
line wrap: on
line diff
--- a/Core.lua	Wed Jun 23 23:47:48 2010 -0700
+++ b/Core.lua	Fri Jun 25 01:17:58 2010 -0700
@@ -1,6 +1,7 @@
 local addonName, addonTable = ...; 
 _G[addonName] = LibStub("AceAddon-3.0"):NewAddon(addonName, "AceConsole-3.0", "AceEvent-3.0", "AceBucket-3.0")
 local addon = _G[addonName]
+addonTable.ItemAuditor = addon
 
 local utils = addonTable.utils
 
@@ -112,6 +113,7 @@
 		results[mailType] = (results[mailType] or {})
 		
 		if mailType == "NonAHMail" and msgCOD > 0 then
+			--[[
 			mailType = 'COD'
 			results[mailType] = (results[mailType] or {})
 			
@@ -130,18 +132,19 @@
 			else
 				self:Debug("Don't know what to do with more than one item type on COD mail.")
 			end
+			]]
 		elseif mailType == "CODPayment" then	
 			itemName = msgSubject:gsub(utils.SubjectPatterns[mailType], function(item) return item end)
 			
-			results[mailType][itemName] = (results[mailType][itemName] or 0) + msgMoney
+			results[mailType][itemName] = (results[mailType][itemName] or 0) - msgMoney
 			
 		elseif mailType == "AHSuccess" then
 			local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
-			results[mailType][itemName] = (results[mailType][itemName] or 0) + deposit + buyout - consignment
+			results[mailType][itemName] = (results[mailType][itemName] or 0) - deposit - buyout + consignment
 
 		elseif mailType == "AHWon" then
 			local invoiceType, itemName, playerName, bid, buyout, deposit, consignment = GetInboxInvoiceInfo(mailIndex);
-			results[mailType][itemName] = (results[mailType][itemName] or 0) - bid
+			results[mailType][itemName] = (results[mailType][itemName] or 0) + bid
 		elseif mailType == "AHExpired" or mailType == "AHCancelled" or mailType == "AHOutbid" then
 			-- These should be handled when you pay the deposit at the AH
 		else
@@ -153,12 +156,20 @@
 	return results   
 end
 
-function addon:GetItem(link)
-	link = utils:GetSafeLink(link)
-	DevTools_Dump(link)
-	if self.items[link] == nil then
-		local itemName = GetItemInfo(link)
+function addon:GetItem(link, viewOnly)
+	if viewOnly == nil then
+		viewOnly = false
+	end
 	
+	local itemName = nil
+	if self:GetSafeLink(link) == nil then
+		itemName = link
+	else
+		link = self:GetSafeLink(link)
+		itemName = GetItemInfo(link)
+	end
+	
+	if self.db.factionrealm.item_account[itemName] ~= nil then
 		self.items[link] = {
 			count = Altoholic:GetItemCount(utils:GetIDFromLink(link)),
 			invested = abs(self.db.factionrealm.item_account[itemName] or 0),
@@ -166,32 +177,67 @@
 		self.db.factionrealm.item_account[itemName] = nil
 	end
 	
+	if viewOnly == false and self.items[link] == nil then
+		local itemName = GetItemInfo(link)
+	
+		self.items[link] = {
+			count = Altoholic:GetItemCount(self:GetIDFromLink(link)),
+			invested = abs(self.db.factionrealm.item_account[itemName] or 0),
+		}
+		
+	end
+	
+	
+	
+	if viewOnly == true and self.items[link] == nil then
+		return {count = 0, invested = 0}
+	elseif viewOnly == true then
+		return {count = self.items[link].count, invested = self.items[link].invested}
+	end
 	return self.items[link]
 end
 
 function addon:RemoveItem(link)
-	link = utils:GetSafeLink(link)
-	self.items[link] = nil
+	self.db.factionrealm.item_account[link] = nil
+	link = self:GetSafeLink(link)
+	if link ~= nil then
+		self.items[link] = nil
+	end
 end
+--[[
+ItemAuditor:SaveValue('Scroll of Enchant Weapon - Exceptional Spellpower', 1)
 
+ DevTools_Dump(ItemAuditor.db.factionrealm.item_account)
+ 
+ = ItemAuditor:GetItem('Scroll of Enchant Weapon - Exceptional Spellpower', true).invested
+]]
 function addon:SaveValue(link, value)
-	local item_account = self.db.factionrealm.item_account
+	local item = nil
+	local realLink = self:GetSafeLink(link)
+	local itemName = nil
+	if realLink == nil then
+		itemName = link
+		
+		self.db.factionrealm.item_account[itemName] = (self.db.factionrealm.item_account[itemName] or 0) + value
+		
+		item = {invested = self.db.factionrealm.item_account[itemName], count = 1}
+	else
 	
-	local item = self:GetItem(link)
+		item = self:GetItem(realLink)
 	
-	item.invested = item.invested + value
+		item.invested = item.invested + value
+		
+		itemName = GetItemInfo(realLink)
+	end
 	
-	local itemName = GetItemInfo(link)
 	if abs(value) > 0 then
 		self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(item.invested) .. "(change: " .. utils:FormatMoney(value) .. ")")
 	end
 	
-	if item.invested > 0 then
+	if item.invested <= 0 then
 		self:Debug("Updated price of " .. itemName .. " to " .. utils:FormatMoney(0))
 		self:RemoveItem(link)
-	end
-	
-	if item.count == 0 then 
+	elseif item.count == 0 then 
 		self:Print("You ran out of " .. itemName .. " and never recovered " .. utils:FormatMoney(item.invested))
 		self:RemoveItem(link)
 	end
@@ -220,23 +266,44 @@
 	end
 end
 
+function addon:GetItemID(itemName)
+	return utils:GetItemID(itemName)
+end
+
+function addon:GetSafeLink(link)
+	local newLink = nil
+
+	if link ~= string.match(link, '.-:[-0-9]+[:0-9]*') then
+		newLink = link and string.match(link, "|H(.-):([-0-9]+):([0-9]+)|h")
+	end
+	if newLink == nil then
+		local itemID = self:GetItemID(link)
+		if itemID ~= nil then
+			_, newLink = GetItemInfo(itemID)
+			return self:GetSafeLink(newLink)
+		end
+	end
+	return newLink and string.gsub(newLink, ":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:GetItemCost(link, countModifier)
-	local item = self:GetItem(link)
+	local item = self:GetItem(link, true)
 
-	local invested = item.invested
-	
-	if invested > 0 then
-		local ItemID = utils:GetIDFromLink(link)
-		if ItemID ~= nil then
-			local count = self:GetItem(link).count
-			
-			if countModifier ~= nil then
-				count = count - countModifier
-			end
-			if count > 0 then 
-				return ceil(invested), ceil(invested/count), count
-			end
+	if item.invested > 0 then
+		local count = item.count
+		
+		if countModifier ~= nil then
+			count = count - countModifier
 		end
+		if count > 0 then 
+			return ceil(item.invested), ceil(item.invested/item.count), count
+		end
+		
 	end
 	return 0, 0, 0
 end
--- a/Modules/Events.lua	Wed Jun 23 23:47:48 2010 -0700
+++ b/Modules/Events.lua	Fri Jun 25 01:17:58 2010 -0700
@@ -4,12 +4,11 @@
 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()
+	
+	addon:ConvertItems()
 end
  
  function addon:MAIL_SHOW()
@@ -81,7 +80,7 @@
 end
 
 function addon:UpdateAudit()
-	-- self:Debug("UpdateAudit")
+	self:Debug("UpdateAudit")
 	local currentInventory = self:GetCurrentInventory()
 	local diff =  addon:GetInventoryDiff(self.lastInventory, currentInventory)
 	-- this is only here for debugging
@@ -104,8 +103,8 @@
 	elseif utils:tcount(diff.items) == 1 then
 		self:Debug("purchase or sale")
 		
-		for itemName, count in pairs(diff.items) do
-			self:SaveValue(itemName, diff.money)
+		for link, count in pairs(diff.items) do
+			self:SaveValue(link, 0 - diff.money)
 		end
 	elseif utils:tcount(diff.items) > 1 then
 		
@@ -113,17 +112,17 @@
 			-- we must have created/converted something
 			-- self:Debug("conversion")
 			local totalChange = 0
-			for itemName, change in pairs(negative) do
-				local _, itemCost, count = self:GetItemCost(itemName, change)
-				self:SaveValue(itemName, abs(itemCost * change))
+			for link, change in pairs(negative) do
+				local _, itemCost, count = self:GetItemCost(link, change)
+				self:SaveValue(link, itemCost * change)
 				
-				totalChange = totalChange + abs(itemCost * change)
+				totalChange = totalChange + (itemCost * abs(change))
 			end
 			
 			local valuePerItem = totalChange / positiveCount
 			
-			for itemName, change in pairs(positive) do
-				self:SaveValue(itemName, 0-abs(valuePerItem * change))
+			for link, change in pairs(positive) do
+				self:SaveValue(link, valuePerItem * change)
 			end
 		end
 	end
--- a/Modules/Utils.lua	Wed Jun 23 23:47:48 2010 -0700
+++ b/Modules/Utils.lua	Fri Jun 25 01:17:58 2010 -0700
@@ -5,6 +5,7 @@
 AceConsole:Embed(addon)
 
 addonTable.utils = addon
+IAUtils = addon
 
 function addon:FormatMoney(money)
 	local prefix = ""
@@ -36,9 +37,30 @@
 		end
 	end
 	
+	if tmp_item_cache[itemName] == nil then
+		for link, data in pairs(ItemAuditor.db.factionrealm.items) do
+			local name, itemLink = GetItemInfo (link);
+			if name == itemName then
+				local _, _, _, _, itemID = string.find(itemLink, "|?c?f?f?(%x*)|?H?([^:]*):?(%d+):?(%d*):?(%d*):?(%d*):?(%d*):?(%d*):?(%-?%d*):?(%-?%d*):?(%d*)|?h?%[?([^%[%]]*)%]?|?h?|?r?")
+				tmp_item_cache[itemName] = tonumber(itemID)
+			end
+		end
+		
+	end
+	
 	return tmp_item_cache[itemName]
 end
 
+function addon:GetLinkFromName(itemName)
+	local itemID = self:GetItemID(itemName)
+	local itemLink
+	if itemID ~= nil then
+		_, itemLink = GetItemInfo(itemID)
+	end
+	
+	return itemLink
+end
+
 function addon:SaveItemID(itemName, id)
 	item_db[itemName] = tonumber(id)
 end
@@ -71,17 +93,7 @@
    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