Difference between revisions of "Module:Card/Hero"

From Artifact Wiki
Jump to: navigation, search
m
(Update module to work with the new database)
 
Line 29: Line 29:
 
local cargoOutput = p.getCargo(name)
 
local cargoOutput = p.getCargo(name)
 
 
width = width or properties.getCardWidth()
+
local cardImage = ''
local page = name
+
local imageFile, color, attack, armor, health, abilities, rarity, description = ''
+
if (not p.isempty(cargoOutput)) then
 
+
if (not p.isempty(cargoOutput['image'])) then
-- When available, the icon, cooldown, type and description get set to the one found in the database.
+
cardImage = 'File:' .. cargoOutput['image']
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
 
end
else
 
description = properties.getNoDatabaseDescription(name, "hero")
 
 
end
 
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)
+
if (p.isempty(cardImage)) then
+
return ''
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
 
end
 
 
-- Format the ability tooltips
+
width = width or properties.getCardWidth()
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)
+
local card = string.format('[[%s|%spx|link=%s|%s]]', cardImage, width, name, name)
 
 
return tooltipCard
+
return card
 
end
 
end
  
 
-- Retrieves the color, image, attack, armor, health and ability from the hero through the cargo database (defined via the Hero Infobox template).
 
-- 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)
 
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]
+
return cargo.query('Cards', 'Cards.CardImage=image', { where='Cards.Name="' .. name .. '"'})[1]
 
end
 
end
  

Latest revision as of 03:37, 14 April 2020

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)
	
	local cardImage = ''
	
	if (not p.isempty(cargoOutput)) then
		if (not p.isempty(cargoOutput['image'])) then
			cardImage = 'File:' .. cargoOutput['image']
		end
	end
	
	if (p.isempty(cardImage)) then
		return ''
	end
	
	width = width or properties.getCardWidth()

	local card = string.format('[[%s|%spx|link=%s|%s]]', cardImage, width, name, name)
	
	return card
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', 'Cards.CardImage=image', { where='Cards.Name="' .. 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