Jump to content

Module:Ability

From Creopedia

This documentation is transcluded from Module:Ability/doc.

A meta module for all ability-related templates.

Page Description
Module:Ability/Components/Infobox Renders the infobox

require( 'strict' )

local gameData = require( 'Module:GameData' )

local p = {}

local abilityData = {}
local templateArgs = {}

--- Wikitext entry point for the module
---
--- @param frame mw.frame
--- @return string
function p.main( frame )
	local getArgs = require( 'Module:Arguments' ).getArgs
	return p._main( getArgs( frame ) )
end

--- Lua entry point for the module
---
--- @param args table
--- @return string
function p._main( args )
	args = args or {}

	local abilityName = args.name or mw.title.getCurrentTitle().fullText

	abilityData = p.getData( abilityName )
	templateArgs = args

	local wikitext = {
		p.renderInfobox( abilityName ),
		p.renderCategories( p.getCategories() )
	}

	return table.concat( wikitext )
end

--- Get the game data
---
--- @param abilityName string
--- @return table
function p.getData( abilityName )
	return gameData.getAbility( abilityName )
end

--- Return the wikitext for the infobox
---
--- @param abilityName string
--- @return string
function p.renderInfobox( abilityName )
	local data = abilityData or p.getData( abilityName )

	data.name = abilityName

	return require( 'Module:Ability/Components/Infobox' ).render( {
		data = data,
		args = templateArgs
	} )
end

--- Get the page categories
---
--- @return table
function p.getCategories()
	local categories = {
		'Abilities'
	}

	if abilityData.evocreo2 then
		table.insert( categories, 'Abilities in EvoCreo 2' )
	end

	if abilityData.evocreo then
		table.insert( categories, 'Abilities in EvoCreo' )
	end

	return categories
end

--- Return the wikitext for the category
---
--- @param categories table
--- @return string
function p.renderCategories( categories )
	local wikitext = {}

	for _, category in ipairs( categories ) do
		table.insert( wikitext, string.format( '[[Category:%s]]', category ) )
	end

	return table.concat( wikitext, '' )
end

--- Get the thumbnail for an ability
---
--- @param name string
--- @return string
function p.getThumbnail( name )
	return string.format( '%s - ability - evocreo2.png', string.lower( name ) )
end

return p