Module:DissectDlcItemName

From Primal Fear Wiki
Jump to navigation Jump to search

Documentation for this module may be created at Module:DissectDlcItemName/doc

local p = {}
local MAPPING_PATTERNS = {
	-- Add name parts that should be checked to retrieve an icon.
	-- Each entry must have following information:
	-- 1: Pattern to find that will be removed for display. Parenthesis need to be escaped with %.
	-- 2: Icon file name
	-- 3: Link name for the icon
	{' %(The Island%)', 'The Island Icon.png', 'The Island'},
	{' %(Scorched Earth%)', 'Scorched Earth Icon.png', 'Scorched Earth'},
	{' %(Aberration%)', 'Aberration Icon.png', 'Aberration'},
	{' %(Extinction%)', 'Extinction Icon.png', 'Extinction'},
	{' %(Genesis%)', 'Genesis Part 1 Icon.png', 'Genesis'},
	{' %(Genesis: Part 1%)', 'Genesis Part 1 Icon.png', 'Genesis: Part 1'},
	{' %(Genesis: Part 2%)', 'Genesis Part 2 Icon.png', 'Genesis: Part 2'},
	{' %(Primitive Plus%)', 'Primitive Plus Icon.png', 'Primitive Plus'},
	{' %(The Center%)', 'The Center Icon.png', 'The Center'},
	{' %(Ragnarok%)', 'Ragnarok Icon.png', 'Ragnarok'},
	{' %(Valguero%)', 'Valguero Icon.png', 'Valguero'},
	{' %(Crystal Isles%)', 'Crystal Isles Icon.png', 'Crystal Isles'},
	{' %(Lost Island%)', 'Lost Island Icon.png', 'Lost Island'},
	-- MODS
	{' %(Primal Fear%)', 'PrimalFearIcon.png', 'Primal Fear'},
}
local EXTRA_DLC_NAMES = {
	-- Add alternative DLC names to look for when querying for a DLC icon with
	-- no item name.
	-- This is a "from-to" mapping. The right side should match a single
	-- entry in MAPPING_PATTERNS by their third value. Left side should be lower-case.
	["island"] = "The Island",
	["center"] = "The Center",
	["scorched"] = "Scorched Earth",
	["primitive"] = "Primitive Plus",
}

-- Helper function to use within other Lua modules to dissect an item name into
-- display name, DLC icon and DLC article.
-- Returns nil if DLC suffix is missing or unrecognized.
function p.tryMatch(name)
	for _, entry in ipairs(MAPPING_PATTERNS) do
		if string.find(name, entry[1]) ~= nil then
			return {
				["displayName"] = string.gsub(name, entry[1], ''),
				["dlcIcon"] = entry[2],
				["dlcArticle"] = entry[3]
			}
		end
	end
	return nil
end

-- Helper function for wiki templates to dissect item names into variables.
-- Sets every variable to nothing if DLC suffix is missing or unrecognized.
-- Accepts five parameters:
-- 1: Item name
-- 2: Variable name for the display name
-- 3: Variable name for the DLC icon file name
-- 4: Variable name for the DLC article name
function p.tryMatchW(frame)
	local args = frame.args
	local result = p.tryMatch(args[1])
	local varDisplayName = args[2]
	local varDlcIcon = args[3]
	local varDlcArticle = args[4]
	
	local displayName = result and result.displayName or ''
	local dlcIcon = result and result.dlcIcon or ''
	local dlcArticle = result and result.dlcArticle or ''

	frame:callParserFunction( '#vardefine', varDisplayName, displayName )
	frame:callParserFunction( '#vardefine', varDlcIcon, dlcIcon )
	frame:callParserFunction( '#vardefine', varDlcArticle, dlcArticle )

	return ''
end

-- Helper function for wiki templates to get an icon name for a DLC.
-- Sets every variable to nothing if DLC suffix is missing or unrecognized.
-- Accepts three parameters:
-- 1: DLC name
-- 2: Variable name for the DLC icon file name
-- 3: Variable name for the DLC article name
function p.getIcon(frame)
	local args = frame.args
	local dlc = mw.text.trim(mw.ustring.lower(args[1]))
	local varDlcIcon = args[2]
	local varDlcArticle = args[3]

	if EXTRA_DLC_NAMES[dlc] then
		dlc = mw.ustring.lower(EXTRA_DLC_NAMES[dlc])
	end

	local dlcIcon = ''
	local dlcArticle = ''

	for _, entry in ipairs(MAPPING_PATTERNS) do
		local third = mw.ustring.lower(entry[3])
		if dlc == third or (string.sub(third, 1, 4) == 'mod:' and 'mod:'..dlc == third) then
			dlcIcon = entry[2]
			dlcArticle = entry[3]
			break
		end
	end

	frame:callParserFunction( '#vardefine', varDlcIcon, dlcIcon )
	frame:callParserFunction( '#vardefine', varDlcArticle, dlcArticle )

	return ''
end

return p