Jump to content

Module:Rarity

From Creopedia

This documentation is transcluded from Module:Rarity/doc.

Output a rarity badge using Module:BadgeLua.

Implements Template:Rarity.


require( 'strict' )

local rarity = mw.loadJsonData( 'Module:Rarity/Rarity.json' )

local p = {}


--- Get the rarity id
---
--- @param rarityName string
--- @return string
local function getRarityId( rarityName )
	return (rarityName:lower():gsub( ' ', '_' ))
end

--- A tier's place in the scale, as a sort key.
---
--- Sorted on its own text, a rarity column runs Ascendant, Common, Mega Prime,
--- which is alphabetical order and tells the reader nothing about how rare
--- anything is. Sorting on the tier's number instead runs it Common to
--- Nebulous, the way the game states the scale.
---
--- The number is padded because a browser that reads the column as text, which
--- it will as soon as one cell holds anything but a number, would otherwise put
--- Nebulous at 10 between Common at 1 and Uncommon at 2.
---
--- EvoCreo's five tiers are a subset of EvoCreo 2's ten, spelled Ascendent
--- rather than Ascendant, so one set of numbers orders both games.
---
--- @param order number
--- @return string
local function getSortKey( order )
	return string.format( '%02d', order )
end

--- Render the rarity badge
---
--- @param rarityId string
--- @param rarityData table
--- @return string
local function renderRarity( rarityId, rarityData )
	local badge = require( 'Module:BadgeLua' )

	-- There is no page per tier, so link the badge at the tier's own row of
	-- the table on [[Rarity]]. Each of those rows carries a matching id.
	return badge.render( {
		icon = rarityData.icon,
		text = rarityData.name,
		link = rarityData.page or ( 'Rarity#' .. rarityData.name ),
		sortValue = getSortKey( rarityData.order ),
		class = 't-rarity t-rarity--' .. rarityId .. ' cp-image-pixelated'
	} )
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 )[1] )
end

--- Render the rarity badge
---
--- @param rarityName string
--- @return string
function p._main( rarityName )
	local rarityId = getRarityId( rarityName )
	local rarityData = rarity[rarityId]

	if not rarityData then
		error( 'Invalid rarity name: ' .. rarityName )
	end

	return mw.getCurrentFrame():extensionTag {
		name = 'templatestyles', args = { src = 'Module:Rarity/styles.css' }
	} .. renderRarity( rarityId, rarityData )
end

--- The sort key for a rarity, for a cell that wants to state it itself
---
--- The badge already carries this, and a cell holding nothing but a badge needs
--- no more than that. A cell holding more does: the outfits table pairs a
--- female and a male badge in one cell, and a sorter reading the pair would
--- read the labels around them too. Such a cell states the key on itself, which
--- the sorter takes in preference to anything inside it.
---
--- @param rarityName string
--- @return string|nil
function p.getSortKey( rarityName )
	local rarityData = rarity[getRarityId( rarityName )]

	return rarityData and getSortKey( rarityData.order )
end


return p