Module:BoostedDropsLine/Sandbox

This is an old revision of this page, as edited by Alex (talk | contribs) at 00:11, 17 October 2024. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Documentation for this module may be created at Module:BoostedDropsLine/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']
	local Name = args['Name']
	local AltName = args['AltName'] --intended to be used when links are formatted differently, e.x. when they are multiple sources
	local Source = args['Source']
	local AltSource = args['AltSource'] --intended to be used when links are formatted differently, e.x. when they are multiple sources
	local Rarity = args['Rarity']

	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 = Rarity * 2
	local Rarity3 = Rarity * 3
	local rare1_class, rare2_class, rare3_class
	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)
	
	rare1_text = '1/' .. commas(sigfig(1/Rarity, 3))
	rare2_text = '1/' .. commas(sigfig((math.floor(1/Rarity2)), 4))
	rare3_text = '1/' .. commas(sigfig((math.floor(1/Rarity3)), 4))
	
		--Use name as link, or altname exactly as written
	if Image == isNothing then
		image = mw.ustring.format('[[File:%s.png]]', Name)
		else
		image = Image
	end

	
	--Use name as link, or altname exactly as written
	if Name == isNothing then
		Name = AltName
		else
		Name = mw.ustring.format('[[%s]]', Name)
	end
	
	--Use single monster source as link, or altsource if Source is empty
	if Source == isNothing then
		Source = AltSource
		else
		
		Source = mw.ustring.format('[[%s]]', Source)
	end
	
	--create rows for table	
	local 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()

	return tostring(ret)
end

return p;