Jump to content

Module:Creo/Components/ItemMoves

From Creopedia

This documentation is transcluded from Module:Creo/Components/ItemMoves/doc.

Output the item moves table for Module:Creo using Module:TableLua.


require( 'strict' )

local gameTabs = require( 'Module:GameTabs' )
local tableLua = require( 'Module:TableLua' )
local itemLink = require( 'Module:ItemLink' )._main
local element = require( 'Module:Element' )._main
local energy = require( 'Module:Energy' )._main
local nameCell = require( 'Module:Creo/NameCell' ).render
local getMoveArticle = require( 'Module:Move' ).getArticle
local getMoveThumbnail = require( 'Module:Move' ).getThumbnail
local ecGameData = require( 'Module:GameData/EvoCreo' )
local ec2GameData = require( 'Module:GameData/EvoCreo2' )

local p = {}

--- Both games have their own move art, so the icon follows the tab it is shown
--- on rather than always using the EvoCreo 2 artwork.
---
--- @param name string
--- @param gameId string
--- @return string
local function getMoveWikitext( name, gameId )
	return nameCell( getMoveThumbnail( name, gameId ), getMoveArticle( name ), name )
end

--- @param gameId string
--- @return TableColumn[]
local function getColumns( gameId )
	if gameId == 'evocreo2' then
		return {
			{ id = 'tome', label = 'Tome' },
			{ id = 'name', label = 'Name' },
			{ id = 'element', label = 'Element' },
			{ id = 'category', label = 'Category' },
			{ id = 'type', label = 'Type' },
			{ id = 'power', label = 'Power', textAlign = 'number' },
			{ id = 'accuracy', label = 'Accuracy', textAlign = 'number' },
			{ id = 'energy', label = 'Energy', textAlign = 'number' }
		}
	elseif gameId == 'evocreo' then
		return {
			{ id = 'tome', label = 'Tome' },
			{ id = 'name', label = 'Name' },
			{ id = 'element', label = 'Element' },
			{ id = 'category', label = 'Category' },
			{ id = 'type', label = 'Type' },
			{ id = 'power', label = 'Power', textAlign = 'number' },
			{ id = 'accuracy', label = 'Accuracy', textAlign = 'number' },
			-- EvoCreo moves take a number of turns to recharge; they do not
			-- consume energy the way EvoCreo 2 moves do.
			{ id = 'recharge', label = 'Recharge', textAlign = 'number' }
		}
	end

	return {}
end

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

	if gameId == 'evocreo2' then
		for _, moveMap in ipairs( data ) do
			if moveMap.itemId ~= nil then
				local itemData = ec2GameData.getItemById( moveMap.itemId )
				if itemData ~= nil then
					local row = {
						itemLink( { text = itemData.name } ),
						getMoveWikitext( moveMap.move.name, gameId ),
						element( moveMap.move.element.name ),
						moveMap.move.moveClass.name,
						moveMap.move.moveType.name,
						moveMap.move.damage,
						string.format( '%d %%', moveMap.move.accuracy ),
						energy( moveMap.move.recharge )
					}
					table.insert( rows, row )
				end
			end
		end
	elseif gameId == 'evocreo' then
		for _, move in ipairs( data ) do
			if move.id ~= nil then
				local moveData = ecGameData.getMove( move.move )
				if moveData ~= nil then
					local row = {
						itemLink( {
							text = ecGameData.getName( move.id ),
							game = gameId
						} ),
						getMoveWikitext( ecGameData.getName( move.move ), gameId ),
						element( moveData.element ),
						moveData.type or '-',
						moveData.skilltype or '-',
						moveData.basedamage,
						string.format( '%d %%', tonumber( moveData.accuracy or 1 ) * 100 ),
						energy( moveData.recharge )
					}
					table.insert( rows, row )
				end
			end
		end
	end

	return rows
end

--- @param data table
--- @param gameId string
--- @return TableProps
local function getTableProps( data, gameId )
	return {
		caption = 'Item moves',
		hideCaption = true,
		columns = getColumns( gameId ),
		data = getRows( data, gameId ),
		sort = {
			tome = 'asc',
		},
		class = table.concat( {
			't-creo-item-moves-table',
			'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 getItemMovesHtml( data )
	local html = mw.html.create( 'div' ):addClass( 't-creo-item-moves' )

	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( getItemMovesHtml( context.data ) )
end


return p