Jump to content

Module:Trait

From Creopedia

This documentation is transcluded from Module:Trait/doc.

A meta module for all trait-related templates.

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

require( 'strict' )

local gameData = require( 'Module:GameData' )

local p = {}

local traitData = {}
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 traitName = args.name or mw.title.getCurrentTitle().fullText

	traitData = p.getData( traitName )
	templateArgs = args

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

	return table.concat( wikitext )
end

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

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

	data.name = traitName

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

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

	if traitData.evocreo2 then
		table.insert( categories, 'Traits in EvoCreo 2' )
	end

	if traitData.evocreo then
		table.insert( categories, 'Traits 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 a trait
---
--- The hyphenated trait names were uploaded with a space instead, so
--- Force-field is File:Force field - trait - evocreo2.png. Normalising the
--- separator here keeps those six files reachable rather than lower-casing the
--- page title verbatim and red-linking them.
---
--- @param name string
--- @return string
function p.getThumbnail( name )
	local file = string.lower( name ):gsub( '%-', ' ' )

	return string.format( '%s - trait - evocreo2.png', file )
end

return p