Module:PriceCalculator

From HighSpell Wiki
Revision as of 09:10, 26 June 2025 by Ryan (talk | contribs)
Jump to navigation Jump to search

Documentation for this module may be created at Module:PriceCalculator/doc

local p = {}

-- Internal function to calculate price
function p._calculate(price, priceType)
	-- Sanitize input: convert to string, remove commas
	price = tostring(price):gsub(",", "")
	price = tonumber(price)

	-- Error handling
	if not price or price < 0 then
		return "Invalid price: " .. tostring(price)
	end

	-- Lookup conversion factors
	local percent = {
		sell = 0.375,
		minor = 0.667,
		major = 1.0
	}

	local factor = percent[priceType]
	if not factor then
		return "Invalid type: " .. tostring(priceType)
	end

	-- Calculate and return rounded result
	return math.floor(price * factor + 0.5)
end

-- Entry point for #invoke
function p.calculate(frame)
	local args = frame.args -- Use direct args from #invoke
	local price = args.price or args[1]
	local priceType = args.type or args[2]
	return p._calculate(price, priceType)
end

return p