Jump to content

Module:Move/Components/Learnsets

From Creopedia

This documentation is transcluded from Module:Move/Components/Learnsets/doc.

Output the learnsets table for Module:Move using Module:TableLua.


require( 'strict' )

local gameTabs = require( 'Module:GameTabs' )
local tableLua = require( 'Module:TableLua' )
local creoLink = require( 'Module:CreoLink' )._main
local element = require( 'Module:Element' )._main
local level = require( 'Module:Level' )._main
local itemLink = require( 'Module:ItemLink' )._main
local ecGameData = require( 'Module:GameData/EvoCreo' )

local p = {}

--- Helper function to get the icon link for a tome
---
--- The EvoCreo 1 learnset data title cases every word ('Tome Of Electricity'),
--- which is not how the articles are titled, so those names are normalised.
---
--- @param itemName string
--- @param gameId string
--- @return string
local function getTomeWikitext( itemName, gameId )
	if gameId == 'evocreo' then
		itemName = ecGameData.getName( itemName )
	end

	return itemLink( { text = itemName } )
end

--- @return TableColumn[]
local function getColumns()
	return {
		{ id = 'creoName',    label = 'Creo' },
		{ id = 'creoElement', label = 'Element' },
		{ id = 'level',       label = 'Level' },
		{ id = 'tome',        label = 'Tome' }
	}
end

--- @param data table
--- @param gameId string
--- @return TableRow[]
local function getRows( data, gameId )
	local learnsets = {}

	for _, learnset in ipairs( data.fromLevel ) do
		table.insert( learnsets, {
			isLevel = true,
			data = learnset,
		} )
	end

	for _, learnset in ipairs( data.fromItem ) do
		table.insert( learnsets, {
			isLevel = false,
			data = learnset,
		} )
	end

	table.sort( learnsets, function( a, b )
		return a.data.creoName < b.data.creoName
	end )

	local rows = {}
	for i, item in ipairs( learnsets ) do
		local learnset = item.data
		if item.isLevel then
			rows[i] = {
				creoLink( { name = learnset.creoName } ),
				element( learnset.creoElement ),
				level( learnset.level ),
				'-'
			}
		else
			rows[i] = {
				creoLink( { name = learnset.creoName } ),
				element( learnset.creoElement ),
				'-',
				getTomeWikitext( learnset.itemName, gameId )
			}
		end
	end

	return rows
end

--- @param data table
--- @param gameId string
--- @return TableProps
local function getTableProps( data, gameId )
	return {
		caption = 'Learnsets',
		hideCaption = true,
		columns = getColumns(),
		data = getRows( data, gameId ),
		class = table.concat( {
			't-move-learnsets-table',
			'sortable', -- manually add the sortable class because we don't use the sort prop for sorting
			'wikitable--fluid' -- Citizen built-in class for broad tables
		}, ' ' )
	}
end

--- @param gameData table
--- @param gameId string
--- @return string
local function getGameTabWikitext( gameData, gameId )
	local html = mw.html.create( 'div' ):addClass( 't-tabbedCard-table-container' )
	html:wikitext( tableLua.render( getTableProps( gameData, gameId ) ) )
	return tostring( html )
end

--- @param data table
--- @return mw.html
local function getLearnsetsHtml( data )
	local html = mw.html.create( 'div' ):addClass( 't-move-learnsets' )

	local gameTabsData = gameTabs.mapGameArgs( data, function ( gameData, game )
		return getGameTabWikitext( gameData, game.id )
	end )
	gameTabsData.noPadding = true

	html:wikitext( gameTabs._main( gameTabsData ) )

	return html
end

--- @param context ComponentContext
--- @return string
function p.render( context )
	return tostring( getLearnsetsHtml( context.data ) )
end

return p