Module:Card/Hero

From Artifact Wiki
Jump to: navigation, search

Documentation for Module:Card/Hero [edit] [edit doc]

Info Icon.png See also: Hero Card template.

This module provides a heroes formatted card without the need of additional arguments. The resources are retrieved from the Cargo table 'Heroes', defined in the Hero Infobox template.

Usage


Within templates:

{{#invoke:Card/Hero|main}}

Within modules:

1 -- At the start of the program
2 local getHeroCard = require('Module:Card/Hero').getHeroCard
1 -- Where you need the spell card
2 local heroCard = getHeroCard('hero name', width) -- width is optional

local cargo = mw.ext.cargo
local getArgs = require('Module:Arguments').main
local getCardLayout = require('Module:Card/Layout').getCardLayout2
local getHeroIcon = require('Module:Hero Icon').getHeroIcon
local properties = require('Module:Properties')
local getAbility  = require('Module:Card/Ability').getAbilityCard
local p = {}


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


function p._main(args)
	local name = args[1]	-- 1. template argument (Name)
	local width = args[2]	-- 2. template argument (Width) [optional]
	
	return p.getCard(name, width)
end

-- Returns the formatted Hero Card. You can use this function in other modules.
function p.getCard(name, width)
	if(p.isempty(name)) then
		return ''
	end
	
	local cargoOutput = p.getCargo(name)
	
	width = width or properties.getCardWidth()
	local page = name
	local imageFile, color, attack, armor, health, abilities, rarity, description = ''	

	-- When available, the icon, cooldown, type and description get set to the one found in the database.
	if(cargoOutput ~= nil) then
		if(not p.isempty(cargoOutput['image'])) then
			imageFile = cargoOutput['image']
		end
		if(not p.isempty(cargoOutput['attack'])) then
			attack = cargoOutput['attack']
		end
		if(not p.isempty(cargoOutput['armor'])) then
			armor = cargoOutput['armor']
		end
		if(not p.isempty(cargoOutput['health'])) then
			health = cargoOutput['health']
    	end
		if(not p.isempty(cargoOutput['color'])) then
			color = cargoOutput['color']
		end
		if(not p.isempty(cargoOutput['_pageTitle'])) then
			page = cargoOutput['_pageTitle']
		end
		if(not p.isempty(cargoOutput['ability'])) then
			abilities = cargoOutput['ability']
		end
		if(not p.isempty(cargoOutput['rarity'])) then
			rarity = cargoOutput['rarity']
		end
	else
		description = properties.getNoDatabaseDescription(name, "hero")
	end
	
	local heroCard = getCardLayout(width, page, name, "Hero", color, rarity, nil, nil, cardset, illustrator, nil, name, nil, nil, releaseDate, imageFile, "Hero", "Hero", "Hero", nil, nil, nil, attack, armor, health, description, ability)
	
	local abilityCards = ""
	local abilityIcons = ""
	
	if(not p.isempty(abilities)) then
		abilityTable = {}
		i = 1
		for ability in string.gmatch(abilities, "[^,]+") do
			abilityTable[i] = ability
			i = i + 1
		end
		
		for i=1, #abilityTable do
			local ability = p.trim(abilityTable[i])
			
			local abilityCargo = p.getAbilityCargo(ability)
			
			if(not p.isempty(abilityCargo)) then
				-- Add ability icons to the card
				local abilityPage = abilityCargo['_pageName'] or page
				local abilityName = abilityCargo['title'] or abilityPage
				local abilityFile = abilityCargo['icon']
				if(p.isempty(abilityFile)) then
					abilityFile =  "File:No Image.png"
				end
				local abilityWidth = p.floor(0.286 * width)
				local marginLeft = p.round(0.03 * width)
				local abilityIcon = string.format([=[<div style="border: solid gray 2px; display: inline-block; margin-left: %spx;">[[%s|%spx|%s|link=%s]]</div>]=], marginLeft, abilityFile, abilityWidth, abilityName, abilityPage)
				abilityIcons = abilityIcons .. abilityIcon
				
				-- Add ability to the ability tooltips
				local abilityCard = getAbility(ability, width)
				abilityCards = abilityCards .. abilityCard
			end
		end
		
		-- Format the ability icons
		local marginBottom = p.floor(0.5 * width)
		if(not p.isempty(abilityIcons)) then
			abilityIcons = string.format([=[<div style="display: inline-flex; flex-flow: row nowrap; position: absolute; left: 0; bottom: %spx;"> %s </div>]=], marginBottom, abilityIcons)
		end
	end
	
	-- Format the ability tooltips
	local tooltipCard = string.format([=[<div class="tooltip2" style="margin: 2px; position: relative;">%s %s<div class="tooltipbox2" style="display:inline-flex; flex-flow: column nowrap; top: 0; right: 100&#x25;;> %s </div></div>]=],
		heroCard, abilityIcons, abilityCards)
	
	return tooltipCard
end

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

function p.getAbilityCargo(name)
	return cargo.query('cards', '_pageName, title, icon', { where='cards.type="Ability" AND cards.title="' .. name .. '"'})[1]
end

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

function p.round(d)
	local numDecimalPlaces = 2
	local mult = 10^(numDecimalPlaces or 0)
	return math.floor(d * mult + 0.5) / mult
end

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

function p.trim(s)
   return (s:gsub("^%s*(.-)%s*$", "%1"))
end

return p