Jump to content

Module:ElementChart

From Creopedia

This documentation is transcluded from Module:ElementChart/doc.

Implements Template:ElementChart.


require( 'strict' )

local TableLua = require( 'Module:TableLua' )
local Element = require( 'Module:Element' )

local p = {}

--- @param elementName string
--- @return string
local function getElementWikitext( elementName )
	return Element._main( elementName )
end

--- @param elements table
--- @return table
local function getBonusMap( elements )
	local bonusMap = {}
	for _, attackElement in ipairs( elements ) do
		bonusMap[attackElement.id] = {}
		if attackElement.elementComparisonMaps then
			for _, comparison in ipairs( attackElement.elementComparisonMaps ) do
				bonusMap[attackElement.id][comparison.elementToCompareId] = comparison.bonus
			end
		end
	end
	return bonusMap
end

--- @param elements table
--- @return TableColumn[]
local function getColumns( elements )
	local columns = {
		{ label = '' }
	}

	for _, element in ipairs( elements ) do
		table.insert( columns, {
			label = getElementWikitext( element.name )
		} )
	end

	return columns
end

--- @param elements table
--- @param bonusMap table
--- @return TableRow[]
local function getRows( elements, bonusMap )
	local rows = {}

	for _, attackElement in ipairs( elements ) do
		local row = {
			getElementWikitext( attackElement.name )
		}

		for _, defenseElement in ipairs( elements ) do
			local bonus = bonusMap[attackElement.id][defenseElement.id]

			local cellContent = mw.html.create( 'div' ):addClass( 't-element-chart__cell' )

			if type( bonus ) == 'number' then
				if bonus > 0 then
					cellContent:addClass( 't-element-chart__cell--strong' ):wikitext( tostring( bonus ) )
				elseif bonus < 0 then
					cellContent:addClass( 't-element-chart__cell--weak' ):wikitext( tostring( bonus ) )
				else
					cellContent:addClass( 't-element-chart__cell--neutral' )
				end
			end

			table.insert( row, tostring( cellContent ) )
		end

		table.insert( rows, row )
	end

	return rows
end

--- @param elements table
--- @return TableProps
local function getTableProps( elements )
	local bonusMap = getBonusMap( elements )
	return {
		caption = 'Element chart',
		hideCaption = true,
		columns = getColumns( elements ),
		data = getRows( elements, bonusMap ),
		class = table.concat( {
			't-element-chart',
			'wikitable--border' -- Citizen built-in class for vertical borders
		}, ' ' )
	}
end

--- Get the element table
---
--- @return table
local function getElementTable()
	return mw.loadJsonData( 'Module:GameData/EvoCreo2/ElementTable.json' )
end

--- 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 )
	local elementTable = getElementTable()
	return mw.getCurrentFrame():extensionTag {
		name = 'templatestyles', args = { src = 'Module:ElementChart/styles.css' }
	} .. TableLua.render( getTableProps( elementTable ) )
end

return p