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

local p = {};
local lang = mw.language.getContentLanguage()

local rarity_class = {
	{ 1, 'table-bg-blue' },
	{ 1/25, 'table-bg-green' },
	{ 1/99.99, 'table-bg-yellow' },
	{ 1/999.99, 'table-bg-orange' },
	{ 1/9999999, 'table-bg-red' }
}

function get_rarity_class(val)
	for i,v in ipairs(rarity_class) do
		curr = v
		if val >= v[1] then
			break
		end
	end
	return curr[2]
end

function sigfig(n, f)
	f = math.floor(f-1)
	if n == 0 then return 0 end
	local m = math.floor(math.log10(n))
	local v = n / (10^(m-f))
	v = math.floor(v) * 10^(m-f)
	return v
end

function commas(n)
	if tonumber(n) then
		return lang:formatNum(tonumber(n))
	else
		return n
	end
end
p.commas = commas

function expr(t)
	t = t:gsub(',', '')
	local err, val = pcall(mw.ext.ParserFunctions.expr, t)
	if err then
		return tonumber(val)
	else
		return false
	end
end

function p.main(frame)
	local args = frame:getParent().args
	return p._main(args)
end

function p._main(args)
	local Image = args['Image']			-- only used if Name is blank, or wish to override default image
	local Name = args['Name']			-- name of item or drop
	local OptName = args['OptName'] 	-- intended to be used when links are formatted differently, e.x. when they are multiple sources
	local Source = args['Source']		-- source of drop
	local OptSource = args['OptSource'] -- intended to be used when links are formatted differently, e.x. when they are multiple sources
	local Rarity = args['Rarity']		-- base rarity of drop
	local Multi1 = args['Multi1']		-- Optional change to tier 1 rarity multiplier, default to 2x 
	local Multi2 = args['Multi2']		-- Optional change to tier 2 rarity multiplier, default to 3x
	local Multi3 = args['Multi2']		-- Optional change to tier 3 rarity multiplier, default to 4x
	local Tiers = args['Tiers']			-- Optional parameter to determine quantity of rarity tiers to create on table row, including base rarity (default is 3)

	rarity_value = Rarity:gsub(',','') --temp place to put this without overriding rarity
	local rv1, rv2 = string.match(rarity_value, '([%d%.]+)/([%d%.]+)')
	if rv1 and rv2 then
		Rarity = commas(rv1) .. '/' .. commas(rv2)
		Rarity = rv1/rv2
	else
		Rarity = expr(Rarity)
	end

	local Rarity2 				
	local Rarity3
	local Rarity3	
	local TotalTiers
	
	if Multi1 == isNothing then 	-- Make droprate 2x by default
		Rarity2 = Rarity * 2
		else
			Rarity2 = Rarity * Multi1
	end
	
	if Multi2 == isNothing then 	-- Make droprate 3x by default
		Rarity3 = Rarity * 3
		else
			Rarity3 = Rarity * Multi2
	end

	if Multi3 == isNothing then 	-- Make droprate 4x by default
		Rarity4 = Rarity * 4
		else
			Rarity4 = Rarity * Multi3
	end
	
	if Tiers == isNothing then
		TotalTiers = 3
		else
			TotalTiers = Tiers
	end
	
	local rare1_class, rare2_class, rare3_class	-- assign colors for rarity 
	local rare1_text, rare2_text, rare3_text
	
	rare1_class = get_rarity_class(Rarity)
	rare2_class = get_rarity_class(Rarity2)
	rare3_class = get_rarity_class(Rarity3)
	rare4_class = get_rarity_class(Rarity4)
	
	rare1_text = '1/' .. commas(1/Rarity, 3)
	rare2_text = '1/' .. commas(sigfig((math.floor(1/Rarity2 + 0.001)), 5))
	rare3_text = '1/' .. commas(sigfig((math.floor(1/Rarity3 + 0.001)), 5))
	rare4_text = '1/' .. commas(sigfig((math.floor(1/Rarity4 + 0.001)), 5))
	
	--Use Name as image link, or OptName exactly as written
	if Image == isNothing then
		image = mw.ustring.format('[[File:%s.png]]', Name)
		else
		image = mw.ustring.format('[[File:%s.png]]', Image)
	end
	
	--Use Name as link, or OptName exactly as written
	if Name == isNothing then
		Name = OptName
		else
		Name = mw.ustring.format('[[%s]]', Name)
	end
	
	--Use Source as link, or OptSource exactly as written if Source is empty
	if Source == isNothing then
		Source = OptSource
		else
		Source = mw.ustring.format('[[%s]]', Source)
	end
	
	--create rows for table	
	local ret
	if TotalTiers == "4" then
		ret = mw.html.create('tr')
			:tag('td')
				:addClass('inventory-image')
				:wikitext(image)
			:done()
			:tag('td')
				:css('text-align','left')
				:addClass('item-col')
				:wikitext(Name)
			:done()
			:tag('td')
				:css('text-align','center')
				:addClass('source-col')
				:wikitext(Source)
			:done()
			:tag('td')
				:addClass(rare1_class)
				:wikitext(rare1_text)
			:done()
			:tag('td')
				:addClass(rare2_class)
				:wikitext(rare2_text)
			:done()
			:tag('td')
				:addClass(rare3_class)
				:wikitext(rare3_text)
			:done()
			:tag('td')
				:addClass(rare4_class)
				:wikitext(rare4_text)
			:done()		
	else
		ret = mw.html.create('tr')
			:tag('td')
				:addClass('inventory-image')
				:wikitext(image)
			:done()
			:tag('td')
				:css('text-align','left')
				:addClass('item-col')
				:wikitext(Name)
			:done()
			:tag('td')
				:css('text-align','center')
				:addClass('source-col')
				:wikitext(Source)
			:done()
			:tag('td')
				:addClass(rare1_class)
				:wikitext(rare1_text)
			:done()
			:tag('td')
				:addClass(rare2_class)
				:wikitext(rare2_text)
			:done()
			:tag('td')
				:addClass(rare3_class)
				:wikitext(rare3_text)
			:done()
	end
	return tostring(ret)
end

return p;