view utils.lua @ 21:25b9f98f9bba

The default is now to craft enough reagents to be able to craft one more item
author contrebasse
date Sun, 10 Apr 2011 21:31:10 +0200
parents 250d01156e21
children 5f3a5b88fb19
line wrap: on
line source
local addonName, A = ...

-- Lua functions
local tonumber = tonumber
local select = select
local sfind = string.find

-- Wow functions

-- DEBUG Print
function A.DEBUG(msg)
	-- GLOBALS: DEFAULT_CHAT_FRAME
	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,sfind(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
	-- Wow functions
	local GetTradeSkillInfo = GetTradeSkillInfo
	local GetNumTradeSkills = GetNumTradeSkills
	local GetTradeSkillItemLink = GetTradeSkillItemLink

	-- 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

		-- 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
end -- do