Module:Hero Icon

From Artifact Wiki
Jump to: navigation, search

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

local cargo = mw.ext.cargo
local getArgs = require( 'Module:Arguments' ).main
local p = {}


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


function p._main( args )
	local name = args[1]
	local size = args[2]
	local background = args['bg']

	return p.getHeroIcon(name, size, background)
end


function p.getHeroIcon(name, size, background)
	if(p.isempty(background)) then
		background = 'yes'
	end
	size = size or 20
	
	-- Fix size format, if needed
	if(string.sub(size, -2) == 'px') then
		size = string.gsub(size, "px", "")
	end
	
	local icon = ''

	local cargoOutput = p.getCargo(name)

	-- When available, the icon and color get set to the one found in the database.
	if(cargoOutput ~= nil) then
		icon = cargoOutput['icon']
	end
	
	local formattedImage = ''

	-- If the hero has an icon, it gets set as an image with a link to the hero's page.
	if (icon ~= nil and icon ~= '') then
		if(background == false or background == 'false' or background == 'no' or background == 'none') then
			formattedImage = string.format('[[%s|%spx|link=%s|%s]]', icon, size, name, name)..' '
		else
			local bgImage = string.format('[[%s|link=%s|%spx|%s]]', 'File:Icon_BG.png', name, size, name)
			local bgSize = p.floor(size * 0.8)
			formattedImage = string.format('<div style="display: inline-block;"><div style="position: relative; display: flex; justify-content: center; align-items: center;"> <div style="z-index: 3;">[[%s|%spx|link=%s|%s]]</div><div style="position: absolute;">%s</div></div></div>', icon, bgSize, name, name, bgImage)..' '
		end
	end

	return formattedImage
end


-- Retrieves the icon and color from the hero through the cargo database (defined via the Hero Infobox template).
function p.getCargo(name)
	return cargo.query('cards', 'icon', { where='cards.type="Hero" AND cards.title="' .. name .. '"'})[1]
end

function p.floor(d)
	return math.floor(d)
end

function p.isempty(s)
  return s == nil or s == ''
end

return p