diff utils.lua @ 3:ed0582126cae

The base features semms to work.
author contrebasse
date Sat, 02 Apr 2011 01:49:39 +0200
parents eba26c900e99
children 250d01156e21
line wrap: on
line diff
--- a/utils.lua	Tue Mar 29 22:06:36 2011 +0200
+++ b/utils.lua	Sat Apr 02 01:49:39 2011 +0200
@@ -1,21 +1,45 @@
 local addonName, A = ...
 
-do
-	-- WoW frames
-	local DEFAULT_CHAT_FRAME = DEFAULT_CHAT_FRAME
+-- DEBUG Print
+function A.DEBUG(msg)
+	DEFAULT_CHAT_FRAME:AddMessage(msg or "nil",1,0,0)
+end -- function
 
-	function A.DEBUG(msg)
-		DEFAULT_CHAT_FRAME:AddMessage(msg or "nil",1,0,0)
-	end -- function
+-- Returns the item ID from its link
+function A.link2ID(link)
+	return tonumber(select(3,string.find(link or "", "-*:(%d+)[:|].*")) or "")
+end -- function
+
+-- Returns the button number for the reagents buttons
+function A.buttonNumber(btn)
+	-- "TradeSkillReagentN"
+	return tonumber(btn:GetName():sub(-1))
 end
 
-do
-	-- Lua functions
-	local select = select
-	local tonumber = tonumber
-	local sfind = string.find
+-- Gives the number of craftable objects
+function A.numMakable(reagentID)
+	-- Look for the recipe to make the item
+	local reagentIndex = A.findSkillIndex(reagentID)
+	if not reagentIndex then return 0 end
 
-	function A.link2ID(link)
-		return tonumber(select(3,sfind(link or "", "-*:(%d+)[:|].*")) or "")
-	end -- function
-end -- do
+	-- Check how many items we can craft
+	local skillName, skillType, numReagentMakable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(reagentIndex)
+	return numReagentMakable or 0, reagentIndex
+end
+
+-- Find the first tradeskill index of the recipe to make an item
+function A.findSkillIndex(itemID)
+	for i = 1,GetNumTradeSkills() do
+		local skillName, skillType, numAvailable, isExpanded, serviceType, numSkillUps = GetTradeSkillInfo(i)
+		if skillType == "header" then
+		else
+			if skillName then
+				local ID = A.link2ID(GetTradeSkillItemLink(i))
+				if ID and ID == itemID then
+					return i
+				end -- if
+			end -- if
+		end -- if
+	end -- for
+	A.DEBUG("Tradeskill not found for "..itemID)
+end -- function