Quantcast
-- SageReforge.lua
-- Credit to emelio, read prior to writing this lib

----------------------------------------------------------------------------------------------------
local Class = sage.class.Class

----------------------------------------------------------------------------------------------------
-- A specific reforging from one stat to another
local Reforging do
	local class = Class:NewClass{name="Reforging"}

	-- @param instance The object being init'd
	-- @param index The numeric id for a specific reforging as found in the item string
	-- @param from A {index=, key=, shortName} for the stat being reduced
	-- @param to   A {index=, key=, shortName} for the stat being increased
	function class:_Initialize(instance, index, from, to)		-- self is class
		instance.index = index
		instance.from = from
		instance.to = to
	end

	function class:GetShortNames()
		return self.from.shortName, self.to.shortName
	end

	function class:Describe()
		return self.from.key .. "->" .. self.to.key
	end

	function class:_Dump(expectedIndex)
		self:Debug(expectedIndex, "/", self.index, self.from.shortName, "->", self.to.shortName, tostring(self))
	end

	function class:AdjustStats(stats)
		local from, to = self:GetShortNames()
		if not stats[from] then
			error("Stat " .. tostring(from) .. " not found on item " .. tostring(self.itemLink))
		end
		if stats[to] then
			error("Stat " .. tostring(to) .. " found on item " .. tostring(self.itemLink))
		end

		local amountMoved = math.floor(0.4 * stats[from])
		stats[from] = stats[from] - amountMoved
		stats[to] = amountMoved
	end


	Reforging = class
end

----------------------------------------------------------------------------------------------------
-- The collection of all possible Reforging's
local Reforgings do
	local class = Class:NewClass{name="Reforgings"}

	local MAGIC_OFFSET = 113
	local reforgeableStats = {
			{index=1, key="Spirit", shortName="ITEM_MOD_SPIRIT_SHORT"},
			{index=2, key="Dodge", shortName="ITEM_MOD_DODGE_RATING_SHORT"},
			{index=3, key="Parry", shortName="ITEM_MOD_PARRY_RATING_SHORT"},
			{index=4, key="Hit", shortName="ITEM_MOD_HIT_RATING_SHORT"},
			{index=5, key="Crit", shortName="ITEM_MOD_CRIT_RATING_SHORT"},
			{index=6, key="Haste", shortName="ITEM_MOD_HASTE_RATING_SHORT"},
			{index=7, key="Expertise", shortName="ITEM_MOD_EXPERTISE_RATING_SHORT"},
			{index=8, key="Mastery", shortName="ITEM_MOD_MASTERY_RATING_SHORT"}}

	local REFORGING_BY_ID = {} do
		local index = MAGIC_OFFSET
		-- the association between ids and Reforging's is empirically derived
		for _, from in ipairs(reforgeableStats) do
			for _, to in ipairs(reforgeableStats) do
				if from~=to then
					REFORGING_BY_ID[index] = Reforging:New(index, from, to)
					index = index + 1
				end
			end
		end
	end


	function class:_Initialize(instance, reforgingById)		--self is class
		instance.reforgingById = reforgingById or REFORGING_BY_ID
	end

	function class:All()
		return self.reforgingById
	end

	-- Finds the reforging for the given itemId
	-- @param itemId An itemId ("item:...") or item link
	-- @return The reforging in effect for the item, or nil if none in effect
	function class:Find(itemId)
		local id = tonumber(itemId:match("item:%d+:%d+:%d+:%d+:%d+:%d+:%-?%d+:%-?%d+:%d+:(%d+)"))
		local result = self.reforgingById[id or 0]		-- 0 will return nil
		return result
	end

	Reforgings = class
end


namespace("sage.reforge")
sage.reforge.Reforging = Reforging
sage.reforge.Reforgings = Reforgings