Difference between revisions of "Module:Hero Icon"

From Artifact Wiki
Jump to: navigation, search
m (Fix return value if the hero does not have an icon)
m
Line 35: Line 35:
  
 
-- When available, the icon get set to the one found in the database.
 
-- When available, the icon get set to the one found in the database.
if(cargoOutput ~= nil) then
+
if(not p.isEmpty(cargoOutput)) then
 
-- Check if icon is available
 
-- Check if icon is available
if(cargoOutput['icon'] ~= nil) then
+
if(not p.isEmpty(cargoOutput['icon'])) then
 
icon = 'File:' .. cargoOutput['icon']
 
icon = 'File:' .. cargoOutput['icon']
 
end
 
end
Line 45: Line 45:
  
 
-- If the hero has an icon, it gets set as an image with a link to the hero's page.
 
-- 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 (not p.isEmpty(icon)) then
 
if(background == false or background == 'false' or background == 'no' or background == 'none') 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)..' '
 
formattedImage = string.format('[[%s|%spx|link=%s|%s]]', icon, size, name, name)..' '

Revision as of 03:19, 14 April 2020

Documentation for Module:Hero Icon [edit] [edit doc]

Info Icon.png See also: Hero ID template.

This module provides a hero's icon without the need of aditional arguments. They are retrieved from the Cargo table 'Heroes', defined in the Hero Infobox template.

Preview


Hero Icon template: Axe

Usage


{{#invoke:Hero Icon|main}}
or
{{Hero Icon|<hero name>|<icon size>}}

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 get set to the one found in the database.
	if(not p.isEmpty(cargoOutput)) then
		-- Check if icon is available
		if(not p.isEmpty(cargoOutput['icon'])) then
			icon = 'File:' .. cargoOutput['icon']
		end
	end
	
	local formattedImage = ''

	-- If the hero has an icon, it gets set as an image with a link to the hero's page.
	if (not p.isEmpty(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 from the hero through the cargo database (defined via the Hero Infobox template).
function p.getCargo(name)
	return cargo.query('Cards, Heroes', 'Heroes.HeroIconRaw=icon', { where='Cards.Name="' .. name .. '"', join="Cards.ID=Heroes.ID"})[1]
end

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

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

return p